示例#1
0
def test_validate_config_path_error_with_env_not_exist_dir(exists_mock, expanduser_mock):
    expanduser_mock.return_value = USER_HOME_PATH
    exists_mock.return_value = False

    with pytest.raises(ConfigInitError):
        Config.get_config_path()

    assert exists_mock.call_count == 1
示例#2
0
def test_validate_config_path_error(os_env_get, exists_mock, expanduser_mock):
    expanduser_mock.return_value = USER_HOME_PATH
    exists_mock.side_effect = [False, False]
    os_env_get.return_value = ""

    with pytest.raises(ConfigInitError):
        Config.get_config_path()

    assert exists_mock.call_count == 2
示例#3
0
def test_validate_config_path_success_with_env(exists_mock, expanduser_mock):
    expanduser_mock.return_value = USER_HOME_PATH
    exists_mock.return_value = True

    result = Config.get_config_path()

    assert result == USER_CUSTOM_PATH
    assert exists_mock.call_count == 1
示例#4
0
def test_validate_config_path_success_with_user_local_dir(
        os_env_get, exists_mock, expanduser_mock):
    expanduser_mock.return_value = USER_HOME_PATH
    exists_mock.return_value = True
    os_env_get.return_value = ""

    result = Config.get_config_path()

    assert result == os.path.join(USER_HOME_PATH, NCTL_CONFIG_DIR_NAME)
    assert exists_mock.call_count == 1
示例#5
0
def test_validate_config_path_success_with_app_dir(os_env_get, exists_mock,
                                                   expanduser_mock):
    expanduser_mock.return_value = USER_HOME_PATH
    exists_mock.side_effect = [False, True]
    os_env_get.return_value = ""

    result = Config.get_config_path()

    assert result == os.path.join(APP_DIR_PATH, NCTL_CONFIG_DIR_NAME)
    assert exists_mock.call_count == 2
示例#6
0
def get_template_version(template_name: str) -> Optional[str]:
    chart_file_location = os.path.join(Config.get_config_path(), "packs",
                                       template_name,
                                       Template.CHART_FILE_LOCATION,
                                       Template.CHART_FILE_NAME)

    if os.path.isfile(chart_file_location):
        with open(chart_file_location, "r") as file:
            local_template = extract_chart_description(file.read(), local=True)
            return local_template.local_version

    return None
示例#7
0
 def render_from_existing_template(self, src_template_name: str):
     local_templates_dir = os.path.join(Config.get_config_path(), "packs")
     src_template_dir = os.path.join(local_templates_dir, src_template_name)
     dest_template_dir = os.path.join(local_templates_dir, self.name)
     try:
         shutil.copytree(src_template_dir, dest_template_dir)
         chart_yaml_path = os.path.join(dest_template_dir,
                                        Template.CHART_FILE_LOCATION,
                                        Template.CHART_FILE_NAME)
         self.update_chart_yaml(chart_yaml_path)
     except (IOError, KeyError, shutil.Error):
         shutil.rmtree(
             dest_template_dir
         )  # Clear destination directory in case of template copy error
         raise
示例#8
0
def get_local_templates() -> Dict[str, Template]:
    local_model_list = {}
    path = os.path.join(Config.get_config_path(), "packs")

    for (dirpath, dirnames, filenames) in os.walk(path):
        template_name = os.path.split(os.path.split(dirpath)[0])[1]
        chart_file_location = os.path.join(path, template_name,
                                           Template.CHART_FILE_LOCATION,
                                           Template.CHART_FILE_NAME)
        if os.path.isfile(chart_file_location):
            with open(chart_file_location, "r") as file:
                local_template = extract_chart_description(file.read(),
                                                           local=True)
                if local_template:
                    local_model_list[local_template.name] = local_template

    return local_model_list
示例#9
0
def install(state: State, template_name: str):
    chart_file_location = os.path.join(Config.get_config_path(), "packs",
                                       template_name)

    with spinner(text=Texts.GETTING_LIST_OF_TEMPLATES_MSG):
        repository_name, access_token = get_repository_configuration()

        try:
            remote_templates = get_remote_templates(repository_name,
                                                    access_token)
        except ExceptionWithMessage as e:
            click.echo(e.message)
            sys.exit(1)

        remote_template_counterpart = remote_templates.get(template_name)

        if not remote_template_counterpart:
            click.echo(
                Texts.REMOTE_TEMPLATE_NOT_FOUND.format(
                    template_name=template_name))
            sys.exit(1)

    local_templates = get_local_templates()
    local_template_counterpart = local_templates.get(template_name)

    if local_template_counterpart:
        click.confirm(Texts.LOCAL_VERSION_ALREADY_INSTALLED.format(
            local_version=local_template_counterpart.local_version,
            template_name=local_template_counterpart.name,
            remote_version=remote_template_counterpart.remote_version),
                      abort=True)

        # noinspection PyBroadException
        try:
            shutil.rmtree(chart_file_location)
        except Exception:
            logger.exception("failed to remove local copy of template!")

    with spinner(text=Texts.DOWNLOADING_TEMPLATE):
        repository_name, access_token = get_repository_configuration()

        g = Github(repository_name, access_token)

        g.download_whole_directory(template_name, chart_file_location)

    click.echo("successfully installed!")
示例#10
0
def install(ctx: click.Context, template_name: str):
    packs_location = os.path.join(Config.get_config_path(), "packs")
    chart_file_location = os.path.join(packs_location, template_name)
    repository_address = get_repository_address()

    with spinner(
            text=Texts.GETTING_LIST_OF_TEMPLATES_MSG) as templates_spinner:
        try:
            remote_template = load_remote_template(
                template_name, repository_address=repository_address)
        except Exception:
            templates_spinner.stop()
            handle_error(logger,
                         user_msg=Texts.FAILED_TO_LOAD_TEMPLATE.format(
                             template_name=template_name),
                         log_msg=Texts.FAILED_TO_LOAD_TEMPLATE.format(
                             template_name=template_name),
                         add_verbosity_msg=ctx.obj.verbosity == 0)
            sys.exit(1)

        if not remote_template:
            templates_spinner.stop()
            handle_error(logger,
                         user_msg=Texts.REMOTE_TEMPLATE_NOT_FOUND.format(
                             template_name=template_name),
                         log_msg=Texts.REMOTE_TEMPLATE_NOT_FOUND.format(
                             template_name=template_name),
                         add_verbosity_msg=ctx.obj.verbosity == 0)
            sys.exit(1)

    local_templates = get_local_templates()
    local_template_counterpart = local_templates.get(template_name)

    if local_template_counterpart:
        if (not click.get_current_context().obj.force) and (not click.confirm(
                Texts.LOCAL_VERSION_ALREADY_INSTALLED.format(
                    local_version=local_template_counterpart.local_version,
                    template_name=local_template_counterpart.name,
                    remote_version=remote_template.remote_version))):
            sys.exit(0)
        # noinspection PyBroadException
        try:
            shutil.rmtree(chart_file_location)
        except Exception:
            logger.exception("failed to remove local copy of template!")

    with spinner(text=Texts.DOWNLOADING_TEMPLATE) as download_spinner:
        try:
            download_remote_template(template=remote_template,
                                     repository_address=repository_address,
                                     output_dir_path=packs_location)
        except Exception:
            download_spinner.stop()
            handle_error(logger,
                         user_msg=Texts.FAILED_TO_INSTALL_TEMPLATE.format(
                             template_name=template_name,
                             repository_name=repository_address),
                         log_msg=Texts.FAILED_TO_INSTALL_TEMPLATE.format(
                             template_name=template_name,
                             repository_name=repository_address),
                         add_verbosity_msg=ctx.obj.verbosity == 0)
            sys.exit(1)

    update_resources_in_packs()

    click.echo("successfully installed!")