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
#!/usr/bin/env python
#
# Comamandline tool to manage a kvm guest with monitor.
#
# Usage: kvm-admin guest_name action [arguments]
#
# Depencies:
#           python-argparse
#           python-psutil == 0.1.3 (debian squeeze)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

"""
(c) 2007-2011 Jens Kasten <jens@kasten-edv.de> 
"""

import os
import sys
import argparse
import gettext

from kvmtools.kvm.action import Action

locale_app = sys.argv[0]
if locale_app.endswith('.py'):
    locale_app = locale_app[:len(locale_app)-3]
else:
    t = gettext.translation(locale_app, "/usr/share/locale", fallback=True)
    _ = t.ugettext


class KvmAdmin(object):
    """ Class for commandline usage for a qemu-kvm guest."""

    def __init__(self):
        self.kvm_action = Action()
        self.available_actions = self.kvm_action.available_actions()

    def run(self):
        """Do an action for a domain.
        Call a method based on commandline option.
        """
        self.kvm_action.kvm_domain_name = self.domain
        self.kvm_action.load_command()
        kvm_method = "".join(["kvm_", self.action, "_action"])
        if "monitor" in self.action or "migrate" in self.action:
            if len(sys.argv) >= 4:
                # build string from third option till end
                cmd_monitor = " ".join(str(i) for i in self.monitor)
                getattr(self.kvm_action, kvm_method)(cmd_monitor)
            else:
                raise Exception("Missing monitor argument. Type 'help'.")
        else:
            getattr(self.kvm_action, kvm_method)()

def main():
    kvm_admin = KvmAdmin()
    actions = kvm_admin.available_actions
        
    # manage the arguments
    parser = argparse.ArgumentParser(
        epilog="Available action: %s" % ", ".join(actions))

    group1 = parser.add_argument_group(_("QEMU-KVM domain"))
    group1.add_argument("domain", default=False,
        metavar=(_("domain_name")),
        help=_("Choose a KVM domain name, "
             "its the same like the configuration file name"))
    group2 = parser.add_argument_group("Action for a domain")
    group2.add_argument("action", choices=actions, default=False, 
        help=_("Choose an action for the KVM guest"))
    group3 = parser.add_argument_group("Monitor command")
    group3.add_argument("monitor", nargs="*", default=False,
        metavar="option",
        help=_("One ore more arguments can pass to the monitor."))
    group4 = parser.add_argument_group("Domain")
    group4.add_argument("create", default=False, action="store_true",
        help=_("Create a domain config file."))
    group4.add_argument("modify", default=False, action="store_true",
        help=_("Modify a domain config file."))
    args = parser.parse_args(namespace=kvm_admin)

    if not kvm_admin.create:
        if not kvm_adin.domain in domains:
            parser.error(_("Available domains: %s") % domains)
    # run the action
    #try:
    kvm_admin.run()
#    except Exception, error_msg:
 #       print error_msg
  #      sys.exit(1)


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print _("\rUser break.")
        sys.exit(0)