From ac1f6cf470284c24e11ac1d63e06c5df3264b04b Mon Sep 17 00:00:00 2001 From: jem Date: Mon, 21 Oct 2019 22:37:57 +0200 Subject: [PATCH] use python-terraform instead of own cmd-if --- .vscode/settings.json | 4 +++ build.py | 2 +- src/main/python/ddadevops/__init__.py | 4 +-- src/main/python/ddadevops/terraform.py | 47 ++++++++++++++------------ 4 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cc67606 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "python.linting.pylintEnabled": true, + "python.linting.enabled": true +} \ No newline at end of file diff --git a/build.py b/build.py index dc1f090..3689641 100644 --- a/build.py +++ b/build.py @@ -27,7 +27,7 @@ use_plugin("python.distutils") default_task = "publish" name = "ddadevops" -version = "0.2.0" +version = "0.3.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 662113c..fe527a8 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 .meissa_build import meissa_init_project +from .meissa_build import meissa_init_project, stage, hetzner_api_key, tf_import_name, tf_import_resource 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 +from .terraform import tf_copy_common, tf_plan, 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/terraform.py b/src/main/python/ddadevops/terraform.py index 9f0c2e9..54dd846 100644 --- a/src/main/python/ddadevops/terraform.py +++ b/src/main/python/ddadevops/terraform.py @@ -3,6 +3,7 @@ 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 +from python_terraform import * APPLY_PLAN = "proposed_apply.plan" DESTROY_PLAN = "proposed_destroy.plan" @@ -22,42 +23,36 @@ def tf_copy_common(base_path): 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): +def tf_plan(project): init(project) - terraform(TF_PLAN_CMD, get_hetzner_api_key_as_var(project), ['-out', APPLY_PLAN]) + tf = Terraform(working_dir='.') + tf.plan(capture_output=False, var=get_hetzner_api_key_as_dict(project)) 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) + init(project) cmd = [] + tf = Terraform(working_dir='.') if auto_approve: - cmd.extend(['-auto-approve', '-input=false']) - cmd.append(APPLY_PLAN) - terraform(TF_APPLY_CMD, None, cmd) - write_output() + tf.apply(capture_output=False, auto_approve=True, var=get_hetzner_api_key_as_dict(project)) + else: + tf.apply(capture_output=False, var=get_hetzner_api_key_as_dict(project)) + tf.output(json=IsFlagged) def tf_destroy(project, auto_approve=None): - cmd = [] + tf = Terraform(working_dir='.') if auto_approve: - cmd.extend(['-auto-approve', '-input=false']) - terraform(TF_DESTROY_CMD, get_hetzner_api_key_as_var(project), cmd) - write_output() + tf.destroy(capture_output=False, auto_approve=True, var=get_hetzner_api_key_as_dict(project)) + else: + tf.destroy(capture_output=False, var=get_hetzner_api_key_as_dict(project)) 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) @@ -73,11 +68,12 @@ def terraform(cmd, credentials=None, options=None): return output def init(project): - terraform(TF_INIT_CMD) + tf = Terraform(working_dir='.') + tf.init() try: - terraform(TF_SELECT_WORKSPACE_CMD, None, [stage(project)]) + tf.workspace('select', stage(project)) except: - terraform(TF_NEW_WORKSPACE_CMD, None, [stage(project)]) + tf.workspace('new', stage(project)) def get_hetzner_api_key_as_var(project): my_hetzner_api_key = hetzner_api_key(project) @@ -86,3 +82,10 @@ def get_hetzner_api_key_as_var(project): ret.extend(['-var', 'hetzner_api_key=' + my_hetzner_api_key]) return ret +def get_hetzner_api_key_as_dict(project): + my_hetzner_api_key = hetzner_api_key(project) + ret = {} + if my_hetzner_api_key: + ret['hetzner_api_key'] = my_hetzner_api_key + return ret +