From 14437469a921d90dfd7fe06ca880a0df73fb5097 Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 6 Sep 2019 18:00:15 +0200 Subject: [PATCH] cleanup for first good release --- build.py | 2 +- src/main/python/ddadevops/__init__.py | 6 +- src/main/python/ddadevops/dda_pallet.py | 13 ++-- src/main/python/ddadevops/meissa_build.py | 2 +- src/main/python/ddadevops/python_util.py | 9 +++ src/main/python/ddadevops/terraform.py | 92 +++++++++++------------ 6 files changed, 61 insertions(+), 63 deletions(-) create mode 100644 src/main/python/ddadevops/python_util.py diff --git a/build.py b/build.py index 78898cd..dc1f090 100644 --- a/build.py +++ b/build.py @@ -27,7 +27,7 @@ use_plugin("python.distutils") default_task = "publish" name = "ddadevops" -version = "0.1.13" +version = "0.2.0" summary = "tools to support builds combining gopass, terraform, dda-pallet, aws & hetzner-cloud" description = __doc__ authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")] diff --git a/src/main/python/ddadevops/__init__.py b/src/main/python/ddadevops/__init__.py index fd1a716..662113c 100644 --- a/src/main/python/ddadevops/__init__.py +++ b/src/main/python/ddadevops/__init__.py @@ -4,8 +4,8 @@ terraform, dda-pallet, aws & hetzner-cloud. """ -from .dda_pallet import * -from .meissa_build import * -from .terraform import * +from .meissa_build import meissa_init_project +from .dda_pallet import dda_write_target, dda_uberjar +from .terraform import tf_copy_common, tf_plan_apply, tf_import, tf_apply, 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 ad92f1f..aabe44e 100644 --- a/src/main/python/ddadevops/dda_pallet.py +++ b/src/main/python/ddadevops/dda_pallet.py @@ -1,7 +1,7 @@ -from subprocess import check_output, call, Popen, PIPE -import sys +from .python_util import * import string + TARGET = 'target.edn' TEMPLATE_TARGET_CONTENT = string.Template(""" {:existing [{:node-name "k8s" @@ -9,18 +9,15 @@ TEMPLATE_TARGET_CONTENT = string.Template(""" :provisioning-user {:login "root"}} """) -def write_target(ip): +def dda_write_target(ip): with open(TARGET, "w") as output_file: output_file.write(TEMPLATE_TARGET_CONTENT.substitute({'ip' : ip})) -def uberjar(tenant, application, spec): +def dda_uberjar(tenant, application, spec): cmd = ['java', '-jar', '../../../target/meissa-tenant-server.jar', '--targets', TARGET, '--tenant', tenant, '--application', application, spec] prn_cmd=list(cmd) print(" ".join(prn_cmd)) - if sys.version_info.major == 3: - output = check_output(cmd, encoding='UTF-8') - else: - output = check_output(cmd) + output = execute(cmd) print(output) return output diff --git a/src/main/python/ddadevops/meissa_build.py b/src/main/python/ddadevops/meissa_build.py index f837f4d..2a3f432 100644 --- a/src/main/python/ddadevops/meissa_build.py +++ b/src/main/python/ddadevops/meissa_build.py @@ -1,7 +1,7 @@ import os from .credential import gopass_credential_from_env_path -def init_project(project): +def meissa_init_project(project): project.set_property('stage', os.environ.get('STAGE', 'intergation')) project.set_property('http_net_api_key', gopass_credential_from_env_path('HTTP_NET_API_KEY_PATH')) diff --git a/src/main/python/ddadevops/python_util.py b/src/main/python/ddadevops/python_util.py new file mode 100644 index 0000000..5f7e110 --- /dev/null +++ b/src/main/python/ddadevops/python_util.py @@ -0,0 +1,9 @@ +from subprocess import check_output +import sys + +def execute(cmd): + if sys.version_info.major == 3: + output = check_output(cmd, encoding='UTF-8') + else: + output = check_output(cmd) + return output \ No newline at end of file diff --git a/src/main/python/ddadevops/terraform.py b/src/main/python/ddadevops/terraform.py index a9007d0..9f0c2e9 100644 --- a/src/main/python/ddadevops/terraform.py +++ b/src/main/python/ddadevops/terraform.py @@ -1,9 +1,8 @@ -import os -import sys -import json - -from subprocess import check_output, call, Popen, PIPE +from os import path +from json import load +from subprocess import call from .meissa_build import stage, hetzner_api_key, tf_import_name, tf_import_resource +from .python_util import execute APPLY_PLAN = "proposed_apply.plan" DESTROY_PLAN = "proposed_destroy.plan" @@ -18,11 +17,47 @@ TF_IMPORT_CMD = ['import'] TF_APPLY_CMD = ['apply'] TF_DESTROY_CMD = ['destroy'] -def copy_common(base_path): +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_plan_apply(project): + init(project) + terraform(TF_PLAN_CMD, get_hetzner_api_key_as_var(project), ['-out', APPLY_PLAN]) + +def tf_import(project): + init(project) + terraform(TF_IMPORT_CMD, get_hetzner_api_key_as_var(project), [tf_import_name(project), tf_import_resource(project)]) + +def tf_apply(project, auto_approve=None): + if not path.isfile(APPLY_PLAN): + tf_plan_apply(project) + else: + init(project) + cmd = [] + if auto_approve: + cmd.extend(['-auto-approve', '-input=false']) + cmd.append(APPLY_PLAN) + terraform(TF_APPLY_CMD, None, cmd) + write_output() + +def tf_destroy(project, auto_approve=None): + cmd = [] + if auto_approve: + cmd.extend(['-auto-approve', '-input=false']) + terraform(TF_DESTROY_CMD, get_hetzner_api_key_as_var(project), cmd) + write_output() + +def tf_read_output_json(): + with open(OUTPUT_JSON, 'r') as f: + return load(f) + +def write_output(): + output = terraform(TF_OUTPUT_CMD) + with open(OUTPUT_JSON, "w") as output_file: + output_file.write(output) + def terraform(cmd, credentials=None, options=None): tf_cmd = ['terraform'] tf_cmd.extend(cmd) @@ -34,11 +69,7 @@ def terraform(cmd, credentials=None, options=None): tf_cmd.extend(options) prn_cmd.extend(options) print(" ".join(prn_cmd)) - if sys.version_info.major == 3: - output = check_output(tf_cmd, encoding='UTF-8') - else: - output = check_output(tf_cmd) - print(output) + output = execute(tf_cmd) return output def init(project): @@ -48,15 +79,6 @@ def init(project): except: terraform(TF_NEW_WORKSPACE_CMD, None, [stage(project)]) -def write_output(): - output = terraform(TF_OUTPUT_CMD) - with open(OUTPUT_JSON, "w") as output_file: - output_file.write(output) - -def read_output_json(): - with open(OUTPUT_JSON, 'r') as f: - return json.load(f) - def get_hetzner_api_key_as_var(project): my_hetzner_api_key = hetzner_api_key(project) ret = [] @@ -64,33 +86,3 @@ def get_hetzner_api_key_as_var(project): ret.extend(['-var', 'hetzner_api_key=' + my_hetzner_api_key]) return ret -def plan_apply(project): - init(project) - terraform(TF_PLAN_CMD, get_hetzner_api_key_as_var(project), ['-out', APPLY_PLAN]) - -def tf_import(project): - init(project) - terraform(TF_IMPORT_CMD, get_hetzner_api_key_as_var(project), [tf_import_name(project), tf_import_resource(project)]) - -def plan_destroy(project): - init(project) - terraform(TF_PLAN_CMD, get_hetzner_api_key_as_var(project), ['-destroy']) - -def apply(project, auto_approve=None): - if not os.path.isfile(APPLY_PLAN): - plan_apply(project) - else: - init(project) - cmd = [] - if auto_approve: - cmd.extend(['-auto-approve', '-input=false']) - cmd.append(APPLY_PLAN) - terraform(TF_APPLY_CMD, None, cmd) - write_output() - -def destroy(project, auto_approve=None): - cmd = [] - if auto_approve: - cmd.extend(['-auto-approve', '-input=false']) - terraform(TF_DESTROY_CMD, get_hetzner_api_key_as_var(project), cmd) - write_output()