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(a)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
--
To view, visit
http://gerrit.ovirt.org/21066
To unsubscribe, visit
http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Idc8383cbce78490c8dfab1c253883a06459f1547
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Martin Polednik <mpoledni(a)redhat.com>