[Kimchi-devel] [PATCH 2/9] Change the way that get_vm() and vm_list() search for VMs

Ramon Medeiros ramonn at linux.vnet.ibm.com
Thu May 7 17:40:32 UTC 2015


Instead of searching VMs by name, get_vm() and vm_list() will use as
criteria the title tag to search vms. This is necessary to avoid unicode
conflicts with libvirt.

Signed-off-by: Ramon Medeiros <ramonn at linux.vnet.ibm.com>
---
 src/kimchi/model/vms.py | 55 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 47 insertions(+), 8 deletions(-)

diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index f0182b9..2ae58ee 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -169,7 +169,21 @@ class VMsModel(object):
     @staticmethod
     def get_vms(conn):
         conn_ = conn.get()
-        names = [dom.name().decode('utf-8') for dom in conn_.listAllDomains(0)]
+
+        # retrieve title instead of name
+        names = []
+        for dom in conn_.listAllDomains(0):
+
+            xml = dom.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
+            title = xpath_get_text(xml, '/domain/title')
+
+            # machine does not have title: use name
+            if title == []:
+                names.append(unicode(dom.name()))
+                continue
+
+            # has title: use it
+            names.append(title[0])
         names = sorted(names, key=unicode.lower)
         return names
 
@@ -882,15 +896,40 @@ class VMModel(object):
     @staticmethod
     def get_vm(name, conn):
         conn = conn.get()
+
+        # try to reach vm directly
         try:
-            # outgoing text to libvirt, encode('utf-8')
-            return conn.lookupByName(name.encode("utf-8"))
+            dom = conn.lookupByName(unicode(filter(str.isalnum,
+                                    name.encode('ascii', 'xmlcharrefreplace'))))
+            return dom
+        # error: skip it and wait for the next search
         except libvirt.libvirtError as e:
-            if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
-                raise NotFoundError("KCHVM0002E", {'name': name})
-            else:
-                raise OperationFailed("KCHVM0009E", {'name': name,
-                                                     'err': e.message})
+            pass
+
+        try:
+
+            for dom in conn.listAllDomains(0):
+
+                xml = dom.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
+                title = xpath_get_text(xml, '/domain/title')
+
+                # machine does not have title: use name
+                if title == []:
+                    vm_name = unicode(dom.name())
+                # has title: use it
+                else:
+                    vm_name = title[0]
+
+                # name match: return dom
+                if vm_name == name:
+                    return dom
+
+        except libvirt.libvirtError as e:
+            raise OperationFailed("KCHVM0009E", {'name': name, 'err': e.message})
+
+        # not found
+        raise NotFoundError("KCHVM0002E", {'name': name})
+
 
     def delete(self, name):
         conn = self.conn.get()
-- 
2.1.0




More information about the Kimchi-devel mailing list