add c4k-mixin
parent
1b9e7f20fd
commit
b38b0f1f87
@ -0,0 +1,66 @@
|
||||
from os import chmod
|
||||
from .python_util import execute
|
||||
from .devops_build import DevopsBuild
|
||||
from .credential import gopass_field_from_path, gopass_password_from_path
|
||||
|
||||
def add_c4k_mixin_config(config,
|
||||
c4k_module_name,
|
||||
c4k_config_dict,
|
||||
c4k_auth_dict,
|
||||
grafana_cloud_user=None,
|
||||
grafana_cloud_password=None,
|
||||
grafana_cloud_url='https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push'):
|
||||
if not grafana_cloud_user:
|
||||
grafana_cloud_user = gopass_field_from_path('server/meissa/grafana-cloud', 'grafana-cloud-user')
|
||||
if not grafana_cloud_password:
|
||||
grafana_cloud_password = gopass_password_from_path('server/meissa/grafana-cloud')
|
||||
c4k_auth_dict.update({'mon-auth': {
|
||||
'grafana-cloud-user': grafana_cloud_user,
|
||||
'grafana-cloud-password': grafana_cloud_password
|
||||
}})
|
||||
c4k_config_dict.update({'mon-config': {
|
||||
'grafana-cloud-url': grafana_cloud_url
|
||||
}})
|
||||
config.update({'C4kMixin': {'Config': c4k_config_dict,
|
||||
'Auth': c4k_auth_dict,
|
||||
'Name': c4k_module_name}})
|
||||
return config
|
||||
|
||||
|
||||
class C4kMixin(DevopsBuild):
|
||||
|
||||
def __init__(self, project, config):
|
||||
super().__init__(project, config)
|
||||
self.c4k_mixin_config = config['C4kMixin']['Config']
|
||||
self.c4k_mixin_auth = config['C4kMixin']['Auth']
|
||||
self.c4k_module_name = config['C4kMixin']['Name']
|
||||
self.c4k_mixin_config.update({'mon-config': {
|
||||
'cluster-name': f':{self.c4k_module_name}',
|
||||
'cluster-stage': f':{self.stage}'}})
|
||||
|
||||
def __generate_clojure_map(self, template_dict):
|
||||
clojure_map_str = '{'
|
||||
for key, value in template_dict.items():
|
||||
if isinstance(value, dict):
|
||||
clojure_map_str += f':{key} {self.__generate_clojure_map(value)}\n'
|
||||
else:
|
||||
clojure_map_str += f':{key} "{value}"\n'
|
||||
clojure_map_str += '}'
|
||||
return clojure_map_str
|
||||
|
||||
def write_c4k_config(self):
|
||||
fqdn = self.get('fqdn')
|
||||
self.c4k_mixin_config.update({'fqdn':fqdn})
|
||||
with open(self.build_path() + '/out_config.edn', 'w', encoding="utf-8") as output_file:
|
||||
output_file.write(self.__generate_clojure_map(self.c4k_mixin_config))
|
||||
|
||||
def write_c4k_auth(self):
|
||||
with open(self.build_path() + '/out_auth.edn', 'w', encoding="utf-8") as output_file:
|
||||
output_file.write(self.__generate_clojure_map(self.c4k_mixin_auth))
|
||||
chmod(self.build_path() + '/out_auth.edn', 0o600)
|
||||
|
||||
def c4k_apply(self):
|
||||
cmd = f'c4k-{self.c4k_module_name}-standalone.jar {self.build_path()}/out_config.edn {self.build_path()}/out_auth.edn > {self.build_path()}/out_{self.c4k_module_name}.yaml'
|
||||
output = execute(cmd, True)
|
||||
print(output)
|
||||
return output
|
@ -0,0 +1,48 @@
|
||||
import os
|
||||
from pybuilder.core import Project
|
||||
from src.main.python.ddadevops.c4k_mixin import C4kMixin, add_c4k_mixin_config
|
||||
|
||||
class MyC4kMixin(C4kMixin):
|
||||
pass
|
||||
|
||||
def test_c4k_mixin(tmp_path):
|
||||
|
||||
build_dir = 'build'
|
||||
project_name = 'testing-project'
|
||||
module_name = 'c4k-test'
|
||||
tmp_path_str = str(tmp_path)
|
||||
|
||||
project = Project(tmp_path_str, name=project_name)
|
||||
|
||||
project_config = {
|
||||
'stage': 'test',
|
||||
'project_root_path': tmp_path_str,
|
||||
'module': module_name,
|
||||
'build_dir_name': build_dir
|
||||
}
|
||||
|
||||
config = {'a': 1, 'b': 2}
|
||||
auth = {'c': 3, 'd': 4}
|
||||
|
||||
add_c4k_mixin_config(project_config, module_name, config, auth, grafana_cloud_user='user', grafana_cloud_password='password')
|
||||
|
||||
assert project_config.get('C4kMixin') is not None
|
||||
assert project_config.get('C4kMixin').get('Name') is module_name
|
||||
assert project_config.get('C4kMixin').get('Config') is config
|
||||
assert project_config.get('C4kMixin').get('Auth') is auth
|
||||
|
||||
mixin = MyC4kMixin(project, project_config)
|
||||
mixin.initialize_build_dir()
|
||||
assert mixin.build_path() == f'{tmp_path_str}/{build_dir}/{project_name}/{module_name}'
|
||||
|
||||
mixin.put('fqdn', 'testing.test')
|
||||
|
||||
mixin.write_c4k_config()
|
||||
assert 'fqdn' in mixin.c4k_mixin_config
|
||||
assert 'mon-config' in mixin.c4k_mixin_config
|
||||
assert os.path.exists(f'{mixin.build_path()}/out_config.edn')
|
||||
|
||||
mixin.write_c4k_auth()
|
||||
assert 'mon-auth' in mixin.c4k_mixin_auth
|
||||
assert os.path.exists(f'{mixin.build_path()}/out_auth.edn')
|
||||
|
Loading…
Reference in New Issue