- defs = [ (l[:l.find("=")], l[(l.find("=") + 1):]) for l in lines ]
- defs = [ (a, b.strip("'")) for (a,b) in defs ]
-
- return dict(defs)
- def __pif_on_host(self,pif):
- return self.__pifs.has_key(pif)
-
- def __get_pif_records_from_xapi(self, session, host):
- self.__pifs = {}
- for (p,rec) in session.xenapi.PIF.get_all_records().items():
- if rec['host'] != host:
- continue
- self.__pifs[p] = {}
- for f in PIF_ATTRS:
- self.__pifs[p][f] = rec[f]
- self.__pifs[p]['other_config'] = {}
- for f in PIF_OTHERCONFIG_ATTRS:
- if not rec['other_config'].has_key(f): continue
- self.__pifs[p]['other_config'][f] = rec['other_config'][f]
-
- def __get_vlan_records_from_xapi(self, session):
- self.__vlans = {}
- for v in session.xenapi.VLAN.get_all():
- rec = session.xenapi.VLAN.get_record(v)
- if not self.__pif_on_host(rec['untagged_PIF']):
- continue
- self.__vlans[v] = {}
- for f in VLAN_ATTRS:
- self.__vlans[v][f] = rec[f]
-
- def __get_bond_records_from_xapi(self, session):
- self.__bonds = {}
- for b in session.xenapi.Bond.get_all():
- rec = session.xenapi.Bond.get_record(b)
- if not self.__pif_on_host(rec['master']):
- continue
- self.__bonds[b] = {}
- for f in BOND_ATTRS:
- self.__bonds[b][f] = rec[f]
-
- def __get_network_records_from_xapi(self, session):
- self.__networks = {}
- for n in session.xenapi.network.get_all():
- rec = session.xenapi.network.get_record(n)
- self.__networks[n] = {}
- for f in NETWORK_ATTRS:
- self.__networks[n][f] = rec[f]
- self.__networks[n]['other_config'] = {}
- for f in NETWORK_OTHERCONFIG_ATTRS:
- if not rec['other_config'].has_key(f): continue
- self.__networks[n]['other_config'][f] = rec['other_config'][f]
-
- def __to_xml(self, xml, parent, key, ref, rec, attrs):
- """Encode a database object as XML"""
- e = xml.createElement(key)
- parent.appendChild(e)
- if ref:
- e.setAttribute('ref', ref)
-
- for n,v in rec.items():
- if attrs.has_key(n):
- h,_ = attrs[n]
- h(xml, e, n, v)
- else:
- raise Error("Unknown attribute %s" % n)
- def __from_xml(self, e, attrs):
- """Decode a database object from XML"""
- ref = e.attributes['ref'].value
- rec = {}
- for n in e.childNodes:
- if n.nodeName in attrs:
- _,h = attrs[n.nodeName]
- rec[n.nodeName] = h(n)
- return (ref,rec)
-
- def __init__(self, session_ref=None, cache_file=None):
- if session_ref and cache_file:
- raise Error("can't specify session reference and cache file")
- if cache_file == None:
- session = XenAPI.xapi_local()
-
- if not session_ref:
- log("No session ref given on command line, logging in.")
- session.xenapi.login_with_password("root", "")
- else:
- session._session = session_ref
-
- try:
-
- inventory = self.__read_xensource_inventory()
- assert(inventory.has_key('INSTALLATION_UUID'))
- log("host uuid is %s" % inventory['INSTALLATION_UUID'])
-
- host = session.xenapi.host.get_by_uuid(inventory['INSTALLATION_UUID'])
-
- self.__get_pif_records_from_xapi(session, host)
-
- self.__get_vlan_records_from_xapi(session)
- self.__get_bond_records_from_xapi(session)
- self.__get_network_records_from_xapi(session)
- finally:
- if not session_ref:
- session.xenapi.session.logout()
- else:
- log("Loading xapi database cache from %s" % cache_file)
-
- xml = parseXML(cache_file)
-
- self.__pifs = {}
- self.__bonds = {}
- self.__vlans = {}
- self.__networks = {}
-
- assert(len(xml.childNodes) == 1)
- toplevel = xml.childNodes[0]
-
- assert(toplevel.nodeName == "xenserver-network-configuration")
-
- for n in toplevel.childNodes:
- if n.nodeName == "#text":
- pass
- elif n.nodeName == PIF_XML_TAG:
- (ref,rec) = self.__from_xml(n, PIF_ATTRS)
- self.__pifs[ref] = rec
- elif n.nodeName == BOND_XML_TAG:
- (ref,rec) = self.__from_xml(n, BOND_ATTRS)
- self.__bonds[ref] = rec
- elif n.nodeName == VLAN_XML_TAG:
- (ref,rec) = self.__from_xml(n, VLAN_ATTRS)
- self.__vlans[ref] = rec
- elif n.nodeName == NETWORK_XML_TAG:
- (ref,rec) = self.__from_xml(n, NETWORK_ATTRS)
- self.__networks[ref] = rec
- else:
- raise Error("Unknown XML element %s" % n.nodeName)
-
- def save(self, cache_file):
-
- xml = getDOMImplementation().createDocument(
- None, "xenserver-network-configuration", None)
- for (ref,rec) in self.__pifs.items():
- self.__to_xml(xml, xml.documentElement, PIF_XML_TAG, ref, rec, PIF_ATTRS)
- for (ref,rec) in self.__bonds.items():
- self.__to_xml(xml, xml.documentElement, BOND_XML_TAG, ref, rec, BOND_ATTRS)
- for (ref,rec) in self.__vlans.items():
- self.__to_xml(xml, xml.documentElement, VLAN_XML_TAG, ref, rec, VLAN_ATTRS)
- for (ref,rec) in self.__networks.items():
- self.__to_xml(xml, xml.documentElement, NETWORK_XML_TAG, ref, rec,
- NETWORK_ATTRS)
-
- f = open(cache_file, 'w')
- f.write(xml.toprettyxml())
- f.close()
-
- def get_pif_by_uuid(self, uuid):
- pifs = map(lambda (ref,rec): ref,
- filter(lambda (ref,rec): uuid == rec['uuid'],
- self.__pifs.items()))
- if len(pifs) == 0:
- raise Error("Unknown PIF \"%s\"" % uuid)
- elif len(pifs) > 1:
- raise Error("Non-unique PIF \"%s\"" % uuid)
-
- return pifs[0]
-
- def get_pifs_by_device(self, device):
- return map(lambda (ref,rec): ref,
- filter(lambda (ref,rec): rec['device'] == device,
- self.__pifs.items()))
-
- def get_pif_by_bridge(self, bridge):
- networks = map(lambda (ref,rec): ref,
- filter(lambda (ref,rec): rec['bridge'] == bridge,
- self.__networks.items()))
- if len(networks) == 0:
- raise Error("No matching network \"%s\"")
-
- answer = None
- for network in networks:
- nwrec = self.get_network_record(network)
- for pif in nwrec['PIFs']:
- pifrec = self.get_pif_record(pif)
- if answer:
- raise Error("Multiple PIFs on host for network %s" % (bridge))
- answer = pif
- if not answer:
- raise Error("No PIF on host for network %s" % (bridge))
- return answer
-
- def get_pif_record(self, pif):
- if self.__pifs.has_key(pif):
- return self.__pifs[pif]
- raise Error("Unknown PIF \"%s\" (get_pif_record)" % pif)
- def get_all_pifs(self):
- return self.__pifs
- def pif_exists(self, pif):
- return self.__pifs.has_key(pif)
-
- def get_management_pif(self):
- """ Returns the management pif on host
- """
- all = self.get_all_pifs()
- for pif in all:
- pifrec = self.get_pif_record(pif)
- if pifrec['management']: return pif
- return None