core: rewrite "eval" to "func" attribute handlers

This commit is contained in:
Marek Marczykowski-Górecki 2014-03-26 04:41:28 +01:00
parent 5141aba741
commit e9fe890acb
8 changed files with 102 additions and 46 deletions

View File

@ -92,12 +92,23 @@ class QubesVm(object):
"qid": { "attr": "_qid", "order": 0 },
"name": { "order": 1 },
"dir_path": { "default": None, "order": 2 },
"conf_file": { "eval": 'self.absolute_path(value, self.name + ".conf")', 'order': 3 },
"conf_file": {
"func": lambda value: self.absolute_path(value, self.name +
".conf"),
"order": 3 },
### order >= 10: have base attrs set
"root_img": { "eval": 'self.absolute_path(value, vm_files["root_img"])', 'order': 10 },
"private_img": { "eval": 'self.absolute_path(value, vm_files["private_img"])', 'order': 10 },
"volatile_img": { "eval": 'self.absolute_path(value, vm_files["volatile_img"])', 'order': 10 },
"firewall_conf": { "eval": 'self.absolute_path(value, vm_files["firewall_conf"])', 'order': 10 },
"root_img": {
"func": self._absolute_path_gen(vm_files["root_img"]),
"order": 10 },
"private_img": {
"func": self._absolute_path_gen(vm_files["private_img"]),
"order": 10 },
"volatile_img": {
"func": self._absolute_path_gen(vm_files["volatile_img"]),
"order": 10 },
"firewall_conf": {
"func": self._absolute_path_gen(vm_files["firewall_conf"]),
"order": 10 },
"installed_by_rpm": { "default": False, 'order': 10 },
"template": { "default": None, "attr": '_template', 'order': 10 },
### order >= 20: have template set
@ -107,38 +118,62 @@ class QubesVm(object):
'xml_deserialize': lambda _x: QubesVmLabels[_x] },
"memory": { "default": defaults["memory"], 'order': 20 },
"maxmem": { "default": None, 'order': 25 },
"pcidevs": { "default": '[]', 'order': 25, "eval": \
'[] if value in ["none", None] else eval(value) if value.find("[") >= 0 else eval("[" + value + "]")' },
"pcidevs": {
"default": '[]',
"order": 25,
"func": lambda value: [] if value in ["none", None] else
eval(value) if value.find("[") >= 0 else
eval("[" + value + "]") },
# Internal VM (not shown in qubes-manager, doesn't create appmenus entries
"internal": { "default": False },
"vcpus": { "default": None },
"uses_default_kernel": { "default": True, 'order': 30 },
"uses_default_kernelopts": { "default": True, 'order': 30 },
"kernel": { "default": None, 'order': 31,
'eval': 'collection.get_default_kernel() if self.uses_default_kernel else value' },
"kernelopts": { "default": "", 'order': 31, "eval": \
'value if not self.uses_default_kernelopts else defaults["kernelopts_pcidevs"] if len(self.pcidevs) > 0 else defaults["kernelopts"]' },
"kernel": {
"default": None,
"order": 31,
"func": lambda value: self._collection.get_default_kernel() if
self.uses_default_kernel else value },
"kernelopts": {
"default": "",
"order": 31,
"func": lambda value: value if not self.uses_default_kernelopts\
else defaults["kernelopts_pcidevs"] if len(self.pcidevs)>0 \
else defaults["kernelopts"] },
"mac": { "attr": "_mac", "default": None },
"include_in_backups": { "default": True },
"services": { "default": {}, "eval": "eval(str(value))" },
"services": {
"default": {},
"func": lambda value: eval(str(value)) },
"debug": { "default": False },
"default_user": { "default": "user" },
"qrexec_timeout": { "default": 60 },
"autostart": { "default": False, "attr": "_autostart" },
"backup_content" : { 'default': False },
"backup_size" : { 'default': 0, "eval": "int(value)" },
"backup_size" : {
"default": 0,
"func": int },
"backup_path" : { 'default': "" },
"backup_timestamp": { 'eval': 'datetime.datetime.fromtimestamp('
'int(value)) if value else None' },
"backup_timestamp": {
"func": lambda value:
datetime.datetime.fromtimestamp(int(value)) if value
else None },
##### Internal attributes - will be overriden in __init__ regardless of args
"config_file_template": { "eval": 'system_path["config_template_pv"]' },
"icon_path": { "eval": 'os.path.join(self.dir_path, "icon.png") if self.dir_path is not None else None' },
"config_file_template": {
"func": lambda x: system_path["config_template_pv"] },
"icon_path": {
"func": lambda x: os.path.join(self.dir_path, "icon.png") if
self.dir_path is not None else None },
# used to suppress side effects of clone_attrs
"_do_not_reset_firewall": { "eval": 'False' },
"kernels_dir": { 'eval': 'os.path.join(system_path["qubes_kernels_base_dir"], self.kernel) if self.kernel is not None else ' + \
"_do_not_reset_firewall": { "func": lambda x: False },
"kernels_dir": {
# for backward compatibility (or another rare case): kernel=None -> kernel in VM dir
'os.path.join(self.dir_path, vm_files["kernels_subdir"])' },
"_start_guid_first": { 'eval': 'False' },
"func": lambda x: \
os.path.join(system_path["qubes_kernels_base_dir"],
self.kernel) if self.kernel is not None \
else os.path.join(self.dir_path,
vm_files["kernels_subdir"]) },
"_start_guid_first": { "func": lambda x: False },
}
### Mark attrs for XML inclusion
@ -189,9 +224,9 @@ class QubesVm(object):
def __init__(self, **kwargs):
collection = None
self._collection = None
if 'collection' in kwargs:
collection = kwargs['collection']
self._collection = kwargs['collection']
else:
raise ValueError("No collection given to QubesVM constructor")
@ -199,8 +234,8 @@ class QubesVm(object):
if "xml_element" in kwargs and kwargs["xml_element"].get("template_qid"):
template_qid = kwargs["xml_element"].get("template_qid")
if template_qid.lower() != "none":
if int(template_qid) in collection:
kwargs["template"] = collection[int(template_qid)]
if int(template_qid) in self._collection:
kwargs["template"] = self._collection[int(template_qid)]
else:
raise ValueError("Unknown template with QID %s" % template_qid)
attrs = self.get_attrs_config()
@ -283,6 +318,9 @@ class QubesVm(object):
else:
return os.path.join(self.dir_path, (arg if arg is not None else default))
def _absolute_path_gen(self, default):
return lambda value: self.absolute_path(value, default)
def relative_path(self, arg):
return arg.replace(self.dir_path + '/', '')

View File

@ -39,15 +39,19 @@ class QubesTemplateVm(QubesVm):
def get_attrs_config(self):
attrs_config = super(QubesTemplateVm, self).get_attrs_config()
attrs_config['dir_path']['eval'] = 'value if value is not None else os.path.join(system_path["qubes_templates_dir"], self.name)'
attrs_config['dir_path']['func'] = \
lambda value: value if value is not None else \
os.path.join(system_path["qubes_templates_dir"], self.name)
attrs_config['label']['default'] = defaults["template_label"]
# New attributes
# Image for template changes
attrs_config['rootcow_img'] = { 'eval': 'os.path.join(self.dir_path, vm_files["rootcow_img"])' }
attrs_config['rootcow_img'] = {
'func': lambda x: os.path.join(self.dir_path, vm_files["rootcow_img"]) }
# Clean image for root-cow and swap (AppVM side)
attrs_config['clean_volatile_img'] = { 'eval': 'os.path.join(self.dir_path, vm_files["clean_volatile_img"])' }
attrs_config['clean_volatile_img'] = {
'func': lambda x: os.path.join(self.dir_path, vm_files["clean_volatile_img"]) }
return attrs_config

View File

@ -38,15 +38,20 @@ class QubesNetVm(QubesVm):
def get_attrs_config(self):
attrs_config = super(QubesNetVm, self).get_attrs_config()
attrs_config['dir_path']['eval'] = 'value if value is not None else os.path.join(system_path["qubes_servicevms_dir"], self.name)'
attrs_config['dir_path']['func'] = \
lambda value: value if value is not None else \
os.path.join(system_path["qubes_servicevms_dir"], self.name)
attrs_config['label']['default'] = defaults["servicevm_label"]
attrs_config['memory']['default'] = 200
# New attributes
attrs_config['netid'] = { 'save': 'str(self.netid)', 'order': 30,
'eval': 'value if value is not None else collection.get_new_unused_netid()' }
attrs_config['netprefix'] = { 'eval': '"10.137.{0}.".format(self.netid)' }
attrs_config['dispnetprefix'] = { 'eval': '"10.138.{0}.".format(self.netid)' }
'func': lambda value: value if value is not None else
self._collection.get_new_unused_netid() }
attrs_config['netprefix'] = {
'func': lambda x: "10.137.{0}.".format(self.netid) }
attrs_config['dispnetprefix'] = {
'func': lambda x: "10.138.{0}.".format(self.netid) }
# Dont save netvm prop
attrs_config['netvm'].pop('save')

View File

@ -36,9 +36,10 @@ class QubesProxyVm(QubesNetVm):
def get_attrs_config(self):
attrs_config = super(QubesProxyVm, self).get_attrs_config()
attrs_config['uses_default_netvm']['eval'] = 'False'
attrs_config['uses_default_netvm']['func'] = lambda x: False
# Save netvm prop again
attrs_config['netvm']['save'] = 'str(self.netvm.qid) if self.netvm is not None else "none"'
attrs_config['netvm']['save'] = \
lambda x: str(self.netvm.qid) if self.netvm is not None else "none"
return attrs_config

View File

@ -23,7 +23,7 @@
import os.path
from qubes.qubes import QubesVm,QubesVmLabel,register_qubes_vm_class
from qubes.qubes import QubesVm,QubesVmLabel,register_qubes_vm_class,system_path
class QubesAppVm(QubesVm):
"""
@ -31,7 +31,9 @@ class QubesAppVm(QubesVm):
"""
def get_attrs_config(self):
attrs_config = super(QubesAppVm, self).get_attrs_config()
attrs_config['dir_path']['eval'] = 'value if value is not None else os.path.join(system_path["qubes_appvms_dir"], self.name)'
attrs_config['dir_path']['func'] = \
lambda value: value if value is not None else \
os.path.join(system_path["qubes_appvms_dir"], self.name)
return attrs_config

View File

@ -37,8 +37,8 @@ class QubesDisposableVm(QubesVm):
attrs_config = super(QubesDisposableVm, self).get_attrs_config()
# New attributes
attrs_config['dispid'] = { 'save': 'str(self.dispid)' }
attrs_config['include_in_backups']['eval'] = 'False'
attrs_config['dispid'] = { 'save': lambda: str(self.dispid) }
attrs_config['include_in_backups']['func'] = lambda x: False
return attrs_config

View File

@ -55,18 +55,22 @@ class QubesHVm(QubesVm):
attrs.pop('kernelopts')
attrs.pop('uses_default_kernel')
attrs.pop('uses_default_kernelopts')
attrs['dir_path']['eval'] = 'value if value is not None else os.path.join(system_path["qubes_appvms_dir"], self.name)'
attrs['config_file_template']['eval'] = 'system_path["config_template_hvm"]'
attrs['drive'] = { 'attr': '_drive', 'save': 'str(self.drive)' }
attrs['dir_path']['func'] = lambda value: value if value is not None \
else os.path.join(system_path["qubes_appvms_dir"], self.name)
attrs['config_file_template']['func'] = \
lambda x: system_path["config_template_hvm"]
attrs['drive'] = { 'attr': '_drive',
'save': lambda: str(self.drive) }
attrs['maxmem'].pop('save')
attrs['timezone'] = { 'default': 'localtime', 'save': 'str(self.timezone)' }
attrs['timezone'] = { 'default': 'localtime',
'save': lambda: str(self.timezone) }
attrs['qrexec_installed'] = { 'default': False,
'attr': '_qrexec_installed',
'save': 'str(self._qrexec_installed)' }
'save': lambda: str(self._qrexec_installed) }
attrs['guiagent_installed'] = { 'default' : False,
'attr': '_guiagent_installed',
'save': 'str(self._guiagent_installed)' }
attrs['_start_guid_first']['eval'] = 'True'
'save': lambda: str(self._guiagent_installed) }
attrs['_start_guid_first']['func'] = lambda x: True
attrs['services']['default'] = "{'meminfo-writer': False}"
attrs['memory']['default'] = defaults["hvm_memory"]

View File

@ -42,7 +42,9 @@ class QubesTemplateHVm(QubesHVm):
def get_attrs_config(self):
attrs_config = super(QubesTemplateHVm, self).get_attrs_config()
attrs_config['dir_path']['eval'] = 'value if value is not None else os.path.join(system_path["qubes_templates_dir"], self.name)'
attrs_config['dir_path']['func'] = \
lambda value: value if value is not None else \
os.path.join(system_path["qubes_templates_dir"], self.name)
attrs_config['label']['default'] = defaults["template_label"]
return attrs_config