bin/kvm-admin
author Jens Kasten <jens@kasten-edv.de>
Wed, 26 Jan 2011 14:26:04 +0100
changeset 33 c8f2892d5649
parent 23 06bcb6cf751c
child 37 7aec53b71183
permissions -rwxr-xr-x
add prefix for process 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
     1
#!/usr/bin/env python
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     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
     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
     4
Tool to manage a 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
     5
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     6
(c) 2007-2010 Jens Kasten <jens@kasten-edv.de> 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     7
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     8
This program is free software; you can redistribute it and/or modify
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     9
it under the terms of the GNU General Public License as published by
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    10
the Free Software Foundation; either version 2 of the License, or
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    11
(at your option) any later version.
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    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
    13
This program is distributed in the hope that it will be useful,
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    16
GNU General Public License for more details.
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    17
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    18
You should have received a copy of the GNU General Public License
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    19
along with this program; if not, write to the Free Software
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    21
"""
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    22
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    23
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
    24
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
    25
import re
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    26
06bcb6cf751c add hgignore; move 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
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
    29
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
    30
06bcb6cf751c add hgignore; move 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
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    32
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
    33
    """ 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
    34
06bcb6cf751c add hgignore; move 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
    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
    36
        # 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
    37
        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
    38
        # 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
    39
        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
    40
        # 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
    41
        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
    42
        # 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
    43
        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
    44
        # 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
    45
        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
    46
        # 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
    47
        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
    48
        # 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
    49
        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
    50
        # 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
    51
        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
    52
        # 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
    53
        # 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
    54
        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
    55
        # 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
    56
        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
    57
        # 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
    58
        # 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
    59
        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
    60
        # 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
    61
        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
    62
        # 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
    63
        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
    64
        # 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
    65
        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
    66
        # 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
    67
        # 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
    68
        # 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
    69
        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
    70
    
06bcb6cf751c add hgignore; move 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
    def _get_monitor(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
    72
        """
06bcb6cf751c add hgignore; move 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
        Return a dictionry with type and the path to the socket file or
06bcb6cf751c add hgignore; move 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
        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
    75
        """
06bcb6cf751c add hgignore; move 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
        if "monitor" 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
    77
            _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
    78
            # 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
    79
            # 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
    80
            _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
    81
            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
    82
                # 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
    83
                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
    84
                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
    85
                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
    86
            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
    87
                # 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
    88
                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
    89
                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
    90
                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
    91
        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
    92
            # 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
    93
            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
    94
            _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
    95
            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
    96
            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
    97
        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
    98
06bcb6cf751c add hgignore; move 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
    def _set_pidfile(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
   100
        """
06bcb6cf751c add hgignore; move 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
        Set the absolute path for 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
   102
        """
06bcb6cf751c add hgignore; move 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.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
   104
        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
   105
    
06bcb6cf751c add hgignore; move 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
    def _set_socketfile(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
   107
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   108
        Set the absolute path for socket 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
   109
        """
06bcb6cf751c add hgignore; move 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.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
   111
        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
   112
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   113
    def _get_guest_config_dir(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
   114
        """
06bcb6cf751c add hgignore; move 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
        Return the absolute path for guest configuration directory.
06bcb6cf751c add hgignore; move 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
        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
   118
            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
   119
        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
   120
            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
   121
                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
   122
06bcb6cf751c add hgignore; move 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
    def _get_global_config_dir(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
   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
        Return the absolute path for global configuration directory.
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   126
        """
06bcb6cf751c add hgignore; move 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):
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   134
        """
06bcb6cf751c add hgignore; move 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
        Return all available guests as 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
   136
        """
06bcb6cf751c add hgignore; move 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
        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
   138
        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
   139
        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
   140
            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
   141
                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
   142
        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
   143
            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
   144
        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
   145
            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
   146
                % 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
   147
06bcb6cf751c add hgignore; move 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
    def available_actions(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
   149
        """
06bcb6cf751c add hgignore; move 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
        Return all methods which start with _kvm_ from class 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
        """
06bcb6cf751c add hgignore; move 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
        # 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
   153
        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
   154
        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
   155
            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
   156
                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
   157
        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
   158
            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
   159
        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
   160
            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
   161
06bcb6cf751c add hgignore; move 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
    def _get_guest_config(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
   163
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   164
        Return the absolute path to 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
   165
        """
06bcb6cf751c add hgignore; move 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
        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
   167
        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
   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 _get_global_config(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
   170
        """
06bcb6cf751c add hgignore; move 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
        Return the absolute path to 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
   172
        """
06bcb6cf751c add hgignore; move 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
        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
   174
        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
   175
        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
   176
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   177
    def _qemu_kvm_script(self, 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
   178
        """
06bcb6cf751c add hgignore; move 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
        Return the absoulute path for ifup or ifdown 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
   180
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   181
        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
   182
        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
   183
        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
   184
        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
   185
06bcb6cf751c add hgignore; move 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
    def _check_net_tap(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
   187
        """
06bcb6cf751c add hgignore; move 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
        Examine the -net tap option for ifname and additional scripts and 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   189
        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
   190
        """
06bcb6cf751c add hgignore; move 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
        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
   192
        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
   193
            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
   194
                # 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
   195
                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
   196
                    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
   197
                    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
   198
                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
   199
                    temp_ifname = "=".join(["ifname", 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
   200
                    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
   201
                        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
   202
                    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
   203
                        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
   204
                    ifname = 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
   205
                # 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
   206
                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
   207
                # 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
   208
                # 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
   209
                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
   210
                    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
   211
                    # 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
   212
                    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
   213
                    # 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
   214
                    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
   215
                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
   216
                    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
   217
                    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
   218
                    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
   219
                # 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
   220
                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
   221
                # 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
   222
                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
   223
                    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
   224
                    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
   225
                # 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
   226
                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
   227
                    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
   228
                                        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
   229
                    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
   230
                # 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
   231
                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
   232
            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
   233
                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
   234
        # 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
   235
        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
   236
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   237
    def _add_monitor_to_config(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
   238
        """
06bcb6cf751c add hgignore; move 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
        Append the monitor option to the config 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
   240
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   241
        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
   242
       
06bcb6cf751c add hgignore; move 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
    def _add_pidfile_to_config(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
   244
        """
06bcb6cf751c add hgignore; move 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
        Append the pidfile option to the config 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
   246
        """
06bcb6cf751c add hgignore; move 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
        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
   248
            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
   249
            self.config["pidfile"] = 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
   250
06bcb6cf751c add hgignore; move 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
    def _add_uuid_to_config(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
   252
        """
06bcb6cf751c add hgignore; move 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
        Append an unique uuid to the config 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
   254
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   255
        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
   256
        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
   257
        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
   258
        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
   259
        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
   260
        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
   261
        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
   262
        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
   263
        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
   264
        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
   265
        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
   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
    def _add_name_to_config(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
   268
        """
06bcb6cf751c add hgignore; move 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
        Append a name for window title and process name (on linux 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
   270
        """
06bcb6cf751c add hgignore; move 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
        if "name" in self.config:
33
c8f2892d5649 add prefix for process name
Jens Kasten <jens@kasten-edv.de>
parents: 23
diff changeset
   272
            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
   273
            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
   274
        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
   275
            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
   276
            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
   277
06bcb6cf751c add hgignore; move 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
    def _merge_configs(self, 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
   279
        """
06bcb6cf751c add hgignore; move 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
        Merge global and guest configfile.
06bcb6cf751c add hgignore; move 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
        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
   282
        """
06bcb6cf751c add hgignore; move 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
        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
   284
            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
   285
                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
   286
            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
   287
                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
   288
    
06bcb6cf751c add hgignore; move 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
    def _load_config(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
   290
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   291
        Build user defined 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
   292
        """
06bcb6cf751c add hgignore; move 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
        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
   294
        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
   295
        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
   296
        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
   297
        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
   298
        # 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
   299
        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
   300
        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
   301
        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
   302
        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
   303
        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
   304
        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
   305
            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
   306
            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
   307
06bcb6cf751c add hgignore; move 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
    def _build_command(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
   309
        """
06bcb6cf751c add hgignore; move 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
        Return a tuple.
06bcb6cf751c add hgignore; move 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
        First entry is a list to execute via subprocess
06bcb6cf751c add hgignore; move 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
        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
   313
        """
06bcb6cf751c add hgignore; move 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
        # 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
   315
        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
   316
        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
   317
        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
   318
        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
   319
        # 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
   320
        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
   321
        # 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
   322
        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
   323
            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
   324
                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
   325
        # 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
   326
        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
   327
            # 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
   328
            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
   329
                # 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
   330
                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
   331
                    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
   332
                        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
   333
                        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
   334
                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
   335
                    # 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
   336
                    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
   337
                    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
   338
                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
   339
                    # 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
   340
                    # 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
   341
                    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
   342
            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
   343
                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
   344
                        % 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
   345
                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
   346
        # 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
   347
        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
   348
        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
   349
06bcb6cf751c add hgignore; move 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
    def run(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
   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
        Do an action for a guest domain.
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   353
        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
   354
        """
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   355
        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
   356
        kvm_method = "".join([self._kvm_prefix, 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
   357
        kvm = Kvm(self.guest, self.pidfile, 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
   358
        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
   359
            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
   360
        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
   361
            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
   362
        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
   363
            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
   364
                # 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
   365
                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
   366
                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
   367
            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
   368
                print "Missing argument for monitor. Type 'help' 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
   369
        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
   370
            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
   371
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   372
def main():
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   373
    # check python version, argparse was added in python 2.7 and 3.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
   374
    # when found lower python version import it from kvmtools
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   375
    if sys.version_info >= (2, 7) or sys.version_info >= (3, 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
   376
        import argparse
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   377
    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
   378
        import kvmtools.argparse as argparse
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   379
    
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   380
    try:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   381
        kvm = KvmAdmin()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   382
        guests = kvm.available_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
   383
        actions = kvm.available_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
   384
        
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   385
        # manage the arguments
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   386
        parser = argparse.ArgumentParser(prog="kvm-admin",
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   387
                    description="Cmdline qemu-kvm guest management tool.",
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   388
                    usage="%(prog)s [-h] guest 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
   389
        group1 = parser.add_argument_group("Info to manage a 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
   390
            "Choose a 'guest' from list and an '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
   391
        group1.add_argument("guest", choices=guests, help="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
   392
        group1.add_argument("action", choices=actions, help="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
   393
        group1.add_argument("monitor", nargs="*", default=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
   394
            help="""When choose monitor as action its need 
06bcb6cf751c add hgignore; move 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
                    one or more additional 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
   396
        parser.parse_args(namespace=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
   397
        
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   398
        # run the 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
   399
        kvm.run()
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   400
    except Exception, e:
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   401
        if kvm.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
   402
            raise 
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   403
        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
   404
            print str(e)
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   405
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   406
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   407
if __name__ == "__main__":
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   408
    main()