def bind_client(req, resp, kwargs): req.env['sl_timehook_start_time'] = time.time() client = TimedClient(endpoint_url=cfg.CONF['softlayer']['endpoint'], proxy=cfg.CONF['softlayer']['proxy']) client.auth = None req.env['sl_client'] = client auth_token = req.env.get('auth', None) if auth_token is not None: client.auth = get_auth(auth_token)
def main(args=sys.argv[1:], env=Environment()): """ Entry point for the command-line client. """ # Parse Top-Level Arguments exit_status = 0 resolver = CommandParser(env) try: command, command_args = resolver.parse(args) # Set logging level debug_level = command_args.get('--debug') if debug_level: logger = logging.getLogger() handler = logging.StreamHandler() logger.addHandler(handler) logger.setLevel(DEBUG_LOGGING_MAP.get(debug_level, logging.DEBUG)) kwargs = { 'proxy': command_args.get('--proxy'), 'config_file': command_args.get('--config') } if command_args.get('--timings'): client = TimedClient(**kwargs) else: client = Client(**kwargs) # Do the thing runnable = command(client=client, env=env) data = runnable.execute(command_args) if data: out_format = command_args.get('--format', 'table') if out_format not in VALID_FORMATS: raise ArgumentError('Invalid format "%s"' % out_format) output = format_output(data, fmt=out_format) if output: env.out(output) if command_args.get('--timings'): out_format = command_args.get('--format', 'table') api_calls = client.get_last_calls() timing_table = KeyValueTable(['call', 'time']) for call, _, duration in api_calls: timing_table.add_row([call, duration]) env.err(format_output(timing_table, fmt=out_format)) except InvalidCommand as ex: env.err(resolver.get_module_help(ex.module_name)) if ex.command_name: env.err('') env.err(str(ex)) exit_status = 1 except InvalidModule as ex: env.err(resolver.get_main_help()) if ex.module_name: env.err('') env.err(str(ex)) exit_status = 1 except DocoptExit as ex: env.err(ex.usage) env.err( '\nUnknown argument(s), use -h or --help for available options') exit_status = 127 except KeyboardInterrupt: env.out('') exit_status = 1 except CLIAbort as ex: env.err(str(ex.message)) exit_status = ex.code except SystemExit as ex: exit_status = ex.code except SoftLayerAPIError as ex: if 'invalid api token' in ex.faultString.lower(): env.out("Authentication Failed: To update your credentials, use " "'sl config setup'") else: env.err(str(ex)) exit_status = 1 except SoftLayerError as ex: env.err(str(ex)) exit_status = 1 except Exception: import traceback env.err(traceback.format_exc()) exit_status = 1 sys.exit(exit_status)