2013-10-28 04:59:46 +08:00
|
|
|
# Copyright (C) 2010, 2013 Red Hat, Inc.
|
2010-12-09 06:26:19 +08:00
|
|
|
# Copyright (C) 2010 Cole Robinson <crobinso@redhat.com>
|
|
|
|
#
|
2018-04-04 21:35:41 +08:00
|
|
|
# This work is licensed under the GNU GPLv2 or later.
|
2018-03-21 03:00:02 +08:00
|
|
|
# See the COPYING file in the top-level directory.
|
2010-12-09 06:26:19 +08:00
|
|
|
|
|
|
|
import os
|
2011-04-11 23:00:57 +08:00
|
|
|
import sys
|
2014-09-12 23:28:27 +08:00
|
|
|
import threading
|
2013-06-18 20:46:24 +08:00
|
|
|
import traceback
|
2018-03-15 19:43:56 +08:00
|
|
|
import types
|
2010-12-09 06:26:19 +08:00
|
|
|
|
2013-04-19 04:03:43 +08:00
|
|
|
from gi.repository import Gdk
|
2013-02-17 03:03:30 +08:00
|
|
|
from gi.repository import GLib
|
2012-05-14 21:24:56 +08:00
|
|
|
from gi.repository import GObject
|
2013-04-19 04:03:43 +08:00
|
|
|
from gi.repository import Gtk
|
2010-12-09 06:26:19 +08:00
|
|
|
|
2019-06-17 09:12:39 +08:00
|
|
|
from virtinst import log
|
2020-09-10 03:14:05 +08:00
|
|
|
from virtinst import xmlutil
|
2019-06-17 09:12:39 +08:00
|
|
|
|
2016-04-19 04:42:12 +08:00
|
|
|
from . import config
|
|
|
|
|
2011-04-19 00:39:53 +08:00
|
|
|
|
2013-02-17 03:03:30 +08:00
|
|
|
class vmmGObject(GObject.GObject):
|
2018-03-15 20:10:09 +08:00
|
|
|
# Objects can set this to false to disable leak tracking
|
2011-04-29 05:13:05 +08:00
|
|
|
_leak_check = True
|
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
# Singleton reference, if applicable (vmmSystray, vmmInspection, ...)
|
|
|
|
_instance = None
|
|
|
|
|
2019-05-06 04:25:35 +08:00
|
|
|
# windowlist mapping, if applicable (vmmVMWindow, vmmHost, ...)
|
2018-03-18 07:42:19 +08:00
|
|
|
_instances = None
|
2018-03-15 19:43:56 +08:00
|
|
|
|
2018-03-15 20:10:09 +08:00
|
|
|
# This saves a bunch of imports and typing
|
|
|
|
RUN_FIRST = GObject.SignalFlags.RUN_FIRST
|
|
|
|
|
2013-09-07 07:36:09 +08:00
|
|
|
@staticmethod
|
|
|
|
def idle_add(func, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Make sure idle functions are run thread safe
|
|
|
|
"""
|
|
|
|
def cb():
|
2016-05-18 05:31:19 +08:00
|
|
|
return func(*args, **kwargs)
|
2013-09-07 07:36:09 +08:00
|
|
|
return GLib.idle_add(cb)
|
|
|
|
|
2010-12-09 06:26:19 +08:00
|
|
|
def __init__(self):
|
2012-05-14 21:24:56 +08:00
|
|
|
GObject.GObject.__init__(self)
|
2010-12-09 06:26:19 +08:00
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
self.__cleaned_up = False
|
|
|
|
|
2011-04-11 23:00:57 +08:00
|
|
|
self._gobject_handles = []
|
2018-03-15 19:43:56 +08:00
|
|
|
self._gobject_handles_map = {}
|
2011-04-11 23:00:57 +08:00
|
|
|
self._gobject_timeouts = []
|
2014-09-28 19:37:16 +08:00
|
|
|
self._gsettings_handles = []
|
2011-04-11 23:00:57 +08:00
|
|
|
|
2011-07-23 06:07:57 +08:00
|
|
|
self._signal_id_map = {}
|
|
|
|
self._next_signal_id = 1
|
|
|
|
|
2011-04-13 21:27:02 +08:00
|
|
|
self.object_key = str(self)
|
2011-04-16 06:38:29 +08:00
|
|
|
|
|
|
|
# Config might not be available if we error early in startup
|
2018-03-14 00:13:11 +08:00
|
|
|
if config.vmmConfig.is_initialized() and self._leak_check:
|
2011-04-16 06:38:29 +08:00
|
|
|
self.config.add_object(self.object_key)
|
2011-04-12 00:54:47 +08:00
|
|
|
|
2018-03-16 09:22:14 +08:00
|
|
|
def _get_err(self):
|
|
|
|
from . import error
|
|
|
|
return error.vmmErrorDialog.get_instance()
|
|
|
|
err = property(_get_err)
|
|
|
|
|
2011-04-11 23:00:57 +08:00
|
|
|
def cleanup(self):
|
2018-03-15 19:43:56 +08:00
|
|
|
if self.__cleaned_up:
|
2020-08-23 03:42:08 +08:00
|
|
|
return # pragma: no cover
|
2018-03-15 19:43:56 +08:00
|
|
|
|
2011-04-11 23:00:57 +08:00
|
|
|
# Do any cleanup required to drop reference counts so object is
|
|
|
|
# actually reaped by python. Usually means unregistering callbacks
|
|
|
|
try:
|
2018-03-15 19:43:56 +08:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
if self.__class__._instance == self:
|
|
|
|
# We set this to True which can help us catch instances
|
|
|
|
# where cleanup routines try to reinit singleton classes
|
|
|
|
self.__class__._instance = True
|
2018-03-18 07:42:19 +08:00
|
|
|
|
|
|
|
_instances = self.__class__._instances or {}
|
|
|
|
for k, v in list(_instances.items()):
|
2018-03-15 19:43:56 +08:00
|
|
|
if v == self:
|
2018-03-18 07:42:19 +08:00
|
|
|
_instances.pop(k)
|
2018-03-15 19:43:56 +08:00
|
|
|
|
|
|
|
self._cleanup()
|
|
|
|
|
2014-09-28 19:37:16 +08:00
|
|
|
for h in self._gsettings_handles[:]:
|
|
|
|
self.remove_gsettings_handle(h)
|
2011-04-11 23:00:57 +08:00
|
|
|
for h in self._gobject_handles[:]:
|
2014-02-01 23:44:45 +08:00
|
|
|
if GObject.GObject.handler_is_connected(self, h):
|
|
|
|
self.disconnect(h)
|
2011-04-11 23:00:57 +08:00
|
|
|
for h in self._gobject_timeouts[:]:
|
|
|
|
self.remove_gobject_timeout(h)
|
2020-08-23 03:42:08 +08:00
|
|
|
except Exception: # pragma: no cover
|
2019-06-17 09:12:39 +08:00
|
|
|
log.exception("Error cleaning up %s", self)
|
2011-04-11 23:00:57 +08:00
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
self.__cleaned_up = True
|
|
|
|
|
|
|
|
def _cleanup_on_app_close(self):
|
|
|
|
from .engine import vmmEngine
|
|
|
|
vmmEngine.get_instance().connect(
|
|
|
|
"app-closing", lambda src: self.cleanup())
|
|
|
|
|
2011-07-24 09:16:54 +08:00
|
|
|
def _cleanup(self):
|
|
|
|
raise NotImplementedError("_cleanup must be implemented in subclass")
|
|
|
|
|
2017-07-18 04:55:15 +08:00
|
|
|
def __del__(self):
|
|
|
|
try:
|
2018-03-14 00:13:11 +08:00
|
|
|
if config.vmmConfig.is_initialized() and self._leak_check:
|
2017-07-18 04:55:15 +08:00
|
|
|
self.config.remove_object(self.object_key)
|
2020-08-23 03:42:08 +08:00
|
|
|
except Exception: # pragma: no cover
|
2019-06-17 09:12:39 +08:00
|
|
|
log.exception("Error removing %s", self.object_key)
|
2017-07-18 04:55:15 +08:00
|
|
|
|
2018-03-14 00:13:11 +08:00
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
return config.vmmConfig.get_instance()
|
|
|
|
|
|
|
|
|
2014-03-22 23:21:19 +08:00
|
|
|
# pylint: disable=arguments-differ
|
|
|
|
# Newer pylint can detect, but warns that overridden arguments are wrong
|
|
|
|
|
2011-04-12 01:06:59 +08:00
|
|
|
def connect(self, name, callback, *args):
|
2017-07-18 04:55:15 +08:00
|
|
|
"""
|
|
|
|
GObject connect() wrapper to simplify callers, and track handles
|
|
|
|
for easy cleanup
|
|
|
|
"""
|
2012-05-14 21:24:56 +08:00
|
|
|
ret = GObject.GObject.connect(self, name, callback, *args)
|
2018-03-15 19:43:56 +08:00
|
|
|
|
|
|
|
# If the passed callback is a method of a class instance,
|
|
|
|
# keep a mapping of id(instance):[handles]. This lets us
|
|
|
|
# implement disconnect_by_obj to simplify signal removal
|
|
|
|
if isinstance(callback, types.MethodType):
|
|
|
|
i = id(callback.__self__)
|
|
|
|
if i not in self._gobject_handles_map:
|
|
|
|
self._gobject_handles_map[i] = []
|
|
|
|
self._gobject_handles_map[i].append(ret)
|
|
|
|
|
2012-05-14 21:24:56 +08:00
|
|
|
self._gobject_handles.append(ret)
|
|
|
|
return ret
|
2011-07-23 06:07:57 +08:00
|
|
|
|
2011-04-12 01:06:59 +08:00
|
|
|
def disconnect(self, handle):
|
2017-07-18 04:55:15 +08:00
|
|
|
"""
|
|
|
|
GObject disconnect() wrapper to simplify callers
|
|
|
|
"""
|
2012-05-14 21:24:56 +08:00
|
|
|
ret = GObject.GObject.disconnect(self, handle)
|
|
|
|
self._gobject_handles.remove(handle)
|
|
|
|
return ret
|
2011-04-12 01:06:59 +08:00
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
def disconnect_by_obj(self, instance):
|
|
|
|
"""
|
|
|
|
disconnect() every signal attached to a method of the passed instance
|
|
|
|
"""
|
|
|
|
i = id(instance)
|
|
|
|
for handle in self._gobject_handles_map.get(i, []):
|
|
|
|
if handle in self._gobject_handles:
|
|
|
|
self.disconnect(handle)
|
|
|
|
self._gobject_handles_map.pop(i, None)
|
|
|
|
|
2017-07-18 04:55:15 +08:00
|
|
|
def timeout_add(self, timeout, func, *args):
|
|
|
|
"""
|
|
|
|
GLib timeout_add wrapper to simplify callers, and track handles
|
|
|
|
for easy cleanup
|
|
|
|
"""
|
|
|
|
ret = GLib.timeout_add(timeout, func, *args)
|
|
|
|
self.add_gobject_timeout(ret)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def emit(self, signal_name, *args):
|
|
|
|
"""
|
|
|
|
GObject emit() wrapper to simplify callers
|
|
|
|
"""
|
2020-08-23 03:42:08 +08:00
|
|
|
if not self._is_main_thread(): # pragma: no cover
|
2020-01-16 03:38:24 +08:00
|
|
|
log.error("emitting signal from non-main thread. This is a bug "
|
|
|
|
"please report it. thread=%s self=%s signal=%s",
|
|
|
|
self._thread_name(), self, signal_name)
|
2017-07-18 04:55:15 +08:00
|
|
|
return GObject.GObject.emit(self, signal_name, *args)
|
|
|
|
|
2014-09-28 19:37:16 +08:00
|
|
|
def add_gsettings_handle(self, handle):
|
|
|
|
self._gsettings_handles.append(handle)
|
|
|
|
def remove_gsettings_handle(self, handle):
|
2011-04-11 23:00:57 +08:00
|
|
|
self.config.remove_notifier(handle)
|
2014-09-28 19:37:16 +08:00
|
|
|
self._gsettings_handles.remove(handle)
|
2011-04-11 23:00:57 +08:00
|
|
|
|
|
|
|
def add_gobject_timeout(self, handle):
|
|
|
|
self._gobject_timeouts.append(handle)
|
|
|
|
def remove_gobject_timeout(self, handle):
|
2013-02-17 03:03:30 +08:00
|
|
|
GLib.source_remove(handle)
|
2011-04-11 23:00:57 +08:00
|
|
|
self._gobject_timeouts.remove(handle)
|
|
|
|
|
2020-08-23 03:42:08 +08:00
|
|
|
def _start_thread(self, target=None, name=None, args=None, kwargs=None):
|
|
|
|
# Helper for starting a daemonized thread
|
|
|
|
t = threading.Thread(target=target, name=name,
|
|
|
|
args=args or [], kwargs=kwargs or {})
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
|
|
|
|
##############################
|
|
|
|
# Internal debugging helpers #
|
|
|
|
##############################
|
|
|
|
|
2018-03-15 19:43:56 +08:00
|
|
|
def _refcount(self):
|
|
|
|
return sys.getrefcount(self)
|
|
|
|
|
2013-01-12 05:15:06 +08:00
|
|
|
def _logtrace(self, msg=""):
|
|
|
|
if msg:
|
|
|
|
msg += " "
|
2019-06-17 09:12:39 +08:00
|
|
|
log.debug("%s(%s %s)\n:%s",
|
2013-04-26 00:07:57 +08:00
|
|
|
msg, self.object_key, self._refcount(),
|
2012-01-17 11:04:40 +08:00
|
|
|
"".join(traceback.format_stack()))
|
2011-04-13 21:27:02 +08:00
|
|
|
|
2020-08-23 03:42:08 +08:00
|
|
|
def _gc_get_referrers(self): # pragma: no cover
|
2018-03-15 19:43:56 +08:00
|
|
|
import gc
|
|
|
|
import pprint
|
|
|
|
pprint.pprint(gc.get_referrers(self))
|
2011-04-11 23:00:57 +08:00
|
|
|
|
2020-01-16 03:38:24 +08:00
|
|
|
def _thread_name(self):
|
|
|
|
return threading.current_thread().name
|
|
|
|
|
|
|
|
def _is_main_thread(self):
|
|
|
|
return self._thread_name() == "MainThread"
|
|
|
|
|
2017-07-18 04:55:15 +08:00
|
|
|
|
|
|
|
##############################
|
|
|
|
# Custom signal/idle helpers #
|
|
|
|
##############################
|
|
|
|
|
2011-04-18 06:27:41 +08:00
|
|
|
def connect_once(self, signal, func, *args):
|
2017-07-18 04:55:15 +08:00
|
|
|
"""
|
|
|
|
Like standard glib connect(), but only runs the signal handler
|
|
|
|
once, then unregisters it
|
|
|
|
"""
|
2011-04-18 06:27:41 +08:00
|
|
|
id_list = []
|
|
|
|
|
|
|
|
def wrap_func(*wrapargs):
|
|
|
|
if id_list:
|
|
|
|
self.disconnect(id_list[0])
|
|
|
|
|
|
|
|
return func(*wrapargs)
|
|
|
|
|
|
|
|
conn_id = self.connect(signal, wrap_func, *args)
|
|
|
|
id_list.append(conn_id)
|
|
|
|
|
|
|
|
return conn_id
|
|
|
|
|
|
|
|
def connect_opt_out(self, signal, func, *args):
|
2017-07-18 04:55:15 +08:00
|
|
|
"""
|
|
|
|
Like standard glib connect(), but allows the signal handler to
|
|
|
|
unregister itself if it returns True
|
|
|
|
"""
|
2011-04-18 06:27:41 +08:00
|
|
|
id_list = []
|
|
|
|
|
|
|
|
def wrap_func(*wrapargs):
|
|
|
|
ret = func(*wrapargs)
|
|
|
|
if ret and id_list:
|
|
|
|
self.disconnect(id_list[0])
|
|
|
|
|
|
|
|
conn_id = self.connect(signal, wrap_func, *args)
|
|
|
|
id_list.append(conn_id)
|
|
|
|
|
|
|
|
return conn_id
|
|
|
|
|
2011-04-18 23:12:36 +08:00
|
|
|
def idle_emit(self, signal, *args):
|
|
|
|
"""
|
2013-02-17 03:03:30 +08:00
|
|
|
Safe wrapper for using 'self.emit' with GLib.idle_add
|
2011-04-18 23:12:36 +08:00
|
|
|
"""
|
|
|
|
def emitwrap(_s, *_a):
|
|
|
|
self.emit(_s, *_a)
|
|
|
|
return False
|
|
|
|
|
2012-02-11 03:07:51 +08:00
|
|
|
self.idle_add(emitwrap, signal, *args)
|
2011-04-18 23:12:36 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2010-12-09 06:26:19 +08:00
|
|
|
class vmmGObjectUI(vmmGObject):
|
2013-06-09 07:25:36 +08:00
|
|
|
def __init__(self, filename, windowname, builder=None, topwin=None):
|
2010-12-09 06:26:19 +08:00
|
|
|
vmmGObject.__init__(self)
|
2014-01-29 02:59:31 +08:00
|
|
|
self._external_topwin = bool(topwin)
|
2018-03-15 19:43:56 +08:00
|
|
|
self.__cleaned_up = False
|
2010-12-09 06:26:19 +08:00
|
|
|
|
|
|
|
if filename:
|
2013-06-09 07:25:36 +08:00
|
|
|
uifile = os.path.join(self.config.get_ui_dir(), filename)
|
2012-02-02 06:26:46 +08:00
|
|
|
|
2013-02-17 02:31:46 +08:00
|
|
|
self.builder = Gtk.Builder()
|
|
|
|
self.builder.set_translation_domain("virt-manager")
|
2017-12-21 05:04:36 +08:00
|
|
|
self.builder.add_from_file(uifile)
|
2012-02-02 06:26:46 +08:00
|
|
|
|
Initial snapshot support
This adds initial UI for managing snapshots: list, run/revert, delete,
add, and redefining (for changing <description>) supported, but currently
only for internal snapshots. The UI is mostly in its final form except for
some bells and whistles.
The real remaining question is what do we want to advertise and support.
Internal (qcow2) snapshots are by far the simplest to manage, very
mature, and already have the semantics we want.
However most recent libvirt and qemu work has been to facilitate
external snapshots, which are more extensible and can be performed
live, and with qemu-ga coordination for extra safety. However
they make things much harder for virt-manager at the moment.
Until we have a plan, this work should be considered experimental
and not be relied upon.
2013-08-02 22:18:47 +08:00
|
|
|
if not topwin:
|
|
|
|
self.topwin = self.widget(windowname)
|
|
|
|
self.topwin.hide()
|
|
|
|
else:
|
|
|
|
self.topwin = topwin
|
2013-06-09 07:25:36 +08:00
|
|
|
else:
|
|
|
|
self.builder = builder
|
|
|
|
self.topwin = topwin
|
2010-12-09 06:26:19 +08:00
|
|
|
|
2013-09-02 08:24:15 +08:00
|
|
|
self._err = None
|
|
|
|
|
|
|
|
def _get_err(self):
|
|
|
|
if self._err is None:
|
2014-09-13 04:10:45 +08:00
|
|
|
from . import error
|
2013-09-02 08:24:15 +08:00
|
|
|
self._err = error.vmmErrorDialog(self.topwin)
|
|
|
|
return self._err
|
|
|
|
err = property(_get_err)
|
2011-04-12 01:06:59 +08:00
|
|
|
|
2011-07-15 01:13:13 +08:00
|
|
|
def widget(self, name):
|
2020-09-10 03:14:05 +08:00
|
|
|
ret = self.builder.get_object(name)
|
|
|
|
if not ret:
|
|
|
|
raise xmlutil.DevError("Did not find widget name=%s" % name)
|
|
|
|
return ret
|
2011-07-15 01:13:13 +08:00
|
|
|
|
2011-04-12 01:06:59 +08:00
|
|
|
def cleanup(self):
|
2018-03-15 19:43:56 +08:00
|
|
|
if self.__cleaned_up:
|
2020-08-23 03:42:08 +08:00
|
|
|
return # pragma: no cover
|
2018-03-15 19:43:56 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.close()
|
|
|
|
vmmGObject.cleanup(self)
|
|
|
|
self.builder = None
|
|
|
|
if not self._external_topwin:
|
|
|
|
self.topwin.destroy()
|
|
|
|
self.topwin = None
|
|
|
|
self._err = None
|
2020-08-23 03:42:08 +08:00
|
|
|
except Exception: # pragma: no cover
|
2019-06-17 09:12:39 +08:00
|
|
|
log.exception("Error cleaning up %s", self)
|
2018-03-15 19:43:56 +08:00
|
|
|
|
|
|
|
self.__cleaned_up = True
|
2011-04-18 23:25:28 +08:00
|
|
|
|
2011-07-24 09:16:54 +08:00
|
|
|
def _cleanup(self):
|
|
|
|
raise NotImplementedError("_cleanup must be implemented in subclass")
|
|
|
|
|
2011-04-29 02:38:16 +08:00
|
|
|
def close(self, ignore1=None, ignore2=None):
|
|
|
|
pass
|
|
|
|
|
2019-04-14 05:03:16 +08:00
|
|
|
def is_visible(self):
|
|
|
|
return bool(self.topwin and self.topwin.get_visible())
|
|
|
|
|
2011-04-18 23:25:28 +08:00
|
|
|
def bind_escape_key_close(self):
|
2019-05-24 08:34:55 +08:00
|
|
|
def close_on_escape(src_ignore, event):
|
|
|
|
if Gdk.keyval_name(event.keyval) == "Escape":
|
|
|
|
self.close()
|
|
|
|
self.topwin.connect("key-press-event", close_on_escape)
|
2017-04-28 03:00:17 +08:00
|
|
|
|
2018-04-04 04:12:18 +08:00
|
|
|
def _set_cursor(self, cursor_type):
|
2017-05-03 17:09:53 +08:00
|
|
|
gdk_window = self.topwin.get_window()
|
|
|
|
if not gdk_window:
|
2017-04-28 03:00:17 +08:00
|
|
|
return
|
2018-04-04 01:09:12 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
cursor = Gdk.Cursor.new_from_name(
|
2018-04-04 04:12:18 +08:00
|
|
|
gdk_window.get_display(), cursor_type)
|
2018-04-04 01:09:12 +08:00
|
|
|
gdk_window.set_cursor(cursor)
|
2020-08-23 03:42:08 +08:00
|
|
|
except Exception: # pragma: no cover
|
2018-04-04 01:09:12 +08:00
|
|
|
# If a cursor icon theme isn't installed this can cause errors
|
|
|
|
# https://bugzilla.redhat.com/show_bug.cgi?id=1516588
|
2019-06-17 09:12:39 +08:00
|
|
|
log.debug("Error setting cursor_type=%s",
|
2018-04-04 04:12:18 +08:00
|
|
|
cursor_type, exc_info=True)
|
|
|
|
|
|
|
|
def set_finish_cursor(self):
|
|
|
|
self.topwin.set_sensitive(False)
|
|
|
|
self._set_cursor("progress")
|
|
|
|
|
|
|
|
def reset_finish_cursor(self, topwin=None):
|
|
|
|
if not topwin:
|
|
|
|
topwin = self.topwin
|
|
|
|
topwin.set_sensitive(True)
|
|
|
|
self._set_cursor("default")
|
2018-03-15 19:43:56 +08:00
|
|
|
|
|
|
|
def _cleanup_on_conn_removed(self):
|
|
|
|
from .connmanager import vmmConnectionManager
|
|
|
|
connmanager = vmmConnectionManager.get_instance()
|
|
|
|
|
|
|
|
def _cb(_src, uri):
|
|
|
|
_conn = getattr(self, "conn", None)
|
|
|
|
if _conn and _conn.get_uri() == uri:
|
|
|
|
self.cleanup()
|
|
|
|
return True
|
|
|
|
connmanager.connect_opt_out("conn-removed", _cb)
|