dom0/core: new QubesVmCollection.add_new_vm function

Generic function to add new VM of any type. Old add_*vm marked as depreciated.
This commit is contained in:
Marek Marczykowski 2013-01-17 01:29:32 +01:00
parent 84e85c6a9a
commit 52e16037f2

View File

@ -32,6 +32,7 @@ import re
import shutil import shutil
import uuid import uuid
import time import time
import warnings
from datetime import datetime from datetime import datetime
from qmemman_client import QMemmanClient from qmemman_client import QMemmanClient
@ -2625,14 +2626,45 @@ class QubesVmCollection(dict):
else: else:
assert False, "Attempt to add VM with qid that already exists in the collection!" assert False, "Attempt to add VM with qid that already exists in the collection!"
def add_new_vm(self, vm_type, **kwargs):
if vm_type not in QubesVmClasses.keys():
raise ValueError("Unknown VM type: %s" % vm_type)
qid = self.get_new_unused_qid()
vm = QubesVmClasses[vm_type](qid=qid, collection=self, **kwargs)
if not self.verify_new_vm(vm):
raise QubesException("Wrong VM description!")
self[vm.qid]=vm
# make first created NetVM the default one
if self.default_fw_netvm_qid is None and vm.is_netvm():
self.set_default_fw_netvm(vm)
if self.default_netvm_qid is None and vm.is_proxyvm():
self.set_default_netvm(vm)
# make first created TemplateVM the default one
if self.default_template_qid is None and vm.is_template():
self.set_default_template(vm)
# make first created ProxyVM the UpdateVM
if self.updatevm_qid is None and vm.is_proxyvm():
self.set_updatevm_vm(vm)
# by default ClockVM is the first NetVM
if self.clockvm_qid is None and vm.is_netvm():
self.set_clockvm_vm(vm)
return vm
def add_new_appvm(self, name, template, def add_new_appvm(self, name, template,
dir_path = None, conf_file = None, dir_path = None, conf_file = None,
private_img = None, private_img = None,
label = None): label = None):
qid = self.get_new_unused_qid() warnings.warn("Call to deprecated function, use add_new_vm instead",
vm = QubesAppVm (qid=qid, name=name, template=template, DeprecationWarning, stacklevel=2)
return self.add_new_vm("QubesAppVm", name=name, template=template,
dir_path=dir_path, conf_file=conf_file, dir_path=dir_path, conf_file=conf_file,
private_img=private_img, private_img=private_img,
netvm = self.get_default_netvm(), netvm = self.get_default_netvm(),
@ -2640,43 +2672,29 @@ class QubesVmCollection(dict):
uses_default_kernel = True, uses_default_kernel = True,
label=label) label=label)
if not self.verify_new_vm (vm):
assert False, "Wrong VM description!"
self[vm.qid]=vm
return vm
def add_new_hvm(self, name, label = None): def add_new_hvm(self, name, label = None):
qid = self.get_new_unused_qid() warnings.warn("Call to deprecated function, use add_new_vm instead",
vm = QubesHVm (qid=qid, name=name, DeprecationWarning, stacklevel=2)
netvm = self.get_default_netvm(), return self.add_new_vm("QubesHVm", name=name, label=label)
label=label)
if not self.verify_new_vm (vm):
assert False, "Wrong VM description!"
self[vm.qid]=vm
return vm
def add_new_disposablevm(self, name, template, dispid, def add_new_disposablevm(self, name, template, dispid,
label = None, netvm = None): label = None, netvm = None):
qid = self.get_new_unused_qid() warnings.warn("Call to deprecated function, use add_new_vm instead",
vm = QubesDisposableVm (qid=qid, name=name, template=template, DeprecationWarning, stacklevel=2)
return self.add_new_vm("QubesDisposableVm", name=name, template=template,
netvm = netvm, netvm = netvm,
label=label, dispid=dispid) label=label, dispid=dispid)
if not self.verify_new_vm (vm):
assert False, "Wrong VM description!"
self[vm.qid]=vm
return vm
def add_new_templatevm(self, name, def add_new_templatevm(self, name,
dir_path = None, conf_file = None, dir_path = None, conf_file = None,
root_img = None, private_img = None, root_img = None, private_img = None,
installed_by_rpm = True): installed_by_rpm = True):
qid = self.get_new_unused_qid() warnings.warn("Call to deprecated function, use add_new_vm instead",
vm = QubesTemplateVm (qid=qid, name=name, DeprecationWarning, stacklevel=2)
return self.add_new_vm("QubesTemplateVm", name=name,
dir_path=dir_path, conf_file=conf_file, dir_path=dir_path, conf_file=conf_file,
root_img=root_img, private_img=private_img, root_img=root_img, private_img=private_img,
installed_by_rpm=installed_by_rpm, installed_by_rpm=installed_by_rpm,
@ -2684,69 +2702,33 @@ class QubesVmCollection(dict):
kernel = self.get_default_kernel(), kernel = self.get_default_kernel(),
uses_default_kernel = True) uses_default_kernel = True)
if not self.verify_new_vm (vm):
assert False, "Wrong VM description!"
self[vm.qid]=vm
if self.default_template_qid is None:
self.set_default_template(vm)
return vm
def add_new_netvm(self, name, template, def add_new_netvm(self, name, template,
dir_path = None, conf_file = None, dir_path = None, conf_file = None,
private_img = None, installed_by_rpm = False, private_img = None, installed_by_rpm = False,
label = None): label = None):
qid = self.get_new_unused_qid() warnings.warn("Call to deprecated function, use add_new_vm instead",
netid = self.get_new_unused_netid() DeprecationWarning, stacklevel=2)
vm = QubesNetVm (qid=qid, name=name, template=template, return self.add_new_vm("QubesNetVm", name=name, template=template,
netid=netid, label=label, label=label,
private_img=private_img, installed_by_rpm=installed_by_rpm, private_img=private_img, installed_by_rpm=installed_by_rpm,
kernel = self.get_default_kernel(),
uses_default_kernel = True, uses_default_kernel = True,
dir_path=dir_path, conf_file=conf_file) dir_path=dir_path, conf_file=conf_file)
if not self.verify_new_vm (vm):
assert False, "Wrong VM description!"
self[vm.qid]=vm
if self.default_fw_netvm_qid is None:
self.set_default_fw_netvm(vm)
# by default ClockVM is the first NetVM
if self.clockvm_qid is None:
self.set_clockvm_vm(vm)
return vm
def add_new_proxyvm(self, name, template, def add_new_proxyvm(self, name, template,
dir_path = None, conf_file = None, dir_path = None, conf_file = None,
private_img = None, installed_by_rpm = False, private_img = None, installed_by_rpm = False,
label = None): label = None):
qid = self.get_new_unused_qid() warnings.warn("Call to deprecated function, use add_new_vm instead",
netid = self.get_new_unused_netid() DeprecationWarning, stacklevel=2)
vm = QubesProxyVm (qid=qid, name=name, template=template, return self.add_new_vm("QubesProxyVm", name=name, template=template,
netid=netid, label=label, label=label,
private_img=private_img, installed_by_rpm=installed_by_rpm, private_img=private_img, installed_by_rpm=installed_by_rpm,
dir_path=dir_path, conf_file=conf_file, dir_path=dir_path, conf_file=conf_file,
kernel = self.get_default_kernel(),
uses_default_kernel = True, uses_default_kernel = True,
netvm = self.get_default_fw_netvm()) netvm = self.get_default_fw_netvm())
if not self.verify_new_vm (vm):
assert False, "Wrong VM description!"
self[vm.qid]=vm
if self.default_netvm_qid is None:
self.set_default_netvm(vm)
if self.updatevm_qid is None:
self.set_updatevm_vm(vm)
return vm
def set_default_template(self, vm): def set_default_template(self, vm):
assert vm.is_template(), "VM {0} is not a TemplateVM!".format(vm.name) assert vm.is_template(), "VM {0} is not a TemplateVM!".format(vm.name)
self.default_template_qid = vm.qid self.default_template_qid = vm.qid