def _unregister_opts(self): CONF.reset() category_opt = cfg.SubCommandOpt("category", title="Command categories", help="Available categories") CONF.unregister_opt(category_opt)
category_subparsers = parser.add_subparsers(dest='action') for (action, action_fn) in methods_of(command_object): action = getattr(action_fn, '_cmd_name', action) parser = category_subparsers.add_parser(action) action_kwargs = [] for args, kwargs in getattr(action_fn, 'args', []): parser.add_argument(*args, **kwargs) parser.set_defaults(action_fn=action_fn) parser.set_defaults(action_kwargs=action_kwargs) category_opt = cfg.SubCommandOpt('category', title="Commands", help="Available Commands", handler=add_command_parsers) def get_arg_string(args): arg = None if args[0] == '-': # (Note)zhiteng: args starts with FLAGS.oparser.prefix_chars # is optional args. Notice that cfg module takes care of # actual ArgParser so prefix_chars is always '-'. if args[1] == '-': # This is long optional arg arg = args[2:] else: arg = args[1:] else:
category_subparsers = parser.add_subparsers(dest='action') for (action, action_fn) in methods_of(command_object): parser = category_subparsers.add_parser(action) action_kwargs = [] for args, kwargs in getattr(action_fn, 'args', []): parser.add_argument(*args, **kwargs) parser.set_defaults(action_fn=action_fn) parser.set_defaults(action_kwargs=action_kwargs) CONF.register_cli_opt( cfg.SubCommandOpt('category', title='Command categories', help='Available categories', handler=add_command_parsers)) def get_arg_string(args): arg = None if args[0] == '-': # NOTE(Dinesh_Bhor): args starts with FLAGS.oparser.prefix_chars # is optional args. Notice that cfg module takes care of # actual ArgParser so prefix_chars is always '-'. if args[1] == '-': # This is long optional arg arg = args[2:] else: arg = args[1:] else:
class DBCommand(object): @staticmethod def create_schema(): migration.create_schema() def add_command_parsers(subparsers): parser = subparsers.add_parser("create_schema", help="Create the database schema.") parser.set_defaults(func=DBCommand.create_schema) command_opt = cfg.SubCommandOpt("command", title="Command", help="Available commands", handler=add_command_parsers) def register_sub_command_opts(): cfg.CONF.register_cli_opt(command_opt) def main(): register_sub_command_opts() valid_commands = set([ "create_schema", ]) if not set(sys.argv).intersection(valid_commands): sys.argv.append("create_schema")
# NOTE(cfb): dnsmasq always passes mac, and ip. hostname # is passed if known. We don't care about # hostname, but argparse will complain if we # do not accept it. for action in ['add', 'del', 'old']: parser = subparsers.add_parser(action) parser.add_argument('mac') parser.add_argument('ip') parser.add_argument('hostname', nargs='?', default='') parser.set_defaults(func=globals()[action + '_lease']) CONF.register_cli_opt( cfg.SubCommandOpt('action', title='Action options', help='Available dhcpbridge options', handler=add_action_parsers)) def block_db_access(): class NoDB(object): def __getattr__(self, attr): return self def __call__(self, *args, **kwargs): stacktrace = "".join(traceback.format_stack()) LOG.error(_LE('No db access allowed in nova-dhcpbridge: %s'), stacktrace) raise exception.DBNotAllowed('nova-dhcpbridge') nova.db.api.IMPL = NoDB()
def run(argv, categories): parser = lambda subparsers: _add_command_parsers(categories, subparsers) category_opt = cfg.SubCommandOpt("category", title="Command categories", help="Available categories", handler=parser) CONF.register_cli_opt(category_opt) help_msg = ("Additional custom plugin locations. Multiple files or " "directories may be specified. All plugins in the specified" " directories and subdirectories will be imported. Plugins in" " /opt/rally/plugins and ~/.rally/plugins will always be " "imported.") CONF.register_cli_opt(cfg.ListOpt("plugin-paths", default=os.environ.get( "RALLY_PLUGIN_PATHS"), help=help_msg)) try: rapi = api.API(config_args=argv[1:], skip_db_check=True) except exceptions.RallyException as e: print(e) return(2) if CONF.category.name == "version": print(CONF.version) return(0) if CONF.category.name == "bash-completion": print(_generate_bash_completion_script()) return(0) fn = CONF.category.action_fn fn_args = [encodeutils.safe_decode(arg) for arg in CONF.category.action_args] # api instance always is the first argument fn_args.insert(0, rapi) fn_kwargs = {} for k in CONF.category.action_kwargs: v = getattr(CONF.category, "action_kwarg_" + k) if v is None: continue if isinstance(v, six.string_types): v = encodeutils.safe_decode(v) fn_kwargs[k] = v # call the action with the remaining arguments # check arguments try: validate_args(fn, *fn_args, **fn_kwargs) except MissingArgs as e: # NOTE(mikal): this isn't the most helpful error message ever. It is # long, and tells you a lot of things you probably don't want to know # if you just got a single arg wrong. print(fn.__doc__) CONF.print_help() print("Missing arguments:") for missing in e.missing: for arg in fn.args: if arg[1].get("dest", "").endswith(missing): print(" " + arg[0][0]) break return(1) try: validate_deprecated_args(argv, fn) # skip db check for db and plugin commands if CONF.category.name not in ("db", "plugin"): rapi.check_db_revision() if getattr(fn, "_suppress_warnings", False): with warnings.catch_warnings(): warnings.simplefilter("ignore") ret = fn(*fn_args, **fn_kwargs) else: ret = fn(*fn_args, **fn_kwargs) return(ret) except (IOError, TypeError, ValueError, exceptions.RallyException, jsonschema.ValidationError) as e: if logging.is_debug(): LOG.exception(e) else: print(e) return 1 except sqlalchemy.exc.OperationalError as e: if logging.is_debug(): LOG.exception(e) print(e) print("Looks like Rally can't connect to its DB.") print("Make sure that connection string in rally.conf is proper:") print(CONF.database.connection) return 1 except Exception: print(_("Command failed, please check log for more info")) raise
category_subparsers = parser.add_subparsers(dest='action') for (action, action_fn) in methods_of(command_object): parser = category_subparsers.add_parser(action) action_kwargs = [] for args, kwargs in getattr(action_fn, 'args', []): parser.add_argument(*args, **kwargs) parser.set_defaults(action_fn=action_fn) parser.set_defaults(action_kwargs=action_kwargs) category_opt = cfg.SubCommandOpt('category', title='Command categories', handler=add_command_parsers) def get_arg_string(args): arg = None if args[0] == '-': # (Note)zhiteng: args starts with CONF.oparser.prefix_chars # is optional args. Notice that cfg module takes care of # actual ArgParser so prefix_chars is always '-'. if args[1] == '-': # This is long optional arg arg = args[2:] else: arg = args[1:] else:
parser = subparsers.add_parser('create-armada-app-overrides') parser.set_defaults(func=create_armada_app_overrides_action) parser.add_argument('path', nargs='?') parser.add_argument('app_name', nargs='?') parser.add_argument('namespace', nargs='?') parser = subparsers.add_parser('create-chart-overrides') parser.set_defaults(func=create_chart_override_action) parser.add_argument('path', nargs='?') parser.add_argument('chart_name', nargs='?') parser.add_argument('namespace', nargs='?') CONF.register_cli_opt( cfg.SubCommandOpt('action', title='actions', help='Perform helm override operation', handler=add_action_parsers)) def main(): service.prepare_service(sys.argv) if CONF.action.name == 'create-app-overrides': CONF.action.func(CONF.action.path, CONF.action.app_name, CONF.action.namespace) elif CONF.action.name == 'create-armada-app-overrides': CONF.action.func(CONF.action.path, CONF.action.app_name, CONF.action.namespace) elif CONF.action.name == 'create-chart-overrides': try: CONF.action.func(CONF.action.path, CONF.action.chart_name,
def main(): global exitcode opts = [ cfg.SubCommandOpt('server', title='Server types', help='Available server types', handler=add_command_parsers), cfg.StrOpt('pid-file', metavar='PATH', help='File to use as pid file. Default: ' '/var/run/searchlight/$server.pid.'), cfg.IntOpt('await-child', metavar='DELAY', default=0, help='Period to wait for service death ' 'in order to report exit code ' '(default is to not wait at all).'), cfg.BoolOpt('capture-output', default=False, help='Capture stdout/err in syslog ' 'instead of discarding it.'), cfg.BoolOpt('respawn', default=False, help='Restart service on unexpected death.'), ] CONF.register_cli_opts(opts) config.parse_args(usage=USAGE) @gated_by(CONF.await_child) @gated_by(CONF.respawn) def mutually_exclusive(): sys.stderr.write('--await-child and --respawn are mutually exclusive') sys.exit(1) mutually_exclusive() @gated_by(CONF.respawn) def anticipate_respawn(children): while children: pid, status = os.wait() if pid in children: (pid_file, server, args) = children.pop(pid) running = os.path.exists(pid_file) one_second_ago = time.time() - 1 bouncing = (running and os.path.getmtime(pid_file) >= one_second_ago) if running and not bouncing: args = (pid_file, server, args) new_pid = do_start('Respawn', *args) children[new_pid] = args else: rsn = 'bouncing' if bouncing else 'deliberately stopped' print( _('Suppressed respawn as %(serv)s was %(rsn)s.') % { 'serv': server, 'rsn': rsn }) if CONF.server.command == 'start': children = {} for server in CONF.server.servers: pid_file = get_pid_file(server, CONF.pid_file) args = (pid_file, server, CONF.server.args) pid = do_start('Start', *args) children[pid] = args anticipate_respawn(children) if CONF.server.command == 'status': for server in CONF.server.servers: pid_file = get_pid_file(server, CONF.pid_file) do_check_status(pid_file, server) if CONF.server.command == 'stop': for server in CONF.server.servers: do_stop(server, CONF.server.args) if CONF.server.command == 'shutdown': for server in CONF.server.servers: do_stop(server, CONF.server.args, graceful=True) if CONF.server.command == 'restart': for server in CONF.server.servers: do_stop(server, CONF.server.args) for server in CONF.server.servers: pid_file = get_pid_file(server, CONF.pid_file) do_start('Restart', pid_file, server, CONF.server.args) if CONF.server.command in ('reload', 'force-reload'): for server in CONF.server.servers: pid_file = get_pid_file(server, CONF.pid_file) do_reload(pid_file, server) sys.exit(exitcode)
def add_action_parsers(subparsers): parser = subparsers.add_parser('create-host-overrides') parser.set_defaults(func=create_host_overrides) parser.add_argument('filename', nargs='?') parser = subparsers.add_parser('local-registry-list') parser.set_defaults(func=local_registry_list) parser.add_argument('filename', nargs='?') parser.add_argument('--all-apps', action='store_true', default=False) parser.add_argument('--apps', nargs='*', required=False, default=None) CONF.register_cli_opt( cfg.SubCommandOpt('action', title='actions', help='Perform sysinv operations', handler=add_action_parsers)) def main(): service.prepare_service(sys.argv) if CONF.action.name == 'create-host-overrides': if not CONF.action.filename: LOG.error("filename is required") else: CONF.action.func(CONF.action.filename) elif CONF.action.name == 'local-registry-list': if not CONF.action.filename: LOG.error("filename is required") else:
if CONF.command.info: self.notifier.info({}, CONF.command.event_type, data) if CONF.command.warn: self.notifier.warn({}, CONF.command.event_type, data) if CONF.command.error: self.notifier.error({}, CONF.command.event_type, data) def add_command_parsers(subparsers): for app in APPS: app.add_argument_parser(subparsers) OPTION_LIST = [COMMAND] = [ cfg.SubCommandOpt(name='command', title='Commands', handler=add_command_parsers, help='Available commands') ] APPS = [ServerApp, ClientApp] LOG = logging.getLogger("Service") LOG.setLevel(logging.INFO) ch = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s %(name)s %(levelname)s: %(message)s') ch.setFormatter(formatter) LOG.addHandler(ch) if __name__ == "__main__": CONF.register_cli_opts(OPTION_LIST)
plugin=plugin, version=version)) for arg in args: arg_token = ("--%s" % arg.name if len(arg.name) > 1 else "-%s" % arg.name) version_parser.add_argument(arg_token, dest=arg.name, help=arg.description, default=arg.default, required=arg.required, choices=arg.choices) version_parser.set_defaults(args={arg.name for arg in args}) command_opt = cfg.SubCommandOpt('plugin', title=_('Plugin'), help=_('Available plugins'), handler=add_plugin_parsers) CONF.register_cli_opt(command_opt) def main(): CONF(project='sahara') CONF.reload_config_files() log.setup(CONF, "sahara") LOG.info("Command: {command}".format(command=' '.join(sys.argv))) api.set_logger(LOG) api.set_conf(CONF)
def main(): # FIXME: Split these up into separate components? # host, port, username, password, database cli_opts = [ cfg.SubCommandOpt('command', title="Commands", help="Available commands", handler=add_subcommands), cfg.StrOpt('source', required=False, help='connection URL to the src DB server'), cfg.StrOpt('target', required=False, help='connection URL to the target server'), cfg.StrOpt('batch', required=False, help='YAML file containing connection URLs'), cfg.BoolOpt('exclude-deleted', default=True, help='Exclude table rows marked as deleted. ' 'True by default.'), cfg.IntOpt('chunk-size', default=10000, min=0, help='Number of records to move per chunk. Set to 0 to ' 'disable, default is 10,000.') ] cfg.CONF.register_cli_opts(cli_opts) logging.register_options(cfg.CONF) logging.set_defaults() # read config and initialize logging cfg.CONF(project='psql2mysql') # cfg.CONF.set_override("use_stderr", True) logging.setup(cfg.CONF, 'psql2mysql') # We expect batch file with this syntax: # # keystone: # source: postgresql://keystone:[email protected]/keystone # target: mysql+pymysql://keystone:[email protected]/keystone?charset=utf8 # cinder: # source: postgresql://cinder:[email protected]/cinder # target: if cfg.CONF.batch: try: with open(cfg.CONF.batch, 'r') as f: all_procs = [] for db_name, db in yaml.load(f).iteritems(): all_procs.append( Process(target=db_main_process, args=(db_name, db))) # start all processes and wait until they finish for p in all_procs: p.start() for p in all_procs: p.join() except IOError: print('Batch file "%s" does not exist or cannot be read' % cfg.CONF.batch) sys.exit(2) print("Batch processing done.") sys.exit(0) if not cfg.CONF.source: print("Source database was not specified.") sys.exit(1) check_source_schema(cfg.CONF.source) if cfg.CONF.target: check_target_schema(cfg.CONF.target) try: cfg.CONF.command.func(cfg, cfg.CONF.source, cfg.CONF.target) except Psql2MysqlRuntimeError as e: print(e, file=sys.stderr) sys.exit(1)
def list_cli_opts(): return [ cfg.SubCommandOpt(name="sub_command", handler=sub_commands, help=_("Available commands"), title="syntribos Commands"), cfg.MultiStrOpt("test-types", dest="test_types", short="t", default=[""], sample_default=["SQL", "XSS"], help=_("Test types to run against the target API")), cfg.MultiStrOpt("excluded-types", dest="excluded_types", short="e", default=[""], sample_default=["SQL", "XSS"], help=_("Test types to be excluded from " "current run against the target API")), cfg.BoolOpt("colorize", dest="colorize", short="cl", default=True, help=_("Enable color in syntribos terminal output")), cfg.StrOpt("outfile", short="o", sample_default="out.json", help=_("File to print " "output to")), cfg.StrOpt("format", dest="output_format", short="f", default="json", choices=["json"], ignore_case=True, help=_("The format for outputting results")), cfg.StrOpt("min-severity", dest="min_severity", short="S", default="LOW", choices=syntribos.RANKING, help=_("Select a minimum severity for reported " "defects")), cfg.StrOpt("min-confidence", dest="min_confidence", short="C", default="LOW", choices=syntribos.RANKING, help=_("Select a minimum confidence for reported " "defects")), cfg.BoolOpt("stacktrace", dest="stacktrace", default=True, help=_("Select if Syntribos outputs a stacktrace " " if an exception is raised")), cfg.StrOpt( "custom_root", dest="custom_root", help=_("Filesystem location for syntribos root directory, " "containing logs, templates, payloads, config files. " "Creates directories and skips interactive prompts when " "used with 'syntribos init'"), deprecated_group="init", deprecated_name="custom_install_root") ]
def run(argv, categories): parser = lambda subparsers: _add_command_parsers(categories, subparsers) category_opt = cfg.SubCommandOpt("category", title="Command categories", help="Available categories", handler=parser) CONF.register_cli_opt(category_opt) help_msg = ("Additional custom plugin locations. Multiple files or " "directories may be specified. All plugins in the specified" " directories and subdirectories will be imported. Plugins in" " /opt/rally/plugins and ~/.rally/plugins will always be " "imported.") CONF.register_cli_opt( cfg.ListOpt("plugin-paths", default=os.environ.get("RALLY_PLUGIN_PATHS"), help=help_msg)) try: CONF(argv[1:], project="rally", version=version.version_string(), default_config_files=find_config_files(CONFIG_SEARCH_PATHS)) logging.setup("rally") if not CONF.get("log_config_append"): # The below two lines are to disable noise from request module. The # standard way should be we make such lots of settings on the root # rally. However current oslo codes doesn't support such interface. # So I choose to use a 'hacking' way to avoid INFO logs from # request module where user didn't give specific log configuration. # And we could remove this hacking after oslo.log has such # interface. LOG.debug("INFO logs from urllib3 and requests module are hide.") requests_log = logging.getLogger("requests").logger requests_log.setLevel(logging.WARNING) urllib3_log = logging.getLogger("urllib3").logger urllib3_log.setLevel(logging.WARNING) LOG.debug("urllib3 insecure warnings are hidden.") for warning in ("InsecurePlatformWarning", "SNIMissingWarning", "InsecureRequestWarning"): warning_cls = getattr(urllib3.exceptions, warning, None) if warning_cls is not None: urllib3.disable_warnings(warning_cls) # NOTE(wtakase): This is for suppressing boto error logging. LOG.debug("ERROR log from boto module is hide.") boto_log = logging.getLogger("boto").logger boto_log.setLevel(logging.CRITICAL) except cfg.ConfigFilesNotFoundError as e: cfg_files = e.config_files print(_("Failed to read configuration file(s): %s") % cfg_files) return (2) if CONF.category.name == "version": print(version.version_string()) return (0) if CONF.category.name == "bash-completion": print(_generate_bash_completion_script()) return (0) fn = CONF.category.action_fn fn_args = [ encodeutils.safe_decode(arg) for arg in CONF.category.action_args ] fn_kwargs = {} for k in CONF.category.action_kwargs: v = getattr(CONF.category, "action_kwarg_" + k) if v is None: continue if isinstance(v, six.string_types): v = encodeutils.safe_decode(v) fn_kwargs[k] = v # call the action with the remaining arguments # check arguments try: validate_args(fn, *fn_args, **fn_kwargs) except MissingArgs as e: # NOTE(mikal): this isn't the most helpful error message ever. It is # long, and tells you a lot of things you probably don't want to know # if you just got a single arg wrong. print(fn.__doc__) CONF.print_help() print("Missing arguments:") for missing in e.missing: for arg in fn.args: if arg[1].get("dest", "").endswith(missing): print(" " + arg[0][0]) break return (1) try: for path in CONF.plugin_paths or []: discover.load_plugins(path) validate_deprecated_args(argv, fn) if getattr(fn, "_suppress_warnings", False): with warnings.catch_warnings(): warnings.simplefilter("ignore") ret = fn(*fn_args, **fn_kwargs) else: ret = fn(*fn_args, **fn_kwargs) return (ret) except (IOError, TypeError, ValueError, exceptions.DeploymentNotFound, exceptions.TaskNotFound, jsonschema.ValidationError) as e: if logging.is_debug(): LOG.exception(e) print(e) return 1 except sqlalchemy.exc.OperationalError as e: if logging.is_debug(): LOG.exception(e) print(e) print("Looks like Rally can't connect to its DB.") print("Make sure that connection string in rally.conf is proper:") print(CONF.database.connection) return 1 except Exception: print(_("Command failed, please check log for more info")) raise
def run(argv, categories): parser = lambda subparsers: _add_command_parsers(categories, subparsers) category_opt = cfg.SubCommandOpt("category", title="Command categories", help="Available categories", handler=parser) CONF.register_cli_opt(category_opt) help_msg = ("Additional custom plugin locations. Multiple files or " "directories may be specified. All plugins in the specified" " directories and subdirectories will be imported. Plugins in" " /opt/rally/plugins and ~/.rally/plugins will always be " "imported.") CONF.register_cli_opt( cfg.ListOpt("plugin-paths", default=os.environ.get("RALLY_PLUGIN_PATHS"), help=help_msg)) # NOTE(andreykurilin): this dirty hack is done to unblock the gates. # Currently, we are using oslo.config for CLI purpose (don't do this!) # and it makes the things too complicated. # To discover which CLI method can be affected by warnings and which not # (based on suppress_warnings decorator) we need to obtain a desired # CLI method. It can be done only after initialization of oslo_config # which is located in rally.api.API init method. # Initialization of rally.api.API can produce a warning (for example, # from pymysql), so suppressing of warnings later will not work in such # case (it is what actually had happened now in our CI with the latest # release of PyMySQL). # # https://bitbucket.org/zzzeek/sqlalchemy/issues/4120/mysql-5720-warns-on-tx_isolation try: import pymysql warnings.filterwarnings("ignore", category=pymysql.Warning) except ImportError: pass try: rapi = api.API(config_args=argv[1:], skip_db_check=True) except exceptions.RallyException as e: print(e) return (2) if CONF.category.name == "version": print(CONF.version) return (0) if CONF.category.name == "bash-completion": print(_generate_bash_completion_script()) return (0) fn = CONF.category.action_fn fn_args = [ encodeutils.safe_decode(arg) for arg in CONF.category.action_args ] # api instance always is the first argument fn_args.insert(0, rapi) fn_kwargs = {} for k in CONF.category.action_kwargs: v = getattr(CONF.category, "action_kwarg_" + k) if v is None: continue if isinstance(v, six.string_types): v = encodeutils.safe_decode(v) fn_kwargs[k] = v # call the action with the remaining arguments # check arguments try: validate_args(fn, *fn_args, **fn_kwargs) except MissingArgs as e: # NOTE(mikal): this isn't the most helpful error message ever. It is # long, and tells you a lot of things you probably don't want to know # if you just got a single arg wrong. print(fn.__doc__) CONF.print_help() print("Missing arguments:") for missing in e.missing: for arg in fn.args: if arg[1].get("dest", "").endswith(missing): print(" " + arg[0][0]) break return (1) try: validate_deprecated_args(argv, fn) # skip db check for db and plugin commands if CONF.category.name not in ("db", "plugin"): rapi.check_db_revision() if getattr(fn, "_suppress_warnings", False): with warnings.catch_warnings(): warnings.simplefilter("ignore") ret = fn(*fn_args, **fn_kwargs) else: ret = fn(*fn_args, **fn_kwargs) return ret except (IOError, TypeError, ValueError, exceptions.RallyException, jsonschema.ValidationError) as e: if logging.is_debug(): LOG.exception("Unexpected exception in CLI") else: print(e) return getattr(e, "error_code", 1) except sqlalchemy.exc.OperationalError as e: if logging.is_debug(): LOG.exception("Something went wrong with database") print(e) print("Looks like Rally can't connect to its DB.") print("Make sure that connection string in rally.conf is proper:") print(CONF.database.connection) return 1 except Exception: print("Command failed, please check log for more info") raise
help=val['help'], required=val['required']) # Now options to be registered with oslo_config cli_opts = [ # intentionally long to avoid a conflict with opts from predict, train etc cfg.StrOpt('deepaas_method_output', help="Define an output file, if needed", deprecated_name='deepaas_model_output'), cfg.BoolOpt('deepaas_with_multiprocessing', default=True, help='Activate multiprocessing; default is True'), cfg.SubCommandOpt('methods', title='methods', handler=_add_methods, help='Use \"<method> --help\" to get ' 'more info about options for ' 'each method') ] CONF = cfg.CONF CONF.register_cli_opts(cli_opts) LOG = log.getLogger(__name__) # store DEEPAAS_METHOD output in a file def _store_output(results, out_file): """Function to store model results in the file :param results: what to store (JSON expected) :param out_file: in what file to store
def run(argv, categories): parser = lambda subparsers: _add_command_parsers(categories, subparsers) category_opt = cfg.SubCommandOpt("category", title="Command categories", help="Available categories", handler=parser) CONF.register_cli_opt(category_opt) # help_msg = ("TODO") # CONF.register_cli_opt(cfg.ListOpt("plugin-paths", # default=os.environ.get( # "RALLY_PLUGIN_PATHS"), # help=help_msg)) try: rapi = api.API(config_args=argv[1:], skip_db_check=True) except Exception as e: print(e) return (2) if CONF.category.name == "version": print(CONF.version) return (0) fn = CONF.category.action_fn fn_args = [ encodeutils.safe_decode(arg) for arg in CONF.category.action_args ] # api instance always is the first argument fn_args.insert(0, rapi) fn_kwargs = {} for k in CONF.category.action_kwargs: v = getattr(CONF.category, "action_kwarg_" + k) if v is None: continue if isinstance(v, six.string_types): v = encodeutils.safe_decode(v) fn_kwargs[k] = v # call the action with the remaining arguments # check arguments try: validate_args(fn, *fn_args, **fn_kwargs) except MissingArgs as e: # NOTE(mikal): this isn't the most helpful error message ever. It is # long, and tells you a lot of things you probably don't want to know # if you just got a single arg wrong. print(fn.__doc__) CONF.print_help() print("Missing arguments:") for missing in e.missing: for arg in fn.args: if arg[1].get("dest", "").endswith(missing): print(" " + arg[0][0]) break return (1) try: validate_deprecated_args(argv, fn) # skip db check for db and plugin commands #if CONF.category.name not in ("db", "plugin"): # rapi.check_db_revision() if getattr(fn, "_suppress_warnings", False): with warnings.catch_warnings(): warnings.simplefilter("ignore") ret = fn(*fn_args, **fn_kwargs) else: ret = fn(*fn_args, **fn_kwargs) return ret except Exception: raise Exception
def add_action_parsers(subparsers): # NOTE(cfb): dnsmasq always passes mac, and ip. hostname # is passed if known. We don't care about # hostname, but argparse will complain if we # do not accept it. for action in ['add', 'del', 'old']: parser = subparsers.add_parser(action) parser.add_argument('mac') parser.add_argument('ip') parser.add_argument('hostname', nargs='?', default='') parser.set_defaults(func=globals()[action + '_lease']) CONF.register_cli_opt( cfg.SubCommandOpt('action', title='Action options', help='Available dnsmasq_lease_update options', handler=add_action_parsers)) def main(): # Parse config file and command line options, then start logging # The mac is to be truncated to 17 characters, which is the proper # length of a mac address, in order to handle IPv6 where a DUID # is provided instead of a mac address. The truncated DUID is # then equivalent to the mac address. sysinv_service.prepare_service(sys.argv) LOG = log.getLogger(__name__) if CONF.action.name in ['add', 'del', 'old']: msg = (_("Called '%(action)s' for mac '%(mac)s' with ip '%(ip)s'") % {
"specified by age and granularity, whose value " "must be one of 'days', 'hours', 'minutes' or " "'seconds' (default).")) parser.add_argument('age', type=int, default=30, help=_("Purge action records which were created in " "the specified time period. The time is " "specified by age and granularity. For " "example, granularity=hours and age=2 means " "purging actions created two hours ago. " "Defaults to 30.")) command_opt = cfg.SubCommandOpt('command', title='Commands', help=_('Show available commands.'), handler=add_command_parsers) def main(): logging.register_options(CONF) logging.setup(CONF, 'senlin-manage') CONF.register_cli_opt(command_opt) try: default_config_files = cfg.find_config_files('senlin', 'senlin-engine') CONF(sys.argv[1:], project='senlin', prog='senlin-manage', version=version.version_info.version_string(), default_config_files=default_config_files)
FernetSetup, MappingPurge, MappingEngineTester, PKISetup, SamlIdentityProviderMetadata, TokenFlush, ] def add_command_parsers(subparsers): for cmd in CMDS: cmd.add_argument_parser(subparsers) command_opt = cfg.SubCommandOpt('command', title='Commands', help='Available commands', handler=add_command_parsers) def main(argv=None, config_files=None): CONF.register_cli_opt(command_opt) keystone.conf.configure() sql.initialize() keystone.conf.set_default_for_default_log_levels() CONF(args=argv[1:], project='keystone', version=pbr.version.VersionInfo('keystone').version_string(), usage='%(prog)s [' + '|'.join([cmd.name for cmd in CMDS]) + ']', default_config_files=config_files)
def main1(): CONF.register_cli_opt(cfg.SubCommandOpt('action', handler=add_parsers)) CONF() log_level = logging.WARNING if CONF.verbose is True: log_level = logging.INFO if CONF.debug is True: log_level = logging.DEBUG logging.root.setLevel(log_level) if CONF.log_file is not None: file_log = logging.FileHandler(CONF.log_file) file_log.setFormatter(logging.Formatter(LOG_FORMAT)) logging.root.addHandler(file_log) if not CONF.keystone.auth_url: LOG.error('You must provide a keystone auth' ' url via env[OS_AUTH_URL]') sys.exit(1) if not CONF.keystone.tenant_name: LOG.error('You must provide a tenant name' ' via env[OS_TENANT_NAME]') sys.exit(1) if not CONF.keystone.username: LOG.error('You must provide a username' ' or user id via env[OS_USERNAME]') sys.exit(1) if not CONF.keystone.password: LOG.error('You must provide a password' ' via env[OS_PASSWORD]') sys.exit(1) user_ns = {} auth_url = user_ns['OS_AUTH_URL'] = CONF.keystone.auth_url password = user_ns['OS_PASSWORD'] = CONF.keystone.password tenant = user_ns['OS_TENANT_NAME'] = CONF.keystone.tenant_name username = user_ns['OS_USERNAME'] = CONF.keystone.username endpoint_type = user_ns['OS_ENDPOINT_TYPE'] = CONF.keystone.endpoint_type clientmanager = user_ns['clientmanager'] = os_sdk.create_connection( auth_url, tenant, username, password, endpoint_type=endpoint_type) keystone = user_ns['keystone'] = clientmanager.identity nova = user_ns['nova'] = no_client.Client( '2', session=clientmanager.session) neutron = user_ns['neutron'] = clientmanager.network glance = user_ns['glance'] = clientmanager.image state = user_ns['state'] = SanityState( keystone, nova, glance, neutron, # Trim out the OSLO elements **{k: v for k, v in CONF.iteritems() if not isinstance(v, (cfg.ConfigOpts.SubCommandAttr, cfg.ConfigOpts.GroupAttr))}) sanity = user_ns['insanity'] = SanityController(state) simple = user_ns['simple'] = SimpleSanity(sanity) # All the simple sanity functions should be local for k in dir(simple): if k.startswith('_'): continue user_ns[k] = getattr(simple, k) # Add for each function user_ns['compute'] = compute.Compute(nova) user_ns['network'] = network.Network(neutron, sanity) user_ns['host'] = host.Host(sanity) # Add for_ functions into user space for k in dir(sanity): if k.startswith('for_'): user_ns[k] = getattr(sanity, k) # Add wait_ functions into user space for k in dir(sanity): if k.startswith('wait_'): user_ns[k] = getattr(sanity, k) CONF.action.func(user_ns)