kvmtools/kvm/action.py
author Jens Kasten <jens@kasten-edv.de>
Tue, 10 Apr 2012 07:37:27 +0200
changeset 115 75c299d0bf5a
parent 113 d180b27d13a9
child 116 455b335e203d
permissions -rw-r--r--
fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
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
"""
105
938d696a213e restructe:
Jens Kasten <jens@kasten-edv.de>
parents: 77
diff changeset
     6
(c) 2011-2012 Jens Kasten <jens@kasten-edv.de>
53
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
115
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
    12
from time import sleep, time
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    13
105
938d696a213e restructe:
Jens Kasten <jens@kasten-edv.de>
parents: 77
diff changeset
    14
from kvmtools.kvm.domain import Domain
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    15
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    16
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    17
class Action(Domain):
105
938d696a213e restructe:
Jens Kasten <jens@kasten-edv.de>
parents: 77
diff changeset
    18
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    19
    def __init__(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    20
        Domain.__init__(self)
113
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
    21
        self.kvm_status_full = False
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    22
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    23
    def kvm_error(self, error_message):
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    24
        """Append a error message to error list."""
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    25
        self.kvm_errors.append(error_message)
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    26
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    27
    def available_actions(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    28
        """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
    29
        actions = []
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    30
        for action in dir(self):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    31
            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
    32
                methode = action.replace("kvm_", "").replace("_action", "")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    33
                actions.append(methode)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    34
        return actions
105
938d696a213e restructe:
Jens Kasten <jens@kasten-edv.de>
parents: 77
diff changeset
    35
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    36
    def kvm_show_action(self):
112
0ca5084026ff update README.txt
Jens Kasten <jens@kasten-edv.de>
parents: 105
diff changeset
    37
        """Show the qemu-kvm command as string."""
115
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
    38
        print self.command[1]
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    39
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    40
    def kvm_modify_action(self):
112
0ca5084026ff update README.txt
Jens Kasten <jens@kasten-edv.de>
parents: 105
diff changeset
    41
        """Modify a qemu-kvm domain configuration file."""
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    42
        self.modify()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    43
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    44
    def kvm_create_action(self):
112
0ca5084026ff update README.txt
Jens Kasten <jens@kasten-edv.de>
parents: 105
diff changeset
    45
        """Create a basic qemu-kvm domain configuration file."""
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    46
        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
    47
            self.modify()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    48
        else:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    49
            self.create()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    50
77
b8fe98927e41 uncomment migration method, no use at the moment
Jens Kasten <jens@kasten-edv.de>
parents: 61
diff changeset
    51
    #def kvm_migrate_action(self, command_monitor):
b8fe98927e41 uncomment migration method, no use at the moment
Jens Kasten <jens@kasten-edv.de>
parents: 61
diff changeset
    52
    #    self.kvm_monitor_action(command_monitor)
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    54
    def kvm_monitor_action(self, command_monitor):
112
0ca5084026ff update README.txt
Jens Kasten <jens@kasten-edv.de>
parents: 105
diff changeset
    55
        """Monitor a qemu-kvm domain."""
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
    56
        if not self.is_running():
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    57
            print ("Guest is not running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    58
            return
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    59
        self.monitor_send(command_monitor)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    60
        data = self.monitor_recieve()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    61
        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
    62
        print data
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    63
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    64
    def kvm_boot_action(self):
112
0ca5084026ff update README.txt
Jens Kasten <jens@kasten-edv.de>
parents: 105
diff changeset
    65
        """Boot a qemu-kvm domain."""
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
    66
        if self.is_running():
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    67
            print ("Guest already running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    68
            return True
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    69
        env = os.environ.copy()
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
    70
        # add the  to the enviroment
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    71
        if len(self.bridge) > 0:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    72
            for key, value in self.bridge.iteritems():
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    73
                env[key] = value
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    74
        try:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    75
            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
    76
            result.wait()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    77
            return (True, "")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    78
        except OSError, error_msg:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    79
            return (False, error_msg)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    80
        except IOError, error_msg:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    81
            return (False, error_msg)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    82
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    83
    def kvm_reboot_action(self):
112
0ca5084026ff update README.txt
Jens Kasten <jens@kasten-edv.de>
parents: 105
diff changeset
    84
        """Reboot a qemu-kvm domain."""
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
    85
        if not self.is_running():
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    86
            print ("Guest is not running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    87
            return False
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
    88
        if not self.monitor_send(self.qemu_monitor["reboot"]):
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    89
            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
    90
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    91
    def kvm_shutdown_action(self):
112
0ca5084026ff update README.txt
Jens Kasten <jens@kasten-edv.de>
parents: 105
diff changeset
    92
        """Shutdown a qemu-kvm domain.
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    93
        Its work for windows and linux guests, 
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    94
        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
    95
        """
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
    96
        if not self.is_running():
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    97
            print ("Guest is not running.")
60
70e7bfc80074 fix: missing return in kvmtools/kvm/action.py shutdown()
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
    98
            return
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    99
        flag = 0
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["shutdown"]):
77
b8fe98927e41 uncomment migration method, no use at the moment
Jens Kasten <jens@kasten-edv.de>
parents: 61
diff changeset
   101
            # set a time  out to wait, that the shutdown dialog appears
115
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   102
            if self.shutdown_wait_to_send_enter:
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   103
                sleep(self.shutdown_wait_to_send_enter)
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   104
                self.monitor_send(self.qemu_monitor["enter"])
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   105
            # set start timer
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   106
            timer = time() 
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   107
            while True:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   108
                # some fancy ticker
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   109
                if flag == 0:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   110
                    sign = "\\" 
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   111
                    flag = 1
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   112
                elif flag == 1:    
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   113
                    sign = "|"
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   114
                    flag = 2
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   115
                elif flag == 2:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   116
                    sign = "/"
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   117
                    flag = 3
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   118
                elif flag == 3:
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 = 0
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   121
                if not self.is_running():
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   122
                    sys.stdout.write("Done.         \n")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   123
                    sys.stdout.flush()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   124
                    sys.exit(0) 
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   125
                else:
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   126
                    sys.stdout.write("waiting ... %s\r" % sign)
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   127
                    sys.stdout.flush()
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   128
                    sleep(0.05)
115
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   129
                # when the time out is reached kill the qemu-kvm domain
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   130
                # this is only usefull for init.d system 
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   131
                # otherwise if the domain could from any reason not shutdown
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   132
                # the whole shutdown process is blocked
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   133
                if (timer + self.shudown_time_out) < time():
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   134
                    self.kvm_kill_action()
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   135
        else:
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   136
            print ("Could not send signal shutdown.")
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   137
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   138
    def kvm_kill_action(self):
112
0ca5084026ff update README.txt
Jens Kasten <jens@kasten-edv.de>
parents: 105
diff changeset
   139
        """Kill a qemu-kvm domain.
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   140
        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
   141
        """
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   142
        if not self.is_running():
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   143
            print ("Guest is not running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   144
            return False
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   145
        try:    
115
75c299d0bf5a fix: action.py kvm_kill_action(); add timer for shutdown and cmdline option
Jens Kasten <jens@kasten-edv.de>
parents: 113
diff changeset
   146
            os.kill(seto,lf.kvm_pid, 9)
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   147
            sleep(0.8)
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   148
            self.is_running()
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   149
            sys.exit(0)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   150
        except OSError, error_msg:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   151
            print error_msg
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   152
            self.is_running()
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   153
            sys.exit(1)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   154
113
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   155
    def _status_all(self):
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   156
        """Show status information form all running qemu-kvm domains."""
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   157
        domains = {}
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   158
        # list all files in run dir and get pidfile socketfile and domain name
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   159
        for i in os.listdir(self.kvm_run_dir):
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   160
            domain_name = i.rsplit(".", 1)[0]
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   161
            if not domain_name in domains:
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   162
                domains[domain_name] = {}
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   163
            if i.endswith(".pid"):
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   164
                domains[domain_name]["pidfile"] = os.path.join(self.kvm_run_dir, i)
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   165
            if i.endswith(".socket"):
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   166
                domains[domain_name]["socketfile"] = os.path.join(self.kvm_run_dir, i)
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   167
        # iter over the domains and set the necessary values on the fly
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   168
        counter = len(domains)
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   169
        for domain in domains.iterkeys():
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   170
            self.kvm_pidfile = domains[domain]["pidfile"]
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   171
            self.kvm_socketfile = domains[domain]["socketfile"]
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   172
            self.kvm_domain_name = domain
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   173
            self.monitor_open()
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   174
            self._get_uuid()
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   175
            self._get_status()
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   176
            self._get_process_info()
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   177
            print ("Domain name: %s :: Process name: %s" % \
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   178
                (domain, self.kvm_process_name))
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   179
            print ("Domain uuid: %s" % self.kvm_process_uuid)
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   180
            print ("%s :: Process state: %s" % \
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   181
                (self.kvm_process_status, self.kvm_process_state))
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   182
            if self.kvm_status_full:
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   183
                self._get_vnc()
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   184
                print "UID: %s" % self.kvm_process_uid
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   185
                print "GID: %s" % self.kvm_process_gid
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   186
                print "Groups: %s" % self.kvm_process_groups
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   187
                print "PID: %s :: PPID: %s" % \
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   188
                    (self.kvm_process_pid, self.kvm_process_ppid)
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   189
                print ("VNC information")
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   190
                print self.kvm_process_vnc
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   191
            self.monitor_close()
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   192
            # show only line for seperation if more than one domains
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   193
            if counter > 1:
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   194
                print "-" * 80
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   195
            counter -= 1
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   196
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   197
    def kvm_status_action(self):
112
0ca5084026ff update README.txt
Jens Kasten <jens@kasten-edv.de>
parents: 105
diff changeset
   198
        """Show information about qemu-kvm domain(s)."""
113
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   199
        if self.kvm_domain_name == "all":
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   200
            self._status_all()
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   201
            return True
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   202
        if not self.is_running():
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   203
            print ("Guest is not running.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   204
            return False
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   205
        self.get_process_information()
113
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   206
        print ("Domain name: %s :: Process name: %s" % \
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   207
            (self.kvm_domain_name, self.kvm_process_name))
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   208
        print ("Domain uuid: %s" % self.kvm_process_uuid)
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   209
        print ("%s :: Process state: %s" % \
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   210
            (self.kvm_process_status, self.kvm_process_state))
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   211
        print "UID: %s" % self.kvm_process_uid
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   212
        print "GID: %s" % self.kvm_process_gid
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
   213
        print "Groups: %s" % self.kvm_process_groups
113
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   214
        print "PID: %s :: PPID: %s" % \
d180b27d13a9 add 'status all --full' options, update README
Jens Kasten <jens@kasten-edv.de>
parents: 112
diff changeset
   215
            (self.kvm_process_pid, self.kvm_process_ppid)
61
abba9906ca66 updates and fixes
Jens Kasten <jens@kasten-edv.de>
parents: 60
diff changeset
   216
        print ("VNC information")
abba9906ca66 updates and fixes
Jens Kasten <jens@kasten-edv.de>
parents: 60
diff changeset
   217
        print self.kvm_process_vnc