From fc58f2e80788fa60343c6af10f847b5ce1386a6a Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Mon, 22 May 2023 12:51:35 +0200 Subject: [PATCH] provs config now is done by domain object --- doc/architecture/BuildAndMixins.md | 6 +-- src/main/python/ddadevops/__init__.py | 2 +- src/main/python/ddadevops/domain/c4k.py | 2 +- src/main/python/ddadevops/domain/provs_k3s.py | 39 ++++++++++++++----- ...{provs_k3s_mixin.py => provs_k3s_build.py} | 37 ++++++------------ src/test/python/domain/helper.py | 3 -- src/test/python/domain/test_c4k.py | 4 +- src/test/python/domain/test_provs_k3s.py | 8 +++- 8 files changed, 53 insertions(+), 48 deletions(-) rename src/main/python/ddadevops/{provs_k3s_mixin.py => provs_k3s_build.py} (72%) diff --git a/doc/architecture/BuildAndMixins.md b/doc/architecture/BuildAndMixins.md index 75090a1..b4280cf 100644 --- a/doc/architecture/BuildAndMixins.md +++ b/doc/architecture/BuildAndMixins.md @@ -72,8 +72,8 @@ classDiagram tag_and_push_release() } - class ProvsK3sMixin { - // ProvsK3sMixin -> ProvsK3sBuild + class ProvsK3sBuild { + // ProvsK3sBuild -> ProvsK3sBuild def update_runtime_config(fqdn, ipv4, ipv6=None) write_provs_config() provs_apply(dry_run=False) @@ -95,7 +95,7 @@ classDiagram DevopsTerraformBuild <|-- DigitaloceanTerraformBuild DevopsTerraformBuild <|--ExoscaleMixin DevopsTerraformBuild <|--HetznerMixin - DevopsBuild <|-- ProvsK3sMixin + DevopsBuild <|-- ProvsK3sBuild DigitaloceanTerraformBuild <|-- DigitaloceanBackendPropertiesMixin AwsBackendPropertiesMixin <|-- AwsMfaMixin diff --git a/src/main/python/ddadevops/__init__.py b/src/main/python/ddadevops/__init__.py index c856a40..aab35f6 100644 --- a/src/main/python/ddadevops/__init__.py +++ b/src/main/python/ddadevops/__init__.py @@ -5,7 +5,7 @@ terraform, dda-pallet, aws & hetzner-cloud. """ from .python_util import execute -from .provs_k3s_mixin import ProvsK3sMixin, add_provs_k3s_mixin_config +from .provs_k3s_build import ProvsK3sBuild, add_provs_k3s_mixin_config from .aws_mfa_mixin import AwsMfaMixin, add_aws_mfa_mixin_config from .aws_backend_properties_mixin import AwsBackendPropertiesMixin, add_aws_backend_properties_mixin_config from .c4k_build import C4kBuild, add_c4k_mixin_config diff --git a/src/main/python/ddadevops/domain/c4k.py b/src/main/python/ddadevops/domain/c4k.py index 16b8760..e5cb060 100644 --- a/src/main/python/ddadevops/domain/c4k.py +++ b/src/main/python/ddadevops/domain/c4k.py @@ -21,9 +21,9 @@ class C4k(Validateable): self.c4k_grafana_cloud_password = inp.get('c4k_grafana_cloud_password') self.dns_record: Optional[DnsRecord] = None - # TODO: these functions should be located at TerraformBuild later on. def update_runtime_config(self, dns_record: DnsRecord): self.dns_record = dns_record + self.throw_if_invalid() def validate(self) -> List[str]: result = [] diff --git a/src/main/python/ddadevops/domain/provs_k3s.py b/src/main/python/ddadevops/domain/provs_k3s.py index 0cc3395..6170bd8 100644 --- a/src/main/python/ddadevops/domain/provs_k3s.py +++ b/src/main/python/ddadevops/domain/provs_k3s.py @@ -21,18 +21,16 @@ CONFIG_CERTMANAGER = """certmanager: CONFIG_ECHO = """echo: $echo """ + class K3s(Validateable): def __init__(self, inp: dict): self.k3s_provision_user = inp.get("k3s_provision_user") self.k3s_letsencrypt_email = inp.get("k3s_letsencrypt_email") self.k3s_letsencrypt_endpoint = inp.get("k3s_letsencrypt_endpoint") - self.k3s_app_filename_to_provision = inp.get("k3s_app_filename_to_provision", "provs") - fqdn = inp.get("k3_fqdn") - ipv4 = inp.get("k3_ipv4") - ipv6 = inp.get("k3_ipv6") - self.provision_dns = DnsRecord(fqdn, ipv4=ipv4, ipv6=ipv6) - self.k3s_enable_echo = inp.get("k3s_enable_echo", False) + self.k3s_app_filename_to_provision = inp.get("k3s_app_filename_to_provision") + self.k3s_enable_echo = inp.get("k3s_enable_echo", "false") self.k3s_provs_template = inp.get("k3s_provs_template", None) + self.provision_dns: Optional[DnsRecord] = None def validate(self) -> List[str]: result = [] @@ -44,7 +42,29 @@ class K3s(Validateable): result += self.provision_dns.validate() return result - def config_template(self) -> str: + def update_runtime_config(self, dns_record: DnsRecord): + self.provision_dns = dns_record + self.throw_if_invalid() + + def provs_config(self) -> str: + if not self.provision_dns: + raise ValueError("provision_dns was not set.") + substitutes = { + "fqdn": self.provision_dns.fqdn, + } + if self.provision_dns.ipv4 is not None: + substitutes["ipv4"] = self.provision_dns.ipv4 + if self.provision_dns.ipv6 is not None: + substitutes["ipv6"] = self.provision_dns.ipv6 + if self.k3s_letsencrypt_email is not None: + substitutes["letsencrypt_email"] = self.k3s_letsencrypt_email + if self.k3s_letsencrypt_endpoint is not None: + substitutes["letsencrypt_endpoint"] = self.k3s_letsencrypt_endpoint + if self.k3s_enable_echo is not None: + substitutes["echo"] = self.k3s_enable_echo + return self.__config_template__().substitute(substitutes) + + def __config_template__(self) -> Template: template_text = self.k3s_provs_template if template_text is None: template_text = CONFIG_BASE @@ -54,7 +74,6 @@ class K3s(Validateable): template_text += CONFIG_ECHO if self.provision_dns.ipv4 is not None: template_text += CONFIG_IPV4 - if self.provision_dns.ipv6 is not None: + if self.provision_dns.ipv6 is not None: template_text += CONFIG_IPV6 - return template_text - \ No newline at end of file + return Template(template_text) diff --git a/src/main/python/ddadevops/provs_k3s_mixin.py b/src/main/python/ddadevops/provs_k3s_build.py similarity index 72% rename from src/main/python/ddadevops/provs_k3s_mixin.py rename to src/main/python/ddadevops/provs_k3s_build.py index 65c1e71..386b3c1 100644 --- a/src/main/python/ddadevops/provs_k3s_mixin.py +++ b/src/main/python/ddadevops/provs_k3s_build.py @@ -1,9 +1,11 @@ from string import Template import deprecation from .python_util import execute_live +from .domain import DnsRecord from .devops_build import DevopsBuild + CONFIG_BASE = """ fqdn: $fqdn """ @@ -42,7 +44,7 @@ def add_provs_k3s_mixin_config(config, if ipv6 is not None: template_text += CONFIG_IPV6 - config.update({'ProvsK3sMixin': + config.update({'ProvsK3sBuild': {'fqdn': fqdn, 'provision_user': provision_user, 'ipv4': ipv4, @@ -55,7 +57,7 @@ def add_provs_k3s_mixin_config(config, return config -class ProvsK3sMixin(DevopsBuild): +class ProvsK3sBuild(DevopsBuild): def __init__(self, project, config): inp = config.copy() @@ -70,33 +72,16 @@ class ProvsK3sMixin(DevopsBuild): if BuildType.K3S not in devops.specialized_builds: raise ValueError("K3SBuild requires BuildType.K3S") - def update_runtime_config(self, fqdn, ipv4, ipv6=None): - self.fqdn = fqdn - self.put('fqdn', fqdn) - self.ipv4 = ipv4 - self.put('ipv4', ipv4) - self.ipv6 = ipv6 - self.put('ipv6', ipv6) - template_text = self.k3s_config_template_text - if ipv4 is not None: - template_text += CONFIG_IPV4 - if ipv6 is not None: - template_text += CONFIG_IPV6 - self.k3s_config_template_text = template_text - self.put('k3s_config_template_text', template_text) - template = Template(template_text) - self.k3s_config_template = template - self.put('k3s_config_template', template) + def update_runtime_config(self, dns_record: DnsRecord): + devops = self.devops_repo.get_devops(self.project) + devops.specialized_builds[BuildType.K3S].update_runtime_config(dns_record) + self.devops_repo.set_devops(self.project, devops) def write_provs_config(self): - substitutes = self.get_keys(['fqdn', 'ipv4', 'ipv6', 'letsencrypt_email', - 'letsencrypt_endpoint', 'echo']) + devops = self.devops_repo.get_devops(self.project) + k3s = devops.specialized_builds[BuildType.K3S] with open(self.build_path() + '/out_k3sServerConfig.yaml', "w", encoding="utf-8") as output_file: - output_file.write(self.k3s_config_template.substitute(substitutes)) - - @deprecation.deprecated(deprecated_in="3.1") - def provs_server(self, dry_run=False): - self.provs_apply(dry_run) + output_file.write(k3s.provs_config()) def provs_apply(self, dry_run=False): cmd = ['provs-server.jar', 'k3s', self.provision_user + '@' + self.fqdn, '-c', diff --git a/src/test/python/domain/helper.py b/src/test/python/domain/helper.py index ba53370..a966272 100644 --- a/src/test/python/domain/helper.py +++ b/src/test/python/domain/helper.py @@ -24,9 +24,6 @@ def devops_config(overrides: dict) -> dict: "k3s_letsencrypt_endpoint": "k3s_letsencrypt_endpoint", "k3s_enable_echo": False, "k3s_app_filename_to_provision": "provs", - "k3_fqdn": "example.org", - "k3_ipv4": "1.2.3.4", - "k3_ipv6": "::1", "release_type": "NONE", "release_main_branch": "main", "release_current_branch": "my_feature", diff --git a/src/test/python/domain/test_c4k.py b/src/test/python/domain/test_c4k.py index bc69f1c..029b829 100644 --- a/src/test/python/domain/test_c4k.py +++ b/src/test/python/domain/test_c4k.py @@ -20,7 +20,7 @@ def test_c4k_should_calculate_config(): sut = build_devops({}) c4k = sut.specialized_builds[BuildType.C4K] - c4k.update_runtime_config(DnsRecord("fqdn")) + c4k.update_runtime_config(DnsRecord("fqdn", ipv6="::1")) assert { "fqdn": "fqdn", "mon-cfg": { @@ -36,7 +36,7 @@ def test_c4k_should_calculate_config(): } ) c4k = sut.specialized_builds[BuildType.C4K] - c4k.update_runtime_config(DnsRecord("fqdn")) + c4k.update_runtime_config(DnsRecord("fqdn", ipv6="::1")) assert { "test": "test", "fqdn": "fqdn", diff --git a/src/test/python/domain/test_provs_k3s.py b/src/test/python/domain/test_provs_k3s.py index e4693b1..bbde9f0 100644 --- a/src/test/python/domain/test_provs_k3s.py +++ b/src/test/python/domain/test_provs_k3s.py @@ -13,6 +13,10 @@ def test_creation(): assert BuildType.K3S in sut.specialized_builds assert sut.specialized_builds[BuildType.K3S] -def test_should_calculate_template(): +def test_should_calculate_provs_config(): sut = build_devops({}).specialized_builds[BuildType.K3S] - assert "fqdn:" in sut.config_template() + sut.update_runtime_config( + DnsRecord("example.org", ipv6="::1") + ) + assert "fqdn:" in sut.provs_config() + assert not "$" in sut.provs_config()