From b995887764fcb4a6dc45120b45b5e8590397abf2 Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 8 Nov 2019 20:10:51 +0100 Subject: [PATCH] introduce working dir --- src/main/python/ddadevops/__init__.py | 9 ++-- src/main/python/ddadevops/dda_pallet.py | 38 ++++++++++++----- src/main/python/ddadevops/meissa_build.py | 45 +++++++++++++------- src/main/python/ddadevops/terraform.py | 52 ++++++++++++++--------- 4 files changed, 96 insertions(+), 48 deletions(-) diff --git a/src/main/python/ddadevops/__init__.py b/src/main/python/ddadevops/__init__.py index e759fe7..9c2e95b 100644 --- a/src/main/python/ddadevops/__init__.py +++ b/src/main/python/ddadevops/__init__.py @@ -4,8 +4,11 @@ terraform, dda-pallet, aws & hetzner-cloud. """ -from .meissa_build import meissa_init_project, stage, module, hetzner_api_key, tf_import_name, tf_import_resource, project_dict -from .dda_pallet import dda_write_target, dda_write_domain, dda_uberjar -from .terraform import tf_copy_common, tf_plan, tf_import, tf_apply, tf_output, tf_destroy, tf_read_output_json +from .meissa_build import meissa_init_project, stage, module, hetzner_api_key, \ + project_root_path, build_commons_path, build_target_path, initialize_target, \ + tf_import_name, tf_import_resource +from .dda_pallet import dda_write_target, dda_write_domain, dda_install, dda_configure +from .terraform import tf_copy_common, tf_plan, tf_import, tf_apply, tf_output, tf_destroy, \ + tf_read_output_json __version__ = "${version}" \ No newline at end of file diff --git a/src/main/python/ddadevops/dda_pallet.py b/src/main/python/ddadevops/dda_pallet.py index 5b41801..14450b5 100644 --- a/src/main/python/ddadevops/dda_pallet.py +++ b/src/main/python/ddadevops/dda_pallet.py @@ -1,28 +1,44 @@ from .python_util import * -import string +from .meissa_build import build_target_path, project_root_path +from string import Template TARGET = 'target.edn' -TEMPLATE_TARGET_CONTENT = string.Template(""" +TEMPLATE_TARGET_CONTENT = Template(""" {:existing [{:node-name "k8s" :node-ip "$ipv4"}] :provisioning-user {:login "root"}} """) -def dda_write_target(ipv4): - with open(TARGET, "w") as output_file: +def dda_write_target(project, ipv4): + with open(build_target_path(project) + TARGET, "w") as output_file: output_file.write(TEMPLATE_TARGET_CONTENT.substitute({'ipv4' : ipv4})) -def dda_write_domain(domain_path, substitues): - with open(domain_path, "r") as input_file: +def dda_write_domain(project, domain_file_name, substitues): + with open(build_target_path(project) + domain_file_name, "r") as input_file: domain_input = input_file.read() - domain_template = string.Template(domain_input) - with open('out_' + domain_path, "w") as output_file: + domain_template = Template(domain_input) + with open(build_target_path(project) + 'out_' + domain_file_name, "w") as output_file: output_file.write(domain_template.substitute(substitues)) -def dda_uberjar(tenant, application, spec): - cmd = ['java', '-jar', '../../../target/meissa-tenant-server.jar', '--targets', TARGET, - '--tenant', tenant, '--application', application, spec] +def dda_install(project, tenant, application, domain_file_name): + return dda_uberjar(project, tenant, application, domain_file_name) + +def dda_configure(project, tenant, application, domain_file_name): + return dda_uberjar(project, tenant, application, domain_file_name, True) + +def dda_uberjar(project, tenant, application, domain_file_name, configure_switch=None): + if configure_switch: + cmd = ['java', '-jar', project_root_path(project) + 'target/meissa-tenant-server.jar', \ + '--targets', build_target_path(project) + TARGET, \ + '--tenant', tenant, '--application', application, \ + '--configure', \ + build_target_path(project) + 'out_' + domain_file_name] + else: + cmd = ['java', '-jar', project_root_path(project) + 'target/meissa-tenant-server.jar', \ + '--targets', build_target_path(project) + TARGET, \ + '--tenant', tenant, '--application', application, \ + build_target_path(project) + 'out_' + domain_file_name] prn_cmd=list(cmd) print(" ".join(prn_cmd)) output = execute(cmd) diff --git a/src/main/python/ddadevops/meissa_build.py b/src/main/python/ddadevops/meissa_build.py index 93e3410..6f47265 100644 --- a/src/main/python/ddadevops/meissa_build.py +++ b/src/main/python/ddadevops/meissa_build.py @@ -1,35 +1,50 @@ import os from .credential import gopass_credential_from_env_path +from subprocess import run -def meissa_init_project(project): - project.set_property('stage', os.environ.get('STAGE', 'intergation')) - project.set_property('http_net_api_key', +STAGE = 'stage' +HTTP_NET_API_KEY = 'http_net_api_key' +HETZNER_API_KEY = 'hetzner_api_key' +PROJECT_ROOT_PATH = 'project_root_path' +BUILD_COMMONS_PATH = 'build_commons_path' + +def meissa_init_project(project, project_root_path, build_commons_path): + project.set_property(STAGE, os.environ.get('STAGE', 'intergation')) + project.set_property(PROJECT_ROOT_PATH, project_root_path) + project.set_property(BUILD_COMMONS_PATH, build_commons_path) + project.set_property(HTTP_NET_API_KEY, \ gopass_credential_from_env_path('HTTP_NET_API_KEY_PATH')) - project.set_property('hetzner_api_key', + project.set_property(HETZNER_API_KEY, \ gopass_credential_from_env_path('HETZNER_API_KEY_PATH')) return project def hetzner_api_key(project): - return project.get_property('hetzner_api_key') + return project.get_property(HETZNER_API_KEY) def stage(project): - return project.get_property('stage') + return project.get_property(STAGE) def module(project): return project.get_property('name') +def project_root_path(project): + return project.get_property(PROJECT_ROOT_PATH) + +def build_commons_path(project): + return project.get_property(BUILD_COMMONS_PATH) + +def build_target_path(project): + return project_root_path(project) + 'target/' + project.name + '/' + def tf_import_name(project): return project.get_property('tf_import_name') def tf_import_resource(project): return project.get_property('tf_import_resource') -def project_dict(project): - my_hetzner_api_key = hetzner_api_key(project) - my_module = project.name - ret = {'stage' : stage(project)} - if my_hetzner_api_key: - ret['hetzner_api_key'] = my_hetzner_api_key - if my_module: - ret['module'] = my_module - return ret \ No newline at end of file +def initialize_target(project): + run('rm -rf ' + build_target_path(project), shell=True) + run('mkdir -p ' + build_target_path(project), shell=True) + run('cp *.tf ' + build_target_path(project), shell=True) + run('cp *.tfars ' + build_target_path(project), shell=True) + run('cp *.edn ' + build_target_path(project), shell=True) diff --git a/src/main/python/ddadevops/terraform.py b/src/main/python/ddadevops/terraform.py index 52d3792..1530b25 100644 --- a/src/main/python/ddadevops/terraform.py +++ b/src/main/python/ddadevops/terraform.py @@ -1,50 +1,64 @@ from os import path from json import load -from subprocess import call -from .meissa_build import stage, project_dict, tf_import_name, tf_import_resource +from subprocess import run +from .meissa_build import stage, hetzner_api_key, tf_import_name, tf_import_resource, \ + build_commons_path, build_target_path from .python_util import execute from python_terraform import * OUTPUT_JSON = "output.json" -def tf_copy_common(base_path): - call(['cp', '-f', base_path + '00_build_common/terraform/gitignore_on_target', '.gitignore']) - call(['cp', '-f', base_path + '00_build_common/terraform/aws_provider.tf', 'aws_provider.tf']) - call(['cp', '-f', base_path + '00_build_common/terraform/variables.tf', 'variables.tf']) +def tf_copy_common(project): + run(['cp', '-f', build_commons_path(project) + 'terraform/aws_provider.tf', \ + build_target_path(project) + 'aws_provider.tf']) + run(['cp', '-f', build_commons_path(project) + 'terraform/variables.tf', \ + build_target_path(project) + 'variables.tf']) def tf_plan(project): init(project) - tf = Terraform(working_dir='.') - tf.plan(capture_output=False, var=project_dict(project)) + tf = Terraform(working_dir=build_target_path(project)) + tf.plan(capture_output=False, var=project_vars(project)) def tf_import(project): init(project) - tf = Terraform(working_dir='.') + tf = Terraform(working_dir=build_target_path(project)) tf.import_cmd(tf_import_name(project), tf_import_resource(project), \ - capture_output=False, var=project_dict(project)) + capture_output=False, var=project_vars(project)) def tf_apply(project, p_auto_approve=False): init(project) - tf = Terraform(working_dir='.') - tf.apply(capture_output=False, auto_approve=p_auto_approve, var=project_dict(project)) + tf = Terraform(working_dir=build_target_path(project)) + tf.apply(capture_output=False, auto_approve=p_auto_approve, var=project_vars(project)) tf_output(project) def tf_output(project): - tf = Terraform(working_dir='.') + init(project) + tf = Terraform(working_dir=build_target_path(project)) result = tf.output(json=IsFlagged) - with open(OUTPUT_JSON, "w") as output_file: + with open(build_target_path(project) + OUTPUT_JSON, "w") as output_file: output_file.write(json.dumps(result)) def tf_destroy(project, p_auto_approve=False): - tf = Terraform(working_dir='.') - tf.destroy(capture_output=False, auto_approve=p_auto_approve, var=project_dict(project)) + init(project) + tf = Terraform(working_dir=build_target_path(project)) + tf.destroy(capture_output=False, auto_approve=p_auto_approve, var=project_vars(project)) -def tf_read_output_json(): - with open(OUTPUT_JSON, 'r') as f: +def tf_read_output_json(project): + with open(build_target_path(project) + OUTPUT_JSON, 'r') as f: return load(f) +def project_vars(project): + my_hetzner_api_key = hetzner_api_key(project) + my_module = project.name + ret = {'stage' : stage(project)} + if my_hetzner_api_key: + ret['hetzner_api_key'] = my_hetzner_api_key + if my_module: + ret['module'] = my_module + return ret + def init(project): - tf = Terraform(working_dir='.') + tf = Terraform(working_dir=build_target_path(project)) tf.init() try: tf.workspace('select', stage(project))