Skip to content

Commit fbbfcef

Browse files
authored
fix: generate create and destroy scripts (#120)
Signed-off-by: matttrach <matt.trachier@suse.com>
1 parent 0feba34 commit fbbfcef

3 files changed

Lines changed: 74 additions & 38 deletions

File tree

modules/deploy/create.sh.tpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
cd ${deploy_path}
1+
set -x
2+
pushd ${deploy_path}
3+
pwd
4+
ls -lah
25
. envrc
36
TF_CLI_ARGS_init=""
47
TF_CLI_ARGS_apply=""
@@ -51,4 +54,5 @@ if [ $EXITCODE -eq 0 ]; then
5154
echo "success...";
5255
terraform output -json -state="${deploy_path}/tfstate" > ${deploy_path}/outputs.json
5356
fi
57+
popd
5458
exit $EXITCODE

modules/deploy/destroy.sh.tpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
cd ${deploy_path}
1+
set -x
2+
pushd ${deploy_path}
3+
pwd
4+
ls -lah
25
. envrc
36
TF_CLI_ARGS_init=""
47
TF_CLI_ARGS_apply=""
@@ -8,3 +11,4 @@ if [ -z "${skip_destroy}" ]; then
811
else
912
echo "Not destroying deployed module, it will no longer be managed here."
1013
fi
14+
popd

modules/deploy/main.tf

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -128,56 +128,86 @@ resource "file_local" "instantiate_envrc_snapshot" {
128128
file_local.write_tmp_env,
129129
file_local_snapshot.persist_envrc,
130130
]
131-
directory = local.deploy_path
132-
name = "envrc"
133-
contents = base64decode(file_local_snapshot.persist_envrc.snapshot)
134-
permissions = "0700" # make it executable so it can be sourced
131+
directory = local.deploy_path
132+
name = "envrc"
133+
contents = base64decode(file_local_snapshot.persist_envrc.snapshot)
135134
}
136135

136+
## Deploy ##
137+
resource "file_local" "generate_destroy" {
138+
depends_on = [
139+
file_local.instantiate_envrc_snapshot,
140+
file_local.instantiate_inputs_snapshot,
141+
file_local.instantiate_tpl_snapshot,
142+
]
143+
directory = local.tf_data_dir
144+
name = "destroy.sh"
145+
permissions = "0755"
146+
contents = templatefile("${path.module}/destroy.sh.tpl", {
147+
deploy_path = local.deploy_path
148+
skip_destroy = local.skip_destroy
149+
timeout = local.timeout
150+
})
151+
}
137152
resource "terraform_data" "destroy" {
138153
depends_on = [
139154
file_local.instantiate_envrc_snapshot,
140155
file_local.instantiate_inputs_snapshot,
141156
file_local.instantiate_tpl_snapshot,
157+
file_local.generate_destroy,
142158
]
143159
triggers_replace = {
144160
trigger = local.deploy_trigger
145161
dp = local.deploy_path
146-
sd = local.skip_destroy
147-
to = local.timeout
148162
}
149163
provisioner "local-exec" {
150164
when = destroy
151-
command = templatefile("${path.module}/destroy.sh.tpl", {
152-
deploy_path = self.triggers_replace.dp
153-
skip_destroy = self.triggers_replace.sd
154-
timeout = self.triggers_replace.to
155-
})
165+
# no changing the directory or this won't work on different machines!
166+
command = <<-EOT
167+
set -x
168+
${self.triggers_replace.dp}/destroy.sh
169+
EOT
156170
}
157171
}
158172

173+
resource "file_local" "generate_create" {
174+
depends_on = [
175+
file_local.instantiate_envrc_snapshot,
176+
file_local.instantiate_inputs_snapshot,
177+
file_local.instantiate_tpl_snapshot,
178+
terraform_data.destroy,
179+
]
180+
directory = local.tf_data_dir
181+
name = "create.sh"
182+
permissions = "0755"
183+
contents = templatefile("${path.module}/create.sh.tpl", {
184+
deploy_path = local.deploy_path
185+
init_script = local.init_script
186+
attempts = local.attempts
187+
timeout = local.timeout
188+
interval = local.interval
189+
})
190+
}
159191
resource "terraform_data" "create" {
160192
depends_on = [
161193
file_local.instantiate_envrc_snapshot,
162194
file_local.instantiate_inputs_snapshot,
163195
file_local.instantiate_tpl_snapshot,
196+
file_local.generate_create,
197+
file_local.generate_destroy,
164198
terraform_data.destroy,
165199
]
166200
triggers_replace = {
167201
never = <<-EOT
168-
This resource is only meant to run once,
169-
on the initial deploy,
170-
the secondary create manages updates.
202+
This resource is only meant to run once, on the initial deploy,
203+
the second create (create_after_persist) manages updates.
171204
EOT
172205
}
173206
provisioner "local-exec" {
174-
command = templatefile("${path.module}/create.sh.tpl", {
175-
deploy_path = local.deploy_path
176-
init_script = local.init_script
177-
attempts = local.attempts
178-
timeout = local.timeout
179-
interval = local.interval
180-
})
207+
command = <<-EOT
208+
set -x
209+
${local.tf_data_dir}/create.sh
210+
EOT
181211
}
182212
}
183213

@@ -229,6 +259,8 @@ resource "terraform_data" "create_after_persist" {
229259
file_local.instantiate_envrc_snapshot,
230260
file_local.instantiate_inputs_snapshot,
231261
file_local.instantiate_tpl_snapshot,
262+
file_local.generate_destroy,
263+
file_local.generate_create,
232264
terraform_data.destroy,
233265
terraform_data.create,
234266
file_local.instantiate_state,
@@ -238,13 +270,10 @@ resource "terraform_data" "create_after_persist" {
238270
trigger = local.deploy_trigger
239271
}
240272
provisioner "local-exec" {
241-
command = templatefile("${path.module}/create.sh.tpl", {
242-
deploy_path = local.deploy_path
243-
init_script = local.init_script
244-
attempts = local.attempts
245-
timeout = local.timeout
246-
interval = local.interval
247-
})
273+
command = <<-EOT
274+
set -x
275+
${local.tf_data_dir}/create.sh
276+
EOT
248277
}
249278
}
250279

@@ -255,21 +284,20 @@ resource "terraform_data" "destroy_end" {
255284
file_local.instantiate_tpl_snapshot,
256285
terraform_data.destroy,
257286
terraform_data.create,
287+
file_local.generate_destroy,
288+
file_local.generate_create,
258289
file_local.instantiate_state,
259290
file_local.instantiate_outputs,
260291
terraform_data.create_after_persist,
261292
]
262293
triggers_replace = {
263294
dp = local.deploy_path
264-
sd = local.skip_destroy
265-
to = local.timeout
266295
}
267296
provisioner "local-exec" {
268-
when = destroy
269-
command = templatefile("${path.module}/destroy.sh.tpl", {
270-
deploy_path = self.triggers_replace.dp
271-
skip_destroy = self.triggers_replace.sd
272-
timeout = self.triggers_replace.to
273-
})
297+
when = destroy
298+
command = <<-EOT
299+
set -x
300+
${self.triggers_replace.dp}/destroy.sh
301+
EOT
274302
}
275303
}

0 commit comments

Comments
 (0)