fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
authorJens Kasten <jens@kasten-edv.de>
Tue, 10 Apr 2012 07:37:27 +0200
changeset 115 75c299d0bf5a
parent 114 d146fb6e68ae
child 116 455b335e203d
fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
bin/kvm-admin
kvmtools/header.py
kvmtools/kvm/action.py
--- a/bin/kvm-admin	Sun Apr 08 18:42:45 2012 +0200
+++ b/bin/kvm-admin	Tue Apr 10 07:37:27 2012 +0200
@@ -109,14 +109,27 @@
         if i == "status":
             action.add_argument("--full", default=False, action="store_true", 
                 #metavar=("full status information"),
-                help=_("Display full status information for each running qemu-kvm domain."))
+                help=_("""Display full status information for each 
+                    running qemu-kvm domain."""))
         if i == "monitor":               
             action.add_argument("args_monitor", nargs="*", default=[],
                 #metavar=("monitor argument"),
                 help=_("One ore more arguments to pass to the qemu-kvm monitor."))
+        if i == "shutdown":
+            action.add_argument("--time-out", type=int, 
+                action="store", metavar="'in seconds'",
+                help=_("""Set the time out for waiting till the qemu-kvm domain,
+                    will be killed."""))
+            action.add_argument("--wait-to-send-enter", type=int,
+                action="store", metavar="'in seconds'",
+                help=_("""Set this will send, after a given time in seconds,
+                    an enter signal to the qemu-kvm domain for the 
+                    grafical shutdown dialog."""))
     
     args = parser.parse_args()
     
+    print args
+
     kvm_admin.debug = args.debug
 
     # assign the action and domain name dynamically
@@ -126,13 +139,18 @@
             kvm_admin.kvm_domain_name = str(getattr(args, action))
             break
 
-    # assign the optional monitor arguments 
+    # assign optional monitor arguments 
     if "args_monitor" in args and len(args.args_monitor) > 0:
         kvm_admin.monitor = args.args_monitor        
     else:
         # set default monitor to help
         kvm_admin.monitor =["help"] 
 
+    # assign optional argumente for shutdown
+    if kvm_admin.action == "shutdown":
+        kvm_admin.shutdown_wait_to_send_enter = args.wait_to_send_enter
+        kvm_admin.shutdown_time_out = args.time_out
+       
     # assign all as domain name its used for status
     if kvm_admin.kvm_domain_name == "all" and kvm_admin.action == "status":
         # append "all" as pseudo domain name
@@ -143,7 +161,6 @@
     if not kvm_admin.kvm_domain_name in domains and not "create" == kvm_admin.action:  
         parser.error(_("Available domains\n%s") % domains)
 
-
     # check if some error occurs
     if not kvm_admin.action == "modify" and kvm_admin.show_error():
         sys.exit(1)
--- a/kvmtools/header.py	Sun Apr 08 18:42:45 2012 +0200
+++ b/kvmtools/header.py	Tue Apr 10 07:37:27 2012 +0200
@@ -66,4 +66,10 @@
         self.is_enabled = "enabled"
         # files which should not interpreted as domain config file
         self.exclude_backup_files = [".swp", ]
+        # time to wait for sending a enter command to a domain for 
+        # grafic environment
+        self.shutdown_wait_to_send_enter = None
+        # default value to wait till a domain is killed 
+        # when the timer is reached
+        self.shutdown_time_out = 30 
 
--- a/kvmtools/kvm/action.py	Sun Apr 08 18:42:45 2012 +0200
+++ b/kvmtools/kvm/action.py	Tue Apr 10 07:37:27 2012 +0200
@@ -9,7 +9,7 @@
 import os
 import sys
 from subprocess import Popen, PIPE
-from time import sleep
+from time import sleep, time
 
 from kvmtools.kvm.domain import Domain
 
@@ -35,9 +35,7 @@
 
     def kvm_show_action(self):
         """Show the qemu-kvm command as string."""
-        doc = "teste a show"
-        print "default show"
-        #print self.command[1]
+        print self.command[1]
 
     def kvm_modify_action(self):
         """Modify a qemu-kvm domain configuration file."""
@@ -101,9 +99,11 @@
         flag = 0
         if self.monitor_send(self.qemu_monitor["shutdown"]):
             # set a time  out to wait, that the shutdown dialog appears
-            sleep(1.5)
-            self.monitor_send(self.qemu_monitor["enter"])
-            print ("Shutdown ...")
+            if self.shutdown_wait_to_send_enter:
+                sleep(self.shutdown_wait_to_send_enter)
+                self.monitor_send(self.qemu_monitor["enter"])
+            # set start timer
+            timer = time() 
             while True:
                 # some fancy ticker
                 if flag == 0:
@@ -126,6 +126,12 @@
                     sys.stdout.write("waiting ... %s\r" % sign)
                     sys.stdout.flush()
                     sleep(0.05)
+                # when the time out is reached kill the qemu-kvm domain
+                # this is only usefull for init.d system 
+                # otherwise if the domain could from any reason not shutdown
+                # the whole shutdown process is blocked
+                if (timer + self.shudown_time_out) < time():
+                    self.kvm_kill_action()
         else:
             print ("Could not send signal shutdown.")
 
@@ -137,7 +143,7 @@
             print ("Guest is not running.")
             return False
         try:    
-            os.kill(self.kvm_pid, 9)
+            os.kill(seto,lf.kvm_pid, 9)
             sleep(0.8)
             self.is_running()
             sys.exit(0)