Skip to content

Commit 5e47246

Browse files
authored
Merge pull request #22 from gruntwork-io/yori-allow-delete-provisioner
Destroy provisioner
2 parents 45d9cb3 + 494c13c commit 5e47246

File tree

5 files changed

+86
-11
lines changed

5 files changed

+86
-11
lines changed

examples/pex/main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ module "pex_resource" {
4545
RUN_PEX_TEST_ENV = var.echo_string
4646
}
4747

48+
enable_destroy_provisioner = true
49+
destroy_command_args = "--is-delete"
50+
pass_in_previous_triggers = true
51+
previous_trigger_option = "--triggers-json"
52+
4853
enabled = var.enabled
4954
}
5055

examples/pex/sample-python-script/sample_python_script/main.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,31 @@
77

88
@click.command()
99
@click.option("--is-data/--no-is-data", default=False, help="Whether or not this is run as a data source")
10-
def main(is_data):
10+
@click.option("--is-delete/--no-is-delete", default=False, help="Whether or not this is run as a delete provisioner")
11+
@click.option("--triggers-json", help="JSON encoded triggers for the resource")
12+
def main(is_data, is_delete, triggers_json):
1113
if is_data:
1214
query = json.loads(sys.stdin.read())
13-
print(json.dumps({
14-
"echo": query.get("echo", ""),
15-
"out": "This was successfully run as data source.",
16-
"python_version_info": str(sys.version_info),
17-
}))
15+
print(
16+
json.dumps(
17+
{
18+
"echo": query.get("echo", ""),
19+
"out": "This was successfully run as data source.",
20+
"python_version_info": str(sys.version_info),
21+
"triggers": triggers_json,
22+
}
23+
)
24+
)
25+
elif is_delete:
26+
print("python version: {}".format(sys.version_info))
27+
print("This was successfully run as a ___DELETE___ local-exec provisioner")
28+
print("Environment variable: {}".format(os.environ.get("RUN_PEX_TEST_ENV", None)))
29+
print("Triggers: {}".format(triggers_json))
1830
else:
1931
print("python version: {}".format(sys.version_info))
2032
print("This was successfully run as a local-exec provisioner")
2133
print("Environment variable: {}".format(os.environ.get("RUN_PEX_TEST_ENV", None)))
34+
print("Triggers: {}".format(triggers_json))
2235

2336

2437
if __name__ == "__main__":

modules/run-pex-as-resource/main.tf

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,23 @@ resource "null_resource" "run_pex" {
2121
triggers = var.triggers
2222

2323
provisioner "local-exec" {
24-
command = "python ${module.pex_env.pex_path} ${module.pex_env.entrypoint_path} ${var.script_main_function} ${var.command_args}"
24+
command = "${local.python_call} ${var.command_args}"
25+
environment = merge(
26+
{
27+
PYTHONPATH = module.pex_env.python_path
28+
},
29+
var.env,
30+
)
31+
}
2532

33+
provisioner "local-exec" {
34+
when = destroy
35+
command = (
36+
var.enable_destroy_provisioner
37+
# NOTE: The nested string interpolation can not be extracted because of the reference to self.
38+
? "${local.python_call} ${var.destroy_command_args} ${var.pass_in_previous_triggers ? "${var.previous_trigger_option} '${jsonencode(self.triggers != null ? self.triggers : {})}'" : ""}"
39+
: "echo 'Skipping delete provisioner'"
40+
)
2641
environment = merge(
2742
{
2843
PYTHONPATH = module.pex_env.python_path
@@ -31,3 +46,7 @@ resource "null_resource" "run_pex" {
3146
)
3247
}
3348
}
49+
50+
locals {
51+
python_call = "python ${module.pex_env.pex_path} ${module.pex_env.entrypoint_path} ${var.script_main_function}"
52+
}

modules/run-pex-as-resource/variables.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ variable "command_args" {
3232
default = ""
3333
}
3434

35+
variable "destroy_command_args" {
36+
description = "The arguments to pass to the command as a string on delete"
37+
type = string
38+
39+
# We don't use null here because this is interpolated into the python script.
40+
default = ""
41+
}
42+
3543
variable "triggers" {
3644
description = "A map of arbitrary strings that, when changed, will force the null resource to be replaced, re-running any associated provisioners."
3745
type = map(string)
@@ -49,3 +57,21 @@ variable "enabled" {
4957
type = bool
5058
default = true
5159
}
60+
61+
variable "enable_destroy_provisioner" {
62+
description = "If you set this variable to true, the same command will be called on destroy with the args specified in destroy_command_args."
63+
type = bool
64+
default = false
65+
}
66+
67+
variable "pass_in_previous_triggers" {
68+
description = "If you set this variable to true, this module will pass in the json encoded triggers that were used when the resource was created. If the script expects option args, use var.previous_trigger_option to set which option to pass the triggers json as."
69+
type = bool
70+
default = false
71+
}
72+
73+
variable "previous_trigger_option" {
74+
description = "Pass in the json encoded trigger with this string as the option to passing into the command. E.g, setting this to `--triggers` will pass in the option `--triggers TRIGGERS_JSON`."
75+
type = string
76+
default = ""
77+
}

test/pex_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/gruntwork-io/terratest/modules/random"
99
"github.com/gruntwork-io/terratest/modules/terraform"
10-
"github.com/gruntwork-io/terratest/modules/test-structure"
10+
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
1111
"github.com/stretchr/testify/assert"
1212
)
1313

@@ -16,7 +16,10 @@ func TestRunPex(t *testing.T) {
1616

1717
testFolder := test_structure.CopyTerraformFolderToTemp(t, "..", "examples")
1818
terratestOptions := createBaseTerratestOptions(t, filepath.Join(testFolder, "pex"))
19-
defer terraform.Destroy(t, terratestOptions)
19+
defer func() {
20+
destroyOut := terraform.Destroy(t, terratestOptions)
21+
assert.Contains(t, destroyOut, "___DELETE___")
22+
}()
2023

2124
expectedFoo := random.UniqueId()
2225
terratestOptions.Vars = map[string]interface{}{
@@ -34,9 +37,18 @@ func TestRunPexTriggers(t *testing.T) {
3437

3538
testFolder := test_structure.CopyTerraformFolderToTemp(t, "..", "examples")
3639
terratestOptions := createBaseTerratestOptions(t, filepath.Join(testFolder, "pex"))
37-
defer terraform.Destroy(t, terratestOptions)
38-
3940
expectedFoo := random.UniqueId()
41+
42+
defer func() {
43+
// Remove triggers from the passed in vars to ensure the unique string is not passed in on destroy. This way, we
44+
// can validate that the triggers option is indeed coming from the triggers stored in the state file.
45+
delete(terratestOptions.Vars, "triggers")
46+
47+
destroyOut := terraform.Destroy(t, terratestOptions)
48+
assert.Contains(t, destroyOut, "___DELETE___")
49+
assert.Contains(t, destroyOut, expectedFoo)
50+
}()
51+
4052
terratestOptions.Vars = map[string]interface{}{
4153
"echo_string": expectedFoo,
4254
"triggers": map[string]string{

0 commit comments

Comments
 (0)