diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a465101f..5e6af8a9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -36,13 +36,13 @@ test_openshift_config: - cd ./app-config/openshift - oc login openshift-dev.cern.ch --token="${OPENSHIFT_CONFIG_CHECKER_TOKEN_TEST_CARA}" - python ./fetch-config.py test-cara --output-directory ./test-cara/actual -# - python ./build_config.py test-cara + - python ./generate-config.py test-cara --output-directory ./test-cara/expected # - pytest ./test_config.py --arg test-cara artifacts: paths: - ./app-config/openshift/test-cara/actual -# - ./app-config/openshift/test-cara/expected + - ./app-config/openshift/test-cara/expected # A development installation of CARA tested with pytest. diff --git a/app-config/openshift/fetch-config.py b/app-config/openshift/fetch-config.py index 92ee8fbb..a14b1750 100644 --- a/app-config/openshift/fetch-config.py +++ b/app-config/openshift/fetch-config.py @@ -10,7 +10,7 @@ def configure_parser(parser: argparse.ArgumentParser) -> None: parser.set_defaults(handler=handler) parser.add_argument( "instance", choices=['cara', 'test-cara'], - help="Pick the instance for which you want to generated the config", + help="Pick the instance for which you want to fetch the config", ) parser.add_argument( "--output-directory", default='config', @@ -39,7 +39,7 @@ def fetch_config(output_directory: pathlib.Path): with (output_directory / f'{component}.json').open('wt') as fh: cmd = ['oc', 'get', '--export', '-o', 'json', component] print(f'Running: {" ".join(cmd)}') - subprocess.run(['oc', 'get', '--export', '-o', 'json', component], stdout=fh, check=True) + subprocess.run(cmd, stdout=fh, check=True) print(f'Config in: {output_directory.absolute()}') diff --git a/app-config/openshift/generate-config.py b/app-config/openshift/generate-config.py new file mode 100644 index 00000000..28fd3a3e --- /dev/null +++ b/app-config/openshift/generate-config.py @@ -0,0 +1,65 @@ +import argparse +import pathlib +import subprocess +import sys +import typing + + +def configure_parser(parser: argparse.ArgumentParser) -> None: + parser.description = "Generate the config files which can be later submitted to openshift" + parser.set_defaults(handler=handler) + parser.add_argument( + "instance", choices=['cara', 'test-cara'], + help="Pick the instance for which you want to generate the config", + ) + parser.add_argument( + "--output-directory", default='config', + help="Location to put the config files", + ) + + +def generate_config(output_directory: pathlib.Path, project_name: str, hostname: str, branch: str): + output_directory.mkdir(exist_ok=True, parents=True) + + def oc_process(component_name: str, context: typing.Optional[dict] = None): + cmd = ['oc', 'process', '--local', '-f', f'{component_name}.yaml'] + for ctx_name, ctx_value in (context or {}).items(): + cmd.extend(['--param', f'{ctx_name}={ctx_value}']) + with (output_directory / f'{component_name}.json').open('wt') as fh: + print(f'Running: {" ".join(cmd)}') + subprocess.run(cmd, stdout=fh, check=True) + + # oc_process('route', oc_process + ['route.yaml', '--param', f'HOST={hostname}']) + oc_process('routes', context={'HOST': hostname}) + oc_process('configmap') + oc_process('services') + oc_process('imagestreams') + oc_process('buildconfig', context={'GIT_BRANCH': branch}) + oc_process('deploymentconfig', context={'PROJECT_NAME': project_name}) + + print(f'Config in: {output_directory.absolute()}') + + +def handler(args: argparse.ArgumentParser) -> None: + if args.instance == 'cara': + project_name = 'cara' + branch = 'master' + hostname = 'cara.web.cern.ch' + elif args.instance == 'test-cara': + branch = 'live/test-cara' + project_name = 'test-cara' + hostname = 'test-cara.web.cern.ch' + + generate_config(pathlib.Path(args.output_directory), project_name, hostname, branch) + + +def main(): + parser = argparse.ArgumentParser() + configure_parser(parser) + args = parser.parse_args() + args.handler(args) + + +if __name__ == '__main__': + main() + diff --git a/app-config/openshift/route.yaml b/app-config/openshift/routes.yaml similarity index 100% rename from app-config/openshift/route.yaml rename to app-config/openshift/routes.yaml