Move QubesVM.{name,qid,uuid,label} to BaseVM
Reduce strange code in BaseVM (accessing non-existing self.name) and code duplication.
This commit is contained in:
parent
dc0e1a5481
commit
9f88fa7f0c
@ -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')
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user