vm: move DVM template specific code into separate mixin

No functional change.
This commit is contained in:
Marek Marczykowski-Górecki 2019-02-27 05:50:26 +01:00
parent c5cfb81b94
commit 4a3772bb57
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
3 changed files with 51 additions and 19 deletions

View File

@ -24,10 +24,12 @@ import copy
import qubes.events
import qubes.vm.qubesvm
import qubes.vm.mix.dvmtemplate
from qubes.config import defaults
class AppVM(qubes.vm.qubesvm.QubesVM):
class AppVM(qubes.vm.mix.dvmtemplate.DVMTemplateMixin,
qubes.vm.qubesvm.QubesVM):
'''Application VM'''
template = qubes.VMProperty('template',
@ -35,11 +37,6 @@ class AppVM(qubes.vm.qubesvm.QubesVM):
vmclass=qubes.vm.templatevm.TemplateVM,
doc='Template, on which this AppVM is based.')
template_for_dispvms = qubes.property('template_for_dispvms',
type=bool,
default=False,
doc='Should this VM be allowed to start as Disposable VM')
default_volume_config = {
'root': {
'name': 'root',
@ -97,15 +94,6 @@ class AppVM(qubes.vm.qubesvm.QubesVM):
super(AppVM, self).__init__(app, xml, **kwargs)
@property
def dispvms(self):
''' Returns a generator containing all Disposable VMs based on the
current AppVM.
'''
for vm in self.app.domains:
if hasattr(vm, 'template') and vm.template is self:
yield vm
@qubes.events.handler('domain-load')
def on_domain_loaded(self, event):
''' When domain is loaded assert that this vm has a template.
@ -126,10 +114,6 @@ class AppVM(qubes.vm.qubesvm.QubesVM):
if not self.is_halted():
raise qubes.exc.QubesVMNotHaltedError(self,
'Cannot change template while qube is running')
if any(self.dispvms):
raise qubes.exc.QubesVMInUseError(self,
'Cannot change template '
'while there are DispVMs based on this qube')
@qubes.events.handler('property-set:template')
def on_property_set_template(self, event, name, newvalue, oldvalue=None):

View File

@ -0,0 +1,47 @@
# -*- encoding: utf-8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2017 Marek Marczykowski-Górecki
# <marmarek@invisiblethingslab.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
import qubes.events
class DVMTemplateMixin(qubes.events.Emitter):
'''VM class capable of being DVM template'''
template_for_dispvms = qubes.property('template_for_dispvms',
type=bool,
default=False,
doc='Should this VM be allowed to start as Disposable VM')
@qubes.events.handler('property-pre-set:template')
def __on_property_pre_set_template(self, event, name, newvalue,
oldvalue=None):
# pylint: disable=unused-argument
if any(self.dispvms):
raise qubes.exc.QubesVMInUseError(self,
'Cannot change template '
'while there are DispVMs based on this qube')
@property
def dispvms(self):
''' Returns a generator containing all Disposable VMs based on the
current AppVM.
'''
for vm in self.app.domains:
if getattr(vm, 'template', None) == self:
yield vm

View File

@ -249,6 +249,7 @@ fi
%dir %{python3_sitelib}/qubes/vm/mix/__pycache__
%{python3_sitelib}/qubes/vm/mix/__pycache__/*
%{python3_sitelib}/qubes/vm/mix/__init__.py
%{python3_sitelib}/qubes/vm/mix/dvmtemplate.py
%{python3_sitelib}/qubes/vm/mix/net.py
%dir %{python3_sitelib}/qubes/storage