def set_ip(self, value): if not issubclass(type(value), IPXML): raise xcepts.LibvirtXMLError("value must be a IPXML or subclass") xmltreefile = self.__dict_get__('xml') # IPXML root element is whole IP element tree root = xmltreefile.getroot() root.append(value.xmltreefile.getroot()) xmltreefile.write()
def sync(self, options=None): """Rebuild VM with the config file.""" # If target vm no longer exist, this will raise an exception. try: backup = self.new_from_dumpxml(self.vm_name) except IOError: logging.debug("Failed to backup %s.", self.vm_name) backup = None if not self.undefine(options): raise xcepts.LibvirtXMLError("Failed to undefine %s." % self.vm_name) if not self.define(): if backup: backup.define() raise xcepts.LibvirtXMLError("Failed to define %s, from %s." % (self.vm_name, self.xml))
def marshal_from_address(item, index, libvirtxml): """Convert an Address instance into tag + attributes""" root = item.xmltreefile.getroot() if root.tag == 'address': return (root.tag, dict(list(root.items()))) else: raise xcepts.LibvirtXMLError("Expected a list of address " "instances, not a %s" % str(item))
def marshal_from_route(item, index, libvirtxml): """Convert a dictionary into a tag + attributes""" del index # not used del libvirtxml # not used if not isinstance(item, dict): raise xcepts.LibvirtXMLError("Expected a dictionary of interface " "attributes, not a %s" % str(item)) return ('route', dict(item)) # return copy of dict, not reference
def set_portgroup(self, value): if not issubclass(type(value), PortgroupXML): raise xcepts.LibvirtXMLError("value must be a PortgroupXML" "instance or subclass.") self.del_portgroup() root = self.xmltreefile.getroot() root.append(value.xmltreefile.getroot()) self.xmltreefile.write()
def get_cap(self): """ Return the capability of nodedev_xml. """ try: cap_root = self.xmltreefile.reroot('/capability') except KeyError, detail: raise xcepts.LibvirtXMLError(detail)
def get_portgroup(self): try: portgroup_root = self.xmltreefile.reroot('/portgroup') except KeyError as detail: raise xcepts.LibvirtXMLError(detail) portgroup_xml = PortgroupXML(virsh_instance=self.__dict_get__('virsh')) portgroup_xml.xmltreefile = portgroup_root return portgroup_xml
def marshal_from_values(item, index, libvirtxml): """Convert a EnumXML instance into a tag + attributes""" del index # not used del libvirtxml # not used if isinstance(item, six.string_types): return ("value", {}, item) else: raise xcepts.LibvirtXMLError("Expected a str attributes," " not a %s" % str(item))
def marshal_from_hosts(item, index, libvirtxml): """ Convert an xml object to host tag and xml element """ if isinstance(item, DhcpHostXML): return 'host', item else: raise xcepts.LibvirtXMLError("Expected a list of ip dhcp host " "instances, not a %s" % str(item))
def marshal_from_hostname(item, index, libvirtxml): """Convert a HostnameXML instance into a tag + attributes""" del index # not used del libvirtxml # not used if isinstance(item, str): return ("hostname", {}, item) else: raise xcepts.LibvirtXMLError("Expected a str attributes," " not a %s" % str(item))
def get_ip(self): xmltreefile = self.__dict_get__('xml') try: ip_root = xmltreefile.reroot('/ip') except KeyError as detail: raise xcepts.LibvirtXMLError(detail) ipxml = IPXML(virsh_instance=self.__dict_get__('virsh')) ipxml.xmltreefile = ip_root return ipxml
def define(self): """ Define network from self.xml. """ cmd_result = self.virsh.net_define(self.xml) if cmd_result.exit_status: raise xcepts.LibvirtXMLError("Failed to define network %s.\n" "Detail: %s" % (self.name, cmd_result.stderr))
def start(self): """ Start network with self.virsh. """ cmd_result = self.virsh.net_start(self.name) if cmd_result.exit_status: raise xcepts.LibvirtXMLError("Failed to start network %s.\n" "Detail: %s" % (self.name, cmd_result.stderr))
def create(self): """ Adds non-persistant / transient network to libvirt with net-create """ cmd_result = self.virsh.net_create(self.xml) if cmd_result.exit_status: raise xcepts.LibvirtXMLError("Failed to create transient network %s.\n" "Detail: %s" % (self.name, cmd_result.stderr))
def set_source(self, value): if not issubclass(type(value), SourceXML): raise xcepts.LibvirtXMLError( "Value must be a SourceXML or subclass") xmltreefile = self.__dict_get__('xml') self.del_source() root = xmltreefile.getroot() root.append(value.xmltreefile.getroot()) xmltreefile.write()
def set_ip(self, value): if not issubclass(type(value), IPXML): raise xcepts.LibvirtXMLError("value must be a IPXML or subclass") xmltreefile = self.dict_get('xml') # nuke any existing IP block self.del_ip() # IPXML root element is whole IP element tree root = xmltreefile.getroot() root.append(value.getroot())
def new_from_element(cls, element, virsh_instance=base.base.virsh): # element uses type attribute, class uses type_name edict = dict(list(element.items())) try: edict['type_name'] = edict.pop('type') except (KeyError, AttributeError): raise xcepts.LibvirtXMLError("type attribute is manditory for " "Address class") return cls.new_from_dict(edict, virsh_instance=virsh_instance)
def get(name): """ Returns named device xml element's handler class """ # Module names and device-tags are always all lower-case name = str(name).lower() errmsg = ("Unknown/unsupported type '%s', supported types %s" % (str(name), DEVICE_TYPES)) if name not in DEVICE_TYPES: raise xcepts.LibvirtXMLError(errmsg) mod_path = os.path.abspath(os.path.dirname(__file__)) try: filename, pathname, description = imp.find_module(name, [mod_path]) mod_obj = imp.load_module(name, filename, pathname, description) # Enforce capitalized class names return getattr(mod_obj, name.capitalize()) except TypeError, detail: raise xcepts.LibvirtXMLError(errmsg + ': %s' % str(detail))
def set_xmltreefile(self, value): """ Point instance directly at an already initialized XMLTreeFile instance """ if not issubclass(type(value), xml_utils.XMLTreeFile): raise xcepts.LibvirtXMLError( "xmltreefile value must be XMLTreefile" " type or subclass, not a %s" % type(value)) self.dict_set('xml', value)
def marshal_from_seclabel(item, index, libvirtxml): """Convert a Seclabel instance into tag + attributes""" del index # not used del libvirtxml # not used root = item.xmltreefile.getroot() if root.tag == 'seclabel': return (root.tag, dict(root.items)) else: raise xcepts.LibvirtXMLError("Expected a list of seclabel " "instances, not a %s" % str(item))
def marshal_from_cpu(item, index, libvirtxml): """ Convert a dict to cpu tag and attributes. """ del index del libvirtxml if not isinstance(item, dict): raise xcepts.LibvirtXMLError("Expected a dictionary of cpu " "attributes, not a %s" % str(item)) return ('cpu', dict(item))
def undefine(self): """ Undefine network witch name is self.name. """ self.virsh.net_destroy(self.name) cmd_result = self.virsh.net_undefine(self.name) if cmd_result.exit_status: raise xcepts.LibvirtXMLError("Failed to undefine network %s.\n" "Detail: %s" % (self.name, cmd_result.stderr))
def get_attr(self): """ Return mac attribute dict :return: None if no mac in xml, dict of mac's attributes. """ try: mac_node = self.xmltreefile.reroot('/mac') except KeyError, detail: raise xcepts.LibvirtXMLError(detail)
def get_attr(self): """ Return all-ipv6 attribute dict :return: None if no all-ipv6 in xml, dict of all-ipv6's attributes. """ try: all_node = self.xmltreefile.reroot('/all-ipv6') except KeyError, detail: raise xcepts.LibvirtXMLError(detail)
def get_attr(self): """ Return icmp attribute dict :return: None if no icmp in xml, dict of icmp's attributes. """ try: icmp_node = self.xmltreefile.reroot('/icmp') except KeyError, detail: raise xcepts.LibvirtXMLError(detail)
def marshal_from_sources(item, index, libvirtxml): """ Convert a dict to serial source attributes. """ del index del libvirtxml if not isinstance(item, dict): raise xcepts.LibvirtXMLError("Expected a dictionary of source " "attributes, not a %s" % str(item)) return ('source', dict(item))
def get_attr(self): """ Return udplite attribute dict :return: None if no udplite in xml, dict of udplite's attributes. """ try: udplite_node = self.xmltreefile.reroot('/udplite') except KeyError, detail: raise xcepts.LibvirtXMLError(detail)
def __call__(self): element = self.element_by_parent(self.parent_xpath, self.tag_name, create=False) try: result = int(element.text, self.radix) except ValueError: raise xcepts.LibvirtXMLError("Value of %s in %s is %s," "not a Integer." % (self.tag_name, self.parent_xpath, element.text)) return result
def set_rule(self, value, rule_index=0): """ Delete rule with specific index and add new given value :param rule_index: rule's index number :param value: NwfilterXMLRules instance """ if not issubclass(type(value), NwfilterXMLRules): raise xcepts.LibvirtXMLError( "Value must be a NwfilterXMLRules or subclass") try: source_root = self.xmltreefile.findall('rule') except KeyError as detail: raise xcepts.LibvirtXMLError(detail) if source_root[rule_index] is not None: self.del_rule(rule_index) root = self.xmltreefile.getroot() root.insert(rule_index, value.xmltreefile.getroot()) self.xmltreefile.write()
def marshal_from_timer(item, index, libvirtxml): """Convert a Timer instance into tag + attributes""" del index del libvirtxml timer = item.xmltreefile.find("clock/timer") try: return (timer.tag, dict(timer.items())) except AttributeError: # Didn't find timer raise xcepts.LibvirtXMLError("Expected a list of timer " "instances, not a %s" % str(item))