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

Ramon Medeiros ramonn at linux.vnet.ibm.com
Tue Apr 28 13:16:39 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 | 46 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 38 insertions(+), 8 deletions(-)

diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index c8267c1..6792447 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -150,7 +150,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)
+            root = etree.fromstring(xml)
+
+            # machine does not have title: use name
+            if root.find("title") == None:
+                names.append(root.find("name").text.decode('utf-8'))
+                continue
+
+            # has title: use it
+            names.append(root.find("title").text)
         names = sorted(names, key=unicode.lower)
         return names
 
@@ -865,14 +879,30 @@ class VMModel(object):
     def get_vm(name, conn):
         conn = conn.get()
         try:
-            # outgoing text to libvirt, encode('utf-8')
-            return conn.lookupByName(name.encode("utf-8"))
+
+            for dom in conn.listAllDomains(0):
+
+                xml = dom.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
+                root = etree.fromstring(xml)
+
+                # get name
+                # machine does not have title: use name
+                if root.find("title") == None:
+                    vm_name = root.find("name").text
+                # has title: use it
+                else:
+                    vm_name = root.find("title").text
+
+                # name match: return dom
+                if vm_name == name:
+                    return dom
+
         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})
+            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