bin/kvm-admin
author Jens Kasten <jens@kasten-edv.de>
Tue, 22 Nov 2011 14:42:43 +0100
branchfeatures_editor
changeset 54 17b08e781dc6
parent 53 9eb8be6341f3
child 55 1d4c061d1502
permissions -rwxr-xr-x
update and fixes befor merging branch
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
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    37
locale_app = sys.argv[0]
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    38
if locale_app.endswith('.py'):
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    39
    locale_app = locale_app[:len(locale_app)-3]
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    40
else:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    41
    t = gettext.translation(locale_app, "/usr/share/locale", fallback=True)
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    42
    _ = t.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
    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
06bcb6cf751c add hgignore; move 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
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
    46
    """ 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
    47
06bcb6cf751c add hgignore; move 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
    def __init__(self):
54
17b08e781dc6 update and fixes befor merging branch
Jens Kasten <jens@kasten-edv.de>
parents: 53
diff changeset
    49
        self.kvm_action = Action()
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    50
        self.available_actions = self.kvm_action.available_actions()
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    51
06bcb6cf751c add hgignore; move 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
    def run(self):
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    53
        """Do an action for a domain.
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    54
        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
    55
        """
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    56
        self.kvm_action.kvm_domain_name = self.domain
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    57
        self.kvm_action.load_command()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    58
        kvm_method = "".join(["kvm_", self.action, "_action"])
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    59
        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
    60
            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
    61
                # 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
    62
                cmd_monitor = " ".join(str(i) for i in self.monitor)
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    63
                getattr(self.kvm_action, 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
    64
            else:
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    65
                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
    66
        else:
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    67
            getattr(self.kvm_action, kvm_method)()
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
    68
06bcb6cf751c add hgignore; move 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
def main():
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    70
    kvm_admin = KvmAdmin()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    71
    actions = kvm_admin.available_actions
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    72
        
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    73
    # manage the arguments
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    74
    parser = argparse.ArgumentParser(
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    75
        epilog="Available action: %s" % ", ".join(actions))
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    76
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    77
    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
    78
    group1.add_argument("domain", default=False,
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    79
        metavar=(_("domain_name")),
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    80
        help=_("Choose a KVM domain name, "
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    81
             "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
    82
    group2 = parser.add_argument_group("Action for a domain")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    83
    group2.add_argument("action", choices=actions, default=False, 
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    84
        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
    85
    group3 = parser.add_argument_group("Monitor command")
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    86
    group3.add_argument("monitor", nargs="*", default=False,
44
e1bb11f91cfd fix _add_pidfile_to_config()
Jens Kasten <jens@kasten-edv.de>
parents: 42
diff changeset
    87
        metavar="option",
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    88
        help=_("One ore more arguments can pass to the monitor."))
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    89
    group4 = parser.add_argument_group("Domain")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    90
    group4.add_argument("create", default=False, action="store_true",
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    91
        help=_("Create a domain config file."))
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    92
    group4.add_argument("modify", default=False, action="store_true",
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    93
        help=_("Modify a domain config file."))
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    94
    args = parser.parse_args(namespace=kvm_admin)
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    95
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    96
    if not kvm_admin.create:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    97
        if not kvm_adin.domain in domains:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
    98
            parser.error(_("Available domains: %s") % domains)
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
    99
    # run the action
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   100
    #try:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   101
    kvm_admin.run()
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   102
#    except Exception, error_msg:
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   103
 #       print error_msg
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   104
  #      sys.exit(1)
23
06bcb6cf751c add hgignore; move kvm-admin to bin/kvm-admin and do small fixes; add setup.py for installation
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
   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
06bcb6cf751c add hgignore; move 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
if __name__ == "__main__":
37
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   108
    try:
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   109
        main()
7aec53b71183 update and some fixes
Jens Kasten <jens@kasten-edv.de>
parents: 33
diff changeset
   110
    except KeyboardInterrupt:
53
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   111
        print _("\rUser break.")
9eb8be6341f3 new structur for file and class layout
Jens Kasten <jens@kasten-edv.de>
parents: 44
diff changeset
   112
        sys.exit(0)