[virt-tools-list] [PATCH 4 of 4] cli: add --graphics argument (and SPICE support)

Marc-André Lureau marcandre.lureau at redhat.com
Wed Nov 17 15:54:59 UTC 2010


# HG changeset patch
# User Marc-André Lureau <marcandre.lureau at redhat.com>
# Date 1290007454 -3600
# Node ID c3cf5679b6888ba6932878cb6c1681ea2925bf33
# Parent  51fa3f6bbf1612a124eeb30d8311ae2b98ec30f5
cli: add --graphics argument (and SPICE support)

diff -r 51fa3f6bbf16 -r c3cf5679b688 tests/clitest.py
--- a/tests/clitest.py	Wed Nov 17 14:07:40 2010 +0100
+++ b/tests/clitest.py	Wed Nov 17 16:24:14 2010 +0100
@@ -286,8 +286,14 @@
         "--sdl",
         # VNC w/ lots of options
         "--vnc --keymap ja --vncport 5950 --vnclisten 1.2.3.4",
+        # VNC w/ lots of options, new way
+        "--graphics vnc,port=5950,listen=1.2.3.4,keymap=ja",
+        # SPICE w/ lots of options
+        "--graphics spice,port=5950,tlsport=5950,listen=1.2.3.4 --keymap ja",
         # --video option
         "--vnc --video vga",
+        # --video option
+        "--graphics spice --video qxl",
         # --keymap local,
         "--vnc --keymap local",
         # --keymap none
@@ -299,6 +305,8 @@
         "--vnc --keymap ZZZ",
         # Invalid port
         "--vnc --vncport -50",
+        # Invalid port
+        "--graphics spice,tlsport=-50",
         # Invalid --video
         "--vnc --video foobar",
       ],
diff -r 51fa3f6bbf16 -r c3cf5679b688 virt-image
--- a/virt-image	Wed Nov 17 14:07:40 2010 +0100
+++ b/virt-image	Wed Nov 17 16:24:14 2010 +0100
@@ -53,7 +53,7 @@
     map(lambda kwargs: cli.get_network(kwargs, guest), net_kwargs)
 
 def get_graphics(domain, vnc, vncport, vnclisten, nographics, sdl, keymap,
-                 guest):
+                 graphics, guest):
     if not domain.graphics:
         guest.graphics_dev = None
         return
@@ -61,7 +61,7 @@
     if not (vnc or sdl or nographics):
         vnc = True
     cli.get_graphics(vnc, vncport, vnclisten, nographics, sdl, keymap, [],
-                     guest)
+                     graphics, guest)
 
 ### Option parsing
 def parse_args():
@@ -206,7 +206,8 @@
                  options.network, guest)
 
     get_graphics(image.domain, options.vnc, options.vncport, options.vnclisten,
-                 options.nographics, options.sdl, options.keymap, guest)
+                 options.nographics, options.sdl, options.keymap,
+                 options.graphics, guest)
 
     cli.set_os_variant(guest, options.distro_type, options.distro_variant)
 
diff -r 51fa3f6bbf16 -r c3cf5679b688 virt-install
--- a/virt-install	Wed Nov 17 14:07:40 2010 +0100
+++ b/virt-install	Wed Nov 17 16:24:14 2010 +0100
@@ -44,29 +44,6 @@
 DEFAULT_POOL_PATH = "/var/lib/libvirt/images"
 DEFAULT_POOL_NAME = "default"
 
-def partition(string, sep):
-    if not string:
-        return (None, None, None)
-
-    if string.count(sep):
-        splitres = string.split(sep, 1)
-        ret = (splitres[0], sep, splitres[1])
-    else:
-        ret = (string, None, None)
-    return ret
-
-def get_opt_param(opts, dictnames, val=None):
-    if type(dictnames) is not list:
-        dictnames = [dictnames]
-
-    for key in dictnames:
-        if key in opts:
-            if val == None:
-                val = opts[key]
-            del(opts[key])
-
-    return val
-
 def build_default_pool(guest):
 
     if not virtinst.util.is_storage_capable(guest.conn):
@@ -102,7 +79,7 @@
     menu = None
 
     def set_param(paramname, dictname, val=None):
-        val = get_opt_param(opts, dictname, val)
+        val = cli.get_opt_param(opts, dictname, val)
         if val == None:
             return
 
@@ -150,14 +127,14 @@
     Helper to parse --serial/--parallel options
     """
     # Peel the char type off the front
-    dev_type, ignore, optstring = partition(optstring, ",")
+    dev_type, ignore, optstring = cli.partition(optstring, ",")
 
     opts = cli.parse_optstr(optstring)
 
     dev = VirtualCharDevice.get_dev_instance(guest.conn, char_type, dev_type)
 
     def set_param(paramname, dictname, val=None):
-        val = get_opt_param(opts, dictname, val)
+        val = cli.get_opt_param(opts, dictname, val)
         if val == None:
             return
 
@@ -169,7 +146,7 @@
         setattr(dev, paramname, val)
 
     def parse_host(key):
-        host, ignore, port = partition(opts.get(key), ":")
+        host, ignore, port = cli.partition(opts.get(key), ":")
         if opts.has_key(key):
             del(opts[key])
 
@@ -211,12 +188,12 @@
 
 def parse_watchdog(guest, optstring):
     # Peel the model type off the front
-    model, ignore, optstring = partition(optstring, ",")
+    model, ignore, optstring = cli.partition(optstring, ",")
     opts = cli.parse_optstr(optstring)
     dev = virtinst.VirtualWatchdog(guest.conn)
 
     def set_param(paramname, dictname, val=None):
-        val = get_opt_param(opts, dictname, val)
+        val = cli.get_opt_param(opts, dictname, val)
         if val == None:
             return
 
@@ -250,8 +227,8 @@
     secmodel = guest.seclabel
 
     # Beware, adding boolean options here could upset label comma handling
-    mode = get_opt_param(opts, "type")
-    label = get_opt_param(opts, "label")
+    mode = cli.get_opt_param(opts, "type")
+    label = cli.get_opt_param(opts, "label")
 
     # Try to fix up label if it contained commas
     if label:
@@ -296,7 +273,7 @@
                         "format", "driver_name", "driver_type"]
 
     # Strip media type
-    path, ignore, optstr = partition(path, ",")
+    path, ignore, optstr = cli.partition(path, ",")
     path_type = None
     if path.startswith("path="):
         path_type = "path="
@@ -953,7 +930,7 @@
     # set up graphics and video device information
     cli.get_graphics(options.vnc, options.vncport, options.vnclisten,
                      options.nographics, options.sdl, options.keymap,
-                     options.video, guest)
+                     options.video, options.graphics, guest)
 
     # Set host device info
     cli.get_hostdevs(options.hostdevs, guest)
diff -r 51fa3f6bbf16 -r c3cf5679b688 virtinst/cli.py
--- a/virtinst/cli.py	Wed Nov 17 14:07:40 2010 +0100
+++ b/virtinst/cli.py	Wed Nov 17 16:24:14 2010 +0100
@@ -421,9 +421,12 @@
                     help=_("Address to listen on for VNC connections."))
     vncg.add_option("-k", "--keymap", type="string", dest="keymap",
                     action="callback", callback=check_before_store,
-                    help=_("set up keymap for the VNC console"))
+                    help=_("set up keymap for the graphical console"))
     vncg.add_option("", "--sdl", action="store_true", dest="sdl",
                     help=_("Use SDL for graphics support"))
+    vncg.add_option("", "--graphics", type="string", dest="graphics",
+                    action="callback", callback=check_before_store,
+                    help=_("Set graphics support (ex: --graphics spice,port=1,tlsport=2)"))
     vncg.add_option("", "--nographics", action="store_true",
                     help=_("Don't set up a graphical console for the guest."))
     return vncg
@@ -727,15 +730,46 @@
 
     return net_init_dicts
 
+def parse_graphics(guest, optstring):
+    if optstring is None:
+        return None
+    # Peel the model type off the front
+    type, ignore, optstring = partition(optstring, ",")
+    opts = parse_optstr(optstring)
+    dev = VirtualGraphics()
+
+    def set_param(paramname, dictname, val=None):
+        val = get_opt_param(opts, dictname, val)
+        if val == None:
+            return
+
+        setattr(dev, paramname, val)
+
+    set_param("type", "type", type)
+    set_param("port", "port")
+    set_param("tlsPort", "tlsport")
+    set_param("listen", "listen")
+    set_param("keymap", "keymap")
+
+    if opts:
+        raise ValueError(_("Unknown options %s") % opts.keys())
+
+    return dev
+
 def get_graphics(vnc, vncport, vnclisten, nographics, sdl, keymap,
-                 video_models, guest):
+                 video_models, graphics, guest):
     video_models = video_models or []
 
-    if ((vnc and nographics) or
-        (vnc and sdl) or
-        (sdl and nographics)):
+    try:
+        dev = parse_graphics(guest, graphics)
+        if dev is not None:
+            guest.graphics_dev = dev
+    except Exception, e:
+        fail(_("Error in graphics device parameters: %s") % str(e))
+
+    if (sum(map(int, [vnc != None, nographics != None, sdl != None, dev != None]))) > 1:
         raise ValueError, _("Can't specify more than one of VNC, SDL, "
-                            "or --nographics")
+                            "--graphics or --nographics")
 
     for model in video_models:
         dev = virtinst.VirtualVideoDevice(guest.conn)
@@ -817,3 +851,25 @@
         raise OptionValueError, _("%s option requires an argument") %opt_str
     parser.values.ensure_value(option.dest, []).append(value)
 
+def get_opt_param(opts, dictnames, val=None):
+    if type(dictnames) is not list:
+        dictnames = [dictnames]
+
+    for key in dictnames:
+        if key in opts:
+            if val == None:
+                val = opts[key]
+            del(opts[key])
+
+    return val
+
+def partition(string, sep):
+    if not string:
+        return (None, None, None)
+
+    if string.count(sep):
+        splitres = string.split(sep, 1)
+        ret = (splitres[0], sep, splitres[1])
+    else:
+        ret = (string, None, None)
+    return ret




More information about the virt-tools-list mailing list