From a457b62728e57d59267b647414f1e233527bfafd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Thu, 21 Nov 2013 03:39:08 +0100 Subject: [PATCH] core: more flexible mechanism for template compatibility check Using class method allow the users (Qubes Manager at least) to check for compatibility without having any particular VM instance - useful while creating the VM. --- core-modules/000QubesVm.py | 17 ++++++++++++----- core-modules/01QubesHVm.py | 14 +++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/core-modules/000QubesVm.py b/core-modules/000QubesVm.py index 5257b50c..36d37c3e 100644 --- a/core-modules/000QubesVm.py +++ b/core-modules/000QubesVm.py @@ -459,17 +459,24 @@ class QubesVm(object): raise QubesException("Failed to set autostart for VM via systemctl") self._autostart = bool(value) + @classmethod + def is_template_compatible(cls, template): + """Check if given VM can be a template for this VM""" + # FIXME: check if the value is instance of QubesTemplateVM, not the VM + # type. The problem is while this file is loaded, QubesTemplateVM is + # not defined yet. + if template and (not template.is_template() or template.type != "TemplateVM"): + return False + return True + @property def template(self): return self._template @template.setter def template(self, value): - # FIXME: check if the value is instance of QubesTemplateVM, not the VM - # type. The problem is while this file is loaded, QubesTemplateVM is - # not defined yet. - if value and (not value.is_template() or value.type != "TemplateVM"): - raise QubesException("Only PV template can be a template for the PV VM") + if value and not self.is_template_compatible(value): + raise QubesException("Incompatible template type %s with VM of type %s" % (value.type, self.type)) self._template = value def is_template(self): diff --git a/core-modules/01QubesHVm.py b/core-modules/01QubesHVm.py index 0e657834..2fa7d5a9 100644 --- a/core-modules/01QubesHVm.py +++ b/core-modules/01QubesHVm.py @@ -101,15 +101,11 @@ class QubesHVm(QubesVm): # Any non-template based HVM can be a template itself return self.template is None - @property - def template(self): - return self._template - - @template.setter - def template(self, value): - if value and (not value.is_template() or value.type != "HVM"): - raise QubesException("Only HVM can be a template for the HVM") - self._template = value + @classmethod + def is_template_compatible(cls, template): + if template and (not template.is_template() or template.type != "HVM"): + return False + return True def get_clone_attrs(self): attrs = super(QubesHVm, self).get_clone_attrs()