[Kimchi-devel] [PATCH 4/5] cdrom: update model.py

Rodrigo Trujillo rodrigo.trujillo at linux.vnet.ibm.com
Mon Jan 13 12:18:39 UTC 2014


On 01/06/2014 07:32 AM, lvroyce at linux.vnet.ibm.com wrote:
> From: Royce Lv <lvroyce at linux.vnet.ibm.com>
>
> Implement create/delete/update cdrom functionality in model.py.
> As _live_vm_update() is designed to manipulate multiple update params
> all at once, we construct a dict from cdroms_create to adjust to this usage.
>
> Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
> ---
>   src/kimchi/model.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
>   1 file changed, 46 insertions(+), 2 deletions(-)
>
> diff --git a/src/kimchi/model.py b/src/kimchi/model.py
> index c74c973..4dd9eff 100644
> --- a/src/kimchi/model.py
> +++ b/src/kimchi/model.py
> @@ -78,9 +78,9 @@ ISO_POOL_NAME = u'kimchi_isos'
>   GUESTS_STATS_INTERVAL = 5
>   HOST_STATS_INTERVAL = 1
>   VM_STATIC_UPDATE_PARAMS = {'name': './name'}
> -VM_LIVE_UPDATE_PARAMS = {}
>   STORAGE_SOURCES = {'netfs': {'addr': '/pool/source/host/@name',
>                                'path': '/pool/source/dir/@path'}}
> +DEVICE_FILTER = {'cdrom': {'xml': './devices/disk', 'filter_field': './devices/disk/@device'}}
>
>
>   def _uri_to_name(collection, uri):
> @@ -491,7 +491,51 @@ class Model(object):
>           return dom
>
>       def _live_vm_update(self, dom, params):
> -        pass
> +        VM_LIVE_UPDATE_PARAMS = {
> +            'device': {'handler': functools.partial(dom.attachDeviceFlags,
> +                               flags=libvirt.VIR_DOMAIN_AFFECT_CURRENT),
> +                       'formatter': _get_disk_xml}}
> +
> +        for key, val in params.items():
> +            if key in VM_LIVE_UPDATE_PARAMS:
> +                handler = VM_LIVE_UPDATE_PARAMS[key]['handler']
> +                param = VM_LIVE_UPDATE_PARAMS[key]['formatter'](**val)
> +                try:
> +                    handler(**param)
> +                except libvirt.LibvirtError:
You have missed the "as e:" , because you are using "e" below
> +                    raise OperationFailed("Cannot apply change on vm: %s", e.get_error_message())
> +
> +    def cdroms_create(self, vm_name, params):
> +        path = params.get('path')
> +        if not os.path.isfile(path):
> +            raise InvalidParameter("Path specified for CDROM is not a valid file")
> +        dom = self._get_vm(vm_name)
> +        state = Model.dom_state_map[dom.info()[0]]
> +
> +        if state == 'running':
> +            # CDROM is IDE device, which does not allow hotplug.
> +            raise InvalidOperation("CDROM can just be attatched when vm is shutoff")
Typo: attached
> +        if params['name'] in self.cdroms_get_list(vm_name):
> +            raise InvalidParameter("CDROM with this name already exsited")
Typo: exist
> +
> +        cdrom_param = {'source': {'type': 'file', 'path': params['path']},
> +                       'target': {'name': params['name'], 'type': 'cdrom', 'bus': 'ide'}}
> +        self._live_vm_update(dom, dict(device=cdrom_param))
> +        return params['name']
> +
> +    def cdroms_get_list(self, vm_name):
> +        dom = self._get_vm(vm_name)
> +        xml = dom.XMLDesc(0)
> +        device_xml = xmlutils.xml_get_child(xml, './devices')
> +        cdroms = _parse_vm_disks(device_xml, filter_path='./disk',
> +                                filter_name='device', filter_val='cdrom')
> +        return cdroms
> +
> +    def cdrom_lookup(self, vm_name, dev_name):
> +        dom = self._get_vm(vm_name)
> +        xml = dom.XMLDesc(0)
> +        device_xml = xmlutils.xml_get_child(xml, './devices')
> +        return _parse_device(device_xml, dev_name)
>
>       def vm_update(self, name, params):
>           dom = self._get_vm(name)




More information about the Kimchi-devel mailing list