def main(): try: NalClientShell().main(sys.argv[1:]) except KeyboardInterrupt: utils.exit('... terminating client', exit_code=130) except Exception as e: utils.exit(utils.exception_to_str(e))
def main(self, argv): # Parse args once to find version # NOTE(flepied) Under Python3, parsed arguments are removed # from the list so make a copy for the first parsing base_argv = copy.deepcopy(argv) parser = self.get_base_parser() (options, args) = parser.parse_known_args(base_argv) try: endpoint = self._get_client_url(options) endpoint, url_version = utils.strip_version(endpoint) except ValueError: url_version = None # build available subcommands based on version try: api_version = int(url_version or 1) except ValueError: print("Invalid API version parameter") utils.exit() subcommand_parser = self.get_subcommand_parser(api_version) self.parser = subcommand_parser # Handle top-level --help/-h before attempting to parse # a command off the command line if options.help or not argv: self.do_help(options) return 0 # Parse args again and call whatever callback was selected args = subcommand_parser.parse_args(argv) # Short-circuit and deal with help command right away. if args.func == self.do_help: self.do_help(args) return 0 LOG = logging.getLogger(client_const.SHELL_LOGGER_NAME) LOG.addHandler(logging.StreamHandler()) LOG.setLevel(logging.DEBUG if args.debug else logging.INFO) client = self._get_versioned_client(api_version, args, force_auth=False) try: args.func(client, args) except Exception: # NOTE(kragniz) Print any exceptions raised to stderr # if the --debug flag is set if args.debug: traceback.print_exc() raise exc.CommandError("CommandError")
def get_subcommand_parser(self, version): parser = self.get_base_parser() self.subcommands = {} subparsers = parser.add_subparsers(metavar='<subcommand>') try: submodule = utils.import_versioned_module(version, 'shell') except ImportError: print('"%s" is not a supported API version. Example ' 'values are "1".' % version) utils.exit() self._find_actions(subparsers, submodule) self._find_actions(subparsers, self) # self._add_bash_completion_subparser(subparsers) return parser
def do_node_update(c, args): """Update a node by operate port. In commandline, output status. :param c: call manager. :param args: argument options. """ fields = {} # Filter out None values from args. if not hasattr(args, 'type') or not args.type: msg = "too few arguments: type is required." utils.exit(msg) if not hasattr(args, 'json_data') or not args.json_data: msg = "too few arguments: json_data is required." utils.exit(msg) try: fields = json.loads(args.json_data) except Exception: msg = "json format is incorrect." utils.exit(msg) fields['function_type'] = args.type # Call client. result = c.node.update(fields) # Show result. print(result.get('status', 'non-status')) return result
def do_resource_get(c, args): """Get a new resource you can access. In commandline, output resource data. :param c: call manager. :param args: argument options. """ fields = {} # Filter out None values from args. if not hasattr(args, 'type') or not args.type: msg = "too few arguments: type is required." utils.exit(msg) if not hasattr(args, 'json_data') or not args.json_data: msg = "too few arguments: json_data is required." utils.exit(msg) try: fields = json.loads(args.json_data) except Exception: msg = "json format is incorrect." utils.exit(msg) fields['function_type'] = args.type # Call Client data_list = c.resource.get(fields) # Show result. pprint(data_list)