* make executabel-name configurable

* mv infra code from app to infra
This commit is contained in:
Michael Jerger 2023-03-11 17:16:49 +01:00
parent c23c5287bd
commit ce2e2635f5
6 changed files with 137 additions and 62 deletions

View file

@ -1,11 +1,5 @@
from .domain import Build, DockerBuild, C4kBuild from .domain import Build, DockerBuild, C4kBuild
from .infrastructure import FileApi, ResourceApi, DockerApi from .infrastructure import FileApi, ResourceApi, DockerApi, ExecutionApi
from os import chmod
import yaml
from .python_util import execute
from .credential import gopass_field_from_path, gopass_password_from_path
class BuildService: class BuildService:
@ -26,7 +20,7 @@ class DockerBuildService:
data = self.resource_api.read_resource( data = self.resource_api.read_resource(
"src/main/resources/docker/" + build.name "src/main/resources/docker/" + build.name
) )
self.file_api.write_to_file(build.build_path() + "/" + build.name, data) self.file_api.write_data_to_file(build.build_path() + "/" + build.name, data)
def __copy_build_resources_from_package__(self, build: DockerBuild): def __copy_build_resources_from_package__(self, build: DockerBuild):
self.__copy_build_resource_file_from_package__( self.__copy_build_resource_file_from_package__(
@ -62,32 +56,19 @@ class DockerBuildService:
def test(self, build: DockerBuild): def test(self, build: DockerBuild):
self.docker_api.test(build.name(), build.build_path()) self.docker_api.test(build.name(), build.build_path())
# TODO: move infrastructure fktns to infra apis
class C4kBuildService: class C4kBuildService:
def __init__(self): def __init__(self):
self.file_api = FileApi() self.file_api = FileApi()
self.execution_api = ExecutionApi()
def write_c4k_config(self, c4k_build: C4kBuild): def write_c4k_config(self, c4k_build: C4kBuild):
with open( path = c4k_build.build.build_path() + "/out_c4k_config.yaml"
c4k_build.build.build_path() + "/out_c4k_config.yaml", "w", encoding="utf-8" self.file_api.write_yaml_to_file(path, c4k_build.config())
) as output_file:
yaml.dump(c4k_build.config(), output_file)
def write_c4k_auth(self, c4k_build: C4kBuild): def write_c4k_auth(self, c4k_build: C4kBuild):
with open( path = c4k_build.build.build_path() + "/out_c4k_auth.yaml"
c4k_build.build.build_path() + "/out_c4k_auth.yaml", "w", encoding="utf-8" self.file_api.write_yaml_to_file(path, c4k_build.c4k_mixin_auth)
) as output_file:
yaml.dump(c4k_build.c4k_mixin_auth, output_file)
chmod(c4k_build.build.build_path() + "/out_c4k_auth.yaml", 0o600)
def c4k_apply(self, c4k_build: C4kBuild, ry_run=False): def c4k_apply(self, c4k_build: C4kBuild, dry_run=False):
module = c4k_build.build.module return self.execution_api.execute(c4k_build.command(), dry_run)
build_path = c4k_build.build.build_path()
cmd = f"c4k-{module}-standalone.jar {build_path}/out_c4k_config.yaml {build_path}/out_c4k_auth.yaml > {build_path}/out_{module}.yaml"
output = ""
if dry_run:
print(cmd)
else:
output = execute(cmd, True)
print(output)
return output

View file

@ -3,27 +3,42 @@ from .application import C4kBuildService
from .devops_build import DevopsBuild from .devops_build import DevopsBuild
from .credential import gopass_field_from_path, gopass_password_from_path from .credential import gopass_field_from_path, gopass_password_from_path
def add_c4k_mixin_config(config,
c4k_config_dict, def add_c4k_mixin_config(
c4k_auth_dict, config,
grafana_cloud_user=None, c4k_config_dict,
grafana_cloud_password=None, c4k_auth_dict,
grafana_cloud_url='https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push'): executabel_name=None,
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: if not grafana_cloud_user:
grafana_cloud_user = gopass_field_from_path( grafana_cloud_user = gopass_field_from_path(
'server/meissa/grafana-cloud', 'grafana-cloud-user') "server/meissa/grafana-cloud", "grafana-cloud-user"
)
if not grafana_cloud_password: if not grafana_cloud_password:
grafana_cloud_password = gopass_password_from_path( grafana_cloud_password = gopass_password_from_path(
'server/meissa/grafana-cloud') "server/meissa/grafana-cloud"
c4k_auth_dict.update({'mon-auth': { )
'grafana-cloud-user': grafana_cloud_user, c4k_auth_dict.update(
'grafana-cloud-password': grafana_cloud_password {
}}) "mon-auth": {
c4k_config_dict.update({'mon-cfg': { "grafana-cloud-user": grafana_cloud_user,
'grafana-cloud-url': grafana_cloud_url "grafana-cloud-password": grafana_cloud_password,
}}) }
config.update({'C4kMixin': {'Config': c4k_config_dict, }
'Auth': c4k_auth_dict}}) )
c4k_config_dict.update({"mon-cfg": {"grafana-cloud-url": grafana_cloud_url}})
config.update(
{
"C4kMixin": {
"executabel_name": executabel_name,
"config": c4k_config_dict,
"auth": c4k_auth_dict,
}
}
)
return config return config

View file

@ -71,8 +71,12 @@ class DockerBuild(Validateable):
class C4kBuild(Validateable): class C4kBuild(Validateable):
def __init__(self, build: Build, project, config): def __init__(self, build: Build, project, config):
self.build = build self.build = build
self.c4k_mixin_config = config["C4kMixin"]["Config"] tmp_executabel_name = config["C4kMixin"]["executabel_name"]
self.c4k_mixin_auth = config["C4kMixin"]["Auth"] if not tmp_executabel_name:
tmp_executabel_name = self.build.module
self.executabel_name = tmp_executabel_name
self.c4k_mixin_config = config["C4kMixin"]["config"]
self.c4k_mixin_auth = config["C4kMixin"]["auth"]
tmp = self.c4k_mixin_config["mon-cfg"] tmp = self.c4k_mixin_config["mon-cfg"]
tmp.update({"cluster-name": self.build.module, "cluster-stage": self.build.stage}) tmp.update({"cluster-name": self.build.module, "cluster-stage": self.build.stage})
self.c4k_mixin_config.update({"mon-cfg": tmp}) self.c4k_mixin_config.update({"mon-cfg": tmp})
@ -85,3 +89,10 @@ class C4kBuild(Validateable):
self.c4k_mixin_config.update({'fqdn': fqdn}) self.c4k_mixin_config.update({'fqdn': fqdn})
return self.c4k_mixin_config return self.c4k_mixin_config
def command(self):
module = self.build.module
build_path = self.build.build_path()
config_path = f"{build_path}/out_c4k_config.yaml"
auth_path = f"{build_path}/out_c4k_auth.yaml"
output_path = f"{build_path}/out_{module}.yaml"
return f"c4k-{self.executabel_name}-standalone.jar {config_path} {auth_path} > {output_path}"

View file

@ -1,6 +1,8 @@
from pathlib import Path from pathlib import Path
from sys import stdout from sys import stdout
from pkg_resources import resource_string from pkg_resources import resource_string
from os import chmod
import yaml
from .python_util import execute from .python_util import execute
class ResourceApi(): class ResourceApi():
@ -18,10 +20,15 @@ class FileApi():
def cp_recursive(self, src: str, target_dir: str): def cp_recursive(self, src: str, target_dir: str):
execute('cp -r ' + src + ' ' + target_dir, shell=True) execute('cp -r ' + src + ' ' + target_dir, shell=True)
def write_to_file(self, path: Path, data: bytes): def write_data_to_file(self, path: Path, data: bytes):
with open(path, "w", encoding=stdout.encoding) as output_file: with open(path, "w", encoding="utf-8") as output_file:
output_file.write(data.decode(stdout.encoding)) output_file.write(data.decode(stdout.encoding))
def write_yaml_to_file(self, path: Path, data: map):
with open(path, "w", encoding="utf-8") as output_file:
yaml.dump(data, output_file)
chmod(path, 0o600)
class DockerApi(): class DockerApi():
def image(self, name: str, path: Path): def image(self, name: str, path: Path):
execute('docker build -t ' + name + execute('docker build -t ' + name +
@ -51,3 +58,13 @@ class DockerApi():
execute('docker build -t ' + name + '-test ' + execute('docker build -t ' + name + '-test ' +
'--file ' + path + '/test/Dockerfile ' '--file ' + path + '/test/Dockerfile '
+ path + '/test', shell=True) + path + '/test', shell=True)
class ExecutionApi():
def execute(command: str, dry_run=False):
output = ""
if dry_run:
print(command)
else:
output = execute(command, True)
print(output)
return output

View file

@ -27,9 +27,7 @@ def test_c4k_mixin(tmp_path):
add_c4k_mixin_config(project_config, config, auth, grafana_cloud_user='user', grafana_cloud_password='password') add_c4k_mixin_config(project_config, config, auth, grafana_cloud_user='user', grafana_cloud_password='password')
assert project_config.get('C4kMixin') is not None assert project_config.get('C4kMixin') is not None
assert project_config.get('C4kMixin').get('Config') is config
assert project_config.get('C4kMixin').get('Auth') is auth
mixin = MyC4kMixin(project, project_config) mixin = MyC4kMixin(project, project_config)
mixin.initialize_build_dir() mixin.initialize_build_dir()
assert mixin.build_path() == f'{tmp_path_str}/{build_dir}/{project_name}/{module_name}' assert mixin.build_path() == f'{tmp_path_str}/{build_dir}/{project_name}/{module_name}'

View file

@ -42,11 +42,11 @@ def test_validate_with_reason():
def test_c4k_build_should_update_fqdn(tmp_path): def test_c4k_build_should_update_fqdn(tmp_path):
project = Project(str(tmp_path), name='test-project') project = Project(str(tmp_path), name="test-project")
project_config = { project_config = {
"stage": "test", "stage": "test",
"project_root_path": str(tmp_path), "project_root_path": str(tmp_path),
"module": 'module', "module": "module",
"build_dir_name": "target", "build_dir_name": "target",
} }
config = {"issuer": "staging"} config = {"issuer": "staging"}
@ -63,21 +63,74 @@ def test_c4k_build_should_update_fqdn(tmp_path):
grafana_cloud_password="password", grafana_cloud_password="password",
) )
sut = C4kBuild(Build(project, project_config), project, project_config) sut = C4kBuild(Build(project, project_config), project, project_config)
sut.update_runtime_config('test.de', None, None) sut.update_runtime_config("test.de", None, None)
assert { assert {
"issuer": "staging", "issuer": "staging",
'fqdn': 'test.de', "fqdn": "test.de",
"mon-cfg": { "mon-cfg": {
"cluster-name": "module", "cluster-name": "module",
"cluster-stage": "test", "cluster-stage": "test",
"grafana-cloud-url": "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push", "grafana-cloud-url": "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push",
}, },
} == sut.config() } == sut.config()
assert {'jicofo-auth-password': 'pw2', assert {
'jicofo-component-secret': 'pw3', "jicofo-auth-password": "pw2",
'jvb-auth-password': 'pw1', "jicofo-component-secret": "pw3",
'mon-auth': {'grafana-cloud-password': 'password', "jvb-auth-password": "pw1",
'grafana-cloud-user': 'user'}} == sut.c4k_mixin_auth "mon-auth": {
"grafana-cloud-password": "password",
"grafana-cloud-user": "user",
},
} == sut.c4k_mixin_auth
sut.update_runtime_config sut.update_runtime_config
def test_c4k_build_should_calculate_command(tmp_path):
project = Project(str(tmp_path), name="test-project")
project_config = {
"stage": "test",
"project_root_path": "",
"module": "module",
"build_dir_name": "target",
}
add_c4k_mixin_config(
project_config,
{},
{},
grafana_cloud_user="user",
grafana_cloud_password="password",
)
sut = C4kBuild(Build(project, project_config), project, project_config)
assert (
"c4k-module-standalone.jar "
+ "/target/test-project/module/out_c4k_config.yaml "
+ "/target/test-project/module/out_c4k_auth.yaml > "
+ "/target/test-project/module/out_module.yaml"
== sut.command()
)
project_config = {
"stage": "test",
"project_root_path": "",
"module": "module",
"build_dir_name": "target",
}
add_c4k_mixin_config(
project_config,
{},
{},
executabel_name = "executabel_name",
grafana_cloud_user="user",
grafana_cloud_password="password",
)
sut = C4kBuild(Build(project, project_config), project, project_config)
assert (
"c4k-executabel_name-standalone.jar "
+ "/target/test-project/module/out_c4k_config.yaml "
+ "/target/test-project/module/out_c4k_auth.yaml > "
+ "/target/test-project/module/out_module.yaml"
== sut.command()
)