* 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 .infrastructure import FileApi, ResourceApi, DockerApi
from os import chmod
import yaml
from .python_util import execute
from .credential import gopass_field_from_path, gopass_password_from_path
from .infrastructure import FileApi, ResourceApi, DockerApi, ExecutionApi
class BuildService:
@ -26,7 +20,7 @@ class DockerBuildService:
data = self.resource_api.read_resource(
"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):
self.__copy_build_resource_file_from_package__(
@ -62,32 +56,19 @@ class DockerBuildService:
def test(self, build: DockerBuild):
self.docker_api.test(build.name(), build.build_path())
# TODO: move infrastructure fktns to infra apis
class C4kBuildService:
def __init__(self):
self.file_api = FileApi()
self.execution_api = ExecutionApi()
def write_c4k_config(self, c4k_build: C4kBuild):
with open(
c4k_build.build.build_path() + "/out_c4k_config.yaml", "w", encoding="utf-8"
) as output_file:
yaml.dump(c4k_build.config(), output_file)
path = c4k_build.build.build_path() + "/out_c4k_config.yaml"
self.file_api.write_yaml_to_file(path, c4k_build.config())
def write_c4k_auth(self, c4k_build: C4kBuild):
with open(
c4k_build.build.build_path() + "/out_c4k_auth.yaml", "w", encoding="utf-8"
) as output_file:
yaml.dump(c4k_build.c4k_mixin_auth, output_file)
chmod(c4k_build.build.build_path() + "/out_c4k_auth.yaml", 0o600)
path = c4k_build.build.build_path() + "/out_c4k_auth.yaml"
self.file_api.write_yaml_to_file(path, c4k_build.c4k_mixin_auth)
def c4k_apply(self, c4k_build: C4kBuild, ry_run=False):
module = c4k_build.build.module
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
def c4k_apply(self, c4k_build: C4kBuild, dry_run=False):
return self.execution_api.execute(c4k_build.command(), dry_run)

View file

@ -3,27 +3,42 @@ from .application import C4kBuildService
from .devops_build import DevopsBuild
from .credential import gopass_field_from_path, gopass_password_from_path
def add_c4k_mixin_config(config,
def add_c4k_mixin_config(
config,
c4k_config_dict,
c4k_auth_dict,
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'):
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')
"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-cfg': {
'grafana-cloud-url': grafana_cloud_url
}})
config.update({'C4kMixin': {'Config': c4k_config_dict,
'Auth': c4k_auth_dict}})
"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-cfg": {"grafana-cloud-url": grafana_cloud_url}})
config.update(
{
"C4kMixin": {
"executabel_name": executabel_name,
"config": c4k_config_dict,
"auth": c4k_auth_dict,
}
}
)
return config

View file

@ -71,8 +71,12 @@ class DockerBuild(Validateable):
class C4kBuild(Validateable):
def __init__(self, build: Build, project, config):
self.build = build
self.c4k_mixin_config = config["C4kMixin"]["Config"]
self.c4k_mixin_auth = config["C4kMixin"]["Auth"]
tmp_executabel_name = config["C4kMixin"]["executabel_name"]
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.update({"cluster-name": self.build.module, "cluster-stage": self.build.stage})
self.c4k_mixin_config.update({"mon-cfg": tmp})
@ -85,3 +89,10 @@ class C4kBuild(Validateable):
self.c4k_mixin_config.update({'fqdn': fqdn})
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 sys import stdout
from pkg_resources import resource_string
from os import chmod
import yaml
from .python_util import execute
class ResourceApi():
@ -18,10 +20,15 @@ class FileApi():
def cp_recursive(self, src: str, target_dir: str):
execute('cp -r ' + src + ' ' + target_dir, shell=True)
def write_to_file(self, path: Path, data: bytes):
with open(path, "w", encoding=stdout.encoding) as output_file:
def write_data_to_file(self, path: Path, data: bytes):
with open(path, "w", encoding="utf-8") as output_file:
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():
def image(self, name: str, path: Path):
execute('docker build -t ' + name +
@ -51,3 +58,13 @@ class DockerApi():
execute('docker build -t ' + name + '-test ' +
'--file ' + path + '/test/Dockerfile '
+ 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,8 +27,6 @@ def test_c4k_mixin(tmp_path):
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').get('Config') is config
assert project_config.get('C4kMixin').get('Auth') is auth
mixin = MyC4kMixin(project, project_config)
mixin.initialize_build_dir()

View file

@ -42,11 +42,11 @@ def test_validate_with_reason():
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 = {
"stage": "test",
"project_root_path": str(tmp_path),
"module": 'module',
"module": "module",
"build_dir_name": "target",
}
config = {"issuer": "staging"}
@ -63,21 +63,74 @@ def test_c4k_build_should_update_fqdn(tmp_path):
grafana_cloud_password="password",
)
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 {
"issuer": "staging",
'fqdn': 'test.de',
"fqdn": "test.de",
"mon-cfg": {
"cluster-name": "module",
"cluster-stage": "test",
"grafana-cloud-url": "https://prometheus-prod-01-eu-west-0.grafana.net/api/prom/push",
},
} == sut.config()
assert {'jicofo-auth-password': 'pw2',
'jicofo-component-secret': 'pw3',
'jvb-auth-password': 'pw1',
'mon-auth': {'grafana-cloud-password': 'password',
'grafana-cloud-user': 'user'}} == sut.c4k_mixin_auth
assert {
"jicofo-auth-password": "pw2",
"jicofo-component-secret": "pw3",
"jvb-auth-password": "pw1",
"mon-auth": {
"grafana-cloud-password": "password",
"grafana-cloud-user": "user",
},
} == sut.c4k_mixin_auth
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()
)