init now will work

This commit is contained in:
Michael Jerger 2023-05-22 11:54:34 +02:00
parent 0f212782d5
commit 643602e5b7
4 changed files with 67 additions and 22 deletions

View file

@ -1,10 +1,60 @@
from typing import List, Optional from typing import List, Optional
from string import Template
from .common import ( from .common import (
Validateable, Validateable,
DnsRecord, DnsRecord,
Devops, Devops,
) )
CONFIG_BASE = """
fqdn: $fqdn
"""
CONFIG_IPV4 = """node:
ipv4: $ipv4
"""
CONFIG_IPV6 = """ ipv6: $ipv6
"""
CONFIG_CERTMANAGER = """certmanager:
email: $letsencrypt_email
letsencryptEndpoint: $letsencrypt_endpoint
"""
CONFIG_ECHO = """echo: $echo
"""
class K3s(Validateable): class K3s(Validateable):
def __init__(self, inp: dict): def __init__(self, inp: dict):
pass 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_provs_template = inp.get("k3s_provs_template", None)
def validate(self) -> List[str]:
result = []
result += self.__validate_is_not_empty__("k3s_provision_user")
result += self.__validate_is_not_empty__("k3s_letsencrypt_email")
result += self.__validate_is_not_empty__("k3s_letsencrypt_endpoint")
result += self.__validate_is_not_empty__("k3s_app_filename_to_provision")
if self.provision_dns:
result += self.provision_dns.validate()
return result
def config_template(self) -> str:
template_text = self.k3s_provs_template
if template_text is None:
template_text = CONFIG_BASE
if self.k3s_letsencrypt_endpoint is not None:
template_text += CONFIG_CERTMANAGER
if self.k3s_enable_echo is not None:
template_text += CONFIG_ECHO
if self.provision_dns.ipv4 is not None:
template_text += CONFIG_IPV4
if self.provision_dns.ipv6 is not None:
template_text += CONFIG_IPV6
return template_text

View file

@ -66,27 +66,9 @@ class ProvsK3sMixin(DevopsBuild):
inp["build_types"]=config.get("build_types", []) inp["build_types"]=config.get("build_types", [])
inp["mixin_types"]=config.get("mixin_types", []) inp["mixin_types"]=config.get("mixin_types", [])
super().__init__(project, inp) super().__init__(project, inp)
provs_k3s_mixin_config = config['ProvsK3sMixin'] devops = self.devops_repo.get_devops(self.project)
self.fqdn = provs_k3s_mixin_config['fqdn'] if BuildType.K3S not in devops.specialized_builds:
self.put('fqdn', self.fqdn) raise ValueError("K3SBuild requires BuildType.K3S")
self.provision_user = provs_k3s_mixin_config['provision_user']
self.put('provision_user', self.provision_user)
self.ipv4 = provs_k3s_mixin_config['ipv4']
self.put('ipv4', self.ipv4)
self.ipv6 = provs_k3s_mixin_config['ipv6']
self.put('ipv6', self.ipv6)
self.letsencrypt_email = provs_k3s_mixin_config['letsencrypt_email']
self.put('letsencrypt_email', self.letsencrypt_email)
self.letsencrypt_endpoint = provs_k3s_mixin_config['letsencrypt_endpoint']
self.put('letsencrypt_endpoint', self.letsencrypt_endpoint)
self.echo = provs_k3s_mixin_config['echo']
self.put('echo', self.echo)
self.k3s_config_template_text = provs_k3s_mixin_config['k3s_config_template']
self.k3s_config_template = Template(
provs_k3s_mixin_config['k3s_config_template'])
self.put('k3s_config_template', self.k3s_config_template)
self.app_filename_to_provision = provs_k3s_mixin_config['app_filename_to_provision']
self.put('app_filename_to_provision', self.app_filename_to_provision)
def update_runtime_config(self, fqdn, ipv4, ipv6=None): def update_runtime_config(self, fqdn, ipv4, ipv6=None):
self.fqdn = fqdn self.fqdn = fqdn

View file

@ -19,6 +19,14 @@ def devops_config(overrides: dict) -> dict:
"c4k_grafana_cloud_password": "password", "c4k_grafana_cloud_password": "password",
"c4k_grafana_cloud_url": "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push", "c4k_grafana_cloud_url": "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push",
"c4k_auth": {}, "c4k_auth": {},
"k3s_provision_user": "k3s_provision_user",
"k3s_letsencrypt_email": "k3s_letsencrypt_email",
"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_type": "NONE",
"release_main_branch": "main", "release_main_branch": "main",
"release_current_branch": "my_feature", "release_current_branch": "my_feature",

View file

@ -11,3 +11,8 @@ from .helper import build_devops
def test_creation(): def test_creation():
sut = build_devops({}) sut = build_devops({})
assert BuildType.K3S in sut.specialized_builds assert BuildType.K3S in sut.specialized_builds
assert sut.specialized_builds[BuildType.K3S]
def test_should_calculate_template():
sut = build_devops({}).specialized_builds[BuildType.K3S]
assert "fqdn:" in sut.config_template()