From 9f88fa7f0cab4f7ff7e5acb4856dbcbbfe0856e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Tue, 26 Sep 2017 13:24:22 +0200 Subject: [PATCH] Move QubesVM.{name,qid,uuid,label} to BaseVM Reduce strange code in BaseVM (accessing non-existing self.name) and code duplication. --- qubes/tests/vm/qubesvm.py | 6 ++--- qubes/vm/__init__.py | 51 ++++++++++++++++++++++++++++++--------- qubes/vm/adminvm.py | 8 ++---- qubes/vm/qubesvm.py | 35 --------------------------- 4 files changed, 45 insertions(+), 55 deletions(-) diff --git a/qubes/tests/vm/qubesvm.py b/qubes/tests/vm/qubesvm.py index b98363a4..e7c299aa 100644 --- a/qubes/tests/vm/qubesvm.py +++ b/qubes/tests/vm/qubesvm.py @@ -71,15 +71,15 @@ class TC_00_setters(qubes.tests.QubesTestCase): def test_000_setter_qid(self): self.assertEqual( - qubes.vm.qubesvm._setter_qid(self.vm, self.prop, 5), 5) + qubes.vm._setter_qid(self.vm, self.prop, 5), 5) def test_001_setter_qid_lt_0(self): with self.assertRaises(ValueError): - qubes.vm.qubesvm._setter_qid(self.vm, self.prop, -1) + qubes.vm._setter_qid(self.vm, self.prop, -1) def test_002_setter_qid_gt_max(self): with self.assertRaises(ValueError): - qubes.vm.qubesvm._setter_qid(self.vm, + qubes.vm._setter_qid(self.vm, self.prop, qubes.config.max_qid + 5) @unittest.skip('test not implemented') diff --git a/qubes/vm/__init__.py b/qubes/vm/__init__.py index fe2e1288..853d57a7 100644 --- a/qubes/vm/__init__.py +++ b/qubes/vm/__init__.py @@ -27,6 +27,7 @@ import asyncio import re import string +import uuid import lxml.etree @@ -64,6 +65,26 @@ def validate_name(holder, prop, value): raise qubes.exc.QubesValueError( 'VM name cannot be \'none\' nor \'default\'') +def setter_label(self, prop, value): + ''' Helper for setting the domain label ''' + # pylint: disable=unused-argument + if isinstance(value, qubes.Label): + return value + if isinstance(value, str) and value.startswith('label-'): + return self.app.labels[int(value.split('-', 1)[1])] + + return self.app.get_label(value) + + +def _setter_qid(self, prop, value): + ''' Helper for setting the domain qid ''' + # pylint: disable=unused-argument + value = int(value) + if not 0 <= value <= qubes.config.max_qid: + raise ValueError( + '{} value must be between 0 and qubes.config.max_qid'.format( + prop.__name__)) + return value class Features(dict): '''Manager of the features. @@ -262,6 +283,25 @@ class BaseVM(qubes.PropertyHolder): ''' # pylint: disable=no-member + uuid = qubes.property('uuid', type=uuid.UUID, write_once=True, + clone=False, + doc='UUID from libvirt.') + + name = qubes.property('name', type=str, write_once=True, + clone=False, + doc='User-specified name of the domain.') + + qid = qubes.property('qid', type=int, write_once=True, + setter=_setter_qid, + clone=False, + doc='''Internal, persistent identificator of particular domain. Note + this is different from Xen domid.''') + + label = qubes.property('label', + setter=setter_label, + doc='''Colourful label assigned to VM. This is where the colour of the + padlock is set.''') + def __init__(self, app, xml, features=None, devices=None, tags=None, **kwargs): # pylint: disable=redefined-outer-name @@ -531,14 +571,3 @@ class VMProperty(qubes.property): return untrusted_vmname validate_name(None, self, untrusted_vmname) return untrusted_vmname - - -def setter_label(self, prop, value): - ''' Helper for setting the domain label ''' - # pylint: disable=unused-argument - if isinstance(value, qubes.Label): - return value - if isinstance(value, str) and value.startswith('label-'): - return self.app.labels[int(value.split('-', 1)[1])] - - return self.app.get_label(value) diff --git a/qubes/vm/adminvm.py b/qubes/vm/adminvm.py index 868656fb..dbee80c3 100644 --- a/qubes/vm/adminvm.py +++ b/qubes/vm/adminvm.py @@ -24,10 +24,12 @@ ''' This module contains the AdminVM implementation ''' import libvirt + import qubes import qubes.exc import qubes.vm + class AdminVM(qubes.vm.BaseVM): '''Dom0''' @@ -36,12 +38,6 @@ class AdminVM(qubes.vm.BaseVM): name = qubes.property('name', default='dom0', setter=qubes.property.forbidden) - label = qubes.property('label', - setter=qubes.vm.setter_label, - saver=(lambda self, prop, value: 'label-{}'.format(value.index)), - doc='''Colourful label assigned to VM. This is where the colour of the - padlock is set.''') - qid = qubes.property('qid', default=0, setter=qubes.property.forbidden) diff --git a/qubes/vm/qubesvm.py b/qubes/vm/qubesvm.py index f57dee11..67dcabd2 100644 --- a/qubes/vm/qubesvm.py +++ b/qubes/vm/qubesvm.py @@ -59,17 +59,6 @@ MEM_OVERHEAD_BASE = (3 + 1) * 1024 * 1024 MEM_OVERHEAD_PER_VCPU = 3 * 1024 * 1024 / 2 -def _setter_qid(self, prop, value): - ''' Helper for setting the domain qid ''' - # pylint: disable=unused-argument - value = int(value) - if not 0 <= value <= qubes.config.max_qid: - raise ValueError( - '{} value must be between 0 and qubes.config.max_qid'.format( - prop.__name__)) - return value - - def _setter_kernel(self, prop, value): ''' Helper for setting the domain kernel and running sanity checks on it. ''' # pylint: disable=unused-argument @@ -369,30 +358,6 @@ class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM): # properties loaded from XML # - label = qubes.property('label', - setter=qubes.vm.setter_label, - saver=(lambda self, prop, value: 'label-{}'.format(value.index)), - doc='''Colourful label assigned to VM. This is where the colour of the - padlock is set.''') - -# provides_network = qubes.property('provides_network', -# type=bool, setter=qubes.property.bool, -# doc='`True` if it is NetVM or ProxyVM, false otherwise.') - - qid = qubes.property('qid', type=int, write_once=True, - setter=_setter_qid, - clone=False, - doc='''Internal, persistent identificator of particular domain. Note - this is different from Xen domid.''') - - name = qubes.property('name', type=str, write_once=True, - clone=False, - doc='User-specified name of the domain.') - - uuid = qubes.property('uuid', type=uuid.UUID, write_once=True, - clone=False, - doc='UUID from libvirt.') - virt_mode = qubes.property('virt_mode', type=str, setter=_setter_virt_mode, default='hvm',