[Kimchi-devel] [PATCH] Use lxml to generate graphics XML and update VMTemplate to use it

Aline Manera alinefm at linux.vnet.ibm.com
Tue Nov 4 14:30:36 UTC 2014


Create a new module xmlutils/graphics.py to generate graphics XML and
use this new function on vmtemplate.py

Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 src/kimchi/model/vms.py         |  2 +-
 src/kimchi/vmtemplate.py        | 25 +++++------------------
 src/kimchi/xmlutils/graphics.py | 45 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 21 deletions(-)
 create mode 100644 src/kimchi/xmlutils/graphics.py

diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index f2e4ae3..942f041 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -212,7 +212,7 @@ class VMsModel(object):
         if t._get_storage_type() not in ["iscsi", "scsi"]:
             vol_list = t.fork_vm_storage(vm_uuid)
 
-        graphics = params.get('graphics')
+        graphics = params.get('graphics', {})
         stream_protocols = self.caps.libvirt_stream_protocols
         xml = t.to_vm_xml(name, vm_uuid,
                           libvirt_stream_protocols=stream_protocols,
diff --git a/src/kimchi/vmtemplate.py b/src/kimchi/vmtemplate.py
index 4e5806f..5f7f31e 100644
--- a/src/kimchi/vmtemplate.py
+++ b/src/kimchi/vmtemplate.py
@@ -33,6 +33,7 @@ from kimchi.imageinfo import probe_image, probe_img_info
 from kimchi.isoinfo import IsoImage
 from kimchi.utils import check_url_path, pool_name_from_uri
 from kimchi.xmlutils.disk import get_disk_xml
+from kimchi.xmlutils.graphics import get_graphics_xml
 from kimchi.xmlutils.qemucmdline import get_qemucmdline_xml
 
 
@@ -185,24 +186,6 @@ class VMTemplate(object):
 
         return disks_xml
 
-    def _get_graphics_xml(self, params):
-        graphics_xml = """
-            <graphics type='%(type)s' autoport='yes' listen='%(listen)s'>
-            </graphics>
-        """
-        spicevmc_xml = """
-            <channel type='spicevmc'>
-              <target type='virtio' name='com.redhat.spice.0'/>
-            </channel>
-        """
-        graphics = dict(self.info['graphics'])
-        if params:
-            graphics.update(params)
-        graphics_xml = graphics_xml % graphics
-        if graphics['type'] == 'spice':
-            graphics_xml = graphics_xml + spicevmc_xml
-        return graphics_xml
-
     def to_volume_list(self, vm_uuid):
         storage_path = self._get_storage_path()
         fmt = 'raw' if self._get_storage_type() in ['logical'] else 'qcow2'
@@ -312,10 +295,12 @@ class VMTemplate(object):
         params['qemu-namespace'] = ''
         params['cdroms'] = ''
         params['qemu-stream-cmdline'] = ''
-        graphics = kwargs.get('graphics')
-        params['graphics'] = self._get_graphics_xml(graphics)
         params['cpu_info'] = self._get_cpu_xml()
 
+        graphics = dict(self.info['graphics'])
+        graphics.update(kwargs.get('graphics', {}))
+        params['graphics'] = get_graphics_xml(graphics)
+
         # Current implementation just allows to create disk in one single
         # storage pool, so we cannot mix the types (scsi volumes vs img file)
         storage_type = self._get_storage_type()
diff --git a/src/kimchi/xmlutils/graphics.py b/src/kimchi/xmlutils/graphics.py
new file mode 100644
index 0000000..4c69cd9
--- /dev/null
+++ b/src/kimchi/xmlutils/graphics.py
@@ -0,0 +1,45 @@
+#
+# Project Kimchi
+#
+# Copyright IBM, Corp. 2014
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+
+import lxml.etree as ET
+
+from lxml.builder import E
+
+
+def get_graphics_xml(params):
+    """
+    <graphics type='%(type)s' autoport='yes' listen='%(listen)s'/>
+
+    - For spice graphics:
+
+    <channel type='spicevmc'>
+      <target type='virtio' name='com.redhat.spice.0'/>
+    </channel>
+    """
+    graphics = E.graphics(type=params['type'], listen=params['listen'])
+    graphics_xml = ET.tostring(graphics, encoding='utf-8', pretty_print=True)
+
+    if params['type'] == 'vnc':
+        return graphics_xml
+
+    # For spice graphics, a channel also must be configured
+    channel = E.channel(type='spicevmc')
+    channel.append(E.target(type='virtio', name='com.redhat.spice.0'))
+    channel_xml = ET.tostring(channel, encoding='utf-8', pretty_print=True)
+    return graphics_xml + channel_xml
-- 
1.9.3




More information about the Kimchi-devel mailing list