From de29c1dad844dc64fca1cd6c6fd203b836670c07 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Wed, 24 May 2023 17:50:09 +0200 Subject: [PATCH] fix terraform issues --- build.py | 2 +- .../python/ddadevops/application/__init__.py | 1 + .../application/terraform_service.py | 15 ++++++-------- .../ddadevops/devops_terraform_build.py | 3 ++- .../infrastructure/infrastructure.py | 20 +++++++++++-------- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/build.py b/build.py index 7a5a489..06c0dc9 100644 --- a/build.py +++ b/build.py @@ -28,7 +28,7 @@ use_plugin("python.distutils") default_task = "publish" name = "ddadevops" -version = "4.0.0-dev48" +version = "4.0.0-dev52" summary = "tools to support builds combining gopass, terraform, dda-pallet, aws & hetzner-cloud" description = __doc__ authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")] diff --git a/src/main/python/ddadevops/application/__init__.py b/src/main/python/ddadevops/application/__init__.py index 9f46f1a..f087165 100644 --- a/src/main/python/ddadevops/application/__init__.py +++ b/src/main/python/ddadevops/application/__init__.py @@ -1,2 +1,3 @@ from .image_build_service import ImageBuildService from .release_mixin_services import ReleaseService +from .terraform_service import TerraformService diff --git a/src/main/python/ddadevops/application/terraform_service.py b/src/main/python/ddadevops/application/terraform_service.py index a0986ab..b093f2a 100644 --- a/src/main/python/ddadevops/application/terraform_service.py +++ b/src/main/python/ddadevops/application/terraform_service.py @@ -50,12 +50,10 @@ class TerraformService: print(output) def copy_local_state(self, devops: Devops): - # TODO: orignal was unchecked ... - self.file_api.cp("terraform.tfstate", devops.build_path()) + self.file_api.cp("terraform.tfstate", devops.build_path(), check=False) def rescue_local_state(self, devops: Devops): - # TODO: orignal was unchecked ... - self.file_api.cp(f"{devops.build_path()}/terraform.tfstate", ".") + 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] @@ -63,12 +61,11 @@ class TerraformService: self.__copy_build_resources_from_package__(devops) else: self.__copy_build_resources_from_dir__(devops) - # TODO: orignal was unchecked ... self.copy_local_state(devops) - self.file_api.cp("*.tf", devops.build_path()) - self.file_api.cp("*.properties", devops.build_path()) - self.file_api.cp("*.tfvars", devops.build_path()) - self.file_api.cp_recursive("scripts", devops.build_path()) + 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) def init_client(self, devops: Devops): terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] diff --git a/src/main/python/ddadevops/devops_terraform_build.py b/src/main/python/ddadevops/devops_terraform_build.py index 830c12f..6b0f8ce 100644 --- a/src/main/python/ddadevops/devops_terraform_build.py +++ b/src/main/python/ddadevops/devops_terraform_build.py @@ -1,4 +1,5 @@ from .devops_build import DevopsBuild, create_devops_build_config +from .application import TerraformService def create_devops_terraform_build_config( @@ -48,7 +49,7 @@ class DevopsTerraformBuild(DevopsBuild): inp["mixin_types"] = config.get("mixin_types", []) super().__init__(project, inp) project.build_depends_on("dda-python-terraform") - self.teraform_service = Terraform.prod() + self.teraform_service = TerraformService.prod() def initialize_build_dir(self): super().initialize_build_dir() diff --git a/src/main/python/ddadevops/infrastructure/infrastructure.py b/src/main/python/ddadevops/infrastructure/infrastructure.py index 82ba879..80224ca 100644 --- a/src/main/python/ddadevops/infrastructure/infrastructure.py +++ b/src/main/python/ddadevops/infrastructure/infrastructure.py @@ -20,14 +20,14 @@ class FileApi: self.execution_api.execute("rm -rf " + directory) self.execution_api.execute("mkdir -p " + directory) - def cp(self, src: str, target_dir: str): - self.execution_api.execute(f"cp {src} {target_dir}") + def cp(self, src: str, target_dir: str, check=True): + self.execution_api.execute(f"cp {src} {target_dir}", check=check) - def cp_force(self, src: str, target_dir: str): - self.execution_api.execute("cp -f " + src + "* " + target_dir) + def cp_force(self, src: str, target_dir: str, check=True): + self.execution_api.execute(f"cp -f {src}* {target_dir}", check=check) - def cp_recursive(self, src: str, target_dir: str): - self.execution_api.execute("cp -r " + src + " " + target_dir) + def cp_recursive(self, src: str, target_dir: str, check=True): + self.execution_api.execute(f"cp -r {src} {target_dir}", check=check) def write_data_to_file(self, path: Path, data: bytes): with open(path, "w", encoding="utf-8") as output_file: @@ -47,6 +47,7 @@ class FileApi: with open(path, "r", encoding="utf-8") as input_file: return load(input_file) + class ImageApi: def image(self, name: str, path: Path): run( @@ -101,12 +102,15 @@ class ImageApi: class ExecutionApi: - def execute(self, command: str, dry_run=False, shell=True): + def execute(self, command: str, dry_run=False, shell=True, check=True): output = "" if dry_run: print(command) else: - output = check_output(command, encoding="UTF-8", shell=shell) + # output = check_output(command, encoding="UTF-8", shell=shell) + output = run( + command, encoding="UTF-8", shell=shell, stdout=PIPE, check=check + ).stdout output = output.rstrip() return output