diff --git a/src/main/python/ddadevops/__init__.py b/src/main/python/ddadevops/__init__.py index 0c54fe4..9ec2d16 100644 --- a/src/main/python/ddadevops/__init__.py +++ b/src/main/python/ddadevops/__init__.py @@ -15,6 +15,7 @@ from .aws_mfa_mixin import AwsMfaMixin, add_aws_mfa_mixin_config from .aws_rds_pg_mixin import AwsRdsPgMixin, add_aws_rds_pg_mixin_config from .dda_tenant_mixin import DdaTenantMixin, add_dda_tenant_mixin_config from .dda_simple_mixin import DdaSimpleMixin, add_dda_simple_mixin_config +from .provs_k3s_mixin import ProvsK3sMixin, add_provs_k3s_mixin_config from .python_util import execute __version__ = "${version}" \ No newline at end of file diff --git a/src/main/python/ddadevops/devops_build.py b/src/main/python/ddadevops/devops_build.py index 230f3b2..7ccfca0 100644 --- a/src/main/python/ddadevops/devops_build.py +++ b/src/main/python/ddadevops/devops_build.py @@ -50,3 +50,9 @@ class DevopsBuild: def get(self, key): return self.stack[key] + + def get_keys(self, keys): + result = {} + for key in keys: + result[key] = self.get(key) + return result diff --git a/src/main/python/ddadevops/provs_k3s_mixin.py b/src/main/python/ddadevops/provs_k3s_mixin.py new file mode 100644 index 0000000..3c91ff1 --- /dev/null +++ b/src/main/python/ddadevops/provs_k3s_mixin.py @@ -0,0 +1,111 @@ +from asyncio.windows_events import NULL +from string import Template +from subprocess import run +from .python_util import * +from .devops_build import DevopsBuild + + +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 +""" + + +def add_provs_k3s_mixin_config(config, + provision_user='root', + echo=None, + k3s_config_template=None, + letsencrypt_email=None, + letsencrypt_endpoint=None, + fqdn=None, + ipv4=None, + ipv6=None, + app_filename_to_provision=None): + template_text = k3s_config_template + if(template_text == None): + template_text = config_base + if(letsencrypt_endpoint != None): + template_text += config_certmanager + if(echo != None): + template_text += config_echo + if(ipv4 != None): + template_text += config_ipv4 + if(ipv6 != None): + template_text += config_ipv6 + + config.update({'ProvsK3sMixin': + {'fqdn': fqdn, + 'provision_user': provision_user, + 'ipv4': ipv4, + 'ipv6': ipv6, + 'letsencrypt_email': letsencrypt_email, + 'letsencrypt_endpoint': letsencrypt_endpoint, + 'echo': echo, + 'k3s_config_template': template_text, + 'app_filename_to_provision': app_filename_to_provision}}) + return config + + +class ProvsK3sMixin(DevopsBuild): + + def __init__(self, project, config): + super().__init__(project, config) + provs_k3s_mixin_config = config['ProvsK3sMixin'] + self.fqdn = provs_k3s_mixin_config['fqdn'] + self.provision_user = provs_k3s_mixin_config['provision_user'] + self.ipv4 = provs_k3s_mixin_config['ipv4'] + self.ipv6 = provs_k3s_mixin_config['ipv6'] + self.letsencrypt_email = provs_k3s_mixin_config['letsencrypt_email'] + self.letsencrypt_endpoint = provs_k3s_mixin_config['letsencrypt_endpoint'] + self.echo = provs_k3s_mixin_config['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.app_filename_to_provision = provs_k3s_mixin_config['app_filename_to_provision'] + + 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 != None): + template_text += config_ipv4 + if(ipv6 != 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 write_provs_config(self): + substitutes = self.get_keys(['fqdn', 'ipv4', 'ipv6', 'letsencrypt_email', + 'letsencrypt_endpoint', 'echo']) + with open(self.build_path() + '/out_k3sServerConfig.yaml', "w") as output_file: + output_file.write(self.k3sConfigTemplate.substitute(substitutes)) + + def provs_server(self, dry_run=False): + result = '' + cmd = ['provs-server.jar', 'k3s', self.provision_user + '@' + self.fqdn, '-c', + self.build_path() + '/out_k3sServerConfig.yaml', + '-a', self.build_path() + '/' + self.app_filename_to_provision] + prn_cmd = list(cmd) + print(" ".join(prn_cmd)) + if (not dry_run): + result = execute(cmd) + print(result) + + return result