use python-terraform instead of own cmd-if

This commit is contained in:
jem 2019-10-21 22:37:57 +02:00
parent 14437469a9
commit ac1f6cf470
4 changed files with 32 additions and 25 deletions

4
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,4 @@
{
"python.linting.pylintEnabled": true,
"python.linting.enabled": true
}

View file

@ -27,7 +27,7 @@ use_plugin("python.distutils")
default_task = "publish" default_task = "publish"
name = "ddadevops" name = "ddadevops"
version = "0.2.0" version = "0.3.0"
summary = "tools to support builds combining gopass, terraform, dda-pallet, aws & hetzner-cloud" summary = "tools to support builds combining gopass, terraform, dda-pallet, aws & hetzner-cloud"
description = __doc__ description = __doc__
authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")] authors = [Author("meissa GmbH", "buero@meissa-gmbh.de")]

View file

@ -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 .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}" __version__ = "${version}"

View file

@ -3,6 +3,7 @@ from json import load
from subprocess import call from subprocess import call
from .meissa_build import stage, hetzner_api_key, tf_import_name, tf_import_resource from .meissa_build import stage, hetzner_api_key, tf_import_name, tf_import_resource
from .python_util import execute from .python_util import execute
from python_terraform import *
APPLY_PLAN = "proposed_apply.plan" APPLY_PLAN = "proposed_apply.plan"
DESTROY_PLAN = "proposed_destroy.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/aws_provider.tf', 'aws_provider.tf'])
call(['cp', '-f', base_path + '00_build_common/terraform/variables.tf', 'variables.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) 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): def tf_import(project):
init(project) init(project)
terraform(TF_IMPORT_CMD, get_hetzner_api_key_as_var(project), [tf_import_name(project), tf_import_resource(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): def tf_apply(project, auto_approve=None):
if not path.isfile(APPLY_PLAN): init(project)
tf_plan_apply(project)
else:
init(project)
cmd = [] cmd = []
tf = Terraform(working_dir='.')
if auto_approve: if auto_approve:
cmd.extend(['-auto-approve', '-input=false']) tf.apply(capture_output=False, auto_approve=True, var=get_hetzner_api_key_as_dict(project))
cmd.append(APPLY_PLAN) else:
terraform(TF_APPLY_CMD, None, cmd) tf.apply(capture_output=False, var=get_hetzner_api_key_as_dict(project))
write_output() tf.output(json=IsFlagged)
def tf_destroy(project, auto_approve=None): def tf_destroy(project, auto_approve=None):
cmd = [] tf = Terraform(working_dir='.')
if auto_approve: if auto_approve:
cmd.extend(['-auto-approve', '-input=false']) tf.destroy(capture_output=False, auto_approve=True, var=get_hetzner_api_key_as_dict(project))
terraform(TF_DESTROY_CMD, get_hetzner_api_key_as_var(project), cmd) else:
write_output() tf.destroy(capture_output=False, var=get_hetzner_api_key_as_dict(project))
def tf_read_output_json(): def tf_read_output_json():
with open(OUTPUT_JSON, 'r') as f: with open(OUTPUT_JSON, 'r') as f:
return load(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): def terraform(cmd, credentials=None, options=None):
tf_cmd = ['terraform'] tf_cmd = ['terraform']
tf_cmd.extend(cmd) tf_cmd.extend(cmd)
@ -73,11 +68,12 @@ def terraform(cmd, credentials=None, options=None):
return output return output
def init(project): def init(project):
terraform(TF_INIT_CMD) tf = Terraform(working_dir='.')
tf.init()
try: try:
terraform(TF_SELECT_WORKSPACE_CMD, None, [stage(project)]) tf.workspace('select', stage(project))
except: except:
terraform(TF_NEW_WORKSPACE_CMD, None, [stage(project)]) tf.workspace('new', stage(project))
def get_hetzner_api_key_as_var(project): def get_hetzner_api_key_as_var(project):
my_hetzner_api_key = hetzner_api_key(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]) ret.extend(['-var', 'hetzner_api_key=' + my_hetzner_api_key])
return ret 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