On 09/06/2011 02:11 PM, Nan Zhang wrote:
--- repos/domain/update_devflag.py | 159 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 159 insertions(+), 0 deletions(-) create mode 100644 repos/domain/update_devflag.py diff --git a/repos/domain/update_devflag.py b/repos/domain/update_devflag.py new file mode 100644 index 0000000..b2d8d15 --- /dev/null +++ b/repos/domain/update_devflag.py @@ -0,0 +1,159 @@ +#!/usr/bin/evn python +"""Update virtual device to guest from an XML file +""" + +__author__ = 'Nan Zhang: nzhang@xxxxxxxxxx' +__date__ = 'Fri Sep 2, 2011' +__version__ = '0.1.0' +__credits__ = 'Copyright (C) 2011 Red Hat, Inc.' +__all__ = ['usage', 'update_devflag'] + +import os +import re +import sys +import time
Unused import time.
+from xml.dom import minidom + +def append_path(path): + """Append root path of package""" + if path in sys.path: + pass + else: + sys.path.append(path) + +pwd = os.getcwd() +result = re.search('(.*)libvirt-test-API', pwd) +append_path(result.group(0)) + +from lib import connectAPI +from lib import domainAPI +from utils.Python import utils +from utils.Python import xmlbuilder +from exception import LibvirtAPI +from repos import domain
Unused import domain.
+ +def usage(): + print '''usage: mandatory arguments: + guestname + devtype + flag + ''' + +def check_params(params): + """Verify inputing parameter dictionary""" + logger = params['logger'] + keys = ['guestname', 'devtype', 'flag'] + for key in keys: + if key not in params: + logger.error("%s is required" %key) + usage() + return 1 + return 0 + +def create_image(img_name, img_size, logger): + """Create an image file""" + stat, ret = commands.getstatusoutput("dd if=/dev/zero of=%s bs=1 \
Unimport 'commands' module.
+ count=1 seek=%s" % (img_name, img_size)) + if stat == 0: + logger.debug("create image result:\n%s" % ret) + return True + else: + return False + +def check_updated_device(guestname, domobj, srcfile): + """Check if the device is updated""" + xmlobj = domobj.get_xml_desc(guestname) + domxml = minidom.parseString(xmlobj) + + for diskTag in domxml.getElementsByTagName("source"): + if diskTag.parentNode.getAttribute("device") == 'cdrom': + upfile = diskTag.getAttribute("file") + elif diskTag.parentNode.getAttribute('device') == 'floppy': + upfile = diskTag.getAttribute("file") + else: + logger.error("No matched device was found.")
Undefined variable 'logger'.
+ + if upfile == srcfile: + return True, upfile + else: + return False, upfile + +def update_devflag(params): + """Update virtual device to a domain from xml""" + + # Initiate and check parameters + params_check_result = check_params(params) + if params_check_result: + return 1 + logger = params['logger'] + guestname = params['guestname'] + devtype = params['devtype'] + if devtype == 'cdrom': + xmlargs = {} + xmlargs['guesttype'] = 'kvm' + xmlargs['hdmodel'] = 'ide' + xmlargs['bootcd'] = '/var/lib/libvirt/boot/cdrom.img' + srcfile = xmlargs['bootcd'] + create_image(srcfile, 100M, logger)
Invalid syntax, s/100M/'100M'/
+ elif devtype = 'floppy':
Invalid syntax, s/=/==/.
+ xmlargs = {} + xmlargs['floppysource'] = '/var/lib/libvirt/boot/floppy.img' + srcfile = xmlargs['floppysource'] + create_image(srcfile, 2M, logger)
Same as above.
+ else: + logger.error("Wrong device type was specified.") + return 1 + + if not params.has_key('flag'): + flag = VIR_DOMAIN_AFFECT_CONFIG
Undefined variable 'VIR_DOMAIN_AFFECT_CONFIG'.
+ + # Connect to local hypervisor connection URI + util = utils.Utils() + uri = util.get_uri('127.0.0.1') + conn = connectAPI.ConnectAPI() + virconn = conn.open(uri) + + caps = conn.get_caps() + logger.debug(caps) + + # Generate device XML for updating + domobj = domainAPI.DomainAPI(virconn) + newxmlobj = xmlbuilder.XmlBuilder() + + if devtype == 'cdrom': + newdevxml = newxmlobj.build_cdrom(xmlargs) + elif devtype == 'floppy': + newdevxml = newxmlobj.build_floppy(xmlargs) + + logger.debug("block device xml desc:\n%s" %newdevxml) + + try: + try: + domobj.update_device_flag(guestname, newdevxml, flag) + res, upfile = check_updated_device(guestname, domobj, srcfile) + if res: + logger.info("success to update '%s' device: %s\n" % \ + devtype, upfile) + else: + logger.error("fail to update '%s' device: %s\n" % \ + devtype, upfile) + except LibvirtAPI, e: + logger.error("API error message: %s, error code is %s" % + (e.response()['message'], e.response()['code'])) + conn.close() + logger.info("closed hypervisor connection") + return 1 + finally: + conn.close() + logger.info("closed hypervisor connection") + + return 0 + +def update_devflag_clean(params): + """Clean testing environment""" + if params['devtype'] == 'cdrom': + os.system('rm -f /var/lib/libvirt/boot/cdrom.img') + elif params['devtype'] == 'floppy': + os.system('rm -f /var/lib/libvirt/boot/floppy.img') + else: + logger.debug("image file was not found.")
Undefined variable 'logger', should define a global 'logger' variable. Alex -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list