Exemplo n.º 1
0
def handle_configure(cmd, defaults=None, list_defaults=None, scope=None):
    if defaults:
        defaults_section = cmd.cli_ctx.config.defaults_section_name
        with ConfiguredDefaultSetter(cmd.cli_ctx.config, scope.lower() == 'local'):
            for default in defaults:
                parts = default.split('=', 1)
                if len(parts) == 1:
                    raise CLIError('usage error: --defaults STRING=STRING STRING=STRING ...')
                cmd.cli_ctx.config.set_value(defaults_section, parts[0], _normalize_config_value(parts[1]))
        return
    if list_defaults:
        with ConfiguredDefaultSetter(cmd.cli_ctx.config, scope.lower() == 'local'):
            defaults_result = cmd.cli_ctx.config.items(cmd.cli_ctx.config.defaults_section_name)
        return [x for x in defaults_result if x.get('value')]

    # if nothing supplied, we go interactively
    try:
        print(MSG_INTRO)
        _handle_global_configuration(cmd.cli_ctx.config)
        print(MSG_CLOSING)
        # TODO: log_telemetry('configure', **answers)
    except NoTTYException:
        raise CLIError('This command is interactive and no tty available.')
    except (EOFError, KeyboardInterrupt):
        print()
Exemplo n.º 2
0
    def test_configured_default_setter(self):
        config = mock.MagicMock()
        config.use_local_config = None
        with ConfiguredDefaultSetter(config, True):
            self.assertEqual(config.use_local_config, True)
        self.assertIsNone(config.use_local_config)

        config.use_local_config = True
        with ConfiguredDefaultSetter(config, False):
            self.assertEqual(config.use_local_config, False)
        self.assertTrue(config.use_local_config)
Exemplo n.º 3
0
    def save(self, cmd):
        from azure.cli.core.util import ConfiguredDefaultSetter

        with ConfiguredDefaultSetter(cmd.cli_ctx.config, False):
            cmd.cli_ctx.config.set_value('quantum', 'group', self.resource_group)
            cmd.cli_ctx.config.set_value('quantum', 'workspace', self.name)
            cmd.cli_ctx.config.set_value('quantum', 'location', self.location)
def check_version(config, reported_version, today):
    # Retrieve the last version-check date
    date_checked = None
    try:
        with ConfiguredDefaultSetter(config, False):
            date_checked = config.get('quantum', 'version_check_date', None)

        # Save the today's date if it hasn't already been saved
        if date_checked is None or date_checked != today:
            with ConfiguredDefaultSetter(config, False):
                config.set_value('quantum', 'version_check_date', today)
    except:
        pass  # Ignore errors here. This error is expected in the unit tests because cli_ctx is unavailable.

    # Retrieve the list of available versions of the quantum extension
    available_versions = None
    latest_version_dict = None
    latest_version = None

    if date_checked is None or date_checked != today:  # This logic is intentionally duplicated...
        # The preceding line with this logical expression needed to be inside the previous "try" block.
        try:
            available_versions = list_versions("quantum")
        except:
            pass  # If an error occurs here, we ignore it.

        if available_versions is not None:
            latest_version_dict = available_versions[len(available_versions) -
                                                     1]

        if latest_version_dict is not None:
            latest_version = latest_version_dict['version'].split(' ')[0]

        if reported_version != latest_version:
            return (
                f"\nVersion {reported_version} of the quantum extension is installed locally,"
                f" but version {latest_version} is now available.\n"
                "You can use 'az extension update -n quantum' to upgrade.\n")
Exemplo n.º 5
0
def _handle_global_configuration(config, cloud_forbid_telemetry):
    # print location of global configuration
    print(MSG_GLOBAL_SETTINGS_LOCATION.format(config.config_path))
    # set up the config parsers
    file_config = configparser.ConfigParser()
    config_exists = file_config.read([config.config_path])
    should_modify_global_config = False
    if config_exists:
        # print current config and prompt to allow global config modification
        _print_cur_configuration(file_config)
        should_modify_global_config = prompt_y_n(MSG_PROMPT_MANAGE_GLOBAL,
                                                 default='n')
        answers['modify_global_prompt'] = should_modify_global_config
    if not config_exists or should_modify_global_config:
        # no config exists yet so configure global config or user wants to modify global config
        with ConfiguredDefaultSetter(config, False):
            output_index = prompt_choice_list(MSG_PROMPT_GLOBAL_OUTPUT,
                                              OUTPUT_LIST,
                                              default=get_default_from_config(
                                                  config, 'core', 'output',
                                                  OUTPUT_LIST))
            answers['output_type_prompt'] = output_index
            answers['output_type_options'] = str(OUTPUT_LIST)
            enable_file_logging = prompt_y_n(MSG_PROMPT_FILE_LOGGING,
                                             default='n')
            if cloud_forbid_telemetry:
                allow_telemetry = False
            else:
                allow_telemetry = prompt_y_n(MSG_PROMPT_TELEMETRY, default='y')
            answers['telemetry_prompt'] = allow_telemetry
            cache_ttl = None
            while not cache_ttl:
                try:
                    cache_ttl = prompt(
                        MSG_PROMPT_CACHE_TTL) or DEFAULT_CACHE_TTL
                    # ensure valid int by casting
                    cache_value = int(cache_ttl)
                    if cache_value < 1:
                        raise ValueError
                except ValueError:
                    logger.error('TTL must be a positive integer')
                    cache_ttl = None
            # save the global config
            config.set_value('core', 'output',
                             OUTPUT_LIST[output_index]['name'])
            config.set_value('core', 'collect_telemetry',
                             'yes' if allow_telemetry else 'no')
            config.set_value('core', 'cache_ttl', cache_ttl)
            config.set_value('logging', 'enable_log_file',
                             'yes' if enable_file_logging else 'no')
Exemplo n.º 6
0
def _set_webapp_up_default_args(cmd, resource_group_name, location, name, registry_server):
    from azure.cli.core.util import ConfiguredDefaultSetter
    with ConfiguredDefaultSetter(cmd.cli_ctx.config, True):
        logger.warning("Setting 'az containerapp up' default arguments for current directory. "
                       "Manage defaults with 'az configure --scope local'")

        cmd.cli_ctx.config.set_value('defaults', 'resource_group_name', resource_group_name)
        logger.warning("--resource-group/-g default: %s", resource_group_name)

        cmd.cli_ctx.config.set_value('defaults', 'location', location)
        logger.warning("--location/-l default: %s", location)

        cmd.cli_ctx.config.set_value('defaults', 'name', name)
        logger.warning("--name/-n default: %s", name)

        # cmd.cli_ctx.config.set_value('defaults', 'managed_env', managed_env)
        # logger.warning("--environment default: %s", managed_env)

        cmd.cli_ctx.config.set_value('defaults', 'registry_server', registry_server)
        logger.warning("--registry-server default: %s", registry_server)
Exemplo n.º 7
0
    def save(self, cmd):
        from azure.cli.core.util import ConfiguredDefaultSetter

        with ConfiguredDefaultSetter(cmd.cli_ctx.config, False):
            cmd.cli_ctx.config.set_value('quantum', 'target_id',
                                         self.target_id)