kvmtools/kvm/action.py
author Jens Kasten <jens@kasten-edv.de>
Tue, 22 Nov 2011 03:59:30 +0100
branchfeatures_editor
changeset 53 9eb8be6341f3
child 54 17b08e781dc6
permissions -rw-r--r--
new structur for file and class layout
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     1
#
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     2
# Modulde which contain the methodes which can call from commandline
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     3
#
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     4
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     5
"""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     6
(c) 2011 Jens Kasten <jens@kasten-edv.de>
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     7
"""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     8
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     9
import os
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    10
import sys
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    11
from subprocess import Popen, PIPE
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    12
from time import sleep
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    13
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    14
from kvmtools.kvm.monitor import KvmMonitor
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    15
from kvmtools.config.domain import Domain
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    16
from kvmtools.kvm.build_config import BuildConfig
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    17
from kvmtools.kvm.build_command import BuildCommand
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    18
from kvmtools.system_utils import System
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    19
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    20
class KvmAction(Domain, BuildConfig, BuildCommand, KvmMonitor, System):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    21
    
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    22
    def __init__(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    23
        Domain.__init__(self)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    24
        BuildConfig.__init__(self)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    25
        BuildCommand.__init__(self)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    26
        System.__init__(self)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    27
        self.command = ()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    28
        self.socket = None
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    29
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    30
    def load_command(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    31
        """Load config"""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    32
        try:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    33
            self.build_config()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    34
        except Exception:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    35
            self.kvm_modify_action()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    36
        self.command = self.build_command()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    37
        KvmMonitor.__init__(self)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    38
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    39
    def available_actions(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    40
        """Return all methods which start with kvm_ and end with _action."""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    41
        actions = []
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    42
        for action in dir(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    43
            if action.startswith("kvm_") and action.endswith("_action"):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    44
                methode = action.replace("kvm_", "").replace("_action", "")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    45
                actions.append(methode)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    46
        return actions
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    47
   
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    48
    def kvm_show_action(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    49
        """show the command as string"""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    50
        print self.command[1]
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    51
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    52
    def kvm_modify_action(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    53
        """Modify a domain configuration file."""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    54
        self.modify()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    55
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    56
    def kvm_create_action(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    57
        """Create a domain configuration file."""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    58
        if os.path.isfile(self.kvm_domain_file):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    59
            self.modify()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    60
        else:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    61
            self.create()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    62
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    63
    def kvm_migrate_action(self, command_monitor):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    64
        self.kvm_monitor_action(command_monitor)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    65
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    66
    def kvm_monitor_action(self, command_monitor):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    67
        """Monitor to the qemu-kvm guest on commandline."""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    68
        if not self.is_running(self.kvm_pid):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    69
            print ("Guest is not running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    70
            return
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    71
        self.monitor_send(command_monitor)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    72
        data = self.monitor_recieve()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    73
        data = "\n".join([i for i in data])
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    74
        print data
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    75
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    76
    def kvm_boot_action(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    77
        """Boot the qemu-kvm guest."""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    78
        if self.is_running(self.kvm_pid):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    79
            print ("Guest already running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    80
            return True
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    81
        env = os.environ.copy()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    82
        # add the bridge(s) to the enviroment
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    83
        if len(self.bridge) > 0:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    84
            for key, value in self.bridge.iteritems():
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    85
                env[key] = value
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    86
        try:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    87
            result = Popen(self.command[0], env=env, stdin=PIPE, stdout=PIPE)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    88
            result.wait()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    89
            return (True, "")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    90
        except OSError, error_msg:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    91
            return (False, error_msg)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    92
        except IOError, error_msg:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    93
            return (False, error_msg)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    94
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    95
    def kvm_reboot_action(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    96
        """Reboot the guest."""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    97
        if not self.is_running(self.kvm_pid):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    98
            print ("Guest is not running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    99
            return False
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   100
        if self.monitor_send(self.qemu_monitor["reboot"]):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   101
            print ("Rebooting ...")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   102
        else:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   103
            print ("Could not send signal reboot to guest.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   104
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   105
    def kvm_shutdown_action(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   106
        """Shutdown the guest.
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   107
        Its work for windows and linux guests, 
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   108
        but not on linux when the Xserver is looked.
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   109
        """
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   110
        if not self.system.is_running(self.kvm_pid):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   111
            print ("Guest is not running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   112
        flag = 0
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   113
        if self.monitor_send(self.qemu_monitor["shutdown"]):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   114
            self.monitor_send(self.qemu_monitor["enter"])
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   115
            print ("Shutdown ...")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   116
            while True:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   117
                # some fancy ticker
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   118
                if flag == 0:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   119
                    sign = "\\" 
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   120
                    flag = 1
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   121
                elif flag == 1:    
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   122
                    sign = "|"
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   123
                    flag = 2
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   124
                elif flag == 2:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   125
                    sign = "/"
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   126
                    flag = 3
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   127
                elif flag == 3:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   128
                    sign = "-"
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   129
                    flag = 0
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   130
                sys.stdout.write("waiting ... %s\r" % sign)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   131
                sys.stdout.flush()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   132
                sleep(0.05)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   133
                # if self._check_is_running():
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   134
                if not os.path.isfile(self.kvm_pidfile):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   135
                    sys.stdout.write("Done.         \n")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   136
                    sys.stdout.flush()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   137
                    sys.exit(0) 
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   138
                if not self.is_running(self.kvm_pid):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   139
                    if os.path.isfile(self.kvm_pidfile):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   140
                        os.remove(self.kvm_pidfile)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   141
            else:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   142
                print ("Could not send signal shutdown.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   143
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   144
    def kvm_kill_action(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   145
        """Kill the guest.
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   146
        Dangerous option, its simply like pull the power cable out.
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   147
        """
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   148
        if not self.is_running(self.kvm_pid):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   149
            print ("Guest is not running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   150
            return False
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   151
        try:    
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   152
            os.kill(self.kvm_pid, 9)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   153
            sleep(0.8)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   154
            sys.exit(0)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   155
        except OSError, error_msg:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   156
            print error_msg
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   157
            sys.exit(1)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   158
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   159
    def kvm_status_action(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   160
        """Show information about the guest."""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   161
        if not self.is_running(self.kvm_pid):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   162
            print ("Guest is not running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   163
            return False
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   164
        process = self.get_process_information()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   165
        info = self.get_process_info(self.kvm_pid)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   166
        print "Name: %s" % process['Name'].split("[")[0]
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   167
        print "Cmdline: %s" % info["cmdline"]
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   168
        print "rss in %d KB" % info["rss"]
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   169
        print "vms in %d KB" % info["vms"]
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   170
        print "%s" % process["Status"]
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   171
        print "Guest uuid: %s" % process['Uuid']
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   172
        print "State: %s" % process['State']
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   173
        print "UID: %s" % process['Uid']
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   174
        print "GID: %s" % process['Gid']
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   175
        print "Groups: %s" % process['Groups']
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   176
        print "PID: %s :: PPID: %s" % (process['Pid'], process['PPid'])
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   177
        print "VNC: %s" % process["VNC"]
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   178
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   179
    def _check_is_running(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   180
        """Check if the process is running by a given pidfile."""
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   181
        if os.path.isfile(self.pidfile):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   182
            with open(self.pidfile) as file_descriptor:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   183
                self._pid = int(file_descriptor.readline().strip())
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   184
                file_descriptor.close()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   185
            # check if process alive
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   186
            try:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   187
                os.kill(self._pid, 0)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   188
                return True
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   189
            except OSError:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   190
                return False
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   191
            except IOError:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   192
                return False
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   193
        else:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   194
            process1 = Popen(['ps', 'aux'], stdout=PIPE, stderr=PIPE)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   195
            search = "kvm_" + self.guest
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   196
            process2 = Popen(['grep', search], stdin=process1.stdout, 
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   197
                stdout=PIPE, stderr=PIPE)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   198
            result = process2.communicate()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   199
            status = result[0].split("\n")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   200
            # search for pid
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   201
            if len(status) > 1:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   202
                # iterate over the ps aux output per line
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   203
                for i in status:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   204
                    if not "grep" in i:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   205
                        pid = i.split(" ")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   206
                        # remove first element, its the user name
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   207
                        del pid[0]
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   208
                        for j in pid:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   209
                            if j == "":
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   210
                                continue
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   211
                            else:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   212
                                # found pid 
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   213
                                self._pid = int(j)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   214
                                return True
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   215
                return False
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   216
            else:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   217
                return False