Пример #1
0
 def __create_preview_from_template_if_not_existing(
         self, template_git_repo: GitRepo, target_git_repo: GitRepo,
         gitops_config: GitOpsConfig) -> bool:
     preview_namespace = gitops_config.get_preview_namespace(
         self.__args.preview_id)
     full_preview_folder_path = target_git_repo.get_full_file_path(
         preview_namespace)
     preview_env_already_exist = os.path.isdir(full_preview_folder_path)
     if preview_env_already_exist:
         logging.info("Use existing folder for preview: %s",
                      preview_namespace)
         return False
     logging.info("Create new folder for preview: %s", preview_namespace)
     full_preview_template_folder_path = template_git_repo.get_full_file_path(
         gitops_config.preview_template_path)
     if not os.path.isdir(full_preview_template_folder_path):
         raise GitOpsException(
             f"The preview template folder does not exist: {gitops_config.preview_template_path}"
         )
     logging.info("Using the preview template folder: %s",
                  gitops_config.preview_template_path)
     shutil.copytree(
         full_preview_template_folder_path,
         full_preview_folder_path,
     )
     return True
Пример #2
0
 def __create_preview_from_template_if_not_existing(
         self, git_repo: GitRepo, gitops_config: GitOpsConfig) -> bool:
     preview_namespace = gitops_config.get_preview_namespace(
         self.__args.preview_id)
     full_preview_folder_path = git_repo.get_full_file_path(
         preview_namespace)
     preview_env_already_exist = os.path.isdir(full_preview_folder_path)
     if preview_env_already_exist:
         logging.info("Use existing folder for preview: %s",
                      preview_namespace)
         return False
     logging.info("Create new folder for preview: %s", preview_namespace)
     preview_template_folder_name = f".preview-templates/{gitops_config.application_name}"
     full_preview_template_folder_path = git_repo.get_full_file_path(
         preview_template_folder_name)
     if not os.path.isdir(full_preview_template_folder_path):
         raise GitOpsException(
             f"The preview template folder does not exist: {preview_template_folder_name}"
         )
     logging.info("Using the preview template folder: %s",
                  preview_template_folder_name)
     shutil.copytree(
         full_preview_template_folder_path,
         full_preview_folder_path,
     )
     self.__update_yaml_file(git_repo, f"{preview_namespace}/Chart.yaml",
                             "name", preview_namespace)
     return True
Пример #3
0
def __get_repo_apps(team_config_git_repo: GitRepo) -> Set[str]:
    team_config_git_repo.clone()
    repo_dir = team_config_git_repo.get_full_file_path(".")
    return {
        name
        for name in os.listdir(repo_dir)
        if os.path.isdir(os.path.join(repo_dir, name))
        and not name.startswith(".")
    }
Пример #4
0
    def test_finalize(self):
        testee = GitRepo(self.__mock_repo_api)

        testee.clone()

        tmp_dir = testee.get_full_file_path("..")
        self.assertTrue(path.exists(tmp_dir))

        testee.finalize()
        self.assertFalse(path.exists(tmp_dir))
Пример #5
0
 def __update_yaml_file(git_repo: GitRepo, file_path: str, key: str,
                        value: Any) -> bool:
     full_file_path = git_repo.get_full_file_path(file_path)
     try:
         return update_yaml_file(full_file_path, key, value)
     except (FileNotFoundError, IsADirectoryError) as ex:
         raise GitOpsException(f"No such file: {file_path}") from ex
     except YAMLException as ex:
         raise GitOpsException(f"Error loading file: {file_path}") from ex
     except KeyError as ex:
         raise GitOpsException(
             f"Key '{key}' not found in file: {file_path}") from ex
Пример #6
0
    def test_enter_and_exit_magic_methods(self):
        testee = GitRepo(self.__mock_repo_api)

        self.assertEqual(testee, testee.__enter__())

        testee.clone()

        tmp_dir = testee.get_full_file_path("..")
        self.assertTrue(path.exists(tmp_dir))

        testee.__exit__(None, None, None)
        self.assertFalse(path.exists(tmp_dir))
Пример #7
0
def __find_apps_config_from_repo(
        team_config_git_repo: GitRepo, root_config_git_repo: GitRepo
) -> Tuple[str, str, Set[str], Set[str], str]:
    apps_from_other_repos: Set[str] = set(
    )  # Set for all entries in .applications from each config repository
    found_app_config_file = None
    found_app_config_file_name = None
    found_apps_path = "applications"
    found_app_config_apps: Set[str] = set()
    bootstrap_entries = __get_bootstrap_entries(root_config_git_repo)
    team_config_git_repo_clone_url = team_config_git_repo.get_clone_url()
    for bootstrap_entry in bootstrap_entries:
        if "name" not in bootstrap_entry:
            raise GitOpsException(
                "Every bootstrap entry must have a 'name' property.")
        app_file_name = "apps/" + bootstrap_entry["name"] + ".yaml"
        logging.info("Analyzing %s in root repository", app_file_name)
        app_config_file = root_config_git_repo.get_full_file_path(
            app_file_name)
        try:
            app_config_content = yaml_file_load(app_config_file)
        except FileNotFoundError as ex:
            raise GitOpsException(
                f"File '{app_file_name}' not found in root repository."
            ) from ex
        if "config" in app_config_content:
            app_config_content = app_config_content["config"]
            found_apps_path = "config.applications"
        if "repository" not in app_config_content:
            raise GitOpsException(
                f"Cannot find key 'repository' in '{app_file_name}'")
        if app_config_content["repository"] == team_config_git_repo_clone_url:
            logging.info("Found apps repository in %s", app_file_name)
            found_app_config_file = app_config_file
            found_app_config_file_name = app_file_name
            found_app_config_apps = __get_applications_from_app_config(
                app_config_content)
        else:
            apps_from_other_repos.update(
                __get_applications_from_app_config(app_config_content))

    if found_app_config_file is None or found_app_config_file_name is None:
        raise GitOpsException(
            f"Couldn't find config file for apps repository in root repository's 'apps/' directory"
        )

    return (
        found_app_config_file,
        found_app_config_file_name,
        found_app_config_apps,
        apps_from_other_repos,
        found_apps_path,
    )
Пример #8
0
def __get_bootstrap_entries(root_config_git_repo: GitRepo) -> Any:
    root_config_git_repo.clone()
    bootstrap_values_file = root_config_git_repo.get_full_file_path(
        "bootstrap/values.yaml")
    try:
        bootstrap_yaml = yaml_file_load(bootstrap_values_file)
    except FileNotFoundError as ex:
        raise GitOpsException(
            "File 'bootstrap/values.yaml' not found in root repository."
        ) from ex
    if "bootstrap" not in bootstrap_yaml:
        raise GitOpsException(
            "Cannot find key 'bootstrap' in 'bootstrap/values.yaml'")
    return bootstrap_yaml["bootstrap"]
Пример #9
0
    def __update_values(self, git_repo: GitRepo) -> Dict[str, Any]:
        args = self.__args
        single_commit = args.single_commit or args.commit_message
        full_file_path = git_repo.get_full_file_path(args.file)
        updated_values = {}
        for key, value in args.values.items():
            try:
                updated_value = update_yaml_file(full_file_path, key, value)
            except (FileNotFoundError, IsADirectoryError) as ex:
                raise GitOpsException(f"No such file: {args.file}") from ex
            except YAMLException as ex:
                raise GitOpsException(
                    f"Error loading file: {args.file}") from ex
            except KeyError as ex:
                raise GitOpsException(
                    f"Key '{key}' not found in file: {args.file}") from ex

            if not updated_value:
                logging.info("Yaml property %s already up-to-date", key)
                continue

            logging.info("Updated yaml property %s to %s", key, value)
            updated_values[key] = value

            if not single_commit:
                self.__commit(git_repo,
                              f"changed '{key}' to '{value}' in {args.file}")

        if single_commit and updated_values:
            if args.commit_message:
                message = args.commit_message
            elif len(updated_values) == 1:
                key, value = list(updated_values.items())[0]
                message = f"changed '{key}' to '{value}' in {args.file}"
            else:
                updates_count = len(updated_values)
                message = f"updated {updates_count} value{'s' if updates_count > 1 else ''} in {args.file}"
                message += f"\n\n{yaml_dump(updated_values)}"
            self.__commit(git_repo, message)

        return updated_values
Пример #10
0
 def __delete_folder_if_exists(git_repo: GitRepo, folder_name: str) -> bool:
     folder_full_path = git_repo.get_full_file_path(folder_name)
     if not os.path.exists(folder_full_path):
         return False
     shutil.rmtree(folder_full_path, ignore_errors=True)
     return True