last wip comment :-)

This commit is contained in:
Michael Jerger 2023-03-05 13:01:50 +01:00
parent 68ffbf23e2
commit 2acda459b2
8 changed files with 86 additions and 88 deletions

View file

@ -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 .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 .credential import gopass_password_from_path, gopass_field_from_path
from .domain import Validateable, Build, DockerBuild from .domain import Validateable, Build, DockerBuild, C4kBuild
from .application import BuildService, DockerBuildService from .application import BuildService, DockerBuildService, C4kBuildService
__version__ = "${version}" __version__ = "${version}"

View file

@ -1,4 +1,4 @@
from .domain import Build, DockerBuild from .domain import Build, DockerBuild, C4kBuild
from .infrastructure import FileApi, ResourceApi, DockerApi from .infrastructure import FileApi, ResourceApi, DockerApi
@ -52,3 +52,25 @@ class DockerBuildService():
def test(self, build: DockerBuild): def test(self, build: DockerBuild):
self.docker_api.test(build.name(), build.build_path()) 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

View file

@ -1,5 +1,7 @@
from os import chmod from os import chmod
import yaml import yaml
from .domain import C4kBuild
from .application import C4kBuildService
from .python_util import execute from .python_util import execute
from .devops_build import DevopsBuild from .devops_build import DevopsBuild
from .credential import gopass_field_from_path, gopass_password_from_path from .credential import gopass_field_from_path, gopass_password_from_path
@ -32,7 +34,9 @@ def add_c4k_mixin_config(config,
class C4kMixin(DevopsBuild): class C4kMixin(DevopsBuild):
def __init__(self, project, config): 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_config = config['C4kMixin']['Config']
self.c4k_mixin_auth = config['C4kMixin']['Auth'] self.c4k_mixin_auth = config['C4kMixin']['Auth']
self.c4k_module_name = config['C4kMixin']['Name'] self.c4k_module_name = config['C4kMixin']['Name']

View file

@ -1,31 +1,41 @@
from .domain import Build from .domain import Build
from .application import BuildService from .application import BuildService
def create_devops_build_config(stage, project_root_path, module,
build_dir_name='target'): def create_devops_build_config(
return {'stage': stage, stage, project_root_path, module, build_dir_name="target"
'project_root_path': project_root_path, ):
'module': module, return {
'build_dir_name': build_dir_name} "stage": stage,
"project_root_path": project_root_path,
"module": module,
"build_dir_name": build_dir_name,
}
def get_devops_build(project): def get_devops_build(project):
return project.get_property('devops_build') return project.get_property("devops_build")
# TODO: Remove from here! # TODO: Remove from here!
def get_tag_from_latest_commit(): def get_tag_from_latest_commit():
try: try:
value = run('git describe --abbrev=0 --tags --exact-match', shell=True, value = run(
capture_output=True, check=True) "git describe --abbrev=0 --tags --exact-match",
return value.stdout.decode('UTF-8').rstrip() shell=True,
capture_output=True,
check=True,
)
return value.stdout.decode("UTF-8").rstrip()
except CalledProcessError: except CalledProcessError:
return None return None
class DevopsBuild:
class DevopsBuild:
def __init__(self, project, config): def __init__(self, project, config):
self.build = Build(project, config) self.build = Build(project, config)
self.build_service = BuildService() self.build_service = BuildService()
project.set_property('devops_build', self) project.set_property("devops_build", self)
def name(self): def name(self):
return self.build.name() return self.build.name()

View file

@ -2,10 +2,10 @@ from typing import List
from .python_util import filter_none from .python_util import filter_none
class Validateable(): class Validateable:
def __validate_is_not_empty__(self, field_name: str) -> List[str]: def __validate_is_not_empty__(self, field_name: str) -> List[str]:
value = self.__dict__[field_name] 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."] return [f"Field '{field_name}' may not be empty."]
else: else:
return [] return []
@ -17,16 +17,12 @@ class Validateable():
return len(self.validate()) < 1 return len(self.validate()) < 1
class Build(Validateable): class Build(Validateable):
def __init__(self, project, config): def __init__(self, project, config):
# deprecate stage self.stage = config["stage"]
self.stage = config['stage'] self.project_root_path = config["project_root_path"]
self.project_root_path = config['project_root_path'] self.module = config["module"]
self.module = config['module'] self.build_dir_name = config["build_dir_name"]
self.build_dir_name = config['build_dir_name']
self.stack = {} self.stack = {}
self.project = project self.project = project
@ -34,37 +30,44 @@ class Build(Validateable):
return self.project.name return self.project.name
def build_path(self): def build_path(self):
path = [self.project_root_path, path = [self.project_root_path, self.build_dir_name, self.name(), self.module]
self.build_dir_name, return "/".join(filter_none(path))
self.name(),
self.module]
return '/'.join(filter_none(path))
def put(self, key, value): def __put__(self, key, value):
self.stack[key] = value self.stack[key] = value
def get(self, key): def __get__(self, key):
return self.stack[key] return self.stack[key]
def get_keys(self, keys): def __get_keys__(self, keys):
result = {} result = {}
for key in keys: for key in keys:
result[key] = self.get(key) result[key] = self.get(key)
return result return result
class DockerBuild(Build): class DockerBuild(Validateable):
def __init__(self, project, config): def __init__(self, project, config):
super().__init__(project, config) project.build_depends_on("dda-python-terraform")
project.build_depends_on('dda-python-terraform') self.build = Build(project, config)
self.dockerhub_user = config['dockerhub_user'] self.dockerhub_user = config["dockerhub_user"]
self.dockerhub_password = config['dockerhub_password'] self.dockerhub_password = config["dockerhub_password"]
self.use_package_common_files = config['use_package_common_files'] self.use_package_common_files = config["use_package_common_files"]
self.build_commons_path = config['build_commons_path'] self.build_commons_path = config["build_commons_path"]
self.docker_build_commons_dir_name = config['docker_build_commons_dir_name'] self.docker_build_commons_dir_name = config["docker_build_commons_dir_name"]
self.docker_publish_tag = config['docker_publish_tag'] self.docker_publish_tag = config["docker_publish_tag"]
def docker_build_commons_path(self): def docker_build_commons_path(self):
list = [self.build_commons_path, list = [self.build_commons_path, self.docker_build_commons_dir_name]
self.docker_build_commons_dir_name] return "/".join(filter_none(list)) + "/"
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})

View file

View file

@ -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."