Commit 9d7d4878 authored by Seblu's avatar Seblu
Browse files

implement start/stop/pause/resume/destroy

parent 6cb6ee00
Loading
Loading
Loading
Loading
+22 −15
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ class Cli(object):
        self.prompt = "> "
        self.rpc = None
        self.alias = Alias()
        self.history = History()
        self.history = History(self)

    def start(self, line=""):
        '''Start a CLI'''
@@ -38,7 +38,7 @@ class Cli(object):
        # start readline and load history
        if self.isinteractive:
            import readline
            self.history.readline = readline
            self.readline = readline
            self.history.load(self.settings.get("history", ""))
            self.history.maxsize(self.settings.get("hsize", None))
        # load alias
@@ -159,49 +159,56 @@ class Alias(dict):
                fparser.set("alias", n, v)
            fparser.write(open(filename, "w"))


class History(object):
    '''History class'''
    def __init__(self, readline=None):
        self.readline = readline
    def __init__(self, cli):
        self.cli = cli

    def __nonzero__(self):
        return not self.readline is None
        return not self.cli.readline is None

    def __getattribute__(self, name):
        r = object.__getattribute__(self, "readline")
        if name == "readline":
        r = object.__getattribute__(self, "cli")
        if name == "cli":
            return r
        if r is None:
        if r.readline is None:
            return lambda *a,**k: None
        return object.__getattribute__(self, name)

    def __iter__(self):
        for i in range(1, len(self)):
            yield self.readline.get_history_item(i)
            yield self.cli.readline.get_history_item(i)

    def __len__(self):
        return self.readline.get_current_history_length()
        return self.cli.readline.get_current_history_length()

    def load(self, path):
        '''Load history from a file'''
        try:
            self.readline.read_history_file(path)
            self.cli.readline.read_history_file(path)
        except IOError:
            pass

    def save(self, path):
        '''Save history into path'''
        try:
            self.readline.write_history_file(path)
            self.cli.readline.write_history_file(path)
        except IOError:
            pass

    def maxsize(self, size=None):
        '''Set or return max history size'''
        if size is not None:
            self.readline.set_history_length(size)
        return self.readline.get_history_length()
            self.cli.readline.set_history_length(size)
        return self.cli.readline.get_history_length()

    def removelast(self):
        '''Remove last history line'''
        self.cli.readline.remove_history_item(self.cli.readline.get_current_history_length())

    def clear(self):
        '''Clear history'''
        self.cli.readline.clear_history()


class CliHandler(RpcHandler):
+41 −20
Original line number Diff line number Diff line
@@ -169,30 +169,51 @@ class Command(object):
            #for key, val in item.items():
            #    printer.out("%s: %s "%(key, val))
    cmd_list.usage = "list [tql]"
    cmd_list.desc = "Print information about tags"

    def cmd_stop(self, argv):
        '''Stop an object'''
        if len(argv) == 0:
    def _startstopsdestroypauseresume(self, argv):
        # arg stuff
        if len(argv) == 1:
            raise BadArgument()
        tql = str.join(" ", argv[1:])
        # print tql list result
        items = self.cli.rpc.list(tql)
        if len(items) == 0:
            printer.out("No selected object")
            return
        printer.out("Your request give the following result:")
        for item in items:
            pprint.pprint(item)
        if raw_input("Are you sure? (yes/no)") == "yes":
            items = self.cli.rpc.stop(tql)
    cmd_stop.usage = "stop [tql]"
    cmd_stop.desc = "Stop objects"
            printer.out("%s"%item["id"])
        printer.out("Count: %s"%len(items))
        if raw_input("Are you sure to %s? (yes/NO) "%argv[0]) != "yes":
            raise Exception("Aborted")
        if len(items) > 5:
            if raw_input("You request is on more than 5 objets. Are you really sure to %s its? (Yes, I am) "%argv[0]) != "Yes, I am":
                raise Exception("Aborted")
        self.cli.rpc[argv[0]](tql)


    def cmd_start(self, argv):
        '''Start an object'''
        if len(argv) == 0:
            raise BadArgument()
        tql = str.join("", argv[1:])
        items = self.cli.rpc.list(tql)
        for item in items:
            pprint.pprint(item)
        if raw_input("Are you sure? (yes/no)") == "yes":
            items = self.cli.rpc.start(tql)
        '''Start objects'''
        self._startstopsdestroypauseresume(argv)
    cmd_start.usage = "start [tql]"
    cmd_start.desc = "Start objects"

    def cmd_stop(self, argv):
        '''Stop objects'''
        self._startstopsdestroypauseresume(argv)
    cmd_stop.usage = "stop [tql]"

    def cmd_pause(self, argv):
        '''Pause objects'''
        self._startstopsdestroypauseresume(argv)
    cmd_pause.usage = "pause [tql]"

    def cmd_resume(self, argv):
        '''Resume objects'''
        self._startstopsdestroypauseresume(argv)
    cmd_resume.usage = "resume [tql]"

    def cmd_destroy(self, argv):
        '''Force objects to stop'''
        self._startstopsdestroypauseresume(argv)
    cmd_destroy.usage = "destroy [tql]"
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ Standards-Version: 3.8.0

Package: cc-cli
Architecture: all
Depends: ${misc:Depends}, ${python:Depends}, python (<< 3), python-sjrpc (>= 6)
Depends: ${misc:Depends}, ${python:Depends}, python (<< 3), python-sjrpc (>= 7)
XB-Python-Version: ${python:Versions}
Description: CloudControl CLI
 This package provides the Command Line Interface to CloudControl.