def main(args, file=sys.stdout): #pylint: disable=redefined-builtin _logging.configure_logging(args) if len(args) > 0 and args[0] == '--version': show_version_info_exit(file) azure_folder = os.path.expanduser('~/.azure') if not os.path.exists(azure_folder): os.makedirs(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) config = Configuration(args) APPLICATION.initialize(config) try: cmd_result = APPLICATION.execute(args) # Commands can return a dictionary/list of results # If they do, we print the results. if cmd_result and cmd_result.result: from azure.cli.core._output import OutputProducer formatter = OutputProducer.get_formatter(APPLICATION.configuration.output_format) OutputProducer(formatter=formatter, file=file).out(cmd_result) except Exception as ex: # pylint: disable=broad-except from azure.cli.core.telemetry import log_telemetry log_telemetry('Error', log_type='trace') error_code = handle_exception(ex) return error_code
def create_certificate(client, vault_base_url, certificate_name, certificate_policy, disabled=False, expires=None, not_before=None, tags=None): from azure.cli.command_modules.keyvault.keyvaultclient.generated.models import \ (CertificateAttributes) cert_attrs = CertificateAttributes(not disabled, not_before, expires) logger.info("Starting long running operation 'keyvault certificate create'") client.create_certificate( vault_base_url, certificate_name, certificate_policy, cert_attrs, tags) if certificate_policy['issuer_parameters']['name'].lower() == 'unknown': # return immediately for a pending certificate return client.get_certificate_operation(vault_base_url, certificate_name) # otherwise loop until the certificate creation is complete while True: check = client.get_certificate_operation(vault_base_url, certificate_name) if check.status != 'inProgress': logger.info("Long running operation 'keyvault certificate create' finished with result %s.", check) # pylint: disable=line-too-long return check try: time.sleep(10) except KeyboardInterrupt: logger.info("Long running operation wait cancelled.") raise except Exception as client_exception: log_telemetry('client exception', log_type='trace') message = getattr(client_exception, 'message', client_exception) try: message = str(message) + ' ' + json.loads(client_exception.response.text) \ ['error']['details'][0]['message'] except: #pylint: disable=bare-except pass raise CLIError('{}'.format(message))
def execute(self, unexpanded_argv): argv = Application._expand_file_prefixed_files(unexpanded_argv) command_table = self.configuration.get_command_table() self.raise_event(self.COMMAND_TABLE_LOADED, command_table=command_table) self.parser.load_command_table(command_table) self.raise_event(self.COMMAND_PARSER_LOADED, parser=self.parser) if len(argv) == 0: az_subparser = self.parser.subparsers[tuple()] _help.show_welcome(az_subparser) log_telemetry('welcome') return None if argv[0].lower() == 'help': argv[0] = '--help' args = self.parser.parse_args(argv) self.raise_event(self.COMMAND_PARSER_PARSED, command=args.command, args=args) results = [] for expanded_arg in _explode_list_args(args): self.session['command'] = expanded_arg.command try: _validate_arguments(expanded_arg) except: # pylint: disable=bare-except err = sys.exc_info()[1] getattr(expanded_arg, '_parser', self.parser).validation_error(str(err)) # Consider - we are using any args that start with an underscore (_) as 'private' # arguments and remove them from the arguments that we pass to the actual function. # This does not feel quite right. params = dict([(key, value) for key, value in expanded_arg.__dict__.items() if not key.startswith('_')]) params.pop('subcommand', None) params.pop('func', None) params.pop('command', None) log_telemetry( expanded_arg.command, log_type='pageview', output_type=self.configuration.output_format, parameters=[p for p in unexpanded_argv if p.startswith('-')]) result = expanded_arg.func(params) result = todict(result) results.append(result) if len(results) == 1: results = results[0] event_data = {'result': results} self.raise_event(self.TRANSFORM_RESULT, event_data=event_data) self.raise_event(self.FILTER_RESULT, event_data=event_data) return CommandResultItem( event_data['result'], table_transformer=command_table[args.command].table_transformer, is_query_active=self.session['query_active'])
def main(args, file=sys.stdout): #pylint: disable=redefined-builtin _logging.configure_logging(args) if len(args) > 0 and args[0] == '--version': show_version_info_exit(file) azure_folder = os.path.expanduser('~/.azure') if not os.path.exists(azure_folder): os.makedirs(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) config = Configuration(args) APPLICATION.initialize(config) try: cmd_result = APPLICATION.execute(args) # Commands can return a dictionary/list of results # If they do, we print the results. if cmd_result and cmd_result.result: formatter = OutputProducer.get_formatter( APPLICATION.configuration.output_format) OutputProducer(formatter=formatter, file=file).out(cmd_result) except Exception as ex: # pylint: disable=broad-except log_telemetry('Error', log_type='trace') error_code = handle_exception(ex) return error_code
def format_help(self): is_group = self.is_group() log_telemetry('show help', prog=self.prog) _help.show_help(self.prog.split()[1:], self._actions[-1] if is_group else self, is_group) self.exit()
def format_help(self): from azure.cli.core.telemetry import log_telemetry is_group = self.is_group() log_telemetry('show help', prog=self.prog) _help.show_help(self.prog.split()[1:], self._actions[-1] if is_group else self, is_group) self.exit()
def get_command_table(): '''Loads command table(s) ''' installed_command_modules = [] try: mods_ns_pkg = import_module('azure.cli.command_modules') installed_command_modules = [modname for _, modname, _ in \ pkgutil.iter_modules(mods_ns_pkg.__path__)] except ImportError: pass logger.info('Installed command modules %s', installed_command_modules) cumulative_elapsed_time = 0 for mod in installed_command_modules: try: start_time = timeit.default_timer() import_module('azure.cli.command_modules.' + mod).load_commands() elapsed_time = timeit.default_timer() - start_time logger.debug("Loaded module '%s' in %.3f seconds.", mod, elapsed_time) cumulative_elapsed_time += elapsed_time except Exception: #pylint: disable=broad-except # Changing this error message requires updating CI script that checks for failed # module loading. logger.error("Error loading command module '%s'", mod) log_telemetry('Error loading module', module=mod) logger.debug(traceback.format_exc()) logger.debug("Loaded all modules in %.3f seconds. "\ "(note: there's always an overhead with the first module loaded)", cumulative_elapsed_time) _update_command_definitions(command_table) ordered_commands = OrderedDict(command_table) return ordered_commands
def __call__(self, poller): logger.info("Starting long running operation '%s'", self.start_msg) correlation_message = '' while not poller.done(): try: correlation_message = 'Correlation ID: {}' \ .format(json.loads(poller._response.__dict__['_content']) #pylint: disable=protected-access ['properties']['correlationId']) except: #pylint: disable=bare-except pass try: self._delay() except KeyboardInterrupt: logger.error('Long running operation wait cancelled. %s', correlation_message) raise try: result = poller.result() except ClientException as client_exception: log_telemetry('client exception', log_type='trace') message = getattr(client_exception, 'message', client_exception) try: message = str(message) + ' ' + json.loads(client_exception.response.text) \ ['error']['details'][0]['message'] except: #pylint: disable=bare-except pass raise CLIError('{} {}'.format(message, correlation_message)) logger.info("Long running operation '%s' completed with result %s", self.start_msg, result) return result
def handle_configure(): try: print(MSG_INTRO) _handle_global_configuration() # TODO: uncomment when implemented # _handle_context_configuration() print(MSG_CLOSING) log_telemetry('configure', **answers) except (EOFError, KeyboardInterrupt): print()
def create_certificate(client, vault_base_url, certificate_name, certificate_policy, disabled=False, expires=None, not_before=None, tags=None): from azure.cli.command_modules.keyvault.keyvaultclient.generated.models import \ (CertificateAttributes) cert_attrs = CertificateAttributes(not disabled, not_before, expires) logger.info( "Starting long running operation 'keyvault certificate create'") client.create_certificate(vault_base_url, certificate_name, certificate_policy, cert_attrs, tags) if certificate_policy['issuer_parameters']['name'].lower() == 'unknown': # return immediately for a pending certificate return client.get_certificate_operation(vault_base_url, certificate_name) # otherwise loop until the certificate creation is complete while True: check = client.get_certificate_operation(vault_base_url, certificate_name) if check.status != 'inProgress': logger.info("Long running operation 'keyvault certificate create' finished with result %s.", check) # pylint: disable=line-too-long return check try: time.sleep(10) except KeyboardInterrupt: logger.info("Long running operation wait cancelled.") raise except Exception as client_exception: log_telemetry('client exception', log_type='trace') message = getattr(client_exception, 'message', client_exception) try: message = str(message) + ' ' + json.loads(client_exception.response.text) \ ['error']['details'][0]['message'] except: #pylint: disable=bare-except pass raise CLIError('{}'.format(message))
def get_command_table(module_name=None): '''Loads command table(s) When `module_name` is specified, only commands from that module will be loaded. If the module is not found, all commands are loaded. ''' loaded = False # TODO Remove check for acs module. Issue #1110 if module_name and module_name != 'acs': try: import_module('azure.cli.command_modules.' + module_name) logger.info("Successfully loaded command table from module '%s'.", module_name) loaded = True except ImportError: logger.info("Loading all installed modules as module with name '%s' not found.", module_name) #pylint: disable=line-too-long except Exception: #pylint: disable=broad-except pass if not loaded: installed_command_modules = [] try: mods_ns_pkg = import_module('azure.cli.command_modules') installed_command_modules = [modname for _, modname, _ in \ pkgutil.iter_modules(mods_ns_pkg.__path__)] except ImportError: pass logger.info('Installed command modules %s', installed_command_modules) logger.info('Loading command tables from all installed modules.') for mod in installed_command_modules: try: import_module('azure.cli.command_modules.' + mod) except Exception: #pylint: disable=broad-except # Changing this error message requires updating CI script that checks for failed # module loading. logger.error("Error loading command module '%s'", mod) log_telemetry('Error loading module', module=mod) logger.debug(traceback.format_exc()) _update_command_definitions(command_table) ordered_commands = OrderedDict(command_table) return ordered_commands
def _execute_command(kwargs): from msrest.paging import Paged from msrest.exceptions import ClientException from msrestazure.azure_operation import AzureOperationPoller from azure.common import AzureException client = client_factory(kwargs) if client_factory else None try: result = operation(client, **kwargs) if client else operation( **kwargs) # apply results transform if specified if transform_result: return transform_result(result) # otherwise handle based on return type of results if isinstance(result, AzureOperationPoller): return LongRunningOperation('Starting {}'.format(name))(result) elif isinstance(result, Paged): return list(result) else: return result except ClientException as client_exception: log_telemetry('client exception', log_type='trace') message = getattr(client_exception, 'message', client_exception) raise CLIError(message) except AzureException as azure_exception: log_telemetry('azure exception', log_type='trace') message = re.search(r"([A-Za-z\t .])+", str(azure_exception)) raise CLIError('\n{}'.format( message.group(0) if message else str(azure_exception))) except ValueError as value_error: log_telemetry('value exception', log_type='trace') raise CLIError(value_error)
def _execute_command(kwargs): from msrest.paging import Paged from msrest.exceptions import ClientException from msrestazure.azure_operation import AzureOperationPoller from azure.common import AzureException client = client_factory(kwargs) if client_factory else None try: op = get_op_handler(operation) result = op(client, **kwargs) if client else op(**kwargs) # apply results transform if specified if transform_result: return transform_result(result) # otherwise handle based on return type of results if isinstance(result, AzureOperationPoller): return LongRunningOperation('Starting {}'.format(name))(result) elif isinstance(result, Paged): return list(result) else: return result except ClientException as client_exception: log_telemetry('client exception', log_type='trace') message = getattr(client_exception, 'message', client_exception) raise _polish_rp_not_registerd_error(CLIError(message)) except AzureException as azure_exception: log_telemetry('azure exception', log_type='trace') message = re.search(r"([A-Za-z\t .])+", str(azure_exception)) raise CLIError('\n{}'.format(message.group(0) if message else str(azure_exception))) except ValueError as value_error: log_telemetry('value exception', log_type='trace') raise CLIError(value_error) except CLIError as cli_error: raise _polish_rp_not_registerd_error(cli_error)
def get_command_table(module_name=None): '''Loads command table(s) When `module_name` is specified, only commands from that module will be loaded. If the module is not found, all commands are loaded. ''' loaded = False if module_name: try: import_module('azure.cli.command_modules.' + module_name) logger.info("Successfully loaded command table from module '%s'.", module_name) loaded = True except ImportError: logger.info("Loading all installed modules as module with name '%s' not found.", module_name) #pylint: disable=line-too-long except Exception: #pylint: disable=broad-except pass if not loaded: installed_command_modules = [] try: mods_ns_pkg = import_module('azure.cli.command_modules') installed_command_modules = [modname for _, modname, _ in \ pkgutil.iter_modules(mods_ns_pkg.__path__)] except ImportError: pass logger.info('Installed command modules %s', installed_command_modules) logger.info('Loading command tables from all installed modules.') for mod in installed_command_modules: try: import_module('azure.cli.command_modules.' + mod) except Exception: #pylint: disable=broad-except # Changing this error message requires updating CI script that checks for failed # module loading. logger.error("Error loading command module '%s'", mod) log_telemetry('Error loading module', module=mod) logger.debug(traceback.format_exc()) _update_command_definitions(command_table) ordered_commands = OrderedDict(command_table) return ordered_commands
def validation_error(self, message): log_telemetry('validation error', log_type='trace', prog=self.prog) return super(AzCliCommandParser, self).error(message)
def validation_error(self, message): from azure.cli.core.telemetry import log_telemetry log_telemetry('validation error', log_type='trace', prog=self.prog) return super(AzCliCommandParser, self).error(message)
def error(self, message): from azure.cli.core.telemetry import log_telemetry log_telemetry('parse error', message=message, prog=self.prog) return super(AzCliCommandParser, self).error(message)
from azure.cli.core.telemetry import (init_telemetry, user_agrees_to_telemetry, telemetry_flush, log_telemetry) try: try: if user_agrees_to_telemetry(): init_telemetry() except Exception: #pylint: disable=broad-except pass args = sys.argv[1:] # Check if we are in argcomplete mode - if so, we # need to pick up our args from environment variables if os.environ.get('_ARGCOMPLETE'): comp_line = os.environ.get('COMP_LINE') if comp_line: args = comp_line.split()[1:] sys.exit(azure.cli.main.main(args)) except KeyboardInterrupt: log_telemetry('keyboard interrupt') sys.exit(1) finally: try: if user_agrees_to_telemetry(): telemetry_flush() except Exception: #pylint: disable=broad-except pass
def error(self, message): log_telemetry('parse error', message=message, prog=self.prog) return super(AzCliCommandParser, self).error(message)
def execute(self, unexpanded_argv): argv = Application._expand_file_prefixed_files(unexpanded_argv) command_table = self.configuration.get_command_table() self.raise_event(self.COMMAND_TABLE_LOADED, command_table=command_table) self.parser.load_command_table(command_table) self.raise_event(self.COMMAND_PARSER_LOADED, parser=self.parser) if len(argv) == 0: enable_autocomplete(self.parser) az_subparser = self.parser.subparsers[tuple()] _help.show_welcome(az_subparser) log_telemetry("welcome") return None if argv[0].lower() == "help": argv[0] = "--help" # Rudimentary parsing to get the command nouns = [] for noun in argv: if noun[0] == "-": break nouns.append(noun) command = " ".join(nouns) if argv[-1] in ("--help", "-h") or command in command_table: self.configuration.load_params(command) self.raise_event(self.COMMAND_TABLE_PARAMS_LOADED, command_table=command_table) self.parser.load_command_table(command_table) if self.session["completer_active"]: enable_autocomplete(self.parser) args = self.parser.parse_args(argv) self.raise_event(self.COMMAND_PARSER_PARSED, command=args.command, args=args) results = [] for expanded_arg in _explode_list_args(args): self.session["command"] = expanded_arg.command try: _validate_arguments(expanded_arg) except CLIError: raise except: # pylint: disable=bare-except err = sys.exc_info()[1] getattr(expanded_arg, "_parser", self.parser).validation_error(str(err)) # Consider - we are using any args that start with an underscore (_) as 'private' # arguments and remove them from the arguments that we pass to the actual function. # This does not feel quite right. params = dict([(key, value) for key, value in expanded_arg.__dict__.items() if not key.startswith("_")]) params.pop("subcommand", None) params.pop("func", None) params.pop("command", None) log_telemetry( expanded_arg.command, log_type="pageview", output_type=self.configuration.output_format, parameters=[p for p in unexpanded_argv if p.startswith("-")], ) result = expanded_arg.func(params) result = todict(result) results.append(result) if len(results) == 1: results = results[0] event_data = {"result": results} self.raise_event(self.TRANSFORM_RESULT, event_data=event_data) self.raise_event(self.FILTER_RESULT, event_data=event_data) return CommandResultItem( event_data["result"], table_transformer=command_table[args.command].table_transformer, is_query_active=self.session["query_active"], )