bin/kvm-admin
author Jens Kasten <jens@kasten-edv.de>
Wed, 23 Nov 2011 02:42:49 +0100
branchfeatures_editor
changeset 55 1d4c061d1502
parent 54 17b08e781dc6
child 59 8465f1e93a07
permissions -rwxr-xr-x
update and fixes for new structur
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
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
     9
#           python-psutil == 0.1.3 (debian squeeze)
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    10
#
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    11
# 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
    12
# 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
    13
# 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
    14
# (at your option) any later version.
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    15
#
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    16
# 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
    17
# 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
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    19
# GNU General Public License for more details.
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    20
# 
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    21
# 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
    22
# 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
    23
# 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
    24
#
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
    25
06bcb6cf751c add hgignore; move 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
"""
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    27
(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
    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
06bcb6cf751c add hgignore; move 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 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
    31
import sys
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    32
import argparse
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    33
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
    34
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
    35
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
    36
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    37
LOCALEAPP = sys.argv[0]
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    38
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
    39
_ = 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
    40
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    41
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    42
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
    43
    """ 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
    44
06bcb6cf751c add hgignore; move 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
    def __init__(self):
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    46
        Action.__init__(self)
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
    47
        self._args_value = None
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:
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
    56
            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
    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))
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    90
    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
    91
    group1.add_argument("domain", default=False,
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    92
        metavar=(_("domain_name")),
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    93
        help=_("Choose a KVM domain name, "
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    94
             "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
    95
    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
    96
    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
    97
        help=_("Choose an action for the KVM guest"))
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    98
    group3 = parser.add_argument_group("Monitor command")
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    99
    group3.add_argument("monitor", nargs="*", default=False,
44
e1bb11f91cfd fix _add_pidfile_to_config()
Jens Kasten <jens@kasten-edv.de>
parents: 42
diff changeset
   100
        metavar="option",
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   101
        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
   102
    args = parser.parse_args()
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   103
55
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   104
    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
   105
        kvm_admin.kvm_domain_name = args.domain
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   106
    else:
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   107
        parser.error(_("Available domains: %s") % domains)
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   108
    
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   109
    kvm_admin.action = args.action
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   110
    if args.monitor:
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   111
        kvm_admin.monitor = args.monitor
1d4c061d1502 update and fixes for new structur
Jens Kasten <jens@kasten-edv.de>
parents: 54
diff changeset
   112
    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
   113
        sys.exit(1)
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   114
    kvm_admin.run()
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
   115
06bcb6cf751c add hgignore; move 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 __name__ == "__main__":
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   118
    try:
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   119
        main()
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   120
    except KeyboardInterrupt:
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   121
        print _("\rUser break.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   122
        sys.exit(0)