[Kimchi-devel] [PATCH V3 2/5] Update mockmodel for spice support

apporc appleorchard2000 at gmail.com
Sun Dec 22 08:43:07 UTC 2013


There is one patch in the mailling list from Markwu, which have
code about vnc in mockmodel.py removed. But at the time i rebased
this patch, that patch is not accepted yet, so i have to keep it here,
so that the test can pass.

See: [project-kimchi] [PATCH 4/4] Remove vnc related code in mockmodel

Signed-off-by: apporc <appleorchard2000 at gmail.com>
---
 src/kimchi/mockmodel.py | 49 ++++++++++++++++++++++++++++++++++---------------
 1 file changed, 34 insertions(+), 15 deletions(-)

diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
index 348127a..66abec8 100644
--- a/src/kimchi/mockmodel.py
+++ b/src/kimchi/mockmodel.py
@@ -51,7 +51,6 @@ from kimchi.exception import InvalidOperation, InvalidParameter
 from kimchi.exception import MissingParameter, NotFoundError, OperationFailed
 from kimchi.objectstore import ObjectStore
 from kimchi.screenshot import VMScreenshot
-from kimchi.utils import is_digit
 from kimchi.vmtemplate import VMTemplate
 
 
@@ -59,19 +58,21 @@ class MockModel(object):
     def __init__(self, objstore_loc=None):
         self.reset()
         self.objstore = ObjectStore(objstore_loc)
-        self.vnc_port = 5999
         self.distros = self._get_distros()
 
         # open vnc port
         # make it here to make sure it will be available on server startup
-        cmd = config.find_qemu_binary()
-        args = [cmd, "-vnc", ":99"]
-
-        cmd  = "ps aux | grep '%s' | grep -v grep" % " ".join(args)
-        proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
+        qemu = config.find_qemu_binary()
+        self.vnc_port = 5999
+        self.spice_port = 5998
+        vnc_args = [qemu, "-vnc", ":99"]
+        spice_args = [qemu, "-spice", "port=5998"]
 
+        cmd = 'netstat -tulnp|egrep "{0}.*qemu|{0}.*kvm"'
+        for item in [(vnc_args, self.vnc_port), (spice_args, self.spice_port)]:
+            proc = subprocess.Popen(cmd.format(item[1]), shell=True, stdout=subprocess.PIPE)
         if len(proc.stdout.readlines()) == 0:
-            p = subprocess.Popen(args, close_fds=True)
+            subprocess.Popen(item[0], close_fds=True)
 
     def get_capabilities(self):
         return {'libvirt_stream_protocols': ['http', 'https', 'ftp', 'ftps', 'tftp'],
@@ -121,7 +122,14 @@ class MockModel(object):
             vm.info['screenshot'] = self.vmscreenshot_lookup(name)
         else:
             vm.info['screenshot'] = None
-        vm.info['graphics']['port'] = self._mock_graphics_ports.get(name, None)
+        vm.info['graphics']['port'] = self._mock_graphics_ports.get(name)
+        if vm.info['graphics']['type'] == 'vnc':
+            vm.info['graphics']['real_port'] = self.vnc_port
+        elif vm.info['graphics']['type'] == 'spice':
+            vm.info['graphics']['real_port'] = self.spice_port
+        else:
+            vm.info['graphics']['real_port'] = None
+
         return vm.info
 
     def vm_delete(self, name):
@@ -140,8 +148,12 @@ class MockModel(object):
         self._get_vm(name).info['state'] = 'shutoff'
 
     def vm_connect(self, name):
-        vnc_port = kimchi.vnc.new_ws_proxy(self.vnc_port)
-        self._mock_graphics_ports[name] = vnc_port
+        vm_info = self.vm_lookup(name)
+        if vm_info['grahpics']['type'] == 'vnc':
+            port = kimchi.vnc.new_ws_proxy(self.vnc_port)
+        else:
+            port = kimchi.vnc.new_ws_proxy(self.spice_port)
+        self._mock_graphics_ports[name] = port
 
     def vms_create(self, params):
         t_name = kimchi.model.template_name_from_uri(params['template'])
@@ -159,8 +171,13 @@ class MockModel(object):
         t = self._get_template(t_name, vm_overrides)
         t.validate()
 
-        vm = MockVM(vm_uuid, name, t.info)
-        icon = t.info.get('icon')
+        t_info = copy.deepcopy(t.info)
+        graphics = params.get('graphics')
+        if graphics:
+            t_info.update({'graphics': graphics})
+
+        vm = MockVM(vm_uuid, name, t_info)
+        icon = t_info.get('icon')
         if icon:
             vm.info['icon'] = icon
 
@@ -210,7 +227,6 @@ class MockModel(object):
 
         new_t.update(params)
         ident = name
-
         new_storagepool = new_t.get(u'storagepool', '')
         try:
             self._get_storagepool(kimchi.model.pool_name_from_uri(new_storagepool))
@@ -628,7 +644,10 @@ class MockVM(object):
                      'memory': template_info['memory'],
                      'cpus': template_info['cpus'],
                      'icon': None,
-                     'graphics': {'type': 'vnc', 'port': None}}
+                     'graphics': {'type': 'vnc', 'listen': '0.0.0.0', 'port': None,
+                                  'real_port': None}
+                     }
+        self.info['graphics'].update(template_info['graphics'])
 
 
 class MockStoragePool(object):
-- 
1.8.1.2




More information about the Kimchi-devel mailing list