On 01/22/2014 10:14 AM, Mark Wu wrote:
On 01/22/2014 01:25 AM, shaohef(a)linux.vnet.ibm.com wrote:
> From: ShaoHe Feng <shaohef(a)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(a)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 ?
lxml.objectify.ElementMaker from objectify.so,
http://lxml.de/objectify.html
lxml.builder.ElementMaker from builder.py
but seems they are same usage for both tow class instances.
In [15]: lxml.objectify.ElementMaker
Type: type
String Form:<type 'lxml.objectify.ElementMaker'>
File: /usr/lib64/python2.7/site-packages/lxml/objectify.so
Docstring:
ElementMaker(self, namespace=None, nsmap=None, annotate=True,
makeelement=None)
In [17]: lxml.builder.ElementMaker?
Type: type
String Form:<class 'lxml.builder.ElementMaker'>
File: /usr/lib64/python2.7/site-packages/lxml/builder.py
Definition: lxml.builder.ElementMaker(self, tag, *children, **attrib)
Docstring:
Element generator factory.
...
Constructor information:
Definition:lxml.builder.ElementMaker(self, typemap=None, namespace=None,
nsmap=None, makeelement=None)
> +
> + 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(a)linux.vnet.ibm.com>
IBM Linux Technology Center