kvmtools/kvm/action.py
branchfeatures_editor
changeset 54 17b08e781dc6
parent 53 9eb8be6341f3
child 55 1d4c061d1502
--- a/kvmtools/kvm/action.py	Tue Nov 22 03:59:30 2011 +0100
+++ b/kvmtools/kvm/action.py	Tue Nov 22 14:42:43 2011 +0100
@@ -11,13 +11,13 @@
 from subprocess import Popen, PIPE
 from time import sleep
 
-from kvmtools.kvm.monitor import KvmMonitor
 from kvmtools.config.domain import Domain
+from kvmtools.kvm.monitor import Monitor
 from kvmtools.kvm.build_config import BuildConfig
 from kvmtools.kvm.build_command import BuildCommand
-from kvmtools.system_utils import System
+from kvmtools.kvm.system import System
 
-class KvmAction(Domain, BuildConfig, BuildCommand, KvmMonitor, System):
+class Action(Domain, BuildConfig, BuildCommand, Monitor, System):
     
     def __init__(self):
         Domain.__init__(self)
@@ -31,10 +31,12 @@
         """Load config"""
         try:
             self.build_config()
-        except Exception:
-            self.kvm_modify_action()
+        except Exception, error_msg:
+            print error_msg
+            #self.kvm_modify_action()
         self.command = self.build_command()
-        KvmMonitor.__init__(self)
+        Monitor.__init__(self)
+        self.get_pid()
 
     def available_actions(self):
         """Return all methods which start with kvm_ and end with _action."""
@@ -65,7 +67,7 @@
 
     def kvm_monitor_action(self, command_monitor):
         """Monitor to the qemu-kvm guest on commandline."""
-        if not self.is_running(self.kvm_pid):
+        if not self.is_running():
             print ("Guest is not running.")
             return
         self.monitor_send(command_monitor)
@@ -75,11 +77,11 @@
 
     def kvm_boot_action(self):
         """Boot the qemu-kvm guest."""
-        if self.is_running(self.kvm_pid):
+        if self.is_running():
             print ("Guest already running.")
             return True
         env = os.environ.copy()
-        # add the bridge(s) to the enviroment
+        # add the  to the enviroment
         if len(self.bridge) > 0:
             for key, value in self.bridge.iteritems():
                 env[key] = value
@@ -94,12 +96,10 @@
 
     def kvm_reboot_action(self):
         """Reboot the guest."""
-        if not self.is_running(self.kvm_pid):
+        if not self.is_running():
             print ("Guest is not running.")
             return False
-        if self.monitor_send(self.qemu_monitor["reboot"]):
-            print ("Rebooting ...")
-        else:
+        if not self.monitor_send(self.qemu_monitor["reboot"]):
             print ("Could not send signal reboot to guest.")
 
     def kvm_shutdown_action(self):
@@ -107,7 +107,7 @@
         Its work for windows and linux guests, 
         but not on linux when the Xserver is looked.
         """
-        if not self.system.is_running(self.kvm_pid):
+        if not self.is_running():
             print ("Guest is not running.")
         flag = 0
         if self.monitor_send(self.qemu_monitor["shutdown"]):
@@ -127,17 +127,14 @@
                 elif flag == 3:
                     sign = "-"
                     flag = 0
-                sys.stdout.write("waiting ... %s\r" % sign)
-                sys.stdout.flush()
-                sleep(0.05)
-                # if self._check_is_running():
-                if not os.path.isfile(self.kvm_pidfile):
+                if not self.is_running():
                     sys.stdout.write("Done.         \n")
                     sys.stdout.flush()
                     sys.exit(0) 
-                if not self.is_running(self.kvm_pid):
-                    if os.path.isfile(self.kvm_pidfile):
-                        os.remove(self.kvm_pidfile)
+                else:
+                    sys.stdout.write("waiting ... %s\r" % sign)
+                    sys.stdout.flush()
+                    sleep(0.05)
             else:
                 print ("Could not send signal shutdown.")
 
@@ -145,73 +142,32 @@
         """Kill the guest.
         Dangerous option, its simply like pull the power cable out.
         """
-        if not self.is_running(self.kvm_pid):
+        if not self.is_running():
             print ("Guest is not running.")
             return False
         try:    
             os.kill(self.kvm_pid, 9)
             sleep(0.8)
+            self.is_running()
             sys.exit(0)
         except OSError, error_msg:
             print error_msg
+            self.is_running()
             sys.exit(1)
 
     def kvm_status_action(self):
         """Show information about the guest."""
-        if not self.is_running(self.kvm_pid):
+        if not self.is_running():
             print ("Guest is not running.")
             return False
-        process = self.get_process_information()
-        info = self.get_process_info(self.kvm_pid)
-        print "Name: %s" % process['Name'].split("[")[0]
-        print "Cmdline: %s" % info["cmdline"]
-        print "rss in %d KB" % info["rss"]
-        print "vms in %d KB" % info["vms"]
-        print "%s" % process["Status"]
-        print "Guest uuid: %s" % process['Uuid']
-        print "State: %s" % process['State']
-        print "UID: %s" % process['Uid']
-        print "GID: %s" % process['Gid']
-        print "Groups: %s" % process['Groups']
-        print "PID: %s :: PPID: %s" % (process['Pid'], process['PPid'])
-        print "VNC: %s" % process["VNC"]
-
-    def _check_is_running(self):
-        """Check if the process is running by a given pidfile."""
-        if os.path.isfile(self.pidfile):
-            with open(self.pidfile) as file_descriptor:
-                self._pid = int(file_descriptor.readline().strip())
-                file_descriptor.close()
-            # check if process alive
-            try:
-                os.kill(self._pid, 0)
-                return True
-            except OSError:
-                return False
-            except IOError:
-                return False
-        else:
-            process1 = Popen(['ps', 'aux'], stdout=PIPE, stderr=PIPE)
-            search = "kvm_" + self.guest
-            process2 = Popen(['grep', search], stdin=process1.stdout, 
-                stdout=PIPE, stderr=PIPE)
-            result = process2.communicate()
-            status = result[0].split("\n")
-            # search for pid
-            if len(status) > 1:
-                # iterate over the ps aux output per line
-                for i in status:
-                    if not "grep" in i:
-                        pid = i.split(" ")
-                        # remove first element, its the user name
-                        del pid[0]
-                        for j in pid:
-                            if j == "":
-                                continue
-                            else:
-                                # found pid 
-                                self._pid = int(j)
-                                return True
-                return False
-            else:
-                return False
+        self.get_process_information()
+        print "Process name: %s" % self.kvm_process_name
+        print "%s" % self.kvm_process_status
+        print "Domain uuid: %s" % self.kvm_process_uuid
+        print "Process state: %s" % self.kvm_process_state
+        print "UID: %s" % self.kvm_process_uid
+        print "GID: %s" % self.kvm_process_gid
+        print "Groups: %s" % self.kvm_process_groups
+        print "PID: %s :: PPID: %s" % (self.kvm_process_pid, 
+            self.kvm_process_ppid)
+        print "VNC: %s" % self.kvm_process_vnc