def build_option_parser(self, description, version): """Return an argparse option parser for this application. Subclasses may override this method to extend the parser with more global options. :param description: full description of the application :paramtype description: str :param version: version number for the application :paramtype version: str """ parser = argparse.ArgumentParser( description=description, add_help=False, ) parser.add_argument( '--version', action='version', version='%(prog)s {0}'.format(version), ) parser.add_argument( '-v', '--verbose', action='count', dest='verbose_level', default=self.DEFAULT_VERBOSE_LEVEL, help='Increase verbosity of output. Can be repeated.', ) parser.add_argument( '-q', '--quiet', action='store_const', dest='verbose_level', const=0, help='suppress output except warnings and errors', ) parser.add_argument( '-H', '--Help', action=HelpAction, nargs=0, default=self, # tricky help="show this help message and exit", ) parser.add_argument( '--debug', default=False, action='store_true', help='show tracebacks on errors', ) # Global arguments parser.add_argument( '--os_auth_strategy', metavar='<auth_strategy>', default=env('OS_AUTH_STRATEGY', default='keystone'), help='Authentication strategy (Env: OS_AUTH_STRATEGY' ', default keystone). For now, any other value will' ' disable the authentication') parser.add_argument( '--os_auth_url', metavar='<auth_url>', default=env('OS_AUTH_URL'), help='Authentication URL (Env: OS_AUTH_URL)') parser.add_argument( '--os_tenant_name', metavar='<auth_tenant_name>', default=env('OS_TENANT_NAME'), help='Authentication tenant name (Env: OS_TENANT_NAME)') parser.add_argument( '--os_username', metavar='<auth_username>', default=utils.env('OS_USERNAME'), help='Authentication username (Env: OS_USERNAME)') parser.add_argument( '--os_password', metavar='<auth_password>', default=utils.env('OS_PASSWORD'), help='Authentication password (Env: OS_PASSWORD)') parser.add_argument( '--os_region_name', metavar='<auth_region_name>', default=env('OS_REGION_NAME'), help='Authentication region name (Env: OS_REGION_NAME)') parser.add_argument( '--os_token', metavar='<token>', default=env('OS_TOKEN'), help='Defaults to env[OS_TOKEN]') parser.add_argument( '--os_url', metavar='<url>', default=env('OS_URL'), help='Defaults to env[OS_URL]') return parser
def build_option_parser(self, description, version): """Return an argparse option parser for this application. Subclasses may override this method to extend the parser with more global options. :param description: full description of the application :paramtype description: str :param version: version number for the application :paramtype version: str """ parser = argparse.ArgumentParser(description=description, add_help=False) parser.add_argument("--version", action="version", version="%(prog)s {0}".format(version)) parser.add_argument( "-v", "--verbose", action="count", dest="verbose_level", default=self.DEFAULT_VERBOSE_LEVEL, help="Increase verbosity of output. Can be repeated.", ) parser.add_argument( "-q", "--quiet", action="store_const", dest="verbose_level", const=0, help="suppress output except warnings and errors", ) parser.add_argument( "-h", "--help", action=HelpAction, nargs=0, default=self, help="show this help message and exit" # tricky ) parser.add_argument("--debug", default=False, action="store_true", help="show tracebacks on errors") # Global arguments parser.add_argument( "--os-auth-strategy", metavar="<auth-strategy>", default=env("OS_AUTH_STRATEGY", default="keystone"), help="Authentication strategy (Env: OS_AUTH_STRATEGY" ", default keystone). For now, any other value will" " disable the authentication", ) parser.add_argument("--os_auth_strategy", help=argparse.SUPPRESS) parser.add_argument( "--os-auth-url", metavar="<auth-url>", default=env("OS_AUTH_URL"), help="Authentication URL (Env: OS_AUTH_URL)", ) parser.add_argument("--os_auth_url", help=argparse.SUPPRESS) parser.add_argument( "--os-tenant-name", metavar="<auth-tenant-name>", default=env("OS_TENANT_NAME"), help="Authentication tenant name (Env: OS_TENANT_NAME)", ) parser.add_argument("--os_tenant_name", help=argparse.SUPPRESS) parser.add_argument( "--os-username", metavar="<auth-username>", default=utils.env("OS_USERNAME"), help="Authentication username (Env: OS_USERNAME)", ) parser.add_argument("--os_username", help=argparse.SUPPRESS) parser.add_argument( "--os-password", metavar="<auth-password>", default=utils.env("OS_PASSWORD"), help="Authentication password (Env: OS_PASSWORD)", ) parser.add_argument("--os_password", help=argparse.SUPPRESS) parser.add_argument( "--os-region-name", metavar="<auth-region-name>", default=env("OS_REGION_NAME"), help="Authentication region name (Env: OS_REGION_NAME)", ) parser.add_argument("--os_region_name", help=argparse.SUPPRESS) parser.add_argument("--os-token", metavar="<token>", default=env("OS_TOKEN"), help="Defaults to env[OS_TOKEN]") parser.add_argument("--os_token", help=argparse.SUPPRESS) parser.add_argument( "--endpoint-type", metavar="<endpoint-type>", default=env("OS_ENDPOINT_TYPE", default="publicURL"), help="Defaults to env[OS_ENDPOINT_TYPE] or publicURL.", ) parser.add_argument("--os-url", metavar="<url>", default=env("OS_URL"), help="Defaults to env[OS_URL]") parser.add_argument("--os_url", help=argparse.SUPPRESS) parser.add_argument( "--insecure", action="store_true", default=env("QUANTUMCLIENT_INSECURE", default=False), help='Explicitly allow quantumclient to perform "insecure" ' "SSL (https) requests. The server's certificate will " "not be verified against any certificate authorities. " "This option should be used with caution.", ) return parser
def build_option_parser(self, description, version): """Return an argparse option parser for this application. Subclasses may override this method to extend the parser with more global options. :param description: full description of the application :paramtype description: str :param version: version number for the application :paramtype version: str """ parser = argparse.ArgumentParser(description=description, add_help=False) parser.add_argument("--version", action="version", version="%(prog)s {0}".format(version)) parser.add_argument( "-v", "--verbose", action="count", dest="verbose_level", default=self.DEFAULT_VERBOSE_LEVEL, help="Increase verbosity of output. Can be repeated.", ) parser.add_argument( "-q", "--quiet", action="store_const", dest="verbose_level", const=0, help="suppress output except warnings and errors", ) parser.add_argument( "-H", "--Help", action=HelpAction, nargs=0, default=self, help="show this help message and exit" # tricky ) parser.add_argument("--debug", default=False, action="store_true", help="show tracebacks on errors") # Global arguments parser.add_argument( "--os-auth-strategy", metavar="<auth-strategy>", default=env("OS_AUTH_STRATEGY", default="keystone"), help="Authentication strategy (Env: OS_AUTH_STRATEGY" ", default keystone). For now, any other value will" " disable the authentication", ) parser.add_argument( "--os-auth-url", metavar="<auth-url>", default=env("OS_AUTH_URL"), help="Authentication URL (Env: OS_AUTH_URL)", ) parser.add_argument( "--os-tenant-name", metavar="<auth-tenant-name>", default=env("OS_TENANT_NAME"), help="Authentication tenant name (Env: OS_TENANT_NAME)", ) parser.add_argument( "--os-username", metavar="<auth-username>", default=utils.env("OS_USERNAME"), help="Authentication username (Env: OS_USERNAME)", ) parser.add_argument( "--os-password", metavar="<auth-password>", default=utils.env("OS_PASSWORD"), help="Authentication password (Env: OS_PASSWORD)", ) parser.add_argument( "--os-region-name", metavar="<auth-region-name>", default=env("OS_REGION_NAME"), help="Authentication region name (Env: OS_REGION_NAME)", ) parser.add_argument( "--os-quantum-api-version", metavar="<quantum-api-version>", default=env("OS_QUANTUM_API_VERSION", default="2.0"), help="QAUNTUM API version, default=2.0 " "(Env: OS_QUANTUM_API_VERSION)", ) parser.add_argument("--os-token", metavar="<token>", default=env("OS_TOKEN"), help="Defaults to env[OS_TOKEN]") parser.add_argument("--os-url", metavar="<url>", default=env("OS_URL"), help="Defaults to env[OS_URL]") return parser
def build_option_parser(self, description, version): """Return an argparse option parser for this application. Subclasses may override this method to extend the parser with more global options. :param description: full description of the application :paramtype description: str :param version: version number for the application :paramtype version: str """ parser = argparse.ArgumentParser( description=description, add_help=False, ) parser.add_argument( '--version', action='version', version=__version__, ) parser.add_argument( '-v', '--verbose', action='count', dest='verbose_level', default=self.DEFAULT_VERBOSE_LEVEL, help='Increase verbosity of output. Can be repeated.', ) parser.add_argument( '-q', '--quiet', action='store_const', dest='verbose_level', const=0, help='suppress output except warnings and errors', ) parser.add_argument( '-h', '--help', action=HelpAction, nargs=0, default=self, # tricky help="show this help message and exit", ) parser.add_argument( '--debug', default=False, action='store_true', help='show tracebacks on errors', ) # Global arguments parser.add_argument( '--os-auth-strategy', metavar='<auth-strategy>', default=env('OS_AUTH_STRATEGY', default='keystone'), help='Authentication strategy (Env: OS_AUTH_STRATEGY' ', default keystone). For now, any other value will' ' disable the authentication') parser.add_argument( '--os_auth_strategy', help=argparse.SUPPRESS) parser.add_argument( '--os-auth-url', metavar='<auth-url>', default=env('OS_AUTH_URL'), help='Authentication URL (Env: OS_AUTH_URL)') parser.add_argument( '--os_auth_url', help=argparse.SUPPRESS) parser.add_argument( '--os-tenant-name', metavar='<auth-tenant-name>', default=env('OS_TENANT_NAME'), help='Authentication tenant name (Env: OS_TENANT_NAME)') parser.add_argument( '--os_tenant_name', help=argparse.SUPPRESS) parser.add_argument( '--os-username', metavar='<auth-username>', default=utils.env('OS_USERNAME'), help='Authentication username (Env: OS_USERNAME)') parser.add_argument( '--os_username', help=argparse.SUPPRESS) parser.add_argument( '--os-password', metavar='<auth-password>', default=utils.env('OS_PASSWORD'), help='Authentication password (Env: OS_PASSWORD)') parser.add_argument( '--os_password', help=argparse.SUPPRESS) parser.add_argument( '--os-region-name', metavar='<auth-region-name>', default=env('OS_REGION_NAME'), help='Authentication region name (Env: OS_REGION_NAME)') parser.add_argument( '--os_region_name', help=argparse.SUPPRESS) parser.add_argument( '--os-token', metavar='<token>', default=env('OS_TOKEN'), help='Defaults to env[OS_TOKEN]') parser.add_argument( '--os_token', help=argparse.SUPPRESS) parser.add_argument( '--endpoint-type', metavar='<endpoint-type>', default=env('OS_ENDPOINT_TYPE', default='publicURL'), help='Defaults to env[OS_ENDPOINT_TYPE] or publicURL.') parser.add_argument( '--os-url', metavar='<url>', default=env('OS_URL'), help='Defaults to env[OS_URL]') parser.add_argument( '--os_url', help=argparse.SUPPRESS) parser.add_argument( '--insecure', action='store_true', default=env('QUANTUMCLIENT_INSECURE', default=False), help="Explicitly allow quantumclient to perform \"insecure\" " "SSL (https) requests. The server's certificate will " "not be verified against any certificate authorities. " "This option should be used with caution.") return parser
def build_option_parser(self, description, version): """Return an argparse option parser for this application. Subclasses may override this method to extend the parser with more global options. :param description: full description of the application :paramtype description: str :param version: version number for the application :paramtype version: str """ parser = argparse.ArgumentParser( description=description, add_help=False, ) parser.add_argument( '--version', action='version', version='%(prog)s {0}'.format(version), ) parser.add_argument( '-v', '--verbose', action='count', dest='verbose_level', default=self.DEFAULT_VERBOSE_LEVEL, help='Increase verbosity of output. Can be repeated.', ) parser.add_argument( '-q', '--quiet', action='store_const', dest='verbose_level', const=0, help='suppress output except warnings and errors', ) parser.add_argument( '-h', '--help', action=HelpAction, nargs=0, default=self, # tricky help="show this help message and exit", ) parser.add_argument( '--debug', default=False, action='store_true', help='show tracebacks on errors', ) # Global arguments parser.add_argument( '--os-auth-strategy', metavar='<auth-strategy>', default=env('OS_AUTH_STRATEGY', default='keystone'), help='Authentication strategy (Env: OS_AUTH_STRATEGY' ', default keystone). For now, any other value will' ' disable the authentication') parser.add_argument( '--os_auth_strategy', help=argparse.SUPPRESS) parser.add_argument( '--os-auth-url', metavar='<auth-url>', default=env('OS_AUTH_URL'), help='Authentication URL (Env: OS_AUTH_URL)') parser.add_argument( '--os_auth_url', help=argparse.SUPPRESS) parser.add_argument( '--os-tenant-name', metavar='<auth-tenant-name>', default=env('OS_TENANT_NAME'), help='Authentication tenant name (Env: OS_TENANT_NAME)') parser.add_argument( '--os_tenant_name', help=argparse.SUPPRESS) parser.add_argument( '--os-username', metavar='<auth-username>', default=utils.env('OS_USERNAME'), help='Authentication username (Env: OS_USERNAME)') parser.add_argument( '--os_username', help=argparse.SUPPRESS) parser.add_argument( '--os-password', metavar='<auth-password>', default=utils.env('OS_PASSWORD'), help='Authentication password (Env: OS_PASSWORD)') parser.add_argument( '--os_password', help=argparse.SUPPRESS) parser.add_argument( '--os-region-name', metavar='<auth-region-name>', default=env('OS_REGION_NAME'), help='Authentication region name (Env: OS_REGION_NAME)') parser.add_argument( '--os_region_name', help=argparse.SUPPRESS) parser.add_argument( '--os-token', metavar='<token>', default=env('OS_TOKEN'), help='Defaults to env[OS_TOKEN]') parser.add_argument( '--os_token', help=argparse.SUPPRESS) parser.add_argument( '--os-url', metavar='<url>', default=env('OS_URL'), help='Defaults to env[OS_URL]') parser.add_argument( '--os_url', help=argparse.SUPPRESS) return parser
def main(): usagestr = "Usage: %prog [OPTIONS] <command> [args]" parser = OptionParser(usage=usagestr) parser.add_option("-H", "--host", dest="host", type="string", default="127.0.0.1", help="ip address of api host") parser.add_option("-p", "--port", dest="port", type="int", default=9696, help="api poort") parser.add_option("-s", "--ssl", dest="ssl", action="store_true", default=False, help="use ssl") parser.add_option("--debug", dest="debug", action="store_true", default=False, help="print debugging output") parser.add_option("-f", "--logfile", dest="logfile", type="string", default="syslog", help="log file path") parser.add_option("-t", "--token", dest="token", type="string", default=None, help="authentication token") parser.add_option( '--version', default=utils.env('QUANTUM_VERSION', default=DEFAULT_QUANTUM_VERSION), help='Accepts 1.1 and 1.0, defaults to env[QUANTUM_VERSION].') options, args = parser.parse_args() if options.debug: LOG.setLevel(logging.DEBUG) else: LOG.setLevel(logging.WARN) if options.logfile == "syslog": LOG.addHandler(logging.handlers.SysLogHandler(address='/dev/log')) else: LOG.addHandler(logging.handlers.WatchedFileHandler(options.logfile)) # Set permissions on log file os.chmod(options.logfile, 0644) version = options.version if not version in commands: LOG.error("Unknown API version specified:%s", version) parser.print_help() sys.exit(1) if len(args) < 1: parser.print_help() help(version) sys.exit(1) cmd = args[0] if cmd not in commands[version].keys(): LOG.error("Unknown command: %s" % cmd) help(version) sys.exit(1) # Build argument list for CLI command # The argument list will include the version number as well args, filters = build_cmd(cmd, commands[version][cmd]["args"], commands[version][cmd].get("filters", None), args[1:], options.version) if not args: sys.exit(1) LOG.info("Executing command \"%s\" with args: %s" % (cmd, args)) client = instantiate_client(options.host, options.port, options.ssl, args[0], options.token, options.version) # append filters to arguments # this will allow for using the same prototype for v10 and v11 # TODO: Use **kwargs instead of *args (keyword is better than positional) if filters: args.append(filters) commands[version][cmd]["func"](client, *args) LOG.info("Command execution completed") sys.exit(0)