2006-06-28 23:50:17 +04:00
#
# Copyright (C) 2006 Red Hat, Inc.
# 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
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
2006-06-14 18:59:40 +04:00
import gobject
2006-06-14 22:36:26 +04:00
import gtk
2006-06-15 00:20:06 +04:00
import sys
2006-07-17 21:08:58 +04:00
import libvirt
2006-07-19 02:14:44 +04:00
from time import sleep
2006-06-14 18:59:40 +04:00
from virtManager . about import vmmAbout
from virtManager . connect import vmmConnect
from virtManager . connection import vmmConnection
from virtManager . preferences import vmmPreferences
2006-06-15 00:20:06 +04:00
from virtManager . manager import vmmManager
from virtManager . details import vmmDetails
from virtManager . console import vmmConsole
2006-06-14 18:59:40 +04:00
class vmmEngine :
def __init__ ( self , config ) :
2006-06-15 00:20:06 +04:00
self . windowConnect = None
2006-06-14 18:59:40 +04:00
self . windowPreferences = None
self . windowAbout = None
self . connections = { }
self . timer = None
self . last_timeout = 0
self . config = config
self . config . on_stats_update_interval_changed ( self . reschedule_timer )
self . schedule_timer ( )
self . tick ( )
2006-06-15 00:20:06 +04:00
def _do_connection_disconnected ( self , connection , hvuri ) :
2006-06-27 22:16:13 +04:00
del self . connections [ hvuri ]
2006-06-14 22:36:26 +04:00
2006-06-15 00:20:06 +04:00
if len ( self . connections . keys ( ) ) == 0 and self . windowConnect == None :
2006-06-14 22:36:26 +04:00
gtk . main_quit ( )
def _connect_to_uri ( self , connect , uri , readOnly ) :
2006-06-27 22:16:13 +04:00
self . windowConnect = None
2006-06-27 18:08:55 +04:00
try :
2006-06-14 22:36:26 +04:00
conn = self . get_connection ( uri , readOnly )
self . show_manager ( uri )
2006-06-27 18:08:55 +04:00
except :
print " Unable to open connection to hypervisor URI ' " + str ( uri ) + " ' "
print str ( sys . exc_info ( ) [ 0 ] ) + " " + str ( sys . exc_info ( ) [ 1 ] )
if len ( self . connections . keys ( ) ) == 0 :
gtk . main_quit ( )
2006-06-14 22:36:26 +04:00
2006-06-27 18:08:55 +04:00
def _connect_cancelled ( self , connect ) :
2006-06-27 22:16:13 +04:00
self . windowConnect = None
2006-06-27 18:08:55 +04:00
if len ( self . connections . keys ( ) ) == 0 :
2006-06-14 22:36:26 +04:00
gtk . main_quit ( )
2006-06-15 00:56:49 +04:00
def _do_vm_removed ( self , connection , hvuri , vmuuid ) :
if self . connections [ hvuri ] [ " windowDetails " ] . has_key ( vmuuid ) :
2006-07-13 21:35:40 +04:00
self . connections [ hvuri ] [ " windowDetails " ] [ vmuuid ] . close ( )
2006-06-15 00:56:49 +04:00
del self . connections [ hvuri ] [ " windowDetails " ] [ vmuuid ]
if self . connections [ hvuri ] [ " windowConsole " ] . has_key ( vmuuid ) :
2006-07-13 21:35:40 +04:00
self . connections [ hvuri ] [ " windowConsole " ] [ vmuuid ] . close ( )
2006-06-15 00:56:49 +04:00
del self . connections [ hvuri ] [ " windowConsole " ] [ vmuuid ]
2006-06-14 18:59:40 +04:00
def reschedule_timer ( self , ignore1 , ignore2 , ignore3 , ignore4 ) :
self . schedule_timer ( )
def schedule_timer ( self ) :
interval = self . get_config ( ) . get_stats_update_interval ( ) * 1000
if self . timer != None :
gobject . source_remove ( self . timer )
self . timer = None
self . timer = gobject . timeout_add ( interval , self . tick )
def tick ( self ) :
for uri in self . connections . keys ( ) :
try :
2006-06-15 00:20:06 +04:00
self . connections [ uri ] [ " connection " ] . tick ( )
2006-07-11 23:36:54 +04:00
except KeyboardInterrupt :
raise KeyboardInterrupt
2006-06-14 18:59:40 +04:00
except :
print str ( sys . exc_info ( ) [ 0 ] ) + " " + str ( sys . exc_info ( ) [ 1 ] )
print " Error refreshing connection " + uri
return 1
def change_timer_interval ( self , ignore1 , ignore2 , ignore3 , ignore4 ) :
gobject . source_remove ( self . timer )
self . schedule_timer ( )
def get_config ( self ) :
return self . config
2006-06-15 00:20:06 +04:00
def _do_show_about ( self , src ) :
self . show_about ( )
def _do_show_preferences ( self , src ) :
self . show_preferences ( )
def _do_show_connect ( self , src ) :
self . show_connect ( )
def _do_show_manager ( self , src , uri ) :
self . show_manager ( uri )
def _do_show_details ( self , src , uri , uuid ) :
self . show_details ( uri , uuid )
def _do_show_console ( self , src , uri , uuid ) :
self . show_console ( uri , uuid )
2006-07-17 21:08:58 +04:00
def _do_save_domain ( self , src , uri , uuid ) :
2006-07-19 02:14:44 +04:00
self . save_domain ( src , uri , uuid )
2006-06-15 00:20:06 +04:00
2006-06-14 18:59:40 +04:00
def show_about ( self ) :
if self . windowAbout == None :
self . windowAbout = vmmAbout ( self . get_config ( ) )
self . windowAbout . show ( )
def show_preferences ( self ) :
if self . windowPreferences == None :
self . windowPreferences = vmmPreferences ( self . get_config ( ) )
self . windowPreferences . show ( )
2006-06-15 00:20:06 +04:00
def show_connect ( self ) :
if self . windowConnect == None :
self . windowConnect = vmmConnect ( self . get_config ( ) , self )
self . windowConnect . connect ( " completed " , self . _connect_to_uri )
2006-06-27 19:17:46 +04:00
self . windowConnect . connect ( " cancelled " , self . _connect_cancelled )
2006-06-15 00:20:06 +04:00
self . windowConnect . show ( )
2006-06-14 18:59:40 +04:00
def show_console ( self , uri , uuid ) :
con = self . get_connection ( uri )
2006-06-15 00:20:06 +04:00
if not ( self . connections [ uri ] [ " windowConsole " ] . has_key ( uuid ) ) :
console = vmmConsole ( self . get_config ( ) ,
2006-06-15 01:52:49 +04:00
con . get_vm ( uuid ) )
2006-06-15 00:20:06 +04:00
console . connect ( " action-show-details " , self . _do_show_details )
self . connections [ uri ] [ " windowConsole " ] [ uuid ] = console
self . connections [ uri ] [ " windowConsole " ] [ uuid ] . show ( )
2006-06-14 18:59:40 +04:00
2006-07-14 01:44:49 +04:00
def show_details_performance ( self , uri , uuid ) :
win = self . show_details ( uri , uuid )
win . activate_performance_page ( )
def show_details_config ( self , uri , uuid ) :
win = self . show_details ( uri , uuid )
win . activate_config_page ( )
2006-06-14 18:59:40 +04:00
def show_details ( self , uri , uuid ) :
con = self . get_connection ( uri )
2006-06-15 00:20:06 +04:00
if not ( self . connections [ uri ] [ " windowDetails " ] . has_key ( uuid ) ) :
details = vmmDetails ( self . get_config ( ) ,
2006-06-15 01:52:49 +04:00
con . get_vm ( uuid ) )
2006-06-15 00:20:06 +04:00
details . connect ( " action-show-console " , self . _do_show_console )
2006-07-17 21:08:58 +04:00
details . connect ( " action-save-domain " , self . _do_save_domain )
2006-06-15 00:20:06 +04:00
self . connections [ uri ] [ " windowDetails " ] [ uuid ] = details
self . connections [ uri ] [ " windowDetails " ] [ uuid ] . show ( )
2006-07-14 01:44:49 +04:00
return self . connections [ uri ] [ " windowDetails " ] [ uuid ]
2006-06-14 18:59:40 +04:00
def show_manager ( self , uri ) :
con = self . get_connection ( uri )
2006-06-15 00:20:06 +04:00
if self . connections [ uri ] [ " windowManager " ] == None :
manager = vmmManager ( self . get_config ( ) ,
2006-06-15 01:52:49 +04:00
con )
2006-06-15 00:20:06 +04:00
manager . connect ( " action-show-console " , self . _do_show_console )
manager . connect ( " action-show-details " , self . _do_show_details )
manager . connect ( " action-show-preferences " , self . _do_show_preferences )
manager . connect ( " action-show-about " , self . _do_show_about )
manager . connect ( " action-show-connect " , self . _do_show_connect )
self . connections [ uri ] [ " windowManager " ] = manager
self . connections [ uri ] [ " windowManager " ] . show ( )
2006-06-14 18:59:40 +04:00
def get_connection ( self , uri , readOnly = True ) :
2006-06-27 22:16:13 +04:00
if not ( self . connections . has_key ( uri ) ) :
self . connections [ uri ] = {
2006-06-15 00:56:49 +04:00
" connection " : vmmConnection ( self . get_config ( ) , uri , readOnly ) ,
2006-06-15 00:20:06 +04:00
" windowManager " : None ,
" windowDetails " : { } ,
" windowConsole " : { }
}
2006-06-27 22:16:13 +04:00
self . connections [ uri ] [ " connection " ] . connect ( " disconnected " , self . _do_connection_disconnected )
self . connections [ uri ] [ " connection " ] . connect ( " vm-removed " , self . _do_vm_removed )
self . connections [ uri ] [ " connection " ] . tick ( )
2006-06-15 00:20:06 +04:00
2006-06-27 22:16:13 +04:00
return self . connections [ uri ] [ " connection " ]
2006-07-17 21:08:58 +04:00
2006-07-19 02:14:44 +04:00
def save_domain ( self , src , uri , uuid ) :
2006-07-17 21:08:58 +04:00
con = self . get_connection ( uri , False )
vm = con . get_vm ( uuid )
status = vm . status ( )
if status in [ libvirt . VIR_DOMAIN_SHUTDOWN , libvirt . VIR_DOMAIN_SHUTOFF , libvirt . VIR_DOMAIN_CRASHED , libvirt . VIR_DOMAIN_PAUSED ] :
print " Save requested, but machine is shutdown / shutoff / paused "
else :
2006-07-19 02:14:44 +04:00
self . fcdialog = gtk . FileChooserDialog ( " Save Virtual Machine " ,
src . window . get_widget ( " vmm-details " ) ,
2006-07-18 03:37:24 +04:00
gtk . FILE_CHOOSER_ACTION_SAVE ,
( gtk . STOCK_CANCEL , gtk . RESPONSE_CANCEL ,
gtk . STOCK_SAVE , gtk . RESPONSE_ACCEPT ) ,
None )
2006-07-19 02:14:44 +04:00
self . fcdialog . set_do_overwrite_confirmation ( True )
response = self . fcdialog . run ( )
self . fcdialog . hide ( )
2006-07-18 03:37:24 +04:00
if ( response == gtk . RESPONSE_ACCEPT ) :
2006-07-19 02:14:44 +04:00
print " file save dialog should now be hidden "
uri_to_save = self . fcdialog . get_filename ( )
# show a lovely bouncing progress bar until the vm actually saves
self . pbar_dlg = gtk . Dialog ( " Saving VM Image " ,
src . window . get_widget ( " vmm-details " ) )
self . pbar_dlg . connect ( " destroy " , self . destroy_pbar_dlg )
self . pbar = gtk . ProgressBar ( )
self . pbar_dlg . vbox . add ( self . pbar )
self . pbar . show ( )
print " About to present the progress bar "
self . timer = gobject . timeout_add ( 100 , self . pbar . pulse )
self . pbar_dlg . present ( )
# actually save the vm
vm . save ( uri_to_save )
self . pbar_dlg . destroy ( )
self . fcdialog . destroy ( )
def destroy_pbar_dlg ( self , widget , data = None ) :
gobject . source_remove ( self . timer )
self . timer = 0
widget . destroy ( ) ;