Skip to content
Snippets Groups Projects
vm.py 4.16 KiB
Newer Older
Seblu's avatar
Seblu committed

'''
CloudControl VM related commands
'''

from cccli.exception import *
from sjrpc.core.exceptions import *
from cccli.printer import Printer, color
Seblu's avatar
Seblu committed
from cccli.command.command import OptionCommand
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
class VmCommand(OptionCommand):
Seblu's avatar
Seblu committed
    '''Command for vm style'''

Seblu's avatar
Seblu committed
    def __init__(self, cli, argv0):
        OptionCommand.__init__(self, cli, argv0)
        self.option.set_usage("%prog [options] <tql>")
        self.option.add_option("--raw", action="store_true", dest="raw",
Seblu's avatar
Seblu committed
                               help="Don't append filter on request")
Seblu's avatar
Seblu committed
        self.option.add_option("--direct", action="store_true", dest="direct",
Seblu's avatar
Seblu committed
                               help="Directly send tql to server (don't list before)")
Seblu's avatar
Seblu committed
        self.option.add_option("--tg", action="store_true", dest="noask",
Seblu's avatar
Seblu committed
                               help="Don't ask confirmation (Dangerous)")
Seblu's avatar
Seblu committed

    def _vm_action(self, argv, filters=None):
        try:
            (options, args) = self.option.parse_args(argv[1:])
Seblu's avatar
Seblu committed
        except SystemExit:
            return
        if len(args) == 0:
            raise cmdBadArgument()
        tql = str.join("", args)
        # append securty options by command name
        if filters is not None and not options.raw:
            tql += filters
        if options.direct:
            try:
                objs = self.cli.rpc.call(argv[0], tql)
            except RpcError as e:
                raise cmdError("RPCError: %s"%str(e))
        else:
            # get objects id
            try:
                objs = self.cli.rpc.call("list", tql)
            except RpcError as e:
                raise cmdError("RPCError: %s"%str(e))
            # no result, goodbye
            if len(objs) == 0:
                raise cmdWarning("tql: '%s': No result."%tql)
            self.printer.out("You will %s:"%argv[0])
            for obj in objs:
                self.printer.out("%sid:%s%s%s"%(color["green"],color["yellow"],obj["id"],color["reset"]))
            self.printer.out("%sCount: %s%s"%(color["green"],color["reset"], len(objs)))
            # be sure boby want do that
            if not options.force:
                self.printer.out("%sProceed?%s"%(color["lred"], color["reset"]))
                if self.printer.ask("Answer (yes/NO): ") != "yes":
                    raise cmdWarning("Aborted")
                if len(objs) > 5:
                    self.printer.out("%sYou request is on more than 5 objets!%s"%(color["yellow"], color["reset"]))
                    self.printer.out("%sAre you really sure?%s"%(color["lred"], color["reset"]))
                    if self.printer.ask("Answer (Sir, yes Sir!): ") != "Sir, yes Sir!":
                        raise cmdWarning("Bad Answer. Aborted")
            # execute action for each object
            for obj in objs:
                self.printer.out("%s%s%s %s%s%s"%(color["lblue"],
                                                  argv[0],
                                                  color["reset"],
                                                  color["yellow"],
                                                  obj["id"],
                                                  color["reset"]))
                try:
                    self.cli.rpc.call(argv[0], "id:%s"%obj["id"])
                except RpcError as e:
                    self.printer.error("RPCError: %s"%str(e))


class Command_start(VmCommand):
    '''Start a stopped vm'''
    def __call__(self, argv):
        self._vm_action(argv, "&role=vm&status=stopped")


class Command_stop(VmCommand):
    '''Stop a running vm'''

    def __call__(self, argv):
        self._vm_action(argv, "&role=vm&status=running")


class Command_destroy(VmCommand):
    '''Force a vm to stop'''

    def __call__(self, argv):
        self._vm_action(argv, "&role=vm&status!=stopped")

Seblu's avatar
Seblu committed
class Command_restart(VmCommand):
    '''Restart a vm'''

    def __call__(self, argv):
        self._vm_action(argv, "&role=vm&status=running")


Seblu's avatar
Seblu committed
class Command_pause(VmCommand):
    '''Pause a running vm'''

    def __call__(self, argv):
        self._vm_action(argv, "&role=vm&status=running")


class Command_resume(VmCommand):
    '''Resume a paused vm'''

    def __call__(self, argv):
        self._vm_action(argv, "&role=vm&status=stalled")