2006-06-29 03:50:17 +08:00
|
|
|
#
|
2014-06-26 17:51:38 +08:00
|
|
|
# Copyright (C) 2006, 2013-2014 Red Hat, Inc.
|
2006-06-29 03:50:17 +08:00
|
|
|
# Copyright (C) 2006 Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2007-11-21 00:12:20 +08:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
# MA 02110-1301 USA.
|
2006-06-29 03:50:17 +08:00
|
|
|
#
|
2007-11-21 00:12:20 +08:00
|
|
|
|
2006-09-26 06:41:47 +08:00
|
|
|
import logging
|
2017-10-11 19:35:53 +08:00
|
|
|
import queue
|
2009-07-12 09:23:16 +08:00
|
|
|
import threading
|
2014-04-16 22:32:52 +08:00
|
|
|
import traceback
|
2006-06-14 22:59:40 +08:00
|
|
|
|
2016-06-07 23:33:21 +08:00
|
|
|
from gi.repository import Gio
|
|
|
|
from gi.repository import GLib
|
|
|
|
from gi.repository import Gtk
|
|
|
|
|
2014-09-13 04:10:45 +08:00
|
|
|
from . import packageutils
|
|
|
|
from .baseclass import vmmGObject
|
|
|
|
from .connect import vmmConnect
|
2018-03-16 09:22:14 +08:00
|
|
|
from .connmanager import vmmConnectionManager
|
2018-03-14 01:00:59 +08:00
|
|
|
from .inspection import vmmInspection
|
2006-06-14 22:59:40 +08:00
|
|
|
|
2011-06-09 04:33:20 +08:00
|
|
|
DETAILS_PERF = 1
|
|
|
|
DETAILS_CONFIG = 2
|
|
|
|
DETAILS_CONSOLE = 3
|
|
|
|
|
2013-07-07 21:42:21 +08:00
|
|
|
(PRIO_HIGH,
|
|
|
|
PRIO_LOW) = range(1, 3)
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2010-12-10 01:37:48 +08:00
|
|
|
class vmmEngine(vmmGObject):
|
2018-01-10 01:58:38 +08:00
|
|
|
CLI_SHOW_MANAGER = "manager"
|
2015-11-25 04:21:26 +08:00
|
|
|
CLI_SHOW_DOMAIN_CREATOR = "creator"
|
|
|
|
CLI_SHOW_DOMAIN_EDITOR = "editor"
|
|
|
|
CLI_SHOW_DOMAIN_PERFORMANCE = "performance"
|
|
|
|
CLI_SHOW_DOMAIN_CONSOLE = "console"
|
|
|
|
CLI_SHOW_HOST_SUMMARY = "summary"
|
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
@classmethod
|
|
|
|
def get_instance(cls):
|
|
|
|
if not cls._instance:
|
|
|
|
cls._instance = cls()
|
|
|
|
return cls._instance
|
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
__gsignals__ = {
|
|
|
|
"app-closing": (vmmGObject.RUN_FIRST, None, []),
|
|
|
|
}
|
|
|
|
|
2010-12-10 01:37:48 +08:00
|
|
|
def __init__(self):
|
|
|
|
vmmGObject.__init__(self)
|
2009-11-16 04:17:03 +08:00
|
|
|
|
2018-03-15 17:53:58 +08:00
|
|
|
self._exiting = False
|
2015-12-04 04:54:52 +08:00
|
|
|
|
2018-03-15 17:53:58 +08:00
|
|
|
self._window_count = 0
|
2015-12-04 04:54:52 +08:00
|
|
|
self._gtkapplication = None
|
|
|
|
self._init_gtk_application()
|
2009-07-28 10:30:01 +08:00
|
|
|
|
2018-03-15 17:53:58 +08:00
|
|
|
self._timer = None
|
2013-07-07 08:03:42 +08:00
|
|
|
self._tick_counter = 0
|
2009-07-12 09:23:16 +08:00
|
|
|
self._tick_thread_slow = False
|
2013-07-07 08:03:42 +08:00
|
|
|
self._tick_thread = threading.Thread(name="Tick thread",
|
|
|
|
target=self._handle_tick_queue,
|
|
|
|
args=())
|
|
|
|
self._tick_thread.daemon = True
|
2017-10-11 19:35:53 +08:00
|
|
|
self._tick_queue = queue.PriorityQueue(100)
|
2009-07-12 09:23:16 +08:00
|
|
|
|
2010-03-04 05:58:50 +08:00
|
|
|
|
2018-03-14 22:29:20 +08:00
|
|
|
@property
|
2018-03-15 01:13:22 +08:00
|
|
|
def _connobjs(self):
|
|
|
|
return vmmConnectionManager.get_instance().conns
|
2018-03-14 22:29:20 +08:00
|
|
|
|
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
def _cleanup(self):
|
|
|
|
if self._timer is not None:
|
|
|
|
GLib.source_remove(self._timer)
|
|
|
|
|
|
|
|
|
|
|
|
#################
|
|
|
|
# init handling #
|
|
|
|
#################
|
2015-12-04 04:54:52 +08:00
|
|
|
|
2017-10-20 23:46:40 +08:00
|
|
|
def _default_startup(self, skip_autostart, cliuri):
|
2018-03-15 06:43:47 +08:00
|
|
|
"""
|
|
|
|
Actual startup routines if we are running a new instance of the app
|
|
|
|
"""
|
2018-03-15 17:53:58 +08:00
|
|
|
from .systray import vmmSystray
|
|
|
|
vmmSystray.get_instance()
|
2018-03-15 06:43:47 +08:00
|
|
|
vmmInspection.get_instance()
|
|
|
|
|
|
|
|
self.add_gsettings_handle(
|
|
|
|
self.config.on_stats_update_interval_changed(
|
|
|
|
self._timer_changed_cb))
|
|
|
|
|
|
|
|
self._schedule_timer()
|
|
|
|
self._tick_thread.start()
|
|
|
|
self._tick()
|
2018-03-09 02:08:17 +08:00
|
|
|
|
2018-03-15 17:53:58 +08:00
|
|
|
uris = list(self._connobjs.keys())
|
2015-12-04 04:54:52 +08:00
|
|
|
if not uris:
|
|
|
|
logging.debug("No stored URIs found.")
|
2013-05-30 05:43:38 +08:00
|
|
|
else:
|
2015-12-04 04:54:52 +08:00
|
|
|
logging.debug("Loading stored URIs:\n%s",
|
|
|
|
" \n".join(sorted(uris)))
|
2013-04-16 07:25:54 +08:00
|
|
|
|
2015-12-04 04:54:52 +08:00
|
|
|
if not skip_autostart:
|
2018-03-15 06:43:47 +08:00
|
|
|
self.idle_add(self._autostart_conns)
|
2013-04-16 07:25:54 +08:00
|
|
|
|
2017-10-20 23:46:40 +08:00
|
|
|
if not self.config.get_conn_uris() and not cliuri:
|
2015-12-04 04:54:52 +08:00
|
|
|
# Only add default if no connections are currently known
|
|
|
|
self.timeout_add(1000, self._add_default_conn)
|
|
|
|
|
|
|
|
def _add_default_conn(self):
|
2018-03-15 06:43:47 +08:00
|
|
|
"""
|
|
|
|
If there's no cached connections, or any requested on the command
|
|
|
|
line, try to determine a default URI and open it, possibly talking
|
|
|
|
to packagekit and other bits
|
|
|
|
"""
|
|
|
|
manager = self._get_manager()
|
2013-09-02 02:19:23 +08:00
|
|
|
|
2010-03-04 05:58:50 +08:00
|
|
|
# Manager fail message
|
|
|
|
msg = _("Could not detect a default hypervisor. Make\n"
|
|
|
|
"sure the appropriate virtualization packages\n"
|
2017-08-14 22:04:43 +08:00
|
|
|
"containing kvm, qemu, libvirt, etc. are\n"
|
|
|
|
"installed, and that libvirtd is running.\n\n"
|
2010-03-04 05:58:50 +08:00
|
|
|
"A hypervisor connection can be manually\n"
|
|
|
|
"added via File->Add Connection")
|
|
|
|
|
|
|
|
logging.debug("Determining default libvirt URI")
|
|
|
|
|
2018-03-15 01:13:22 +08:00
|
|
|
packages_verified = False
|
2010-03-04 05:58:50 +08:00
|
|
|
try:
|
2011-01-15 04:19:58 +08:00
|
|
|
libvirt_packages = self.config.libvirt_packages
|
|
|
|
packages = self.config.hv_packages + libvirt_packages
|
|
|
|
|
2018-03-15 01:13:22 +08:00
|
|
|
packages_verified = packageutils.check_packagekit(
|
|
|
|
manager, manager.err, packages)
|
2017-07-24 16:26:48 +08:00
|
|
|
except Exception:
|
2010-03-04 05:58:50 +08:00
|
|
|
logging.exception("Error talking to PackageKit")
|
|
|
|
|
2017-08-08 05:26:48 +08:00
|
|
|
tryuri = None
|
2018-03-15 01:13:22 +08:00
|
|
|
if packages_verified:
|
2012-07-09 03:38:52 +08:00
|
|
|
tryuri = "qemu:///system"
|
2017-08-08 05:26:48 +08:00
|
|
|
elif not self.config.test_first_run:
|
2015-04-07 04:43:44 +08:00
|
|
|
tryuri = vmmConnect.default_uri()
|
2010-03-04 05:58:50 +08:00
|
|
|
|
|
|
|
if tryuri is None:
|
|
|
|
manager.set_startup_error(msg)
|
|
|
|
return
|
|
|
|
|
2018-03-16 09:22:14 +08:00
|
|
|
# packagekit API via gnome-software doesn't even work nicely these
|
|
|
|
# days. Not sure what the state of this warning is...
|
|
|
|
#
|
|
|
|
# warnmsg = _("The 'libvirtd' service will need to be started.\n\n"
|
|
|
|
# "After that, virt-manager will connect to libvirt on\n"
|
|
|
|
# "the next application start up.")
|
|
|
|
# if not connected and not libvirtd_started:
|
|
|
|
# manager.err.ok(_("Libvirt service must be started"), warnmsg)
|
2013-06-14 05:35:48 +08:00
|
|
|
|
|
|
|
def idle_connect():
|
2018-03-16 09:22:14 +08:00
|
|
|
def _open_completed(c, ConnectError):
|
|
|
|
if ConnectError:
|
|
|
|
self._handle_conn_error(c, ConnectError)
|
|
|
|
return True
|
2013-06-14 05:35:48 +08:00
|
|
|
|
2018-03-16 09:22:14 +08:00
|
|
|
packageutils.start_libvirtd()
|
|
|
|
conn = vmmConnectionManager.get_instance().add_conn(tryuri)
|
|
|
|
conn.set_autoconnect(True)
|
|
|
|
conn.connect_opt_out("open-completed", _open_completed)
|
|
|
|
conn.open()
|
2013-06-14 05:35:48 +08:00
|
|
|
self.idle_add(idle_connect)
|
2010-03-04 05:58:50 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
def _autostart_conns(self):
|
2014-02-01 23:44:45 +08:00
|
|
|
"""
|
|
|
|
We serialize conn autostart, so polkit/ssh-askpass doesn't spam
|
|
|
|
"""
|
2018-03-15 19:43:56 +08:00
|
|
|
if self._exiting:
|
|
|
|
return
|
|
|
|
|
2017-10-11 19:35:53 +08:00
|
|
|
connections_queue = queue.Queue()
|
2018-03-15 01:13:22 +08:00
|
|
|
auto_conns = [conn.get_uri() for conn in self._connobjs.values() if
|
|
|
|
conn.get_autoconnect()]
|
2014-02-01 23:44:45 +08:00
|
|
|
|
|
|
|
def add_next_to_queue():
|
|
|
|
if not auto_conns:
|
2017-10-11 19:35:53 +08:00
|
|
|
connections_queue.put(None)
|
2014-02-01 23:44:45 +08:00
|
|
|
else:
|
2017-10-11 19:35:53 +08:00
|
|
|
connections_queue.put(auto_conns.pop(0))
|
2014-02-01 23:44:45 +08:00
|
|
|
|
2018-03-16 09:22:14 +08:00
|
|
|
def conn_open_completed(_conn, ConnectError):
|
|
|
|
# Explicitly ignore connection errors, we've done that
|
|
|
|
# for a while and it can be noisy
|
|
|
|
logging.debug("Autostart connection error: %s",
|
|
|
|
ConnectError.details)
|
|
|
|
add_next_to_queue()
|
|
|
|
return True
|
2014-02-01 23:44:45 +08:00
|
|
|
|
|
|
|
def handle_queue():
|
|
|
|
while True:
|
2017-10-11 19:35:53 +08:00
|
|
|
uri = connections_queue.get()
|
2014-02-01 23:44:45 +08:00
|
|
|
if uri is None:
|
|
|
|
return
|
2018-03-15 19:43:56 +08:00
|
|
|
if self._exiting:
|
|
|
|
return
|
2018-03-15 01:13:22 +08:00
|
|
|
if uri not in self._connobjs:
|
2014-02-01 23:44:45 +08:00
|
|
|
add_next_to_queue()
|
|
|
|
continue
|
|
|
|
|
2018-03-15 01:13:22 +08:00
|
|
|
conn = self._connobjs[uri]
|
2018-03-16 09:22:14 +08:00
|
|
|
conn.connect_opt_out("open-completed", conn_open_completed)
|
|
|
|
self.idle_add(conn.open)
|
2014-02-01 23:44:45 +08:00
|
|
|
|
|
|
|
add_next_to_queue()
|
2014-09-12 23:28:27 +08:00
|
|
|
self._start_thread(handle_queue, "Conn autostart thread")
|
2008-03-24 23:39:19 +08:00
|
|
|
|
2006-06-15 02:36:26 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
############################
|
|
|
|
# Gtk Application handling #
|
|
|
|
############################
|
2011-04-12 06:35:21 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
def _on_gtk_application_activated(self, ignore):
|
|
|
|
"""
|
|
|
|
Invoked after application.run()
|
|
|
|
"""
|
|
|
|
if not self._application.get_windows():
|
|
|
|
logging.debug("Initial gtkapplication activated")
|
|
|
|
self._application.add_window(Gtk.Window())
|
2006-06-15 04:56:49 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
def _init_gtk_application(self):
|
|
|
|
self._application = Gtk.Application(
|
|
|
|
application_id="org.virt-manager.virt-manager", flags=0)
|
|
|
|
self._application.register(None)
|
|
|
|
self._application.connect("activate",
|
|
|
|
self._on_gtk_application_activated)
|
2015-11-04 04:56:39 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
action = Gio.SimpleAction.new("cli_command",
|
|
|
|
GLib.VariantType.new("(sss)"))
|
|
|
|
action.connect("activate", self._handle_cli_command)
|
|
|
|
self._application.add_action(action)
|
2015-11-04 04:56:39 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
def start(self, uri, show_window, domain, skip_autostart):
|
|
|
|
"""
|
|
|
|
Public entrypoint from virt-manager cli. If app is already
|
|
|
|
running, connect to it and exit, otherwise run our functional
|
|
|
|
default startup.
|
|
|
|
"""
|
|
|
|
# Dispatch dbus CLI command
|
|
|
|
if uri and not show_window:
|
|
|
|
show_window = self.CLI_SHOW_MANAGER
|
|
|
|
data = GLib.Variant("(sss)",
|
|
|
|
(uri or "", show_window or "", domain or ""))
|
2018-03-15 19:43:56 +08:00
|
|
|
|
|
|
|
is_remote = self._application.get_is_remote()
|
|
|
|
if not is_remote:
|
|
|
|
self._default_startup(skip_autostart, uri)
|
2018-03-15 06:43:47 +08:00
|
|
|
self._application.activate_action("cli_command", data)
|
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
if is_remote:
|
2018-03-15 06:43:47 +08:00
|
|
|
logging.debug("Connected to remote app instance.")
|
2007-09-27 09:04:02 +08:00
|
|
|
return
|
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
self._application.run(None)
|
2011-04-12 06:35:21 +08:00
|
|
|
|
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
###########################
|
|
|
|
# timer and tick handling #
|
|
|
|
###########################
|
2007-09-27 09:04:02 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
def _timer_changed_cb(self, *args, **kwargs):
|
2016-04-19 04:42:12 +08:00
|
|
|
ignore1 = args
|
|
|
|
ignore2 = kwargs
|
2018-03-15 06:43:47 +08:00
|
|
|
self._schedule_timer()
|
2006-06-14 22:59:40 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
def _schedule_timer(self):
|
2010-12-10 03:06:00 +08:00
|
|
|
interval = self.config.get_stats_update_interval() * 1000
|
2006-06-14 22:59:40 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
if self._timer is not None:
|
|
|
|
self.remove_gobject_timeout(self._timer)
|
|
|
|
self._timer = None
|
2006-06-14 22:59:40 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
self._timer = self.timeout_add(interval, self._tick)
|
2006-06-14 22:59:40 +08:00
|
|
|
|
2013-07-07 23:06:15 +08:00
|
|
|
def _add_obj_to_tick_queue(self, obj, isprio, **kwargs):
|
2013-07-07 08:03:42 +08:00
|
|
|
if self._tick_queue.full():
|
2009-07-14 21:45:23 +08:00
|
|
|
if not self._tick_thread_slow:
|
2009-07-12 09:23:16 +08:00
|
|
|
logging.debug("Tick is slow, not running at requested rate.")
|
2009-10-02 00:04:03 +08:00
|
|
|
self._tick_thread_slow = True
|
2013-07-07 08:03:42 +08:00
|
|
|
return
|
2009-07-12 09:23:16 +08:00
|
|
|
|
2013-07-07 08:03:42 +08:00
|
|
|
self._tick_counter += 1
|
2013-07-07 21:42:21 +08:00
|
|
|
self._tick_queue.put((isprio and PRIO_HIGH or PRIO_LOW,
|
2013-07-07 23:06:15 +08:00
|
|
|
self._tick_counter,
|
|
|
|
obj, kwargs))
|
2007-03-10 05:22:43 +08:00
|
|
|
|
2018-03-16 09:22:14 +08:00
|
|
|
def schedule_priority_tick(self, conn, kwargs):
|
|
|
|
# Called directly from connection
|
2013-07-07 23:06:15 +08:00
|
|
|
self._add_obj_to_tick_queue(conn, True, **kwargs)
|
2013-07-07 08:03:42 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
def _tick(self):
|
2018-03-15 01:13:22 +08:00
|
|
|
for conn in self._connobjs.values():
|
2013-07-07 23:06:15 +08:00
|
|
|
self._add_obj_to_tick_queue(conn, False,
|
|
|
|
stats_update=True, pollvm=True)
|
2013-07-07 08:03:42 +08:00
|
|
|
return 1
|
2011-07-26 01:31:36 +08:00
|
|
|
|
2015-04-12 02:41:02 +08:00
|
|
|
def _handle_tick_error(self, msg, details):
|
2018-03-15 17:53:58 +08:00
|
|
|
from .systray import vmmSystray
|
|
|
|
if (self._window_count == 1 and
|
|
|
|
vmmSystray.get_instance().is_visible()):
|
2015-04-12 02:41:02 +08:00
|
|
|
# This means the systray icon is running. Don't raise an error
|
|
|
|
# here to avoid spamming dialogs out of nowhere.
|
|
|
|
logging.debug(msg + "\n\n" + details)
|
|
|
|
return
|
|
|
|
self.err.show_err(msg, details=details)
|
|
|
|
|
2013-07-07 08:03:42 +08:00
|
|
|
def _handle_tick_queue(self):
|
|
|
|
while True:
|
2014-04-16 22:32:52 +08:00
|
|
|
ignore1, ignore2, conn, kwargs = self._tick_queue.get()
|
|
|
|
try:
|
2015-04-14 04:56:46 +08:00
|
|
|
conn.tick_from_engine(**kwargs)
|
2017-05-06 00:47:21 +08:00
|
|
|
except Exception as e:
|
2014-04-16 22:32:52 +08:00
|
|
|
tb = "".join(traceback.format_exc())
|
|
|
|
error_msg = (_("Error polling connection '%s': %s")
|
|
|
|
% (conn.get_uri(), e))
|
2015-04-12 02:41:02 +08:00
|
|
|
self.idle_add(self._handle_tick_error, error_msg, tb)
|
2014-04-16 22:32:52 +08:00
|
|
|
|
2015-09-21 02:20:50 +08:00
|
|
|
# Need to clear reference to make leak check happy
|
|
|
|
conn = None
|
2013-07-07 08:03:42 +08:00
|
|
|
self._tick_queue.task_done()
|
2006-06-14 22:59:40 +08:00
|
|
|
return 1
|
|
|
|
|
2013-07-07 08:03:42 +08:00
|
|
|
|
2018-03-15 06:43:47 +08:00
|
|
|
#####################################
|
|
|
|
# window counting and exit handling #
|
|
|
|
#####################################
|
|
|
|
|
2018-03-15 07:06:30 +08:00
|
|
|
def increment_window_counter(self):
|
|
|
|
"""
|
|
|
|
Public function, called by toplevel windows
|
|
|
|
"""
|
2018-03-15 17:53:58 +08:00
|
|
|
self._window_count += 1
|
|
|
|
logging.debug("window counter incremented to %s", self._window_count)
|
2008-06-14 00:12:37 +08:00
|
|
|
|
2018-03-15 07:06:30 +08:00
|
|
|
def decrement_window_counter(self):
|
|
|
|
"""
|
|
|
|
Public function, called by toplevel windows
|
|
|
|
"""
|
2018-03-15 17:53:58 +08:00
|
|
|
self._window_count -= 1
|
|
|
|
logging.debug("window counter decremented to %s", self._window_count)
|
2011-04-13 21:27:02 +08:00
|
|
|
|
2018-03-15 07:06:30 +08:00
|
|
|
self._exit_app_if_no_windows()
|
2008-06-14 00:12:37 +08:00
|
|
|
|
2013-07-13 10:10:16 +08:00
|
|
|
def _can_exit(self):
|
2018-03-15 17:53:58 +08:00
|
|
|
return self._window_count <= 0
|
2013-07-13 10:10:16 +08:00
|
|
|
|
2018-03-15 07:06:30 +08:00
|
|
|
def _exit_app_if_no_windows(self):
|
2018-03-15 17:53:58 +08:00
|
|
|
if self._can_exit():
|
|
|
|
logging.debug("No windows found, requesting app exit")
|
|
|
|
self.exit_app()
|
2015-12-04 04:54:52 +08:00
|
|
|
|
2018-03-15 06:58:22 +08:00
|
|
|
def exit_app(self):
|
|
|
|
"""
|
|
|
|
Public call, manager/details/... use this to force exit the app
|
|
|
|
"""
|
2018-03-15 17:53:58 +08:00
|
|
|
if self._exiting:
|
2011-04-13 21:27:02 +08:00
|
|
|
return
|
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
self._exiting = True
|
2011-04-14 23:26:53 +08:00
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
def _do_exit():
|
|
|
|
try:
|
|
|
|
vmmConnectionManager.get_instance().cleanup()
|
|
|
|
self.emit("app-closing")
|
|
|
|
self.cleanup()
|
2011-04-12 00:54:47 +08:00
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
if self.config.test_leak_debug:
|
|
|
|
objs = self.config.get_objects()
|
|
|
|
# Engine will always appear to leak
|
|
|
|
objs.remove(self.object_key)
|
2008-06-14 00:12:37 +08:00
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
for name in objs:
|
|
|
|
logging.debug("LEAK: %s", name)
|
2015-04-12 02:34:38 +08:00
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
logging.debug("Exiting app normally.")
|
|
|
|
finally:
|
|
|
|
self._application.quit()
|
2012-11-08 18:02:17 +08:00
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
# We stick this in an idle callback, so the exit_app() caller
|
|
|
|
# reference is dropped, and leak check debug doesn't give a
|
|
|
|
# false positive
|
|
|
|
self.idle_add(_do_exit)
|
2010-11-24 07:54:12 +08:00
|
|
|
|
2017-02-23 18:22:27 +08:00
|
|
|
|
2010-11-24 09:13:50 +08:00
|
|
|
##########################################
|
|
|
|
# Window launchers from virt-manager cli #
|
|
|
|
##########################################
|
|
|
|
|
2014-06-03 05:17:47 +08:00
|
|
|
def _find_vm_by_cli_str(self, uri, clistr):
|
|
|
|
"""
|
|
|
|
Lookup a VM by a string passed in on the CLI. Can be either
|
|
|
|
ID, domain name, or UUID
|
|
|
|
"""
|
|
|
|
if clistr.isdigit():
|
|
|
|
clistr = int(clistr)
|
|
|
|
|
2018-03-15 01:13:22 +08:00
|
|
|
for vm in self._connobjs[uri].list_vms():
|
2014-06-03 05:17:47 +08:00
|
|
|
if clistr == vm.get_id():
|
|
|
|
return vm
|
|
|
|
elif clistr == vm.get_name():
|
|
|
|
return vm
|
|
|
|
elif clistr == vm.get_uuid():
|
|
|
|
return vm
|
|
|
|
|
|
|
|
def _cli_show_vm_helper(self, uri, clistr, page):
|
2018-03-15 06:43:47 +08:00
|
|
|
src = self._get_manager()
|
2014-06-03 05:17:47 +08:00
|
|
|
|
|
|
|
vm = self._find_vm_by_cli_str(uri, clistr)
|
|
|
|
if not vm:
|
|
|
|
src.err.show_err("%s does not have VM '%s'" %
|
|
|
|
(uri, clistr), modal=True)
|
|
|
|
return
|
|
|
|
|
2018-03-15 17:53:58 +08:00
|
|
|
try:
|
|
|
|
from .details import vmmDetails
|
|
|
|
details = vmmDetails.get_instance(src, vm)
|
|
|
|
|
|
|
|
if page == DETAILS_PERF:
|
|
|
|
details.activate_performance_page()
|
|
|
|
elif page == DETAILS_CONFIG:
|
|
|
|
details.activate_config_page()
|
|
|
|
elif page == DETAILS_CONSOLE:
|
|
|
|
details.activate_console_page()
|
|
|
|
elif page is None:
|
|
|
|
details.activate_default_page()
|
2014-06-03 05:17:47 +08:00
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
details.show(default_page=False)
|
2018-03-15 17:53:58 +08:00
|
|
|
except Exception as e:
|
|
|
|
src.err.show_err(_("Error launching details: %s") % str(e))
|
|
|
|
|
|
|
|
def _get_manager(self):
|
|
|
|
from .manager import vmmManager
|
|
|
|
return vmmManager.get_instance(self)
|
|
|
|
|
|
|
|
def _show_manager_uri(self, uri):
|
|
|
|
manager = self._get_manager()
|
|
|
|
manager.set_initial_selection(uri)
|
|
|
|
manager.show()
|
2015-11-25 04:21:26 +08:00
|
|
|
|
|
|
|
def _show_host_summary(self, uri):
|
2018-03-15 17:53:58 +08:00
|
|
|
from .host import vmmHost
|
|
|
|
vmmHost.show_instance(self._get_manager(), self._connobjs[uri])
|
2015-11-25 04:21:26 +08:00
|
|
|
|
|
|
|
def _show_domain_creator(self, uri):
|
2018-03-15 17:53:58 +08:00
|
|
|
from .create import vmmCreate
|
|
|
|
vmmCreate.show_instance(self._get_manager(), uri)
|
2015-11-25 04:21:26 +08:00
|
|
|
|
|
|
|
def _show_domain_console(self, uri, clistr):
|
2015-12-04 04:54:52 +08:00
|
|
|
self._cli_show_vm_helper(uri, clistr, DETAILS_CONSOLE)
|
2014-06-03 05:17:47 +08:00
|
|
|
|
2015-11-25 04:21:26 +08:00
|
|
|
def _show_domain_editor(self, uri, clistr):
|
2015-12-04 04:54:52 +08:00
|
|
|
self._cli_show_vm_helper(uri, clistr, DETAILS_CONFIG)
|
2010-11-24 09:13:50 +08:00
|
|
|
|
2015-11-25 04:21:26 +08:00
|
|
|
def _show_domain_performance(self, uri, clistr):
|
2015-12-04 04:54:52 +08:00
|
|
|
self._cli_show_vm_helper(uri, clistr, DETAILS_PERF)
|
2010-11-24 07:54:12 +08:00
|
|
|
|
2015-11-25 04:21:26 +08:00
|
|
|
def _launch_cli_window(self, uri, show_window, clistr):
|
2015-12-04 04:54:52 +08:00
|
|
|
try:
|
|
|
|
logging.debug("Launching requested window '%s'", show_window)
|
2018-01-10 01:58:38 +08:00
|
|
|
if show_window == self.CLI_SHOW_MANAGER:
|
2018-03-15 17:53:58 +08:00
|
|
|
self._show_manager_uri(uri)
|
2018-01-10 01:58:38 +08:00
|
|
|
elif show_window == self.CLI_SHOW_DOMAIN_CREATOR:
|
2015-12-04 04:54:52 +08:00
|
|
|
self._show_domain_creator(uri)
|
|
|
|
elif show_window == self.CLI_SHOW_DOMAIN_EDITOR:
|
|
|
|
self._show_domain_editor(uri, clistr)
|
|
|
|
elif show_window == self.CLI_SHOW_DOMAIN_PERFORMANCE:
|
|
|
|
self._show_domain_performance(uri, clistr)
|
|
|
|
elif show_window == self.CLI_SHOW_DOMAIN_CONSOLE:
|
|
|
|
self._show_domain_console(uri, clistr)
|
|
|
|
elif show_window == self.CLI_SHOW_HOST_SUMMARY:
|
|
|
|
self._show_host_summary(uri)
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Unknown cli window command '%s'" %
|
|
|
|
show_window)
|
|
|
|
finally:
|
|
|
|
# In case of cli error, we may need to exit the app
|
|
|
|
self._exit_app_if_no_windows()
|
2015-11-25 04:21:26 +08:00
|
|
|
|
2018-03-16 09:22:14 +08:00
|
|
|
def _handle_conn_error(self, _conn, ConnectError):
|
|
|
|
msg, details, title = ConnectError
|
|
|
|
modal = self._can_exit()
|
|
|
|
self.err.show_err(msg, details, title, modal=modal)
|
|
|
|
self._exit_app_if_no_windows()
|
2015-11-25 04:21:26 +08:00
|
|
|
|
2015-12-04 04:54:52 +08:00
|
|
|
def _do_handle_cli_command(self, actionobj, variant):
|
|
|
|
ignore = actionobj
|
|
|
|
uri = variant[0]
|
2018-03-16 09:22:14 +08:00
|
|
|
show_window = variant[1] or self.CLI_SHOW_MANAGER
|
2015-12-04 04:54:52 +08:00
|
|
|
domain = variant[2]
|
2015-11-25 04:21:26 +08:00
|
|
|
|
2015-12-04 04:54:52 +08:00
|
|
|
logging.debug("processing cli command uri=%s show_window=%s domain=%s",
|
|
|
|
uri, show_window, domain)
|
2015-11-25 04:21:26 +08:00
|
|
|
if not uri:
|
2015-12-04 04:54:52 +08:00
|
|
|
logging.debug("No cli action requested, launching default window")
|
2018-03-15 17:53:58 +08:00
|
|
|
self._get_manager().show()
|
2015-11-25 04:21:26 +08:00
|
|
|
return
|
|
|
|
|
2018-03-17 05:33:06 +08:00
|
|
|
conn_is_new = uri not in self._connobjs
|
2018-03-16 09:22:14 +08:00
|
|
|
conn = vmmConnectionManager.get_instance().add_conn(uri)
|
|
|
|
if conn.is_active():
|
|
|
|
self.idle_add(self._launch_cli_window,
|
|
|
|
uri, show_window, domain)
|
|
|
|
return
|
2015-12-04 04:54:52 +08:00
|
|
|
|
2018-03-16 09:22:14 +08:00
|
|
|
def _open_completed(_c, ConnectError):
|
|
|
|
if ConnectError:
|
2018-03-17 05:33:06 +08:00
|
|
|
if conn_is_new:
|
|
|
|
logging.debug("Removing failed uri=%s", uri)
|
|
|
|
vmmConnectionManager.get_instance().remove_conn(uri)
|
2018-03-16 09:22:14 +08:00
|
|
|
self._handle_conn_error(conn, ConnectError)
|
2015-11-25 04:21:26 +08:00
|
|
|
else:
|
2018-03-16 09:22:14 +08:00
|
|
|
self._launch_cli_window(uri, show_window, domain)
|
|
|
|
return True
|
|
|
|
|
|
|
|
conn.connect_opt_out("open-completed", _open_completed)
|
|
|
|
conn.open()
|
2015-11-25 04:21:26 +08:00
|
|
|
|
2015-12-04 04:54:52 +08:00
|
|
|
def _handle_cli_command(self, actionobj, variant):
|
|
|
|
try:
|
|
|
|
return self._do_handle_cli_command(actionobj, variant)
|
2017-07-24 16:26:48 +08:00
|
|
|
except Exception:
|
2015-12-04 04:54:52 +08:00
|
|
|
# In case of cli error, we may need to exit the app
|
|
|
|
logging.debug("Error handling cli command", exc_info=True)
|
|
|
|
self._exit_app_if_no_windows()
|