bin/kvm-admin
author Jens Kasten <jens@kasten-edv.de>
Wed, 23 Nov 2011 20:02:56 +0100
changeset 61 abba9906ca66
parent 59 8465f1e93a07
child 71 31e017a5948c
permissions -rwxr-xr-x
updates and fixes
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
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    31
import argparse
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    32
import gettext
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
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
    34
from kvmtools.kvm.action import Action
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    35
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    36
LOCALEAPP = sys.argv[0]
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    37
TRANSLATE = gettext.translation(LOCALEAPP, "/usr/share/locale", fallback=True)
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    38
_ = TRANSLATE.ugettext
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
    39
06bcb6cf751c add hgignore; move 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
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    41
class KvmAdmin(Action):
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
    42
    """ 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
    43
06bcb6cf751c add hgignore; move 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
    def __init__(self):
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    45
        Action.__init__(self)
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    46
        self._args_value = None
61
abba9906ca66 updates and fixes
Jens Kasten <jens@kasten-edv.de>
parents: 59
diff changeset
    47
        self.debug = False
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
    48
06bcb6cf751c add hgignore; move 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
    def run(self):
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    50
        """Do an action for a domain.
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    51
        Call a method based on commandline option.
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
    52
        """
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    53
        kvm_method = "".join(["kvm_", self.action, "_action"])
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    54
        self.monitor_open()
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    55
        if "monitor" in self.action or "migrate" in self.action:
59
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
    56
            if self.monitor:
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
    57
                # 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
    58
                cmd_monitor = " ".join(str(i) for i in self.monitor)
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    59
                getattr(self, kvm_method)(cmd_monitor)
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
    60
            else:
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    61
                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
    62
        else:
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    63
            getattr(self, kvm_method)()
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    64
        self.monitor_close()
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    65
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    66
    def show_error(self):
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    67
        """Display errors if the registered."""
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    68
        errors = self.kvm_errors
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    69
        if len(errors) > 0:
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    70
            for error in errors:
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    71
                print error
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    72
            return True
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    73
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    74
    def _set(self, args_value):
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    75
        self._args_value = args_value
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    76
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    77
    def _get(self):
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    78
        return self._args_value
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    79
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
    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
def main():
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    82
    """Handle the commandline arguments."""
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    83
    kvm_admin = KvmAdmin()
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    84
    actions = kvm_admin.available_actions()
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    85
    domains = kvm_admin.kvm_domain_name_all
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    86
   
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    87
    # manage the arguments
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    88
    parser = argparse.ArgumentParser(
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    89
        epilog="Available domains: %s" % ", ".join(domains))
59
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
    90
    parser.add_argument("--debug", default=False, action="store_true",
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
    91
        help="Print full execption output.")
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    92
    group1 = parser.add_argument_group(_("QEMU-KVM domain"))
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    93
    group1.add_argument("domain", default=False,
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    94
        metavar=(_("domain_name")),
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    95
        help=_("Choose a KVM domain name, "
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    96
             "its the same like the configuration file name"))
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    97
    group2 = parser.add_argument_group("Action for a domain")
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    98
    group2.add_argument("action", choices=actions, 
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    99
        help=_("Choose an action for the KVM guest"))
59
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   100
    group3 = parser.add_argument_group("Monitor commands")
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   101
    group3.add_argument("monitor", nargs="*", default=False,
44
e1bb11f91cfd fix _add_pidfile_to_config()
Jens Kasten <jens@kasten-edv.de>
parents: 42
diff changeset
   102
        metavar="option",
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   103
        help=_("One ore more arguments can pass to the monitor."))
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   104
    args = parser.parse_args()
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   105
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   106
    if args.action == "create" or args.domain in domains: 
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   107
        kvm_admin.kvm_domain_name = args.domain
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   108
    else:
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   109
        parser.error(_("Available domains: %s") % domains)
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   110
    kvm_admin.action = args.action
59
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   111
    kvm_admin.monitor = args.monitor
61
abba9906ca66 updates and fixes
Jens Kasten <jens@kasten-edv.de>
parents: 59
diff changeset
   112
    kvm_admin.debug = args.debug
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   113
    if not args.action == "modify" and kvm_admin.show_error() :
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   114
        sys.exit(1)
59
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   115
    if args.debug:
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   116
        kvm_admin.run()
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   117
    else:
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   118
        try:
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   119
            kvm_admin.run()
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   120
        except Exception, error_msg:
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   121
            print error_msg
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   122
            
8465f1e93a07 remove content from kvmtools/__init__.py; fix: kvm-admin argparse stuff
Jens Kasten <jens@kasten-edv.de>
parents: 55
diff changeset
   123
        
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
   124
if __name__ == "__main__":
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   125
    try:
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   126
        main()
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   127
    except KeyboardInterrupt:
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   128
        print _("\rUser break.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   129
        sys.exit(0)