provs config now is done by domain object

This commit is contained in:
Michael Jerger 2023-05-22 12:51:35 +02:00
parent 643602e5b7
commit fc58f2e807
8 changed files with 53 additions and 48 deletions

View file

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

View file

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

View file

@ -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 = []

View file

@ -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
return Template(template_text)

View file

@ -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',

View file

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

View file

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

View file

@ -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()