Loading cccli/cli.py +2 −0 Original line number Diff line number Diff line Loading @@ -145,6 +145,8 @@ class Cli(object): # execute command self.printer.debug("argv: %s"%argv) self.commands(argv) except cmdExit: pass except cmdBadArgument as e: if str(e): self.printer.error("Bad argument: %s."%str(e)) Loading cccli/command/account.py +14 −36 Original line number Diff line number Diff line Loading @@ -28,53 +28,31 @@ class Command_addaccount(Command): class Command_delaccount(TqlCommand): '''Delete an account''' def __init__(self, cli, argv0): TqlCommand.__init__(self, cli, argv0) def __call__(self, argv): try: (options, args) = self.option.parse_args(argv[1:]) except SystemExit: return if len(args) != 1: raise cmdBadArgument() try: d = self.cli.rpc.call("delaccount", args[0]) if options.status: self.show_status(d) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) self.parse_args(argv) if len(self.args) != 1: raise cmdBadArgument("<tql>") self.rpccall("delaccount", self.args[0]) class Command_close(Command): class Command_close(TqlCommand): '''Disable accounts''' def __call__(self, argv): if len(argv) != 2: raise cmdBadArgument() try: self.cli.rpc.call("close", argv[1]) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) def usage(self): return "Usage: close <tql>" self.parse_args(argv) if len(self.args) != 1: raise cmdBadArgument("<tql>") self.rpccall("close", self.args[0]) class Command_declose(Command): class Command_declose(TqlCommand): '''Enable accounts''' def __call__(self, argv): if len(argv) != 2: raise cmdBadArgument() try: self.cli.rpc.call("declose", argv[1]) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) def usage(self): return "Usage: declose <tql>" self.parse_args(argv) if len(self.args) != 1: raise cmdBadArgument("<tql>") self.rpccall("declose", self.args[0]) class Command_passwd(Command): Loading cccli/command/command.py +65 −17 Original line number Diff line number Diff line Loading @@ -5,6 +5,8 @@ CloudControl CLI command module ''' from cccli.exception import * from sjrpc.core.exceptions import * from cccli.printer import Printer, color from optparse import OptionParser Loading @@ -29,13 +31,40 @@ class Command(object): class OptionCommand(Command): '''Add options parser to Command''' class OptionCommandParser(OptionParser): '''Parser of Option for OptionCommand''' def error(self, e): raise cmdBadArgument(e) def exit(self): raise cmdExit() def __init__(self, cli, argv0): Command.__init__(self, cli, argv0) self.option = OptionParser(prog=argv0) self.optionparser = OptionCommand.OptionCommandParser(prog=argv0) self.options = None self.args = list() def usage(self): return self.option.format_help().strip() '''Return usage string''' return self.optionparser.format_help().strip() def parse_args(self, argv): '''Wrapper to parse_args''' (self.options, self.args) = self.optionparser.parse_args(argv[1:]) def add_option(self, *args, **kwargs): '''Proxy to OptionParser''' self.optionparser.add_option(*args, **kwargs) def remove_option(self, *args, **kwargs): '''Proxy to OptionParser''' self.optionparser.remove_option(*args, **kwargs) def set_usage(self, *args, **kwargs): '''Proxy to OptionParser''' self.optionparser.set_usage(*args, **kwargs) class TqlCommand(OptionCommand): '''Add Tql stuff to Command''' Loading @@ -43,28 +72,47 @@ class TqlCommand(OptionCommand): def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) self.rpc = cli.rpc self.option.set_usage("%prog [options] <tql>") self.set_usage("%prog [options] <tql>") # set tql status stuff self.option.add_option("-s", "--status", action="store_true", dest="status", self.add_option("-s", "--status", action="store_true", dest="status", help="Show status of each tql object") # set tagdisplay stuff self.tdr = self.cli.tagdisplay.resolve self.tdc = self.cli.tagdisplay.color self.option.add_option("-n", "--no-tagdisplay", action="callback", dest="tagdisplay", callback=self.opt_notagdisplay, help="No tagdisplay system") self.tdtc = self.cli.tagdisplay.titlecolor self.add_option("-n", "--no-tagdisplay", action="callback", dest="tagdisplay", callback=self._cb_notagdisplay, help="No tagdisplay custom display") def opt_notagdisplay(self, option, opt, value, parser): def _cb_notagdisplay(self, option, opt, value, parser): '''Callback for option --no-tagdisplay''' self.tdr = lambda tagname, tagvalue: tagvalue self.tdc = lambda tagname: color["reset"] self.tdc = self.cli.tagdisplay.default_color self.tdtc = self.cli.tagdisplay.default_titlecolor def rpccall(self, *args, **kwargs): '''Call a RPC method an show tql return''' try: d = self.rpc.call(*args, **kwargs) if self.options.status: self.show_status(d) return d except RpcError as e: raise cmdError("RPCError: %s"%str(e)) def show_status(self, ans): '''Show status of an Tql request''' try: s = "" for o in ans: s = "%sid: %s%s %sstatus: %s%s %smessage:%s%s%s"%( color["reset"], self.tdc("id"), self.tdr("id", o), color["reset"], self.tdc(""), ans[o][0], color["reset"], self.tdc(""), ans[o][1], s += "%sid: %s%s %sstatus: %s%s %smessage:%s%s%s"%( self.tdtc("id"), self.tdc("id"), self.tdr("id", o), self.tdtc("status"), self.tdc("status"), ans[o][0], self.tdtc("message"), self.tdc("message"), ans[o][1], color["reset"]) if s: self.printer.out(s) except Exception: pass cccli/command/host.py +12 −13 Original line number Diff line number Diff line Loading @@ -21,27 +21,26 @@ class Command_exec(Command): def usage(self): return "Usage: exec <tql> <command>" class Command_shutdown(OptionCommand): '''Shutdown a physical host''' def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) self.option.set_usage("%prog [options] <tql>") self.option.add_option("-r", "--reboot", action="store_true", dest="reboot", self.set_usage("%prog [options] <tql>") self.add_option("-r", "--reboot", action="store_true", dest="reboot", help="Reboot after shutdown (default)") self.option.add_option("-H", "--halt", action="store_false", dest="reboot", self.add_option("-H", "--halt", action="store_false", dest="reboot", help="Halt after shutdown") self.option.add_option("-n", action="store_false", dest="graceful", default=True, self.add_option("-n", action="store_false", dest="graceful", default=True, help="do not go through init but go down real fast") def __call__(self, argv): try: (options, args) = self.option.parse_args(argv[1:]) except SystemExit: return if len(args) != 1: self.parse_args(argv) if len(self.args) != 1: raise cmdBadArgument() try: self.cli.rpc.call("shutdown", args[0], options.reboot, options.graceful) self.cli.rpc.call("shutdown", self.args[0], self.options.reboot, self.options.graceful) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) cccli/command/list.py +14 −17 Original line number Diff line number Diff line Loading @@ -15,36 +15,33 @@ class Command_list(OptionCommand): def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) self.option.set_usage("%prog [options] [tql]") self.option.add_option("-t", action="store_true", dest="table", self.set_usage("%prog [options] [tql]") self.add_option("-t", action="store_true", dest="table", help="column aligment display") self.option.add_option("-l", action="store_true", dest="align", self.add_option("-l", action="store_true", dest="align", help="line aligment display") self.option.add_option("-n", "--no-tagdisplay", action="store_false", dest="tagdisplay", default=True, self.add_option("-n", "--no-tagdisplay", action="store_false", dest="tagdisplay", default=True, help="No tag display system") def __call__(self, argv): try: (options, args) = self.option.parse_args(argv[1:]) except SystemExit: return if options.tagdisplay: self.parse_args(argv) if self.options.tagdisplay: self.td = self.cli.tagdisplay.resolve self.tc = self.cli.tagdisplay.color else: self.td = lambda tagname, tagvalue: unicode(tagvalue) self.tc = lambda tagname: color["reset"] if len(args) == 0: args.append("") if len(self.args) == 0: self.args.append("") try: objs = self.cli.rpc.call("list", str.join("", args)) objs = self.cli.rpc.call("list", str.join("", self.args)) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) if len(objs) == 0: return if options.align: if self.options.align: self._list_align(objs) elif options.table: elif self.options.table: self._list_table(objs) else: self._trivial_list(objs) Loading Loading
cccli/cli.py +2 −0 Original line number Diff line number Diff line Loading @@ -145,6 +145,8 @@ class Cli(object): # execute command self.printer.debug("argv: %s"%argv) self.commands(argv) except cmdExit: pass except cmdBadArgument as e: if str(e): self.printer.error("Bad argument: %s."%str(e)) Loading
cccli/command/account.py +14 −36 Original line number Diff line number Diff line Loading @@ -28,53 +28,31 @@ class Command_addaccount(Command): class Command_delaccount(TqlCommand): '''Delete an account''' def __init__(self, cli, argv0): TqlCommand.__init__(self, cli, argv0) def __call__(self, argv): try: (options, args) = self.option.parse_args(argv[1:]) except SystemExit: return if len(args) != 1: raise cmdBadArgument() try: d = self.cli.rpc.call("delaccount", args[0]) if options.status: self.show_status(d) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) self.parse_args(argv) if len(self.args) != 1: raise cmdBadArgument("<tql>") self.rpccall("delaccount", self.args[0]) class Command_close(Command): class Command_close(TqlCommand): '''Disable accounts''' def __call__(self, argv): if len(argv) != 2: raise cmdBadArgument() try: self.cli.rpc.call("close", argv[1]) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) def usage(self): return "Usage: close <tql>" self.parse_args(argv) if len(self.args) != 1: raise cmdBadArgument("<tql>") self.rpccall("close", self.args[0]) class Command_declose(Command): class Command_declose(TqlCommand): '''Enable accounts''' def __call__(self, argv): if len(argv) != 2: raise cmdBadArgument() try: self.cli.rpc.call("declose", argv[1]) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) def usage(self): return "Usage: declose <tql>" self.parse_args(argv) if len(self.args) != 1: raise cmdBadArgument("<tql>") self.rpccall("declose", self.args[0]) class Command_passwd(Command): Loading
cccli/command/command.py +65 −17 Original line number Diff line number Diff line Loading @@ -5,6 +5,8 @@ CloudControl CLI command module ''' from cccli.exception import * from sjrpc.core.exceptions import * from cccli.printer import Printer, color from optparse import OptionParser Loading @@ -29,13 +31,40 @@ class Command(object): class OptionCommand(Command): '''Add options parser to Command''' class OptionCommandParser(OptionParser): '''Parser of Option for OptionCommand''' def error(self, e): raise cmdBadArgument(e) def exit(self): raise cmdExit() def __init__(self, cli, argv0): Command.__init__(self, cli, argv0) self.option = OptionParser(prog=argv0) self.optionparser = OptionCommand.OptionCommandParser(prog=argv0) self.options = None self.args = list() def usage(self): return self.option.format_help().strip() '''Return usage string''' return self.optionparser.format_help().strip() def parse_args(self, argv): '''Wrapper to parse_args''' (self.options, self.args) = self.optionparser.parse_args(argv[1:]) def add_option(self, *args, **kwargs): '''Proxy to OptionParser''' self.optionparser.add_option(*args, **kwargs) def remove_option(self, *args, **kwargs): '''Proxy to OptionParser''' self.optionparser.remove_option(*args, **kwargs) def set_usage(self, *args, **kwargs): '''Proxy to OptionParser''' self.optionparser.set_usage(*args, **kwargs) class TqlCommand(OptionCommand): '''Add Tql stuff to Command''' Loading @@ -43,28 +72,47 @@ class TqlCommand(OptionCommand): def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) self.rpc = cli.rpc self.option.set_usage("%prog [options] <tql>") self.set_usage("%prog [options] <tql>") # set tql status stuff self.option.add_option("-s", "--status", action="store_true", dest="status", self.add_option("-s", "--status", action="store_true", dest="status", help="Show status of each tql object") # set tagdisplay stuff self.tdr = self.cli.tagdisplay.resolve self.tdc = self.cli.tagdisplay.color self.option.add_option("-n", "--no-tagdisplay", action="callback", dest="tagdisplay", callback=self.opt_notagdisplay, help="No tagdisplay system") self.tdtc = self.cli.tagdisplay.titlecolor self.add_option("-n", "--no-tagdisplay", action="callback", dest="tagdisplay", callback=self._cb_notagdisplay, help="No tagdisplay custom display") def opt_notagdisplay(self, option, opt, value, parser): def _cb_notagdisplay(self, option, opt, value, parser): '''Callback for option --no-tagdisplay''' self.tdr = lambda tagname, tagvalue: tagvalue self.tdc = lambda tagname: color["reset"] self.tdc = self.cli.tagdisplay.default_color self.tdtc = self.cli.tagdisplay.default_titlecolor def rpccall(self, *args, **kwargs): '''Call a RPC method an show tql return''' try: d = self.rpc.call(*args, **kwargs) if self.options.status: self.show_status(d) return d except RpcError as e: raise cmdError("RPCError: %s"%str(e)) def show_status(self, ans): '''Show status of an Tql request''' try: s = "" for o in ans: s = "%sid: %s%s %sstatus: %s%s %smessage:%s%s%s"%( color["reset"], self.tdc("id"), self.tdr("id", o), color["reset"], self.tdc(""), ans[o][0], color["reset"], self.tdc(""), ans[o][1], s += "%sid: %s%s %sstatus: %s%s %smessage:%s%s%s"%( self.tdtc("id"), self.tdc("id"), self.tdr("id", o), self.tdtc("status"), self.tdc("status"), ans[o][0], self.tdtc("message"), self.tdc("message"), ans[o][1], color["reset"]) if s: self.printer.out(s) except Exception: pass
cccli/command/host.py +12 −13 Original line number Diff line number Diff line Loading @@ -21,27 +21,26 @@ class Command_exec(Command): def usage(self): return "Usage: exec <tql> <command>" class Command_shutdown(OptionCommand): '''Shutdown a physical host''' def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) self.option.set_usage("%prog [options] <tql>") self.option.add_option("-r", "--reboot", action="store_true", dest="reboot", self.set_usage("%prog [options] <tql>") self.add_option("-r", "--reboot", action="store_true", dest="reboot", help="Reboot after shutdown (default)") self.option.add_option("-H", "--halt", action="store_false", dest="reboot", self.add_option("-H", "--halt", action="store_false", dest="reboot", help="Halt after shutdown") self.option.add_option("-n", action="store_false", dest="graceful", default=True, self.add_option("-n", action="store_false", dest="graceful", default=True, help="do not go through init but go down real fast") def __call__(self, argv): try: (options, args) = self.option.parse_args(argv[1:]) except SystemExit: return if len(args) != 1: self.parse_args(argv) if len(self.args) != 1: raise cmdBadArgument() try: self.cli.rpc.call("shutdown", args[0], options.reboot, options.graceful) self.cli.rpc.call("shutdown", self.args[0], self.options.reboot, self.options.graceful) except RpcError as e: raise cmdError("RPCError: %s"%str(e))
cccli/command/list.py +14 −17 Original line number Diff line number Diff line Loading @@ -15,36 +15,33 @@ class Command_list(OptionCommand): def __init__(self, cli, argv0): OptionCommand.__init__(self, cli, argv0) self.option.set_usage("%prog [options] [tql]") self.option.add_option("-t", action="store_true", dest="table", self.set_usage("%prog [options] [tql]") self.add_option("-t", action="store_true", dest="table", help="column aligment display") self.option.add_option("-l", action="store_true", dest="align", self.add_option("-l", action="store_true", dest="align", help="line aligment display") self.option.add_option("-n", "--no-tagdisplay", action="store_false", dest="tagdisplay", default=True, self.add_option("-n", "--no-tagdisplay", action="store_false", dest="tagdisplay", default=True, help="No tag display system") def __call__(self, argv): try: (options, args) = self.option.parse_args(argv[1:]) except SystemExit: return if options.tagdisplay: self.parse_args(argv) if self.options.tagdisplay: self.td = self.cli.tagdisplay.resolve self.tc = self.cli.tagdisplay.color else: self.td = lambda tagname, tagvalue: unicode(tagvalue) self.tc = lambda tagname: color["reset"] if len(args) == 0: args.append("") if len(self.args) == 0: self.args.append("") try: objs = self.cli.rpc.call("list", str.join("", args)) objs = self.cli.rpc.call("list", str.join("", self.args)) except RpcError as e: raise cmdError("RPCError: %s"%str(e)) if len(objs) == 0: return if options.align: if self.options.align: self._list_align(objs) elif options.table: elif self.options.table: self._list_table(objs) else: self._trivial_list(objs) Loading