Skip to content

Commit 3aa8e3c

Browse files
authored
[FIX] Honor terraform.remove_local_cache flag (#51)
1 parent e9413ee commit 3aa8e3c

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/ops/terraform/terraform_cmd_generator.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def generate(self, args):
9999
)
100100
terraform_init_command = ''
101101

102+
remove_local_cache = 'rm -rf .terraform && ' if self.ops_config['terraform.remove_local_cache'] else ''
103+
102104
if args.subcommand == 'template':
103105
if args.template_location:
104106
self.copy_static_files(args.template_location, terraform_path)
@@ -118,7 +120,7 @@ def generate(self, args):
118120

119121
if args.subcommand == 'plan':
120122
generate_module_templates = True
121-
terraform_refresh_command = remove_local_cache = ''
123+
terraform_refresh_command = ''
122124
if args.do_refresh:
123125
terraform_refresh_command = "terraform refresh" \
124126
"{variables_file}" \
@@ -129,9 +131,6 @@ def generate(self, args):
129131
if self.ops_config['terraform.landscape'] and not args.raw_plan_output:
130132
landscape = '| landscape'
131133

132-
if self.ops_config['terraform.remove_local_cache']:
133-
remove_local_cache = 'rm -rf .terraform && '
134-
135134
cmd = "cd {root_dir}/{terraform_path} && " \
136135
"{remove_local_cache}" \
137136
"terraform get -update && " \
@@ -164,12 +163,15 @@ def generate(self, args):
164163
self.inventory_generator.clear_cache()
165164
if args.skip_plan:
166165
# Run Terraform apply without running a plan first
167-
cmd = "cd {root_dir}/{terraform_path} && {terraform_init_command}" \
166+
cmd = "cd {root_dir}/{terraform_path} && " \
167+
"{remove_local_cache}" \
168+
"{terraform_init_command}" \
168169
"rm -f {plan_file} && terraform apply {vars}" \
169170
"-refresh=true {state_argument} {variables_file} {auto_approve}".format(
170171
plan_file=plan_file,
171172
root_dir=self.root_dir,
172173
state_argument=state_argument,
174+
remove_local_cache=remove_local_cache,
173175
terraform_init_command=terraform_init_command,
174176
terraform_path=terraform_path,
175177
vars=vars,
@@ -190,10 +192,6 @@ def generate(self, args):
190192

191193
elif args.subcommand == 'destroy':
192194
generate_module_templates = True
193-
remove_local_cache = ''
194-
195-
if self.ops_config['terraform.remove_local_cache']:
196-
remove_local_cache = 'rm -rf .terraform && '
197195
cmd = "cd {root_dir}/{terraform_path} && " \
198196
"{remove_local_cache}" \
199197
"{terraform_init_command}" \
@@ -229,6 +227,7 @@ def generate(self, args):
229227
)
230228
elif args.subcommand == 'taint' or args.subcommand == 'untaint':
231229
cmd = "cd {root_dir}/{terraform_path} && " \
230+
"{remove_local_cache}" \
232231
"{terraform_init_command}" \
233232
"terraform {command} {state_argument} -module={module} {resource}".format(
234233
root_dir=self.root_dir,
@@ -237,7 +236,8 @@ def generate(self, args):
237236
resource=args.resource,
238237
module=args.module,
239238
state_argument=state_argument,
240-
terraform_init_command=terraform_init_command
239+
terraform_init_command=terraform_init_command,
240+
remove_local_cache=remove_local_cache
241241
)
242242
elif args.subcommand == 'show':
243243
if args.plan:
@@ -276,10 +276,13 @@ def generate(self, args):
276276
)
277277
elif args.subcommand == 'validate':
278278
generate_module_templates = True
279-
cmd = "cd {root_dir}/{terraform_path} && {terraform_init_command} " \
279+
cmd = "cd {root_dir}/{terraform_path} && " \
280+
"{remove_local_cache}" \
281+
"{terraform_init_command} " \
280282
"terraform {command} {vars} {variables_file}".format(
281283
command=args.subcommand,
282284
root_dir=self.root_dir,
285+
remove_local_cache=remove_local_cache,
283286
terraform_init_command=terraform_init_command,
284287
terraform_path=terraform_path,
285288
vars=vars,
@@ -290,10 +293,13 @@ def generate(self, args):
290293
# - command = "state push errored.tfstate"
291294
# - command = "force-unlock <LOCK_ID>"
292295
generate_module_templates = True
293-
cmd = "cd {root_dir}/{terraform_path} && {terraform_init_command} " \
296+
cmd = "cd {root_dir}/{terraform_path} && " \
297+
"{remove_local_cache}" \
298+
"{terraform_init_command} " \
294299
"terraform {command}".format(
295300
command=args.subcommand,
296301
root_dir=self.root_dir,
302+
remove_local_cache=remove_local_cache,
297303
terraform_init_command=terraform_init_command,
298304
terraform_path=terraform_path,
299305
)

0 commit comments

Comments
 (0)