Code cleanups in engine.py

Change some code to match the common style. Break apart the overloaded
'get_connection' function into _lookup_connection and add_connection.
This commit is contained in:
Cole Robinson 2009-07-14 14:48:09 -04:00
parent f37612dfe5
commit 74e01e22c5

View File

@ -105,11 +105,17 @@ class vmmEngine(gobject.GObject):
self.windowConnect = None self.windowConnect = None
try: try:
conn = self.get_connection(uri, readOnly, autoconnect) try:
conn = self._lookup_connection(uri)
except Exception, e:
print e
conn = self.add_connection(uri, readOnly, autoconnect)
self.show_manager() self.show_manager()
conn.open() conn.open()
return conn return conn
except: except Exception, e:
print e
return None return None
def _connect_cancelled(self, connect): def _connect_cancelled(self, connect):
@ -245,7 +251,7 @@ class vmmEngine(gobject.GObject):
self.windowPreferences.show() self.windowPreferences.show()
def show_host(self, uri): def show_host(self, uri):
con = self.get_connection(uri) con = self._lookup_connection(uri)
if self.connections[uri]["windowHost"] == None: if self.connections[uri]["windowHost"] == None:
manager = vmmHost(self.get_config(), con) manager = vmmHost(self.get_config(), con)
@ -283,7 +289,7 @@ class vmmEngine(gobject.GObject):
win.activate_config_page() win.activate_config_page()
def show_details(self, uri, uuid): def show_details(self, uri, uuid):
con = self.get_connection(uri) con = self._lookup_connection(uri)
if not(self.connections[uri]["windowDetails"].has_key(uuid)): if not(self.connections[uri]["windowDetails"].has_key(uuid)):
try: try:
@ -384,6 +390,8 @@ class vmmEngine(gobject.GObject):
if autoconnect: if autoconnect:
conn.toggle_autoconnect() conn.toggle_autoconnect()
return conn
def remove_connection(self, uri): def remove_connection(self, uri):
conn = self.connections[uri]["connection"] conn = self.connections[uri]["connection"]
conn.close() conn.close()
@ -400,21 +408,22 @@ class vmmEngine(gobject.GObject):
return handle_id return handle_id
def get_connection(self, uri, readOnly=None, autoconnect=False): def _lookup_connection(self, uri):
if not(self.connections.has_key(uri)): conn = self.connections.get(uri)
self.add_connection(uri, readOnly, autoconnect) if not conn:
raise RuntimeError(_("Unknown connection URI %s") % uri)
return self.connections[uri]["connection"] return conn["connection"]
def save_domain(self, src, uri, uuid): def save_domain(self, src, uri, uuid):
con = self.get_connection(uri, False) conn = self._lookup_connection(uri)
if con.is_remote(): if conn.is_remote():
# FIXME: This should work with remote storage stuff # FIXME: This should work with remote storage stuff
self.err.val_err(_("Saving virtual machines over remote " self.err.val_err(_("Saving virtual machines over remote "
"connections is not yet supported.")) "connections is not yet supported."))
return return
vm = con.get_vm(uuid) vm = conn.get_vm(uuid)
status = vm.status() status = vm.status()
if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, if status in [ libvirt.VIR_DOMAIN_SHUTDOWN,
libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_SHUTOFF,
@ -426,7 +435,7 @@ class vmmEngine(gobject.GObject):
path = util.browse_local(src.window.get_widget("vmm-details"), path = util.browse_local(src.window.get_widget("vmm-details"),
_("Save Virtual Machine"), _("Save Virtual Machine"),
self.config, self.get_connection(uri), self.config, conn,
dialog_type=gtk.FILE_CHOOSER_ACTION_SAVE, dialog_type=gtk.FILE_CHOOSER_ACTION_SAVE,
browse_reason=self.config.CONFIG_DIR_SAVE) browse_reason=self.config.CONFIG_DIR_SAVE)
@ -448,107 +457,134 @@ class vmmEngine(gobject.GObject):
asyncjob.set_error(str(e), "".join(traceback.format_exc())) asyncjob.set_error(str(e), "".join(traceback.format_exc()))
def destroy_domain(self, src, uri, uuid): def destroy_domain(self, src, uri, uuid):
con = self.get_connection(uri, False) conn = self._lookup_connection(uri)
vm = con.get_vm(uuid) vm = conn.get_vm(uuid)
status = vm.status() status = vm.status()
if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, if status in [ libvirt.VIR_DOMAIN_SHUTDOWN,
libvirt.VIR_DOMAIN_SHUTOFF ]: libvirt.VIR_DOMAIN_SHUTOFF ]:
logging.warning("Destroy requested, but machine is shutdown / shutoff") logging.warning("Destroy requested, but machine is "
else: "shutdown/shutoff")
resp = self.err.yes_no(text1=_("About to poweroff virtual machine %s" % vm.get_name()), text2=_("This will immediately poweroff the VM without shutting down the OS and may cause data loss. Are you sure?")) return
if resp:
logging.debug("Destroying vm '%s'." % vm.get_name()) resp = self.err.yes_no(text1=_("About to poweroff virtual "
try: "machine %s" % vm.get_name()),
vm.destroy() text2=_("This will immediately poweroff the VM "
except Exception, e: "without shutting down the OS and may "
self.err.show_err(_("Error shutting down domain: %s" % str(e)), "".join(traceback.format_exc())) "cause data loss. Are you sure?"))
if not resp:
return
logging.debug("Destroying vm '%s'." % vm.get_name())
try:
vm.destroy()
except Exception, e:
self.err.show_err(_("Error shutting down domain: %s" % str(e)),
"".join(traceback.format_exc()))
def suspend_domain(self, src, uri, uuid): def suspend_domain(self, src, uri, uuid):
con = self.get_connection(uri, False) conn = self._lookup_connection(uri)
vm = con.get_vm(uuid) vm = conn.get_vm(uuid)
status = vm.status() status = vm.status()
if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, \
libvirt.VIR_DOMAIN_SHUTOFF, \ if status in [ libvirt.VIR_DOMAIN_SHUTDOWN,
libvirt.VIR_DOMAIN_SHUTOFF,
libvirt.VIR_DOMAIN_CRASHED ]: libvirt.VIR_DOMAIN_CRASHED ]:
logging.warning("Pause requested, but machine is shutdown / shutoff") logging.warning("Pause requested, but machine is shutdown/shutoff")
return
elif status in [ libvirt.VIR_DOMAIN_PAUSED ]: elif status in [ libvirt.VIR_DOMAIN_PAUSED ]:
logging.warning("Pause requested, but machine is already paused") logging.warning("Pause requested, but machine is already paused")
else: return
logging.debug("Pausing vm '%s'." % vm.get_name())
try: logging.debug("Pausing vm '%s'." % vm.get_name())
vm.suspend() try:
except Exception, e: vm.suspend()
self.err.show_err(_("Error pausing domain: %s" % str(e)), except Exception, e:
"".join(traceback.format_exc())) self.err.show_err(_("Error pausing domain: %s" % str(e)),
"".join(traceback.format_exc()))
def resume_domain(self, src, uri, uuid): def resume_domain(self, src, uri, uuid):
con = self.get_connection(uri, False) conn = self._lookup_connection(uri)
vm = con.get_vm(uuid) vm = conn.get_vm(uuid)
status = vm.status() status = vm.status()
if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, \
libvirt.VIR_DOMAIN_SHUTOFF, \ if status in [ libvirt.VIR_DOMAIN_SHUTDOWN,
libvirt.VIR_DOMAIN_SHUTOFF,
libvirt.VIR_DOMAIN_CRASHED ]: libvirt.VIR_DOMAIN_CRASHED ]:
logging.warning("Resume requested, but machine is shutdown / shutoff") logging.warning("Resume requested, but machine is "
elif status in [ libvirt.VIR_DOMAIN_PAUSED ]: "shutdown/shutoff")
logging.debug("Unpausing vm '%s'." % vm.get_name()) return
try:
vm.resume() elif status not in [ libvirt.VIR_DOMAIN_PAUSED ]:
except Exception, e: logging.warning("Unpause requested, but machine is not paused.")
self.err.show_err(_("Error unpausing domain: %s" % str(e)), return
"".join(traceback.format_exc()))
else: logging.debug("Unpausing vm '%s'." % vm.get_name())
logging.warning("Resume requested, but machine is already running") try:
vm.resume()
except Exception, e:
self.err.show_err(_("Error unpausing domain: %s" % str(e)),
"".join(traceback.format_exc()))
def run_domain(self, src, uri, uuid): def run_domain(self, src, uri, uuid):
con = self.get_connection(uri, False) conn = self._lookup_connection(uri)
vm = con.get_vm(uuid) vm = conn.get_vm(uuid)
status = vm.status() status = vm.status()
if status != libvirt.VIR_DOMAIN_SHUTOFF: if status != libvirt.VIR_DOMAIN_SHUTOFF:
logging.warning("Run requested, but domain isn't shutoff.") logging.warning("Run requested, but domain isn't shutoff.")
else: return
logging.debug("Starting vm '%s'." % vm.get_name())
try: logging.debug("Starting vm '%s'." % vm.get_name())
vm.startup() try:
except Exception, e: vm.startup()
self.err.show_err(_("Error starting domain: %s" % str(e)), except Exception, e:
"".join(traceback.format_exc())) self.err.show_err(_("Error starting domain: %s" % str(e)),
"".join(traceback.format_exc()))
def shutdown_domain(self, src, uri, uuid): def shutdown_domain(self, src, uri, uuid):
con = self.get_connection(uri, False) conn = self._lookup_connection(uri)
vm = con.get_vm(uuid) vm = conn.get_vm(uuid)
status = vm.status() status = vm.status()
if not(status in [ libvirt.VIR_DOMAIN_SHUTDOWN, \
libvirt.VIR_DOMAIN_SHUTOFF, \ if status in [ libvirt.VIR_DOMAIN_SHUTDOWN,
libvirt.VIR_DOMAIN_CRASHED ]): libvirt.VIR_DOMAIN_SHUTOFF,
logging.debug("Shutting down vm '%s'." % vm.get_name()) libvirt.VIR_DOMAIN_CRASHED ]:
try: logging.warning("Shut down requested, but the virtual machine is "
vm.shutdown() "already shutting down / powered off")
except Exception, e: return
self.err.show_err(_("Error shutting down domain: %s" % str(e)),
"".join(traceback.format_exc())) logging.debug("Shutting down vm '%s'." % vm.get_name())
else: try:
logging.warning("Shut down requested, but the virtual machine is already shutting down / powered off") vm.shutdown()
except Exception, e:
self.err.show_err(_("Error shutting down domain: %s" % str(e)),
"".join(traceback.format_exc()))
def reboot_domain(self, src, uri, uuid): def reboot_domain(self, src, uri, uuid):
con = self.get_connection(uri, False) conn = self._lookup_connection(uri)
vm = con.get_vm(uuid) vm = conn.get_vm(uuid)
status = vm.status() status = vm.status()
if not(status in [ libvirt.VIR_DOMAIN_SHUTDOWN, \
libvirt.VIR_DOMAIN_SHUTOFF, \ if status in [ libvirt.VIR_DOMAIN_SHUTDOWN,
libvirt.VIR_DOMAIN_CRASHED ]): libvirt.VIR_DOMAIN_SHUTOFF,
logging.debug("Rebooting vm '%s'." % vm.get_name()) libvirt.VIR_DOMAIN_CRASHED ]:
try: logging.warning("Reboot requested, but machine is already "
vm.reboot() "shutting down / shutoff")
except Exception, e: return
self.err.show_err(_("Error shutting down domain: %s" % str(e)),
"".join(traceback.format_exc())) logging.debug("Rebooting vm '%s'." % vm.get_name())
else: try:
logging.warning("Reboot requested, but machine is already shutting down / shutoff") vm.reboot()
except Exception, e:
self.err.show_err(_("Error shutting down domain: %s" % str(e)),
"".join(traceback.format_exc()))
def migrate_domain(self, uri, uuid, desthost): def migrate_domain(self, uri, uuid, desthost):
desturi = None desturi = None
for key in self.connections.keys(): for key in self.connections.keys():
if self.get_connection(key).get_hostname() == desthost: if self._lookup_connection(key).get_hostname() == desthost:
desturi = key desturi = key
break break
@ -557,9 +593,9 @@ class vmmEngine(gobject.GObject):
% desthost) % desthost)
return return
conn = self.get_connection(uri, False) conn = self._lookup_connection(uri)
vm = conn.get_vm(uuid) vm = conn.get_vm(uuid)
destconn = self.get_connection(desturi, False) destconn = self._lookup_connection(desturi, False)
resp = self.err.yes_no(_("Are you sure you want to migrate %s from " resp = self.err.yes_no(_("Are you sure you want to migrate %s from "
"%s to %s?") % "%s to %s?") %
(vm.get_name(), conn.get_hostname(), (vm.get_name(), conn.get_hostname(),