Martin Polednik has uploaded a new change for review.
Change subject: vmDevices: add mechanism to persist vmDevice defaults ......................................................................
vmDevices: add mechanism to persist vmDevice defaults
Multiple vmDevices such as BalloonDevice or watchdogDevice currently do not persist their defaults in class attributes but rather use them in XML directly, hiding their existence. This patch aims to change this behavior by implementing vmDevice._defaults(), which adds the default attributes to instance directly to enable future persistence of these classes.
Change-Id: Idc8383cbce78490c8dfab1c253883a06459f1547 Signed-off-by: Martin Polednik mpoledni@redhat.com --- M tests/vmTests.py M vdsm/vm.py 2 files changed, 45 insertions(+), 29 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/66/21066/1
diff --git a/tests/vmTests.py b/tests/vmTests.py index 1f69f0a..5c813fc 100644 --- a/tests/vmTests.py +++ b/tests/vmTests.py @@ -261,7 +261,7 @@
def testWatchdogXML(self): watchdogXML = '<watchdog action="none" model="i6300esb"/>' - dev = {'device': 'watchdog', 'type': 'watchdog', + dev = {'device': 'watchdog', 'specParams': {'model': 'i6300esb', 'action': 'none'}} watchdog = vm.WatchdogDevice(self.conf, self.log, **dev) self.assertXML(watchdog.getXML(), watchdogXML) diff --git a/vdsm/vm.py b/vdsm/vm.py index 796735f..36b9598 100644 --- a/vdsm/vm.py +++ b/vdsm/vm.py @@ -1167,6 +1167,7 @@
class VmDevice(object): def __init__(self, conf, log, **kwargs): + self.specParams = {} for attr, value in kwargs.iteritems(): try: setattr(self, attr, value) @@ -1175,6 +1176,10 @@ self.conf = conf self.log = log self._deviceXML = None + self._defaults() + + def _defaults(self): + pass
def __str__(self): attrs = [':'.join((a, str(getattr(self, a)))) for a in dir(self) @@ -1192,10 +1197,13 @@ elemAttrs['type'] = deviceType
for attrName in attributes: - if not hasattr(self, attrName): + if attrName in self.specParams: + attr = self.specParams[attrName] + elif hasattr(self, attrName): + attr = getattr(self, attrName) + else: continue
- attr = getattr(self, attrName) if isinstance(attr, dict): element.appendChildWithArgs(attrName, **attr) else: @@ -1215,37 +1223,44 @@
class ControllerDevice(VmDevice): + def _defaults(self): + if self.device == 'virtio-serial': + if 'index' not in self.specParams: + self.index = '0' + if 'ports' not in self.specParams: + self.ports = '16'
def getXML(self): """ Create domxml for controller device """ ctrl = self.createXmlElem('controller', self.device, - ['index', 'model', 'master', 'address']) - if self.device == 'virtio-serial': - ctrl.setAttrs(index='0', ports='16') + ['index', 'model', 'master', 'address', + 'ports'])
return ctrl
class VideoDevice(VmDevice): + def _defaults(self): + if 'vram' not in self.specParams: + self.specParams['vram'] = '32768' + if 'heads' not in self.specParams: + self.specParams['heads'] = '1'
def getXML(self): """ Create domxml for video device """ video = self.createXmlElem('video', None, ['address']) - sourceAttrs = {'vram': self.specParams.get('vram', '32768'), - 'heads': self.specParams.get('heads', '1')} - if 'ram' in self.specParams: - sourceAttrs['ram'] = self.specParams['ram'] + model = self.createXmlElem('model', self.device, + ['vram', 'heads', 'ram'])
- video.appendChildWithArgs('model', type=self.device, **sourceAttrs) + video.appendChild(model) return video
class SoundDevice(VmDevice): - def getXML(self): """ Create domxml for sound device @@ -1256,7 +1271,6 @@
class NetworkInterfaceDevice(VmDevice): - def __init__(self, conf, log, **kwargs): # pyLint can't tell that the Device.__init__() will # set a nicModel attribute, so modify the kwarg list @@ -1646,7 +1660,6 @@
class BalloonDevice(VmDevice): - def getXML(self): """ Create domxml for a memory balloon device. @@ -1662,11 +1675,11 @@
class WatchdogDevice(VmDevice): - def __init__(self, *args, **kwargs): - super(WatchdogDevice, self).__init__(*args, **kwargs) - - if not hasattr(self, 'specParams'): - self.specParams = {} + def _defaults(self): + if 'model' not in self.specParams: + self.specParams['model'] = 'i6300esb' + if 'action' not in self.specParams: + self.specParams['action'] = 'none'
def getXML(self): """ @@ -1677,9 +1690,8 @@ function='0x0'/> </watchdog> """ - m = self.createXmlElem(self.type, None, ['address']) - m.setAttrs(model=self.specParams.get('model', 'i6300esb'), - action=self.specParams.get('action', 'none')) + m = self.createXmlElem(self.device, None, ['address', 'model', + 'action']) return m
@@ -1692,11 +1704,8 @@ <address ... /> </smartcard> """ - card = self.createXmlElem(self.device, None, ['address']) - sourceAttrs = {'mode': self.specParams['mode']} - if sourceAttrs['mode'] != 'host': - sourceAttrs['type'] = self.specParams['type'] - card.setAttrs(**sourceAttrs) + card = self.createXmlElem(self.device, None, ['address', 'mode', + 'type']) return card
@@ -1713,6 +1722,12 @@
class ConsoleDevice(VmDevice): + def _defaults(self): + self.type = 'pty' + self.port = '0' + self.specParams['type'] = 'virtio' + self.specParams['port'] = '0' + def getXML(self): """ Create domxml for a console device. @@ -1721,8 +1736,9 @@ <target type='virtio' port='0'/> </console> """ - m = self.createXmlElem('console', 'pty') - m.appendChildWithArgs('target', type='virtio', port='0') + m = self.createXmlElem(self.device, self.type) + m.appendChildWithArgs('target', None, type=self.specParams['type'], + port=self.specParams['port']) return m
oVirt Jenkins CI Server has posted comments on this change.
Change subject: vmDevices: add mechanism to persist vmDevice defaults ......................................................................
Patch Set 1:
Build Successful
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/4497/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/5297/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/5375/ : SUCCESS
oVirt Jenkins CI Server has posted comments on this change.
Change subject: vmDevices: add mechanism to persist vmDevice defaults ......................................................................
Patch Set 2:
Build Successful
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/4498/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/5298/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/5376/ : SUCCESS
Federico Simoncelli has posted comments on this change.
Change subject: vmDevices: add mechanism to persist vmDevice defaults ......................................................................
Patch Set 2: Code-Review+1
(1 comment)
I like the idea, just check if the getattr part can be simplified.
http://gerrit.ovirt.org/#/c/21066/2/vdsm/vm.py File vdsm/vm.py:
Line 1199: for attrName in attributes: Line 1200: if attrName in self.specParams: Line 1201: attr = self.specParams[attrName] Line 1202: elif hasattr(self, attrName): Line 1203: attr = getattr(self, attrName) getattr also supports a default (if the attr is missing). Line 1204: else: Line 1205: continue Line 1206: Line 1207: if isinstance(attr, dict):
oVirt Jenkins CI Server has posted comments on this change.
Change subject: vmDevices: add mechanism to persist vmDevice defaults ......................................................................
Patch Set 3:
Build Successful
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5984/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6772/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6878/ : SUCCESS
Francesco Romani has posted comments on this change.
Change subject: vmDevices: add mechanism to persist vmDevice defaults ......................................................................
Patch Set 3: Code-Review+1
Federico Simoncelli has posted comments on this change.
Change subject: vmDevices: add mechanism to persist vmDevice defaults ......................................................................
Patch Set 3: Code-Review+1
(1 comment)
I'd like to see _defaults being renamed.
http://gerrit.ovirt.org/#/c/21066/3/vdsm/vm.py File vdsm/vm.py:
Line 1249: pass Line 1250: self.conf = conf Line 1251: self.log = log Line 1252: self._deviceXML = None Line 1253: self._defaults() Can we rename this to something like: _initializeDefaults(...) or something better. Line 1254: Line 1255: def _defaults(self): Line 1256: pass Line 1257:
Francesco Romani has posted comments on this change.
Change subject: vmDevices: add mechanism to persist vmDevice defaults ......................................................................
Patch Set 3:
I agree with Federico's remark. +1 for me once the _defaults() name is improved.
Itamar Heim has posted comments on this change.
Change subject: vmDevices: add mechanism to persist vmDevice defaults ......................................................................
Patch Set 3:
ping
Itamar Heim has abandoned this change.
Change subject: vmDevices: add mechanism to persist vmDevice defaults ......................................................................
Abandoned
no activity. please restore if relevant.
vdsm-patches@lists.fedorahosted.org