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