From 60caf9af7f54482e3ef95511ab501ce9ea89fd78 Mon Sep 17 00:00:00 2001 From: Tomasz Sterna Date: Wed, 9 Feb 2011 21:35:36 +0100 Subject: [PATCH] Refactored QubesVm.is_*vm() methods --- dom0/qvm-core/qubes.py | 68 +++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/dom0/qvm-core/qubes.py b/dom0/qvm-core/qubes.py index d38f101d..cab94a27 100755 --- a/dom0/qvm-core/qubes.py +++ b/dom0/qvm-core/qubes.py @@ -81,12 +81,6 @@ qubes_appmenu_remove_cmd = "/usr/lib/qubes/remove_appvm_appmenus.sh" # as "should be good for everyone" swap_cow_sz = 1024*1024*1024 -VM_TEMPLATE = 'TempleteVM' -VM_APPVM = 'AppVM' -VM_NETVM = 'NetVM' -VM_FWVM = 'FirewallVM' -VM_DISPOSABLEVM = 'DisposableVM' - class XendSession(object): def __init__(self): self.get_xend_session_old_api() @@ -171,7 +165,7 @@ class QubesVm(object): Note that qid is not the same as Xen's domid! """ - def __init__(self, qid, name, type, + def __init__(self, qid, name, dir_path, conf_file = None, uses_default_netvm = True, netvm_vm = None, @@ -196,7 +190,6 @@ class QubesVm(object): else: self.conf_file = dir_path + "/" + conf_file - self.__type = type self.uses_default_netvm = uses_default_netvm self.netvm_vm = netvm_vm @@ -217,10 +210,6 @@ class QubesVm(object): def qid(self): return self.__qid - @property - def type(self): - return self.__type - @property def ip(self): if self.netvm_vm is not None: @@ -271,34 +260,19 @@ class QubesVm(object): self.updateable = False def is_templete(self): - if self.type == VM_TEMPLATE: - return True - else: - return False + return isinstance(self, QubesTemplateVm) def is_appvm(self): - if self.type == VM_APPVM: - return True - else: - return False + return isinstance(self, QubesAppVm) def is_netvm(self): - if self.type == VM_NETVM or self.type == VM_FWVM: - return True - else: - return False + return isinstance(self, QubesNetVm) def is_fwvm(self): - if self.type == VM_FWVM: - return True - else: - return False + return isinstance(self, QubesFirewallVm) def is_disposablevm(self): - if self.type == VM_DISPOSABLEVM: - return True - else: - return False + return isinstance(self, QubesDisposableVm) def add_to_xen_storage(self): if dry_run: @@ -625,7 +599,7 @@ class QubesTemplateVm(QubesVm): private_img = kwargs.pop("private_img") if "private_img" in kwargs else None appvms_conf_file = kwargs.pop("appvms_conf_file") if "appvms_conf_file" in kwargs else None - super(QubesTemplateVm, self).__init__(type=VM_TEMPLATE, label = default_template_label, **kwargs) + super(QubesTemplateVm, self).__init__(label = default_template_label, **kwargs) dir_path = kwargs["dir_path"] @@ -654,6 +628,10 @@ class QubesTemplateVm(QubesVm): self.appmenus_templates_dir = self.dir_path + "/" + default_appmenus_templates_subdir self.appvms = QubesVmCollection() + @property + def type(self): + return "TempleteVM" + def set_updateable(self): if self.is_updateable(): return @@ -922,10 +900,12 @@ class QubesNetVm(QubesServiceVm): if "label" not in kwargs or kwargs["label"] is None: kwargs["label"] = default_servicevm_label - if "type" not in kwargs or kwargs["type"] is None: - kwargs["type"] = VM_NETVM super(QubesNetVm, self).__init__(installed_by_rpm=True, **kwargs) + @property + def type(self): + return "NetVM" + @property def gateway(self): return self.__gateway @@ -969,7 +949,11 @@ class QubesFirewallVm(QubesNetVm): def __init__(self, **kwargs): self.netvm_vm = kwargs.pop("netvm_vm") if "netvm_vm" in kwargs else None - super(QubesFirewallVm, self).__init__(type=VM_FWVM, **kwargs) + super(QubesFirewallVm, self).__init__(**kwargs) + + @property + def type(self): + return "FirewallVM" def create_xml_element(self): element = xml.etree.ElementTree.Element( @@ -1067,7 +1051,7 @@ class QubesDisposableVm(QubesVm): template_vm = kwargs.pop("template_vm") - super(QubesDisposableVm, self).__init__(type=VM_DISPOSABLEVM, dir_path=None, **kwargs) + super(QubesDisposableVm, self).__init__(dir_path=None, **kwargs) qid = kwargs["qid"] assert template_vm is not None, "Missing template_vm for DisposableVM!" @@ -1079,6 +1063,10 @@ class QubesDisposableVm(QubesVm): self.template_vm = template_vm template_vm.appvms[qid] = self + @property + def type(self): + return "DisposableVM" + def create_xml_element(self): element = xml.etree.ElementTree.Element( "QubesDisposableVm", @@ -1108,7 +1096,7 @@ class QubesAppVm(QubesVm): template_vm = kwargs.pop("template_vm") - super(QubesAppVm, self).__init__(type=VM_APPVM, **kwargs) + super(QubesAppVm, self).__init__(**kwargs) qid = kwargs["qid"] dir_path = kwargs["dir_path"] @@ -1132,6 +1120,10 @@ class QubesAppVm(QubesVm): self.swapcow_img = dir_path + "/" + default_swapcow_img + @property + def type(self): + return "AppVM" + def set_updateable(self): if self.is_updateable(): return