diff --git a/bin/cc-cli b/bin/cc-cli index 7840b22e98e8bcf8eea9fd504b6e708db58362ed..df03cb5af735708496f3838cd15223798fa76dc5 100755 --- a/bin/cc-cli +++ b/bin/cc-cli @@ -9,6 +9,7 @@ import os, os.path import sys import cccli from cccli import printer,cli +from cccli.clierror import * import optparse import ConfigParser import pprint @@ -16,16 +17,20 @@ import re import warnings import getpass -settings = {} +settings = { + "port": "1984", + "timeout": "5" + } try: # parse rc file - if "HOME" in os.environ and os.access("%s/.cc-cli.conf"%os.environ["HOME"], os.R_OK): - fparser = ConfigParser.SafeConfigParser() - fparser.read("%s/.cc-cli.conf"%os.environ["HOME"]) + if "HOME" in os.environ: settings["alias"] = "%s/.cc-cli.conf"%os.environ["HOME"] - if fparser.has_section("cli"): - settings.update(fparser.items("cli")) + if os.access("%s/.cc-cli.conf"%os.environ["HOME"], os.R_OK): + fparser = ConfigParser.SafeConfigParser() + fparser.read("%s/.cc-cli.conf"%os.environ["HOME"]) + if fparser.has_section("cli"): + settings.update(fparser.items("cli")) # parse env if "CC_SERVER" in os.environ: @@ -62,25 +67,23 @@ try: dest="port", default="1984", help="server port") + oparser.add_option("-t", "--timeout", + action="store", + dest="timeout", + default="10", + help="connection timeout") + + (options, args) = oparser.parse_args() # options handling - for i in ("debug", "login", "server", "port"): + for i in ("debug", "login", "server", "port", "timeout"): if hasattr(options, i): o = getattr(options, i) - if o != "": + if o: settings[i] = o - # show usage if no default conf and no server given - if "login" not in settings or "server" not in settings: - oparser.print_help() - sys.exit(1) - - # remove pass to prevent stupid guy - #if "pass" in settings: - # del settings["pass"] - # debug stuff if "debug" in settings: cccli.debug = bool(settings["debug"]) @@ -89,30 +92,42 @@ try: printer.debug("Debugging on") printer.debug("Settings: %s"%settings) - # checking needs - if settings["server"] == "": - raise Exception("Invalid server name/ip") + # checking server name + if "server" not in settings: + raise BadArgument("No server address") + + # check port value try: settings["port"] = int(settings["port"]) except: - raise Exception("Invalid port number") + raise BadArgument("Invalid port number") - # asking for login - if settings["login"] == "": + # check timeout value + try: + settings["timeout"] = int(settings["timeout"]) + except: + raise BadArgument("Invalid timeout") + + # check login + if "login" not in settings: settings["login"] = raw_input("Login: ") - # asking for password - if settings["pass"] == "": + # check password + if "pass" not in settings: settings["pass"] = getpass.getpass("Password: ") # start cli cli = cli.Cli(settings, args) cli.start() +except BadArgument, e: + printer.error("Bad Argument: %s"%str(e)) + oparser.print_help() except KeyboardInterrupt: exit(1) except Exception, e: if cccli.debug: printer.fatal(str(e), exitcode=-1) - printer.warn("This is a not expected error, please report it!") raise + printer.warn("This is a not expected error, please report it!") printer.fatal(str(e)) + diff --git a/cccli/cli.py b/cccli/cli.py index f65d96a05915e1177dc0db8a76350bf57954030e..f915d8e74d421b4ae7c26a3005a4a39f602b0e5c 100644 --- a/cccli/cli.py +++ b/cccli/cli.py @@ -24,12 +24,10 @@ import re import readline class Cli(object): - def __init__(self, settings, args): self._settings = settings - if "alias" in settings: - self.alias = Alias(settings["alias"]) - self.alias.load() + self.alias = Alias(settings.get("alias", "")) + self.alias.load() self._interactive = sys.stderr.isatty() and sys.stdin.isatty() self._prompt = "> " self._commands = args @@ -44,7 +42,7 @@ class Cli(object): # run parsing args if len(self._commands) > 0: for c in self._commands: - self._parse(c) + self._parse_line(c) elif self._interactive: self._interactive_parser() else: @@ -53,9 +51,10 @@ class Cli(object): def _connect(self): printer.debug("Connecting...") rpcc = SimpleRpcClient.from_addr(self._settings["server"], - self._settings["port"], - enable_ssl=True, - on_disconnect=self._on_disconnect + self._settings["port"], + enable_ssl=True, + on_disconnect=self._on_disconnect, + timeout=self._settings["timeout"] ) rpcc.start(daemonize=True) self.rpc = ConnectionProxy(rpcc) @@ -108,7 +107,7 @@ class Cli(object): p.wait() ret = p.returncode elif (cmd[0] == "?"): - Command("help").call() + Command(["help"], self).call() else: self._parse_command(cmd) @@ -145,7 +144,6 @@ class Cli(object): '''Lex command argument''' return string.split(" ") - class Alias(dict): ''' Alias wrapper''' def __init__(self, filename): diff --git a/cccli/command.py b/cccli/command.py index 19c630207ca0208cfaa25116078695083e3fc35c..60591714bb262504c8926d4c023826db0957d062 100644 --- a/cccli/command.py +++ b/cccli/command.py @@ -15,7 +15,7 @@ class Command(object): raise BadCommand() # check valid command chars if not re.match("^[a-zA-Z0-9]+", argv[0]): - raise BadCommmand("Invalid command name") + raise BadCommand("Invalid command name") cmdlist = [ x[4:] for x in dir(self) if x.startswith("cmd_") ] matchlist = [ x for x in cmdlist if re.match("%s.+"%argv[0], x) ] if argv[0] in cmdlist: diff --git a/debian/control b/debian/control index 71cbc3f6d850c2be2836f52b78f4564de1d37346..b458b9afd394c59b8aa56193a2bc9ac502972cc7 100644 --- a/debian/control +++ b/debian/control @@ -9,7 +9,7 @@ Standards-Version: 3.8.0 Package: cc-cli Architecture: all -Depends: ${misc:Depends}, ${python:Depends}, python (<< 3), python-sjrpc (>= 5) +Depends: ${misc:Depends}, ${python:Depends}, python (<< 3), python-sjrpc (>= 6) XB-Python-Version: ${python:Versions} Description: CloudControl CLI This package provides the Command Line Interface to CloudControl.