diff --git a/src/main/python/ddadevops/__init__.py b/src/main/python/ddadevops/__init__.py index 666f143..b423561 100644 --- a/src/main/python/ddadevops/__init__.py +++ b/src/main/python/ddadevops/__init__.py @@ -19,7 +19,7 @@ from .devops_terraform_build import DevopsTerraformBuild, create_devops_terrafor from .devops_build import DevopsBuild, create_devops_build_config, get_devops_build, get_tag_from_latest_commit from .credential import gopass_password_from_path, gopass_field_from_path -from .domain import Validateable, Build, DockerBuild -from .application import BuildService, DockerBuildService +from .domain import Validateable, Build, DockerBuild, C4kBuild +from .application import BuildService, DockerBuildService, C4kBuildService __version__ = "${version}" diff --git a/src/main/python/ddadevops/application.py b/src/main/python/ddadevops/application.py index 9af97ba..6853720 100644 --- a/src/main/python/ddadevops/application.py +++ b/src/main/python/ddadevops/application.py @@ -1,4 +1,4 @@ -from .domain import Build, DockerBuild +from .domain import Build, DockerBuild, C4kBuild from .infrastructure import FileApi, ResourceApi, DockerApi @@ -52,3 +52,25 @@ class DockerBuildService(): def test(self, build: DockerBuild): self.docker_api.test(build.name(), build.build_path()) +class C4kBuildService(): + def write_c4k_config(self, build: C4kBuild): + fqdn = self.get('fqdn') + build.c4k_mixin_config.update({'fqdn': fqdn}) + with open(self.build_path() + '/out_c4k_config.yaml', 'w', encoding="utf-8") as output_file: + yaml.dump(self.c4k_mixin_config, output_file) + + def write_c4k_auth(self): + with open(self.build_path() + '/out_c4k_auth.yaml', 'w', encoding="utf-8") as output_file: + yaml.dump(self.c4k_mixin_auth, output_file) + chmod(self.build_path() + '/out_c4k_auth.yaml', 0o600) + + def c4k_apply(self, dry_run=False): + cmd = f'c4k-{self.c4k_module_name}-standalone.jar {self.build_path()}/out_c4k_config.yaml {self.build_path()}/out_c4k_auth.yaml > {self.build_path()}/out_{self.c4k_module_name}.yaml' + output = '' + if dry_run: + print(cmd) + else: + output = execute(cmd, True) + print(output) + return output + diff --git a/src/main/python/ddadevops/c4k_mixin.py b/src/main/python/ddadevops/c4k_mixin.py index 6f5604c..4fe2542 100644 --- a/src/main/python/ddadevops/c4k_mixin.py +++ b/src/main/python/ddadevops/c4k_mixin.py @@ -1,5 +1,7 @@ from os import chmod import yaml +from .domain import C4kBuild +from .application import C4kBuildService from .python_util import execute from .devops_build import DevopsBuild from .credential import gopass_field_from_path, gopass_password_from_path @@ -32,7 +34,9 @@ def add_c4k_mixin_config(config, class C4kMixin(DevopsBuild): def __init__(self, project, config): - super().__init__(project, config) + self.build = C4kBuild(project, config) + self.c4k_build_service = C4kBuildService() + self.c4k_mixin_config = config['C4kMixin']['Config'] self.c4k_mixin_auth = config['C4kMixin']['Auth'] self.c4k_module_name = config['C4kMixin']['Name'] diff --git a/src/main/python/ddadevops/devops_build.py b/src/main/python/ddadevops/devops_build.py index 438ccb0..5087a90 100644 --- a/src/main/python/ddadevops/devops_build.py +++ b/src/main/python/ddadevops/devops_build.py @@ -1,31 +1,41 @@ from .domain import Build from .application import BuildService -def create_devops_build_config(stage, project_root_path, module, - build_dir_name='target'): - return {'stage': stage, - 'project_root_path': project_root_path, - 'module': module, - 'build_dir_name': build_dir_name} + +def create_devops_build_config( + stage, project_root_path, module, build_dir_name="target" +): + return { + "stage": stage, + "project_root_path": project_root_path, + "module": module, + "build_dir_name": build_dir_name, + } + def get_devops_build(project): - return project.get_property('devops_build') + return project.get_property("devops_build") + # TODO: Remove from here! def get_tag_from_latest_commit(): try: - value = run('git describe --abbrev=0 --tags --exact-match', shell=True, - capture_output=True, check=True) - return value.stdout.decode('UTF-8').rstrip() + value = run( + "git describe --abbrev=0 --tags --exact-match", + shell=True, + capture_output=True, + check=True, + ) + return value.stdout.decode("UTF-8").rstrip() except CalledProcessError: return None -class DevopsBuild: +class DevopsBuild: def __init__(self, project, config): self.build = Build(project, config) self.build_service = BuildService() - project.set_property('devops_build', self) + project.set_property("devops_build", self) def name(self): return self.build.name() diff --git a/src/main/python/ddadevops/domain.py b/src/main/python/ddadevops/domain.py index 69a3e9e..29b5a77 100644 --- a/src/main/python/ddadevops/domain.py +++ b/src/main/python/ddadevops/domain.py @@ -2,31 +2,27 @@ from typing import List from .python_util import filter_none -class Validateable(): +class Validateable: def __validate_is_not_empty__(self, field_name: str) -> List[str]: value = self.__dict__[field_name] - if value is None or value == '': + if value is None or value == "": return [f"Field '{field_name}' may not be empty."] else: return [] - + def validate(self) -> List[str]: return [] def is_valid(self) -> bool: return len(self.validate()) < 1 - - class Build(Validateable): - def __init__(self, project, config): - # deprecate stage - self.stage = config['stage'] - self.project_root_path = config['project_root_path'] - self.module = config['module'] - self.build_dir_name = config['build_dir_name'] + self.stage = config["stage"] + self.project_root_path = config["project_root_path"] + self.module = config["module"] + self.build_dir_name = config["build_dir_name"] self.stack = {} self.project = project @@ -34,37 +30,44 @@ class Build(Validateable): return self.project.name def build_path(self): - path = [self.project_root_path, - self.build_dir_name, - self.name(), - self.module] - return '/'.join(filter_none(path)) + path = [self.project_root_path, self.build_dir_name, self.name(), self.module] + return "/".join(filter_none(path)) - def put(self, key, value): + def __put__(self, key, value): self.stack[key] = value - def get(self, key): + def __get__(self, key): return self.stack[key] - def get_keys(self, keys): + def __get_keys__(self, keys): result = {} for key in keys: result[key] = self.get(key) return result -class DockerBuild(Build): +class DockerBuild(Validateable): def __init__(self, project, config): - super().__init__(project, config) - project.build_depends_on('dda-python-terraform') - self.dockerhub_user = config['dockerhub_user'] - self.dockerhub_password = config['dockerhub_password'] - self.use_package_common_files = config['use_package_common_files'] - self.build_commons_path = config['build_commons_path'] - self.docker_build_commons_dir_name = config['docker_build_commons_dir_name'] - self.docker_publish_tag = config['docker_publish_tag'] + project.build_depends_on("dda-python-terraform") + self.build = Build(project, config) + self.dockerhub_user = config["dockerhub_user"] + self.dockerhub_password = config["dockerhub_password"] + self.use_package_common_files = config["use_package_common_files"] + self.build_commons_path = config["build_commons_path"] + self.docker_build_commons_dir_name = config["docker_build_commons_dir_name"] + self.docker_publish_tag = config["docker_publish_tag"] def docker_build_commons_path(self): - list = [self.build_commons_path, - self.docker_build_commons_dir_name] - return '/'.join(filter_none(list)) + '/' + list = [self.build_commons_path, self.docker_build_commons_dir_name] + return "/".join(filter_none(list)) + "/" + + +class C4kBuild(Validateable): + def __init__(self, project, config): + self.build = Build(project, config) + self.c4k_mixin_config = config["C4kMixin"]["Config"] + self.c4k_mixin_auth = config["C4kMixin"]["Auth"] + self.c4k_module_name = config["C4kMixin"]["Name"] + tmp = self.c4k_mixin_config["mon-cfg"] + tmp.update({"cluster-name": self.c4k_module_name, "cluster-stage": self.build.stage}) + self.c4k_mixin_config.update({"mon-cfg": tmp}) diff --git a/src/test/python/__init__.py b/src/test/python/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/test/test_c4k_mixin.py b/src/test/python/test_c4k_mixin.py similarity index 100% rename from src/test/test_c4k_mixin.py rename to src/test/python/test_c4k_mixin.py diff --git a/src/test/test_domain.py b/src/test/test_domain.py deleted file mode 100644 index 3ef12f0..0000000 --- a/src/test/test_domain.py +++ /dev/null @@ -1,41 +0,0 @@ -from src.main.python.ddadevops.domain import Validateable - - -class TestValidateable(Validateable): - def __init__(self, value): - self.field = value - - def validate(self): - return self.__validate_is_not_empty__('field') - - -def test_should_validate_non_empty_strings(): - - sut = TestValidateable("content") - assert sut.is_valid() - - sut = TestValidateable(None) - assert not sut.is_valid() - - sut = TestValidateable('') - assert not sut.is_valid() - - -def test_should_validate_non_empty_others(): - - sut = TestValidateable(1) - assert sut.is_valid() - - sut = TestValidateable(1.0) - assert sut.is_valid() - - sut = TestValidateable(True) - assert sut.is_valid() - - sut = TestValidateable(None) - assert not sut.is_valid() - -def test_validate_with_reason(): - - sut = TestValidateable(None) - assert sut.validate()[0] == "Field 'field' may not be empty."