console: Try to use more consistent parameter names

This commit is contained in:
Cole Robinson 2011-01-13 11:26:38 -05:00
parent 8ced456a9d
commit 4b84a00876
2 changed files with 73 additions and 62 deletions

View File

@ -33,11 +33,9 @@ except:
spice = None spice = None
import os import os
import sys
import signal import signal
import socket import socket
import logging import logging
import traceback
from virtManager import util from virtManager import util
from virtManager.baseclass import vmmGObjectUI from virtManager.baseclass import vmmGObjectUI
@ -62,19 +60,19 @@ class Tunnel(object):
self.errfd = None self.errfd = None
self.pid = None self.pid = None
def open(self, server, addr, port, username, sshport): def open(self, connhost, connuser, connport, gaddr, gport):
if self.outfd is not None: if self.outfd is not None:
return -1 return -1
# Build SSH cmd # Build SSH cmd
argv = ["ssh", "ssh"] argv = ["ssh", "ssh"]
if sshport: if connport:
argv += ["-p", str(sshport)] argv += ["-p", str(connport)]
if username: if connuser:
argv += ['-l', username] argv += ['-l', connuser]
argv += [server] argv += [connhost]
# Build 'nc' command run on the remote host # Build 'nc' command run on the remote host
# #
@ -87,7 +85,8 @@ class Tunnel(object):
# Fedora's 'nc' doesn't have this option, and apparently defaults # Fedora's 'nc' doesn't have this option, and apparently defaults
# to the desired behavior. # to the desired behavior.
# #
nc_params = "%s %s" % (addr, str(port)) nc_params = "%s %s" % (gaddr, gport)
nc_cmd = ( nc_cmd = (
"""nc -q 2>&1 | grep -q "requires an argument";""" """nc -q 2>&1 | grep -q "requires an argument";"""
"""if [ $? -eq 0 ] ; then""" """if [ $? -eq 0 ] ; then"""
@ -169,18 +168,19 @@ class Tunnel(object):
class Tunnels(object): class Tunnels(object):
def __init__(self, server, addr, port, username, sshport): def __init__(self, connhost, connuser, connport, gaddr, gport):
self.server = server self.connhost = connhost
self.addr = addr self.connuser = connuser
self.port = port self.connport = connport
self.username = username
self.sshport = sshport self.gaddr = gaddr
self.gport = gport
self._tunnels = [] self._tunnels = []
def open_new(self): def open_new(self):
t = Tunnel() t = Tunnel()
fd = t.open(self.server, self.addr, self.port, fd = t.open(self.connhost, self.connuser, self.connport,
self.username, self.sshport) self.gaddr, self.gport)
self._tunnels.append(t) self._tunnels.append(t)
return fd return fd
@ -201,6 +201,9 @@ class Viewer(object):
self.config = config self.config = config
self.display = None self.display = None
def get_widget(self):
return self.display
def get_pixbuf(self): def get_pixbuf(self):
return self.display.get_pixbuf() return self.display.get_pixbuf()
@ -243,13 +246,14 @@ class Viewer(object):
logging.debug("Error when getting the grab keys combination: %s" % logging.debug("Error when getting the grab keys combination: %s" %
str(e)) str(e))
def open_host(self, host, user, port, password=None):
raise NotImplementedError()
class VNCViewer(Viewer): class VNCViewer(Viewer):
def __init__(self, console, config): def __init__(self, console, config):
Viewer.__init__(self, console, config) Viewer.__init__(self, console, config)
self.display = gtkvnc.Display() self.display = gtkvnc.Display()
self.sockfd = None
def get_widget(self):
return self.display
def init_widget(self): def init_widget(self):
# Set default grab key combination if found and supported # Set default grab key combination if found and supported
@ -315,12 +319,19 @@ class VNCViewer(Viewer):
def close(self): def close(self):
self.display.close() self.display.close()
if not self.sockfd:
return
self.sockfd.close()
self.sockfd = None
def is_open(self): def is_open(self):
return self.display.is_open() return self.display.is_open()
def open_host(self, uri_ignore, connhost, port): def open_host(self, host, user, port, password=None):
self.display.open_host(connhost, port) ignore = password
ignore = user
self.display.open_host(host, port)
def open_fd(self, fd): def open_fd(self, fd):
self.display.open_fd(fd) self.display.open_fd(fd)
@ -350,9 +361,6 @@ class SpiceViewer(Viewer):
self.display = None self.display = None
self.audio = None self.audio = None
def get_widget(self):
return self.display
def _init_widget(self): def _init_widget(self):
self.set_grab_keys() self.set_grab_keys()
self.console.refresh_scaling() self.console.refresh_scaling()
@ -404,7 +412,12 @@ class SpiceViewer(Viewer):
self.audio = spice.Audio(self.spice_session) self.audio = spice.Audio(self.spice_session)
return return
def open_host(self, uri, connhost_ignore, port_ignore, password=None): def open_host(self, host, user, port, password=None):
uri = "spice://"
uri += (user and str(user) or "")
uri += str(host) + "?port=" + str(port)
logging.debug("spice uri: %s" % uri)
self.spice_session = spice.Session() self.spice_session = spice.Session()
self.spice_session.set_property("uri", uri) self.spice_session.set_property("uri", uri)
if password: if password:
@ -824,9 +837,9 @@ class vmmConsolePages(vmmGObjectUI):
return return
try: try:
(protocol, connhost, (protocol, transport,
gport, trans, username, connhost, connuser, connport,
connport, guri) = self.vm.get_graphics_console() gaddr, gport) = self.vm.get_graphics_console()
except Exception, e: except Exception, e:
# We can fail here if VM is destroyed: xen is a bit racy # We can fail here if VM is destroyed: xen is a bit racy
# and can't handle domain lookups that soon after # and can't handle domain lookups that soon after
@ -859,7 +872,8 @@ class vmmConsolePages(vmmGObjectUI):
if protocol == "vnc": if protocol == "vnc":
self.viewer = VNCViewer(self, self.config) self.viewer = VNCViewer(self, self.config)
self.window.get_widget("console-vnc-viewport").add(self.viewer.get_widget()) self.window.get_widget("console-vnc-viewport").add(
self.viewer.get_widget())
self.viewer.init_widget() self.viewer.init_widget()
elif protocol == "spice": elif protocol == "spice":
self.viewer = SpiceViewer(self, self.config) self.viewer = SpiceViewer(self, self.config)
@ -868,31 +882,32 @@ class vmmConsolePages(vmmGObjectUI):
self.activate_unavailable_page( self.activate_unavailable_page(
_("Connecting to graphical console for guest")) _("Connecting to graphical console for guest"))
logging.debug("Starting connect process for %s: %s %s" %
(guri, connhost, str(gport)))
logging.debug("Starting connect process for "
"proto=%s trans=%s connhost=%s connuser=%s "
"connport=%s gaddr=%s gport=%s" %
(protocol, transport, connhost, connuser, connport,
gaddr, gport))
try: try:
if trans in ("ssh", "ext"): if transport in ("ssh", "ext"):
if self.tunnels: if self.tunnels:
# Tunnel already open, no need to continue # Tunnel already open, no need to continue
return return
self.tunnels = Tunnels(connhost, "127.0.0.1", gport, self.tunnels = Tunnels(connhost, connuser, connport,
username, connport) gaddr, gport)
fd = self.tunnels.open_new() fd = self.tunnels.open_new()
if fd >= 0: if fd >= 0:
self.viewer.open_fd(fd) self.viewer.open_fd(fd)
else: else:
self.viewer.open_host(guri, connhost, str(gport)) self.viewer.open_host(connhost, connuser,
str(gport))
except: except Exception, e:
(typ, value, stacktrace) = sys.exc_info() logging.exception("Error connection to graphical console")
details = \ self.activate_unavailable_page(
"Unable to start virtual machine '%s'" % \ _("Error connecting to graphical console") + ":\n%s" % e)
(str(typ) + " " + str(value) + "\n" + \
traceback.format_exc(stacktrace))
logging.error(details)
def set_credentials(self, src_ignore=None): def set_credentials(self, src_ignore=None):
passwd = self.window.get_widget("console-auth-password") passwd = self.window.get_widget("console-auth-password")

View File

@ -577,15 +577,19 @@ class vmmDomainBase(vmmLibvirtObject):
def get_graphics_console(self): def get_graphics_console(self):
gdevs = self.get_graphics_devices() gdevs = self.get_graphics_devices()
connhost = self.connection.get_uri_hostname() connhost = self.connection.get_uri_hostname()
transport, username = self.connection.get_transport() transport, connuser = self.connection.get_transport()
gport = None
gtype = None
if gdevs:
gport = gdevs[0].port
gtype = gdevs[0].type
if gtype in ['vnc', 'spice']: gdev = gdevs and gdevs[0] or None
gtype = None
gport = None
gaddr = None
if gdevs:
gport = gdev.port
if gport != None:
gport = int(gport) gport = int(gport)
gtype = gdev.type
gaddr = "127.0.0.1"
if connhost == None: if connhost == None:
# Force use of 127.0.0.1, because some (broken) systems don't # Force use of 127.0.0.1, because some (broken) systems don't
@ -599,17 +603,9 @@ class vmmDomainBase(vmmLibvirtObject):
if connhost.count(":"): if connhost.count(":"):
connhost, connport = connhost.split(":", 1) connhost, connport = connhost.split(":", 1)
# Build VNC/SPICE uri for debugging return [gtype, transport,
guri = None connhost, connuser, connport,
if gtype in ['vnc', 'spice']: gaddr, gport]
guri = str(gtype) + "://"
if username:
guri = guri + str(username) + '@'
sep = {'vnc': ':', 'spice': '?port='}[gtype]
guri += str(connhost) + sep + str(gport)
return [gtype, connhost, gport, transport, username, connport,
guri]
def _build_device_list(self, device_type, def _build_device_list(self, device_type,