[Kimchi-devel] [PATCH 1/4] support create/delete VMIface: update model

Sheldon shaohef at linux.vnet.ibm.com
Wed Jan 22 08:23:54 UTC 2014


On 01/22/2014 10:14 AM, Mark Wu wrote:
> On 01/22/2014 01:25 AM, shaohef at linux.vnet.ibm.com wrote:
>> From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
>>
>> attach a network interface to vm in model
>> detach a network interface for vm in model
>>
>> Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
>> ---
>> src/kimchi/model.py | 51 
>> ++++++++++++++++++++++++++++++++++++++++++++++++++-
>> 1 file changed, 50 insertions(+), 1 deletion(-)
>>
>> diff --git a/src/kimchi/model.py b/src/kimchi/model.py
>> index 383509d..fbc920d 100644
>> --- a/src/kimchi/model.py
>> +++ b/src/kimchi/model.py
>> @@ -33,6 +33,7 @@ import logging
>> import os
>> import platform
>> import psutil
>> +import random
>> import re
>> import shutil
>> import subprocess
>> @@ -45,7 +46,7 @@ import uuid
>> from cherrypy.process.plugins import BackgroundTask
>> from cherrypy.process.plugins import SimplePlugin
>> from collections import defaultdict
>> -from lxml import objectify
>> +from lxml import objectify, etree
>> from xml.etree import ElementTree
>>
>>
>> @@ -968,6 +969,44 @@ class Model(object):
>> iface.destroy()
>> iface.undefine()
>>
>> + def vmifaces_create(self, vm, params):
>> + def randomMAC():
>> + mac = [0x52, 0x54, 0x00,
>> + random.randint(0x00, 0x7f),
>> + random.randint(0x00, 0xff),
>> + random.randint(0x00, 0xff)]
>> + return ':'.join(map(lambda x: "%02x" % x, mac))
>> +
>> + if (params["type"] == "network" and
>> + params["network"] not in self.networks_get_list()):
>> + raise InvalidParameter("%s is not an available network" %
>> + params["network"])
>> +
>> + dom = self._get_vm(vm)
>> + macs = (iface.mac.get('address')
>> + for iface in self._get_vmifaces(vm))
>> +
>> + mac = randomMAC()
>> + while True:
>> + if mac not in macs:
>> + break
>> + mac = randomMAC()
>> +
>> + E = objectify.E
>> + children = [E.mac(address=mac)]
>> + ("network" in params.keys() and
>> + children.append(E.source(network=params['network'])))
>> + ("model" in params.keys() and
>> + children.append(E.model(type=params['model'])))
>> + attrib = {"type": params["type"]}
> Any reason not using lxml.builder ?

seems a difference.
objectify support python type date. builder does not support python type 
date.

In [74]: from lxml.builder import E

In [75]: root = E.root(
E.a("5"),
E.b("6.1"),
E.c("True"),
E.d("how", tell="me")
)

In [76]: print(etree.tostring(root, pretty_print=True))
<root>
<a>5</a>
<b>6.1</b>
<c>True</c>
<d tell="me">how</d>
</root>



In [79]: from lxml.objectify import E

In [80]: root = E.root(
E.a(5),
E.b(6.1),
E.c(True),
E.d("how", tell="me")
)

In [81]: print(etree.tostring(root, pretty_print=True))
<root xmlns:py="http://codespeak.net/lxml/objectify/pytype" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<a py:pytype="int">5</a>
<b py:pytype="float">6.1</b>
<c py:pytype="bool">true</c>
<d tell="me" py:pytype="str">how</d>
</root>


>> +
>> + xml = etree.tostring(E.interface(*children, **attrib))
>> +
>> + dom.attachDeviceFlags(xml,
>> + libvirt.VIR_DOMAIN_AFFECT_CURRENT)
> I think we need affect both the running guest state and persist config.
>> +
>> + return mac
>> +
>> def _get_vmifaces(self, vm):
>> dom = self._get_vm(vm)
>> xml = dom.XMLDesc(0)
>> @@ -1003,6 +1042,16 @@ class Model(object):
>>
>> return info
>>
>> + def vmiface_delete(self, vm, mac):
>> + dom = self._get_vm(vm)
>> + iface = self._get_vmiface(vm, mac)
>> +
>> + if iface is None:
>> + raise NotFoundError('iface: "%s"' % mac)
>> +
>> + dom.detachDeviceFlags(etree.tostring(iface),
>> + libvirt.VIR_DOMAIN_AFFECT_CURRENT)
>> +
>> def add_task(self, target_uri, fn, opaque=None):
>> id = self.next_taskid
>> self.next_taskid = self.next_taskid + 1
>
>
>


-- 
Thanks and best regards!

Sheldon Feng(冯少合)<shaohef at linux.vnet.ibm.com>
IBM Linux Technology Center




More information about the Kimchi-devel mailing list