def __replace_values(self, git_repo: GitRepo, gitops_config: GitOpsConfig) -> bool: preview_id = self.__args.preview_id preview_folder_name = gitops_config.get_preview_namespace( self.__args.preview_id) context = GitOpsConfig.Replacement.PreviewContext( gitops_config, preview_id, self.__args.git_hash) any_value_replaced = False for file, replacements in gitops_config.replacements.items(): for replacement in replacements: replacement_value = replacement.get_value(context) value_replaced = self.__update_yaml_file( git_repo, f"{preview_folder_name}/{file}", replacement.path, replacement_value, ) if value_replaced: any_value_replaced = True logging.info( "Replaced property '%s' in '%s' with value: %s", replacement.path, file, replacement_value) else: logging.info("Keep property '%s' in '%s' value: %s", replacement.path, file, replacement_value) return any_value_replaced
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
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
def __create_preview_info_file(self, gitops_config: GitOpsConfig) -> None: preview_id = self.__args.preview_id yaml_file_dump( { "previewId": preview_id, "previewIdHash": gitops_config.create_preview_id_hash(preview_id), "routeHost": gitops_config.get_preview_host(preview_id), "namespace": gitops_config.get_preview_namespace(preview_id), }, "/tmp/gitopscli-preview-info.yaml", )
def __replace_values(self, git_repo: GitRepo, gitops_config: GitOpsConfig) -> bool: preview_folder_name = gitops_config.get_preview_namespace( self.__args.preview_id) any_value_replaced = False for replacement in gitops_config.replacements: replacement_value = self.__get_value_for_variable( gitops_config, replacement.variable) value_replaced = self.__update_yaml_file( git_repo, f"{preview_folder_name}/values.yaml", replacement.path, replacement_value, ) if value_replaced: any_value_replaced = True logging.info("Replaced property '%s' with value: %s", replacement.path, replacement_value) else: logging.info("Keep property '%s' value: %s", replacement.path, replacement_value) return any_value_replaced