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.
#
2013-04-12 01:16:33 +04:00
# pylint: disable=E0611
2013-04-16 21:38:19 +04:00
from gi . repository import Gio
2013-04-12 01:16:33 +04:00
# pylint: enable=E0611
2012-07-08 23:05:28 +04:00
import logging
import time
#############################
# 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 .
"""
if not packages :
logging . debug ( " No PackageKit packages to search for. " )
return
logging . debug ( " Asking PackageKit what ' s installed locally. " )
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 :
2013-09-01 22:19:23 +04:00
packagekit_install ( parent , packages )
2012-07-08 23:05:28 +04:00
except Exception , e :
2013-09-01 22:19:23 +04:00
if " Modify.transactionCancelled " in str ( e ) :
logging . debug ( " PackageKit transaction cancelled. " )
return
2012-07-08 23:05:28 +04:00
errbox . show_err ( _ ( " Error talking to PackageKit: %s " ) % str ( e ) )
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
2013-09-01 22:19:23 +04:00
def packagekit_install ( parent , package_list ) :
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
2013-09-01 22:19:23 +04:00
xid = 0
try :
# Need to import GdkX11 just to get access to get_xid function
# This will likely fail on wayland in the future, so ignore errors
from gi . repository import GdkX11 # pylint: disable=E0611
ignore = GdkX11
if parent and parent . topwin . get_window ( ) :
xid = parent . topwin . get_window ( ) . get_xid ( )
except :
pass
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 )
2013-09-01 22:19:23 +04:00
pk_control . InstallPackageNames ( " (uass) " , xid , package_list , " " ,
2012-07-08 23:05:28 +04:00
timeout = timeout )
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 )
2012-07-08 23:05:28 +04:00
except :
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 )
2012-07-08 23:05:28 +04:00
except :
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
except :
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
except :
logging . exception ( " Failed to talk to system-config-services " )