Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/ops/terraform/terraform_cmd_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def generate(self, args):
)
terraform_init_command = ''

remove_local_cache = 'rm -rf .terraform && ' if self.ops_config['terraform.remove_local_cache'] else ''

if args.subcommand == 'template':
if args.template_location:
self.copy_static_files(args.template_location, terraform_path)
Expand All @@ -118,7 +120,7 @@ def generate(self, args):

if args.subcommand == 'plan':
generate_module_templates = True
terraform_refresh_command = remove_local_cache = ''
terraform_refresh_command = ''
if args.do_refresh:
terraform_refresh_command = "terraform refresh" \
"{variables_file}" \
Expand All @@ -129,9 +131,6 @@ def generate(self, args):
if self.ops_config['terraform.landscape'] and not args.raw_plan_output:
landscape = '| landscape'

if self.ops_config['terraform.remove_local_cache']:
remove_local_cache = 'rm -rf .terraform && '

cmd = "cd {root_dir}/{terraform_path} && " \
"{remove_local_cache}" \
"terraform get -update && " \
Expand Down Expand Up @@ -164,12 +163,15 @@ def generate(self, args):
self.inventory_generator.clear_cache()
if args.skip_plan:
# Run Terraform apply without running a plan first
cmd = "cd {root_dir}/{terraform_path} && {terraform_init_command}" \
cmd = "cd {root_dir}/{terraform_path} && " \
"{remove_local_cache}" \
"{terraform_init_command}" \
"rm -f {plan_file} && terraform apply {vars}" \
"-refresh=true {state_argument} {variables_file} {auto_approve}".format(
plan_file=plan_file,
root_dir=self.root_dir,
state_argument=state_argument,
remove_local_cache=remove_local_cache,
terraform_init_command=terraform_init_command,
terraform_path=terraform_path,
vars=vars,
Expand All @@ -190,10 +192,6 @@ def generate(self, args):

elif args.subcommand == 'destroy':
generate_module_templates = True
remove_local_cache = ''

if self.ops_config['terraform.remove_local_cache']:
remove_local_cache = 'rm -rf .terraform && '
cmd = "cd {root_dir}/{terraform_path} && " \
"{remove_local_cache}" \
"{terraform_init_command}" \
Expand Down Expand Up @@ -229,6 +227,7 @@ def generate(self, args):
)
elif args.subcommand == 'taint' or args.subcommand == 'untaint':
cmd = "cd {root_dir}/{terraform_path} && " \
"{remove_local_cache}" \
"{terraform_init_command}" \
"terraform {command} {state_argument} -module={module} {resource}".format(
root_dir=self.root_dir,
Expand All @@ -237,7 +236,8 @@ def generate(self, args):
resource=args.resource,
module=args.module,
state_argument=state_argument,
terraform_init_command=terraform_init_command
terraform_init_command=terraform_init_command,
remove_local_cache=remove_local_cache
)
elif args.subcommand == 'show':
if args.plan:
Expand Down Expand Up @@ -276,10 +276,13 @@ def generate(self, args):
)
elif args.subcommand == 'validate':
generate_module_templates = True
cmd = "cd {root_dir}/{terraform_path} && {terraform_init_command} " \
cmd = "cd {root_dir}/{terraform_path} && " \
"{remove_local_cache}" \
"{terraform_init_command} " \
"terraform {command} {vars} {variables_file}".format(
command=args.subcommand,
root_dir=self.root_dir,
remove_local_cache=remove_local_cache,
terraform_init_command=terraform_init_command,
terraform_path=terraform_path,
vars=vars,
Expand All @@ -290,10 +293,13 @@ def generate(self, args):
# - command = "state push errored.tfstate"
# - command = "force-unlock <LOCK_ID>"
generate_module_templates = True
cmd = "cd {root_dir}/{terraform_path} && {terraform_init_command} " \
cmd = "cd {root_dir}/{terraform_path} && " \
"{remove_local_cache}" \
"{terraform_init_command} " \
"terraform {command}".format(
command=args.subcommand,
root_dir=self.root_dir,
remove_local_cache=remove_local_cache,
terraform_init_command=terraform_init_command,
terraform_path=terraform_path,
)
Expand Down