def clear_domains(self, virt_type, inst_prefix):
     libvirt = importer.import_module('libvirt')
     if not libvirt:
         LOG.warn("Could not clear out libvirt domains, libvirt not available for python.")
         return
     virt_protocol = LIBVIRT_PROTOCOL_MAP.get(virt_type)
     if not virt_protocol:
         LOG.warn("Could not clear out libvirt domains, no known protocol for virt type %r" % (virt_type))
         return
     with sh.Rooted(True):
         LOG.info("Attempting to clear out leftover libvirt domains using protocol %r" % (virt_protocol))
         try:
             self.restart_service()
         except excp.ProcessExecutionError as e:
             LOG.warn("Could not restart libvirt due to: %s" % (e))
             return
         try:
             conn = libvirt.open(virt_protocol)
         except libvirt.libvirtError as e:
             LOG.warn("Could not connect to libvirt using protocol %r due to: %s" % (virt_protocol, e))
             return
         with contextlib.closing(conn) as ch:
             try:
                 defined_domains = ch.listDefinedDomains()
                 kill_domains = list()
                 for domain in defined_domains:
                     if domain.startswith(inst_prefix):
                         kill_domains.append(domain)
                 if kill_domains:
                     LOG.info("Found %s old domains to destroy (%s)" % (len(kill_domains), ", ".join(sorted(kill_domains))))
                     for domain in sorted(kill_domains):
                         self._destroy_domain(ch, domain)
             except libvirt.libvirtError, e:
                 LOG.warn("Could not clear out libvirt domains due to %s" % (e))
 def _destroy_domain(self, conn, dom_name):
     libvirt = importer.import_module('libvirt')
     try:
         dom = conn.lookupByName(dom_name)
         LOG.debug("Destroying domain (%r) (id=%s) running %r" % (dom_name, dom.ID(), dom.OSType()))
         dom.destroy()
         dom.undefine()
     except libvirt.libvirtError as e:
         LOG.warn("Could not clear out libvirt domain %r due to: %s" % (dom_name, e))
Beispiel #3
0
 def _destroy_domain(self, conn, dom_name):
     libvirt = importer.import_module('libvirt')
     try:
         dom = conn.lookupByName(dom_name)
         LOG.debug("Destroying domain (%r) (id=%s) running %r" %
                   (dom_name, dom.ID(), dom.OSType()))
         dom.destroy()
         dom.undefine()
     except libvirt.libvirtError as e:
         LOG.warn("Could not clear out libvirt domain %r due to: %s" %
                  (dom_name, e))
Beispiel #4
0
 def clear_domains(self, virt_type, inst_prefix):
     libvirt = importer.import_module('libvirt')
     if not libvirt:
         LOG.warn(
             "Could not clear out libvirt domains, libvirt not available for python."
         )
         return
     virt_protocol = LIBVIRT_PROTOCOL_MAP.get(virt_type)
     if not virt_protocol:
         LOG.warn(
             "Could not clear out libvirt domains, no known protocol for virt type %r"
             % (virt_type))
         return
     with sh.Rooted(True):
         LOG.info(
             "Attempting to clear out leftover libvirt domains using protocol %r"
             % (virt_protocol))
         try:
             self.restart_service()
         except excp.ProcessExecutionError as e:
             LOG.warn("Could not restart libvirt due to: %s" % (e))
             return
         try:
             conn = libvirt.open(virt_protocol)
         except libvirt.libvirtError as e:
             LOG.warn(
                 "Could not connect to libvirt using protocol %r due to: %s"
                 % (virt_protocol, e))
             return
         with contextlib.closing(conn) as ch:
             try:
                 defined_domains = ch.listDefinedDomains()
                 kill_domains = list()
                 for domain in defined_domains:
                     if domain.startswith(inst_prefix):
                         kill_domains.append(domain)
                 if kill_domains:
                     LOG.info("Found %s old domains to destroy (%s)" %
                              (len(kill_domains), ", ".join(
                                  sorted(kill_domains))))
                     for domain in sorted(kill_domains):
                         self._destroy_domain(ch, domain)
             except libvirt.libvirtError, e:
                 LOG.warn("Could not clear out libvirt domains due to %s" %
                          (e))