2013-10-28 04:59:46 +08:00
|
|
|
# Copyright (C) 2012-2013 Red Hat, Inc.
|
2012-07-09 03:05:28 +08:00
|
|
|
# Copyright (C) 2012 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.
|
2012-07-09 03:05:28 +08:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
2016-06-07 23:33:21 +08:00
|
|
|
from gi.repository import Gio
|
|
|
|
|
2012-07-09 03:05:28 +08:00
|
|
|
|
|
|
|
#############################
|
|
|
|
# PackageKit lookup helpers #
|
|
|
|
#############################
|
|
|
|
|
2013-09-02 02:19:23 +08:00
|
|
|
def check_packagekit(parent, errbox, packages):
|
2012-07-09 03:05:28 +08:00
|
|
|
"""
|
|
|
|
Returns None when we determine nothing useful.
|
|
|
|
Returns (success, did we just install libvirt) otherwise.
|
|
|
|
"""
|
2013-12-19 05:34:10 +08:00
|
|
|
ignore = errbox
|
|
|
|
|
2012-07-09 03:05:28 +08:00
|
|
|
if not packages:
|
|
|
|
logging.debug("No PackageKit packages to search for.")
|
|
|
|
return
|
2017-10-11 19:35:41 +08:00
|
|
|
if not isinstance(packages, list):
|
2017-08-08 05:26:12 +08:00
|
|
|
packages = [packages]
|
2012-07-09 03:05:28 +08:00
|
|
|
|
2015-05-03 05:34:27 +08:00
|
|
|
logging.debug("PackageKit check/install for packages=%s", packages)
|
2012-07-09 03:05:28 +08:00
|
|
|
try:
|
2013-04-17 01:38:19 +08:00
|
|
|
bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
|
2013-09-02 02:19:23 +08:00
|
|
|
Gio.DBusProxy.new_sync(bus, 0, None,
|
|
|
|
"org.freedesktop.PackageKit",
|
|
|
|
"/org/freedesktop/PackageKit",
|
|
|
|
"org.freedesktop.PackageKit", None)
|
2012-07-09 03:05:28 +08:00
|
|
|
except Exception:
|
|
|
|
logging.exception("Couldn't connect to packagekit")
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2014-04-30 02:34:31 +08:00
|
|
|
for package in packages[:]:
|
|
|
|
if packagekit_isinstalled(package):
|
2015-05-03 05:34:27 +08:00
|
|
|
logging.debug("package=%s already installed, skipping it",
|
|
|
|
package)
|
2014-04-30 02:34:31 +08:00
|
|
|
packages.remove(package)
|
|
|
|
|
|
|
|
if packages:
|
|
|
|
packagekit_install(parent, packages)
|
2015-05-03 05:34:27 +08:00
|
|
|
else:
|
|
|
|
logging.debug("Nothing to install")
|
2017-05-06 00:47:21 +08:00
|
|
|
except Exception as e:
|
2013-12-19 04:45:02 +08:00
|
|
|
# PackageKit frontend should report an error for us, so just log
|
|
|
|
# the actual error
|
2013-12-19 05:34:10 +08:00
|
|
|
logging.debug("Error talking to PackageKit: %s", str(e), exc_info=True)
|
2012-07-09 03:05:28 +08:00
|
|
|
return
|
|
|
|
|
2013-09-02 02:19:23 +08:00
|
|
|
return True
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2012-07-09 03:05:28 +08:00
|
|
|
|
2014-04-30 02:34:31 +08:00
|
|
|
def packagekit_isinstalled(package):
|
|
|
|
bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
|
|
|
|
pk_control = Gio.DBusProxy.new_sync(bus, 0, None,
|
|
|
|
"org.freedesktop.PackageKit",
|
|
|
|
"/org/freedesktop/PackageKit",
|
|
|
|
"org.freedesktop.PackageKit.Query", None)
|
|
|
|
|
|
|
|
return pk_control.IsInstalled("(ss)", package, "")
|
|
|
|
|
|
|
|
|
2013-09-02 02:19:23 +08:00
|
|
|
def packagekit_install(parent, package_list):
|
2015-12-05 00:49:14 +08:00
|
|
|
ignore = parent
|
2013-04-17 01:38:19 +08:00
|
|
|
bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
|
|
|
|
pk_control = Gio.DBusProxy.new_sync(bus, 0, None,
|
|
|
|
"org.freedesktop.PackageKit",
|
|
|
|
"/org/freedesktop/PackageKit",
|
|
|
|
"org.freedesktop.PackageKit.Modify", None)
|
2012-07-09 03:05:28 +08:00
|
|
|
|
|
|
|
# Set 2 hour timeout
|
2013-04-17 01:38:19 +08:00
|
|
|
timeout = 1000 * 60 * 60 * 2
|
2012-07-09 03:05:28 +08:00
|
|
|
logging.debug("Installing packages: %s", package_list)
|
2015-12-05 00:49:14 +08:00
|
|
|
pk_control.InstallPackageNames("(uass)", 0, package_list, "",
|
2012-07-09 03:05:28 +08:00
|
|
|
timeout=timeout)
|
2015-05-03 05:34:27 +08:00
|
|
|
logging.debug("Install completed")
|
2012-07-09 03:05:28 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2012-07-09 03:05:28 +08:00
|
|
|
###################
|
|
|
|
# Service helpers #
|
|
|
|
###################
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2012-07-09 03:05:28 +08:00
|
|
|
def start_libvirtd():
|
|
|
|
"""
|
|
|
|
Connect to systemd and start libvirtd if required
|
|
|
|
"""
|
|
|
|
logging.debug("Trying to start libvirtd through systemd")
|
|
|
|
unitname = "libvirtd.service"
|
|
|
|
|
|
|
|
try:
|
2013-04-17 01:38:19 +08:00
|
|
|
bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
|
2017-07-24 16:26:48 +08:00
|
|
|
except Exception:
|
2012-07-09 03:05:28 +08:00
|
|
|
logging.exception("Error getting system bus handle")
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2013-04-17 01:38:19 +08:00
|
|
|
systemd = Gio.DBusProxy.new_sync(bus, 0, None,
|
2012-07-09 03:05:28 +08:00
|
|
|
"org.freedesktop.systemd1",
|
2013-04-17 01:38:19 +08:00
|
|
|
"/org/freedesktop/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager", None)
|
2017-07-24 16:26:48 +08:00
|
|
|
except Exception:
|
2012-07-09 03:05:28 +08:00
|
|
|
logging.exception("Couldn't connect to systemd")
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2013-04-17 01:38:19 +08:00
|
|
|
unitpath = systemd.GetUnit("(s)", unitname)
|
|
|
|
unit = Gio.DBusProxy.new_sync(bus, 0, None,
|
|
|
|
"org.freedesktop.systemd1", unitpath,
|
|
|
|
"org.freedesktop.systemd1.Unit", None)
|
|
|
|
state = unit.get_cached_property("ActiveState")
|
2012-07-09 03:05:28 +08:00
|
|
|
|
|
|
|
logging.debug("libvirtd state=%s", state)
|
2013-04-17 01:38:19 +08:00
|
|
|
if str(state).lower().strip("'") == "active":
|
2012-07-09 03:05:28 +08:00
|
|
|
logging.debug("libvirtd already active, not starting")
|
|
|
|
return True
|
2017-07-24 16:26:48 +08:00
|
|
|
except Exception:
|
2012-07-09 03:05:28 +08:00
|
|
|
logging.exception("Failed to lookup libvirtd status")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Connect to system-config-services and offer to start
|
|
|
|
try:
|
2012-10-29 08:18:51 +08:00
|
|
|
logging.debug("libvirtd not running, asking system-config-services "
|
|
|
|
"to start it")
|
2013-04-17 01:38:19 +08:00
|
|
|
scs = Gio.DBusProxy.new_sync(bus, 0, None,
|
2012-07-09 03:05:28 +08:00
|
|
|
"org.fedoraproject.Config.Services",
|
2013-04-17 01:38:19 +08:00
|
|
|
"/org/fedoraproject/Config/Services/systemd1",
|
|
|
|
"org.freedesktop.systemd1.Manager", None)
|
|
|
|
scs.StartUnit("(ss)", unitname, "replace")
|
2012-07-09 03:05:28 +08:00
|
|
|
time.sleep(2)
|
2012-10-29 08:18:51 +08:00
|
|
|
logging.debug("Starting libvirtd appeared to succeed")
|
2012-07-09 03:05:28 +08:00
|
|
|
return True
|
2017-07-24 16:26:48 +08:00
|
|
|
except Exception:
|
2012-07-09 03:05:28 +08:00
|
|
|
logging.exception("Failed to talk to system-config-services")
|