small fixes on terraform

This commit is contained in:
jem 2019-11-06 18:16:01 +01:00
parent af3d9d6765
commit b94533e3e6

View file

@ -5,19 +5,8 @@ from .meissa_build import stage, hetzner_api_key, tf_import_name, tf_import_reso
from .python_util import execute
from python_terraform import *
APPLY_PLAN = "proposed_apply.plan"
DESTROY_PLAN = "proposed_destroy.plan"
OUTPUT_JSON = "output.json"
TF_INIT_CMD = ['init']
TF_SELECT_WORKSPACE_CMD = ['workspace', 'select']
TF_NEW_WORKSPACE_CMD = ['workspace', 'new']
TF_OUTPUT_CMD = ['output', '-json']
TF_PLAN_CMD = ['plan']
TF_IMPORT_CMD = ['import']
TF_APPLY_CMD = ['apply']
TF_DESTROY_CMD = ['destroy']
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'])
@ -30,15 +19,13 @@ def tf_plan(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)])
tf = Terraform(working_dir='.')
tf.import_cmd(capture_output=False, var=get_hetzner_api_key_as_dict(project), tf_import_name(project), tf_import_resource(project))
def tf_apply(project, auto_approve=None):
def tf_apply(project, p_auto_approve=False):
init(project)
tf = Terraform(working_dir='.')
if auto_approve:
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.apply(capture_output=False, auto_approve=p_auto_approve, var=get_hetzner_api_key_as_dict(project))
tf_output(project)
def tf_output(project):
@ -47,31 +34,14 @@ def tf_output(project):
with open(OUTPUT_JSON, "w") as output_file:
output_file.write(json.dumps(result))
def tf_destroy(project, auto_approve=None):
def tf_destroy(project, p_auto_approve=False):
tf = Terraform(working_dir='.')
if auto_approve:
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))
tf.destroy(capture_output=False, auto_approve=p_auto_approve, var=get_hetzner_api_key_as_dict(project))
def tf_read_output_json():
with open(OUTPUT_JSON, 'r') as f:
return load(f)
def terraform(cmd, credentials=None, options=None):
tf_cmd = ['terraform']
tf_cmd.extend(cmd)
prn_cmd=list(tf_cmd)
if credentials:
tf_cmd.extend(credentials)
prn_cmd.extend([credentials[0], credentials[1].split('=', 1)[0] + '=xxx'])
if options:
tf_cmd.extend(options)
prn_cmd.extend(options)
print(" ".join(prn_cmd))
output = execute(tf_cmd)
return output
def init(project):
tf = Terraform(working_dir='.')
tf.init()
@ -80,13 +50,6 @@ def init(project):
except:
tf.workspace('new', stage(project))
def get_hetzner_api_key_as_var(project):
my_hetzner_api_key = hetzner_api_key(project)
ret = []
if my_hetzner_api_key:
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 = {}