Commit 460c8634 authored by Seblu's avatar Seblu
Browse files

new start/stop/pause/resume commands

remove global --force-yes options
parent 0ff976c3
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -90,8 +90,6 @@ try:
                       help="History file")
    oparser.add_option("--history-size",action="store",dest="hsize",
                       help="History max entry count")
    oparser.add_option("--force-yes",action="store_true",dest="forceyes",
                       help="Answer Yes to all questions (Dangerous)")
    (options, args) = oparser.parse_args()

    # options handling
+0 −1
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@ class Cli(object):
        self.interactive = sys.stderr.isatty() and sys.stdin.isatty()
        # start printer and load history
        self.printer = Printer(self.interactive,
                               self.settings.get("forceyes", False),
                               historyfile=self.settings.get("history", ""),
                               historysize=self.settings.get("hsize", None))
        # load alias
+69 −39
Original line number Diff line number Diff line
@@ -192,7 +192,7 @@ class Command(object):
            self._list_table(objs)
        else:
            self._list(objs)
    cmd_list.usage = "list [tql]"
    cmd_list.usage = "list [-t] [--help] tql"

    def _list(self, objs):
        for o in objs:
@@ -221,60 +221,90 @@ class Command(object):
                self.printer.out(str(obj.get(t, "")).ljust(v) ,nl=" ")
            self.printer.out()


    def _vm_action(self, argv):
        # arg stuff
        if len(argv) == 1:
    def _vm_action(self, argv, filters=None):
        '''All command about vm are the same'''
        try:
            oparser = OptionParser(prog=argv[0])
            oparser.add_option("--raw", action="store_true", dest="raw",
                               help="Don't append filter on request")
            oparser.add_option("--direct", action="store_true", dest="direct",
                               help="Directly send tql to server (don't list before)")
            oparser.add_option("--force", action="store_true", dest="force",
                               help="Don't ask confirmation (Dangerous)")
            (options, args) = oparser.parse_args(argv[1:])
        except SystemExit:
            return
        if len(args) == 0:
            raise cmdBadArgument()
        tql = str.join("", argv[1:])
        # print tql list result
        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("No selected object")
        self.printer.out("You request give the following result:")
                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)))
        if self.printer.ask("Are you sure to %s%s%s these %s id? (yes/NO) "%(color["lred"],
                                                                             argv[0],
                                                                             color["reset"],
                                                                             len(objs)), "yes") != "yes":
            # 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:
            if self.printer.ask("You request is on more than 5 objets. Are you really sure to %s its? (Sir, yes Sir!) "%argv[0], "Sir, yes Sir!") != "Sir, yes Sir!":
                raise cmdWarning("Aborted")
                    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],tql)
                    self.cli.rpc.call(argv[0], "id:%s"%obj["id"])
                except RpcError as e:
            raise cmdError("RPCError: %s"%str(e))
                    self.printer.error("RPCError: %s"%str(e))

    def cmd_start(self, argv):
        '''Start objects'''
        self._vm_action(argv)
    cmd_start.usage = "start [tql]"
        self._vm_action(argv, "&role=vm&status=stopped")
    cmd_start.usage = "start [--raw] [--direct] [--force] [--help] tql"

    def cmd_stop(self, argv):
        '''Stop objects'''
        self._vm_action(argv)
    cmd_stop.usage = "stop [tql]"
        self._vm_action(argv, "&role=vm&status=running")
    cmd_stop.usage = "stop [--raw] [--direct] [--force] [--help] tql"

    def cmd_pause(self, argv):
        '''Pause objects'''
        self._vm_action(argv)
    cmd_pause.usage = "pause [tql]"
        self._vm_action(argv, "&role=vm&status=running")
    cmd_pause.usage = "pause [--raw] [--direct] [--force] [--help] tql"

    def cmd_resume(self, argv):
        '''Resume objects'''
        self._vm_action(argv)
    cmd_resume.usage = "resume [tql]"
        self._vm_action(argv, "&role=vm&status=stalled")
    cmd_resume.usage = "resume [--raw] [--direct] [--force] [--help] tql"

    def cmd_destroy(self, argv):
        '''Force objects to stop'''
        self._vm_action(argv)
    cmd_destroy.usage = "destroy [tql]"
        self._vm_action(argv, "&role=vm&status!=stopped")
    cmd_destroy.usage = "destroy [--raw] [--direct] [--force] [--help] tql"

    def cmd_clear(self, argv):
        '''Clear tty'''
+2 −5
Original line number Diff line number Diff line
@@ -46,12 +46,11 @@ color = {

class Printer(object):
    '''Print relative class'''
    def __init__(self, interactive=False, forceyes=False, historyfile=None, historysize=None):
    def __init__(self, interactive=False, historyfile=None, historysize=None):
        self.readline = None
        self.history = History()
        self.historyfile = historyfile
        self.historysize = historysize
        self.forceyes = forceyes
        if interactive:
            self.setinteractive()

@@ -120,10 +119,8 @@ class Printer(object):
            raise cliError("Unable to ask a password in non-interactive mode")
        return getpass.getpass(prompt)

    def ask(self, prompt, goodans=None):
    def ask(self, prompt):
        '''Used to ask a question. Default answer not saved to history'''
        if self.forceyes and goodans is not None:
            return goodans
        if self.readline is None:
            raise cliError("Unable to ask question in non-interactive mode")
        return self.getline(prompt, history=False)