From e2fd3b1eb9778332a73a4cb7ccf14991a5b7e717 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Wed, 31 May 2023 08:52:19 +0200 Subject: [PATCH] make local methods more private --- doc/architecture/BuildAndMixins.md | 6 - .../application/terraform_service.py | 153 +++++++++--------- .../ddadevops/devops_terraform_build.py | 2 +- 3 files changed, 77 insertions(+), 84 deletions(-) diff --git a/doc/architecture/BuildAndMixins.md b/doc/architecture/BuildAndMixins.md index b4280cf..6418255 100644 --- a/doc/architecture/BuildAndMixins.md +++ b/doc/architecture/BuildAndMixins.md @@ -18,12 +18,8 @@ classDiagram copy_build_resource_file_from_package(name) copy_build_resources_from_package() copy_build_resources_from_dir() - copy_local_state() - rescue_local_state() initialize_build_dir() post_build() - init_client() - write_output(terraform) read_output_json() plan() plan_fail_on_diff() @@ -49,13 +45,11 @@ classDiagram class AwsBackendPropertiesMixin { def project_vars() copy_build_resources_from_package() - init_client() } class DigitaloceanBackendPropertiesMixin { project_vars(self) copy_build_resources_from_package(self) - init_client(self) } class DevopsImageBuild { diff --git a/src/main/python/ddadevops/application/terraform_service.py b/src/main/python/ddadevops/application/terraform_service.py index fe01938..94d2a2b 100644 --- a/src/main/python/ddadevops/application/terraform_service.py +++ b/src/main/python/ddadevops/application/terraform_service.py @@ -23,87 +23,18 @@ class TerraformService: TerraformApi(), ) - def __copy_build_resource_file_from_package__(self, resource_name, devops: Devops): - data = self.resource_api.read_resource( - f"src/main/resources/terraform/{resource_name}" - ) - self.file_api.write_data_to_file( - Path(f"{devops.build_path()}/{resource_name}"), data - ) - - def __copy_build_resources_from_package__(self, devops: Devops): - terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] - for resource in terraform_domain.resources_from_package(): - self.__copy_build_resource_file_from_package__(resource, devops) - - def __copy_build_resources_from_dir__(self, devops: Devops): - terraform = devops.specialized_builds[BuildType.TERRAFORM] - self.file_api.cp_force( - f"{terraform.build_commons_path()}/*", devops.build_path() - ) - - def __print_terraform_command__(self, terraform: Terraform, devops: Devops): - terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] - if terraform_domain.tf_debug_print_terraform_command: - output = f"cd {devops.build_path()} && {terraform.latest_cmd()}" - print(output) - - # TODO: internal? - def copy_local_state(self, devops: Devops): - terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] - if terraform_domain.is_local_state(): - self.file_api.cp("terraform.tfstate", devops.build_path(), check=False) - - # TODO: internal? - def rescue_local_state(self, devops: Devops): - terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] - if terraform_domain.is_local_state(): - self.file_api.cp( - f"{devops.build_path()}/terraform.tfstate", ".", check=False - ) - def initialize_build_dir(self, devops: Devops): terraform = devops.specialized_builds[BuildType.TERRAFORM] if terraform.tf_use_package_common_files: self.__copy_build_resources_from_package__(devops) else: self.__copy_build_resources_from_dir__(devops) - self.copy_local_state(devops) + self.__copy_local_state__(devops) self.file_api.cp("*.tf", devops.build_path(), check=False) self.file_api.cp("*.properties", devops.build_path(), check=False) self.file_api.cp("*.tfvars", devops.build_path(), check=False) self.file_api.cp_recursive("scripts", devops.build_path(), check=False) - # TODO: internal? - def init_client(self, devops: Devops): - terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] - terraform = Terraform( - working_dir=devops.build_path(), - terraform_semantic_version=terraform_domain.tf_terraform_semantic_version, - ) - if terraform_domain.is_local_state(): - terraform.init() - else: - terraform.init(backend_config=terraform_domain.backend_config()) - self.__print_terraform_command__(terraform, devops) - if terraform_domain.tf_use_workspace: - try: - terraform.workspace("select", devops.stage) - self.__print_terraform_command__(terraform, devops) - except: - terraform.workspace("new", devops.stage) - self.__print_terraform_command__(terraform, devops) - return terraform - - # TODO: internal? - def write_output(self, terraform, devops: Devops): - terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] - result = terraform.output(json=IsFlagged) - self.__print_terraform_command__(terraform, devops) - self.file_api.write_json_to_file( - Path(f"{devops.build_path()}{terraform_domain.tf_output_json_name}"), result - ) - def read_output(self, devops: Devops) -> map: terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] return self.file_api.read_json_fro_file( @@ -116,7 +47,7 @@ class TerraformService: detailed_exitcode = IsFlagged else: detailed_exitcode = None - terraform = self.init_client(devops) + terraform = self.__init_client__(devops) return_code, _, stderr = terraform.plan( detailed_exitcode=detailed_exitcode, capture_output=False, @@ -136,7 +67,7 @@ class TerraformService: auto_approve_flag = IsFlagged else: auto_approve_flag = None - terraform = self.init_client(devops) + terraform = self.__init_client__(devops) if version.parse( terraform_domain.tf_terraform_semantic_version ) >= version.parse("1.0.0"): @@ -158,11 +89,11 @@ class TerraformService: self.__print_terraform_command__(terraform, devops) if return_code > 0: raise RuntimeError(return_code, "terraform error:", stderr) - self.write_output(terraform, devops) + self.__write_output__(terraform, devops) def refresh(self, devops: Devops): terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] - terraform = self.init_client(devops) + terraform = self.__init_client__(devops) return_code, _, stderr = terraform.refresh( var=terraform_domain.project_vars(), var_file=terraform_domain.tf_additional_tfvar_files, @@ -170,7 +101,7 @@ class TerraformService: self.__print_terraform_command__(terraform, devops) if return_code > 0: raise RuntimeError(return_code, "terraform error:", stderr) - self.write_output(terraform, devops) + self.__write_output__(terraform, devops) def destroy(self, devops: Devops, auto_approve=False): terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] @@ -178,7 +109,7 @@ class TerraformService: auto_approve_flag = IsFlagged else: auto_approve_flag = None - terraform = self.init_client(devops) + terraform = self.__init_client__(devops) if version.parse( terraform_domain.tf_terraform_semantic_version ) >= version.parse("1.0.0"): @@ -208,7 +139,7 @@ class TerraformService: tf_import_resource, ): terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] - terraform = self.init_client(devops) + terraform = self.__init_client__(devops) return_code, _, stderr = terraform.import_cmd( tf_import_name, tf_import_resource, @@ -220,3 +151,71 @@ class TerraformService: self.__print_terraform_command__(terraform, devops) if return_code > 0: raise RuntimeError(return_code, "terraform error:", stderr) + + def post_build(self, devops: Devops): + self.__rescue_local_state__(devops) + + def __copy_build_resource_file_from_package__(self, resource_name, devops: Devops): + data = self.resource_api.read_resource( + f"src/main/resources/terraform/{resource_name}" + ) + self.file_api.write_data_to_file( + Path(f"{devops.build_path()}/{resource_name}"), data + ) + + def __copy_build_resources_from_package__(self, devops: Devops): + terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] + for resource in terraform_domain.resources_from_package(): + self.__copy_build_resource_file_from_package__(resource, devops) + + def __copy_build_resources_from_dir__(self, devops: Devops): + terraform = devops.specialized_builds[BuildType.TERRAFORM] + self.file_api.cp_force( + f"{terraform.build_commons_path()}/*", devops.build_path() + ) + + def __print_terraform_command__(self, terraform: Terraform, devops: Devops): + terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] + if terraform_domain.tf_debug_print_terraform_command: + output = f"cd {devops.build_path()} && {terraform.latest_cmd()}" + print(output) + + def __copy_local_state__(self, devops: Devops): + terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] + if terraform_domain.is_local_state(): + self.file_api.cp("terraform.tfstate", devops.build_path(), check=False) + + def __rescue_local_state__(self, devops: Devops): + terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] + if terraform_domain.is_local_state(): + self.file_api.cp( + f"{devops.build_path()}/terraform.tfstate", ".", check=False + ) + + def __init_client__(self, devops: Devops): + terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] + terraform = Terraform( + working_dir=devops.build_path(), + terraform_semantic_version=terraform_domain.tf_terraform_semantic_version, + ) + if terraform_domain.is_local_state(): + terraform.init() + else: + terraform.init(backend_config=terraform_domain.backend_config()) + self.__print_terraform_command__(terraform, devops) + if terraform_domain.tf_use_workspace: + try: + terraform.workspace("select", devops.stage) + self.__print_terraform_command__(terraform, devops) + except: + terraform.workspace("new", devops.stage) + self.__print_terraform_command__(terraform, devops) + return terraform + + def __write_output__(self, terraform, devops: Devops): + terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] + result = terraform.output(json=IsFlagged) + self.__print_terraform_command__(terraform, devops) + self.file_api.write_json_to_file( + Path(f"{devops.build_path()}{terraform_domain.tf_output_json_name}"), result + ) diff --git a/src/main/python/ddadevops/devops_terraform_build.py b/src/main/python/ddadevops/devops_terraform_build.py index 3360cb0..89901a0 100644 --- a/src/main/python/ddadevops/devops_terraform_build.py +++ b/src/main/python/ddadevops/devops_terraform_build.py @@ -22,7 +22,7 @@ class DevopsTerraformBuild(DevopsBuild): def post_build(self): devops = self.devops_repo.get_devops(self.project) - self.teraform_service.rescue_local_state(devops) + self.teraform_service.post_build(devops) def read_output_json(self) -> map: devops = self.devops_repo.get_devops(self.project)