Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 102 lines (87 sloc) 3.314 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#!/usr/bin/python
# Restore SR metadata and VDI names from an XML file
# (c) Anil Madhavapeddy, Citrix Systems Inc, 2008

import atexit
import XenAPI
import os, sys, time
import getopt
from xml.dom.minidom import parse
import codecs

sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
sys.stderr = codecs.getwriter("utf-8")(sys.stderr)

def logout():
    try:
        session.xenapi.session.logout()
    except:
        pass
atexit.register(logout)

def usage():
    print >> sys.stderr, "%s -f <input file> -u <sr uuid>" % sys.argv[0]
    sys.exit(1)

def main(argv):
    session = XenAPI.xapi_local()
    session.xenapi.login_with_password("", "")

    try:
        opts, args = getopt.getopt(argv, "hf:u:", [])
    except getopt.GetoptError, err:
        print str(err)
        usage()

    infile = None
    sruuid = None
    for o,a in opts:
        if o == "-f":
            infile = a
        if o == "-u":
            sruuid = a

    if infile == None or sruuid == None:
        usage()

    try:
        doc = parse(infile)
    except:
        print >> sys.stderr, "Error parsing %s" % infile
        sys.exit(1)

    if doc.documentElement.tagName != "meta":
        print >> sys.stderr, "Unexpected root element while parsing %s" % infile
        sys.exit(1)
    
    for srxml in doc.documentElement.childNodes:
        try:
            uuid = srxml.getAttribute("uuid")
            name_label = srxml.getAttribute("name_label")
            name_descr = srxml.getAttribute("name_description")
        except:
            print >> sys.stderr, "Error parsing SR tag"
            continue
        # only set attributes on the selected SR passed in on cmd line
        if uuid == sruuid:
            try:
                srref = session.xenapi.SR.get_by_uuid(uuid)
                print "Setting SR (%s):" % uuid
                session.xenapi.SR.set_name_label(srref, name_label)
                print " Name: %s " % name_label
                session.xenapi.SR.set_name_description(srref, name_descr)
                print " Description: %s" % name_descr
            except:
                print >> sys.stderr, "Error setting SR data for: %s (%s)" % (uuid, name_label)
                sys.exit(1)
            # go through all the SR VDIs and set the name_label and description
            for vdixml in srxml.childNodes:
                try:
                    vdi_uuid = vdixml.getAttribute("uuid")
                    vdi_label = vdixml.getAttribute("name_label")
                    vdi_descr = vdixml.getAttribute("name_description")
                except:
                    print >> sys.stderr, "Error parsing VDI tag"
                    continue
                try:
                    vdiref = session.xenapi.VDI.get_by_uuid(vdi_uuid)
                    print "Setting VDI (%s):" % vdi_uuid
                    session.xenapi.VDI.set_name_label(vdiref, vdi_label)
                    print " Name: %s" % vdi_label
                    session.xenapi.VDI.set_name_description(vdiref, vdi_descr)
                    print " Description: %s" % vdi_descr
                except:
                    print >> sys.stderr, "Error setting VDI data for: %s (%s)" % (vdi_uuid, name_label)
                    continue
            sys.exit(0)

if __name__ == "__main__":
    main(sys.argv[1:])


Something went wrong with that request. Please try again.