bin/kvm-admin
author Jens Kasten <jens@kasten-edv.de>
Thu, 17 Nov 2011 00:24:00 +0100
changeset 44 e1bb11f91cfd
parent 42 5771d7a20b04
child 53 9eb8be6341f3
permissions -rwxr-xr-x
fix _add_pidfile_to_config()
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     1
#!/usr/bin/env python
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
     2
#
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
     3
# Comamandline tool to manage a kvm guest with monitor.
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
     4
#
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
     5
# Usage: kvm-admin guest_name action [arguments]
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
     6
#
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
     7
# Depencies:
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
     8
#           python-argparse
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
     9
#
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    10
# This program is free software; you can redistribute it and/or modify
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    11
# it under the terms of the GNU General Public License as published by
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    12
# the Free Software Foundation; either version 2 of the License, or
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    13
# (at your option) any later version.
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    14
#
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    15
# This program is distributed in the hope that it will be useful,
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    18
# GNU General Public License for more details.
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    19
# 
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    20
# You should have received a copy of the GNU General Public License
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    21
# along with this program; if not, write to the Free Software
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    22
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    23
#
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    24
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    25
"""
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    26
(c) 2007-2011 Jens Kasten <jens@kasten-edv.de> 
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    27
"""
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    28
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    29
import os
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    30
import sys
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    31
import re
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    32
import argparse
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    33
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    34
from kvmtools.configparser import Parser
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    35
from kvmtools.kvm import Kvm
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    36
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    37
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    38
class KvmAdmin(object):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    39
    """ Class for commandline usage for a qemu-kvm guest."""
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    40
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    41
    def __init__(self):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    42
        # absolute path to the configs, scripts and tools
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    43
        self._base_dir = "/etc/kvm"
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    44
        # path to store pidfile and if needed the socketfile
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    45
        self._run_path = '/var/run/kvm'
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    46
        # directory name to store the ifdown and ifup scripts
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    47
        self._script_dir = 'scripts'
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    48
        # directory name to store the guest configuration files
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    49
        self._guest_config_dir = 'domains'
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    50
        # directory name to store the global configuration file
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    51
        self._global_config_dir = 'config'
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    52
        # name for global configuration file
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    53
        self._global_config = 'kvm.cfg'
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    54
        # argument to show the string which should be execute
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    55
        self._show_config_argument = "show"
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    56
        # prefix for methodes which do some action for the guest
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    57
        self._kvm_prefix = "kvm_"
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    58
        # default telnet port 23, can only use once at time in one guest
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    59
        # otherwise each guest have to set it explicit
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    60
        self._telnet_port = 23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    61
        # store the pidfile 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    62
        self.pidfile = None
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    63
        # contain the type for socket
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    64
        # and if needed socket file or host and port
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    65
        self.monitor_options = {}
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    66
        # this exclude_options are using internal only
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    67
        self.exclude_options = ['qemu-kvm', 'python-debug']
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    68
        # contain all configuration variables
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    69
        self.config = {}
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    70
        # contain all bridges to export them
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    71
        self.bridge = {}
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    72
        # keep the status for debugging the python script while editing
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    73
        # values can be True or False 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    74
        # but should set on global or guest configuration file
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    75
        self.debug = False
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    76
    
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    77
    def _get_monitor(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    78
        """Return a dictionry with type and the path to the socket file or
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    79
        the host and port.
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    80
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    81
        if "monitor" in self.config:
42
5771d7a20b04 fix _get_monitor() in kvm-admin file, thanks for patch Simas ?epaitis
Jens Kasten <jens@kasten-edv.de>
parents: 37
diff changeset
    82
            
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    83
            _monitor = self.config["monitor"]
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    84
            # get the string befor the first comma 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    85
            # and then split this string by colon
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    86
            _type = _monitor.split(',')[0].split(':')
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    87
            if len(_type) == 3:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    88
                # this is usally for tcp
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    89
                self.monitor_options['Type'] = _type[0] 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    90
                self.monitor_options['Host'] = _type[1]
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    91
                self.monitor_options['Port'] = int(_type[2])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    92
            elif len(_type) == 2:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    93
                # this is for telnet, when no port is given
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    94
                self.monitor_options['Type'] = _type[0]
42
5771d7a20b04 fix _get_monitor() in kvm-admin file, thanks for patch Simas ?epaitis
Jens Kasten <jens@kasten-edv.de>
parents: 37
diff changeset
    95
                if _type[0] == 'unix':
5771d7a20b04 fix _get_monitor() in kvm-admin file, thanks for patch Simas ?epaitis
Jens Kasten <jens@kasten-edv.de>
parents: 37
diff changeset
    96
                    self.monitor_options['SocketFile'] = _type[1]
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    97
                self.monitor_options['Host'] = _type[1]
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    98
                self.monitor_options['Port'] = self._telnet_port
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    99
        else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   100
            # set unix socket as default monitor access
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   101
            self._set_socketfile()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   102
            _monitor = "unix:%s,server,nowait" % self.socketfile
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   103
            self.monitor_options["Type"] = "unix"
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   104
            self.monitor_options['SocketFile'] = self.socketfile
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   105
        return _monitor 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   106
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   107
    def _set_pidfile(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   108
        """Set the absolute path for the pidfile."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   109
        self.pidfile = "".join([self.guest, ".pid"])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   110
        self.pidfile = os.path.join(self._run_path, self.pidfile)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   111
    
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   112
    def _set_socketfile(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   113
        """Set the absolute path for the socketfile."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   114
        self.socketfile = "".join([self.guest, ".socket"])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   115
        self.socketfile = os.path.join(self._run_path, self.socketfile)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   116
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   117
    def _get_guest_config_dir(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   118
        """Return the absolute path for guest configuration directory."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   119
        if os.path.isdir(os.path.join(self._base_dir, self._guest_config_dir)):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   120
            return os.path.join(self._base_dir, self._guest_config_dir)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   121
        else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   122
            raise Exception("Guest config directory '%s' does not exists." %
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   123
                self._guest_config_dir)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   124
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   125
    def _get_global_config_dir(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   126
        """Return the absolute path for global configuration directory."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   127
        if os.path.isdir(os.path.join(self._base_dir, self._global_config_dir)):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   128
            return os.path.join(self._base_dir, self._global_config_dir)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   129
        else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   130
            raise Exception("Global config directory '%s' does not exists." %
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   131
                self._global_config_dir)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   132
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   133
    def available_guests(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   134
        """Return all available guests as an dictionary."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   135
        guests = []
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   136
        guest_config_dir = self._get_guest_config_dir()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   137
        for guest in os.listdir(guest_config_dir):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   138
            if os.path.isfile(os.path.join(guest_config_dir, guest)):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   139
                guests.append(guest)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   140
        if len(guests) >= 1:                
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   141
            return guests
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   142
        else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   143
            raise Exception("Guest configuration directory is empty '%s'." 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   144
                % guest_config_dir)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   145
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   146
    def available_actions(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   147
        """Return all methods which start with _kvm_ from class Kvm."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   148
        # add action to show the sting, there is no method for this
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   149
        actions = [self._show_config_argument]
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   150
        for action in dir(Kvm):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   151
            if action.startswith(self._kvm_prefix):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   152
                actions.append(action.replace(self._kvm_prefix, ""))
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   153
        if len(actions) >= 2:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   154
            return actions
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   155
        else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   156
            raise Execption("No action available.")
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   157
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   158
    def _get_guest_config(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   159
        """Return the absolute path to guest configuration file."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   160
        guest_config = os.path.join(self._get_guest_config_dir(), self.guest)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   161
        return guest_config
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   162
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   163
    def _get_global_config(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   164
        """Return the absolute path to global configuration file."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   165
        global_config_dir = self._get_global_config_dir()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   166
        global_config = os.path.join(global_config_dir, self._global_config)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   167
        return global_config
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   168
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   169
    def _qemu_kvm_script(self, script_option):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   170
        """Return the absoulute path for ifup or ifdown script."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   171
        script_option = "".join(["kvm-", script_option])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   172
        script = os.path.join(self._base_dir, self._script_dir)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   173
        script = os.path.join(script, script_option)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   174
        return script
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   175
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   176
    def _check_net_tap(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   177
        """Examine the -net tap option for ifname and additional scripts and 
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   178
        bridge strings.
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   179
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   180
        temp = {} 
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   181
        counter = 0
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   182
        for key, value in self.config["net"].iteritems():
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   183
            if value.startswith("tap"):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   184
                # search for ifname otherwise set it from guest name
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   185
                if re.search("ifname", value):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   186
                    temp_ifname = re.search(",ifname=([a-zA-Z0-9]+)", value)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   187
                    ifname = temp_ifname.group(1)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   188
                else:
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   189
                    ifname = "".join([self.guest, str(counter)])
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   190
                    temp_ifname = "=".join(["ifname", ifname])
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   191
                    if re.match("tap,", value):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   192
                        value = re.sub("tap,", "tap,%s,", value) % temp_ifname
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   193
                    else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   194
                        value = re.sub("tap", "tap,%s", value) % temp_ifname
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   195
                    counter += 1
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   196
                # build the bridge key    
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   197
                bridge_key = "_".join(["bridge", ifname])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   198
                # search for bridge otherwise raise an exception,
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   199
                # because this value is needed
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   200
                if re.search("bridge", value):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   201
                    temp_bridge = re.search(",bridge=([a-zA-Z0-9]+)", value)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   202
                    # get the bridge name from searched group
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   203
                    bridge = temp_bridge.group(1)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   204
                    # remove the bridge from string
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   205
                    value = value.replace(temp_bridge.group(0), "")
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   206
                else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   207
                    msg = "Missing second Value for bridge.\n"
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   208
                    msg = "".join([msg, "Syntax example: bridge=br0"])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   209
                    raise Exception(msg)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   210
                # assign bridge for exporting the bridge name
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   211
                self.bridge[bridge_key] = bridge
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   212
                # search for script 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   213
                if not re.search("script", value):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   214
                    ifup = "=".join(["script", self._qemu_kvm_script('ifup')])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   215
                    value = ",".join([value, ifup])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   216
                # search for downscript
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   217
                if not re.search("downscript", value):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   218
                    ifdown = "=".join(["downscript", 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   219
                                        self._qemu_kvm_script('ifdown')])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   220
                    value = ",".join([value, ifdown])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   221
                # add the cleaned value to temporary dictionary
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   222
                temp[key] = value
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   223
            else:                    
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   224
                temp[key] = value
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   225
        # add the cleand temp dictionary back to config        
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   226
        self.config["net"] = temp
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   227
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   228
    def _add_monitor_to_config(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   229
        """Append the monitor option to the config dictionary."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   230
        self.config["monitor"] = self._get_monitor() 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   231
       
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   232
    def _add_pidfile_to_config(self):
44
e1bb11f91cfd fix _add_pidfile_to_config()
Jens Kasten <jens@kasten-edv.de>
parents: 42
diff changeset
   233
        """Append the pidfile option to the config dictionary or reverse."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   234
        if "pidfile" not in self.config:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   235
            pidfile = os.path.join(self._run_path, self.pidfile)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   236
            self.config["pidfile"] = pidfile
44
e1bb11f91cfd fix _add_pidfile_to_config()
Jens Kasten <jens@kasten-edv.de>
parents: 42
diff changeset
   237
        else:
e1bb11f91cfd fix _add_pidfile_to_config()
Jens Kasten <jens@kasten-edv.de>
parents: 42
diff changeset
   238
            self.pidfile = self.config["pidfile"]
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   239
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   240
    def _add_uuid_to_config(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   241
        """Append an unique uuid to the config dictionary."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   242
        import string
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   243
        import random
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   244
        random.seed(os.urandom(8))
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   245
        charset = string.digits + "abcdef"
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   246
        eight = "".join(random.sample(charset, 8))
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   247
        four_first = "".join(random.sample(charset, 4))
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   248
        four_second = "".join(random.sample(charset, 4))
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   249
        four_third = "".join(random.sample(charset, 4))
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   250
        twelve =  "".join(random.sample(charset, 12))
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   251
        uuid = "-".join([eight, four_first, four_second, four_third, twelve])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   252
        self.config["uuid"] = uuid
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   253
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   254
    def _add_name_to_config(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   255
        """Append a name for window title and process name (on linux only)."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   256
        if "name" in self.config:
33
c8f2892d5649 add prefix for process name
Jens Kasten <jens@kasten-edv.de>
parents: 23
diff changeset
   257
            process_name = "=kvm_".join(["process", self.config["name"]])
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   258
            self.config["name"] = ",".join([self.config["name"], process_name])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   259
        else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   260
            process_name = "=".join(["process", self.guest])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   261
            self.config["name"] = ",".join([self.guest, process_name])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   262
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   263
    def _merge_configs(self, global_config, guest_config):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   264
        """Merge global and guest configfile.
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   265
        Keep this method, maybe add some more configuration files later.
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   266
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   267
        for key in global_config.keys():
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   268
            if key in guest_config:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   269
                self.config[key] = guest_config[key]
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   270
            else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   271
                self.config[key] = global_config[key]
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   272
    
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   273
    def _load_config(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   274
        """Build user defined config."""
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   275
        self._set_pidfile()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   276
        parser = Parser()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   277
        global_config = parser(self._get_global_config())
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   278
        guest_config = parser(self._get_guest_config())
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   279
        self._merge_configs(global_config, guest_config)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   280
        # add internal defaults and do some check
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   281
        self._add_name_to_config()    
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   282
        self._add_uuid_to_config()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   283
        self._add_monitor_to_config()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   284
        self._add_pidfile_to_config()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   285
        self._check_net_tap()    
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   286
        if ("python-debug" in self.config 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   287
            and self.config["python-debug"] == "enabled"):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   288
            self.debug = True
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   289
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   290
    def _build_command(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   291
        """Return a tuple. First entry is a list to execute via subprocess
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   292
        and the second is a string to display it.
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   293
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   294
        # import the auto generatet qemu-kvm options from kvm --help
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   295
        from kvmtools.qemu_kvm_options import qemu_kvm_options
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   296
        self._load_config()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   297
        cmd_execute = []
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   298
        cmd_string = ""
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   299
        # Start to build a list, firstly add the qemu-kvm binary
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   300
        cmd_execute.append(self.config["qemu-kvm"])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   301
        # then remove internal option  
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   302
        for key in self.exclude_options:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   303
            if key in self.config:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   304
                del self.config[key]
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   305
        # iterate over the user config
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   306
        for key, value in self.config.iteritems():
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   307
            # check if key is in qemu_kvm_options
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   308
            if key in qemu_kvm_options:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   309
                # this check search for more option like -drive -drive etc.
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   310
                if isinstance(value, dict):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   311
                    for i in value.itervalues():
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   312
                        cmd_execute.append(''.join(['-', key]))
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   313
                        cmd_execute.append(i)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   314
                elif "enabled" != value:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   315
                    # this qemu-kvm option have an option, so add -key value
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   316
                    cmd_execute.append(''.join(['-', key]))
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   317
                    cmd_execute.append(value)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   318
                else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   319
                    # this qemu-kvm option don't have any option 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   320
                    # so only add -key as argument
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   321
                    cmd_execute.append(''.join(['-', key]))
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   322
            else: 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   323
                msg = ("This option '%s' is not valid for qemu-kvm command." 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   324
                        % key)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   325
                raise Exception(msg)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   326
        # build a string for to display on terminal output
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   327
        cmd_string = " ".join([value for value in cmd_execute])
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   328
        return (cmd_execute, cmd_string)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   329
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   330
    def run(self):
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   331
        """Do an action for a guest domain.
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   332
        Call a method based on commandline option two.
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   333
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   334
        cmd = self._build_command()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   335
        kvm_method = "".join([self._kvm_prefix, self.action])
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   336
        kvm = Kvm(self.guest, self.config['uuid'], self.pidfile, 
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   337
            self.monitor_options)
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   338
        if self._show_config_argument == self.action:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   339
            print "This string would executed:\n%s" % cmd[1]
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   340
        elif "boot" == self.action:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   341
            getattr(kvm, kvm_method)(cmd[0], self.bridge)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   342
        elif "monitor" in self.action or "migrate" in self.action:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   343
            if len(sys.argv) >= 4:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   344
                # build string from third option till end
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   345
                cmd_monitor = " ".join(str(i) for i in self.monitor)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   346
                getattr(kvm, kvm_method)(cmd_monitor)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   347
            else:
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   348
                raise Exception("Missing monitor argument. Type 'help'.")
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   349
        else:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   350
            getattr(kvm, kvm_method)()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   351
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   352
def main():
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   353
    kvm = KvmAdmin()
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   354
    guests = kvm.available_guests()
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   355
    actions = kvm.available_actions()
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   356
        
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   357
    # manage the arguments
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   358
    parser = argparse.ArgumentParser(
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   359
        epilog="Available action: %s" % ", ".join(actions))
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   360
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   361
    group1 = parser.add_argument_group("KVM guest name")
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   362
    group1.add_argument("guest", choices=guests,  
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   363
        metavar=("kvm_guest_name"),
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   364
        help="Choose a KVM guest name, "
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   365
             "its the same like the configuration file")
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   366
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   367
    group2 = parser.add_argument_group("Action for a guest")
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   368
    group2.add_argument("action", choices=actions,  
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   369
        metavar=("action"),
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   370
        help="Choose an action for the KVM guest")
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   371
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   372
    group3 = parser.add_argument_group("Monitor command.")
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   373
    group3.add_argument("monitor", nargs="*", default=False,
44
e1bb11f91cfd fix _add_pidfile_to_config()
Jens Kasten <jens@kasten-edv.de>
parents: 42
diff changeset
   374
        metavar="option",
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   375
        help="One ore more arguments can pass to the monitor.")
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   376
   
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   377
    group4 = parser.add_argument_group("Qemu options")
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   378
    group4.add_argument("--generate-options", default=False, 
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   379
        action="store_true", 
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   380
        help="Generate new qemu-kvm options. From time to time its "
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   381
             "should execute, because they can change.")
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   382
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   383
      
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   384
    args = parser.parse_args(namespace=kvm)
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   385
    # run the action
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   386
    try:
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   387
        if kvm.generate_options:
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   388
            os.system("generate-kvm-options doit")                
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   389
        else:
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   390
            kvm.run()
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   391
    except Exception, error_msg:
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   392
        print error_msg
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   393
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   394
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   395
if __name__ == "__main__":
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   396
    try:
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   397
        main()
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   398
    except KeyboardInterrupt:
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   399
        print "\rUser break."