def isconnected(self,libvirtdomain=None): """ Return Boolean Check if device is connected """ try: if libvirtdomain: libvirtdomain_, error_ = libvirtdomain, None else: libvirtdomain_, error_ = self.domain.getlibvirt() if libvirtdomain_: # current domain xml - libvirt domxml = xmltool.getxml(libvirtdomain_.XMLDesc(0)) # list devices for devicexml in domxml.get('devices'): # check type devicedict = xmltool.get_device_dict(devicexml.get('xml')) # if device is valid if devicedict: # check type if devicedict.get('type') == self.type: # check device == device from (XMLDesc) if self.getdict() == devicedict: return True except Exception, e: print e pass
def import_domains(self,libvirtnode=None): """ Import all domains from Node """ if libvirtnode: libvirtnode_, erro_ = libvirtnode, None else: libvirtnode_, error_ = self.getlibvirt() if libvirtnode_: libvirtdomains = [ libvirtnode_.lookupByID(ID) for ID in libvirtnode_.listDomainsID()[1:] ] for libvirtdomain in libvirtdomains: # import domains if Domain.objects.filter(name=libvirtdomain.name()).count() == 0: new_domain = Domain() new_domain.name=libvirtdomain.name() new_domain.description='Virtual Machine %s' %libvirtdomain.name() new_domain.node=self new_domain.state=libvirtdomain.info()[0] if self.defaultbridge: options = dict(defaultbridge=self.defaultbridge) else: options = None xmlf = xmltool.getxml(libvirtdomain.XMLDesc(0), options) new_domain.type = xmlf.get('type') new_domain.xml= xmlf.get('domain') new_domain.save() # import devices for d in xmlf.get('devices'): new_device = Device() new_device.domain = new_domain new_device.type = d.get('type') new_device.xml = d.get('xml') new_device.save()