Skip to content

Commit 3607021

Browse files
authored
Merge pull request hashicorp#247 from hashicorp/handle-planned-and-finished
handle more run and apply statuses
2 parents 6113634 + 046e5bf commit 3607021

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

operations/automation-script/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The script does the following steps:
2323
1. Creates the workspace if it does not already exist.
2424
1. Creates a new configuration version.
2525
1. Uploads the tar file as a new configuration.
26-
1. Adds Terraform and environment variables from the file variables.csv that was included in the cloned repository if it exists or from the local copy in the same directory as the script. That local version adds one Terraform variable called "name" and two Environment variables to the workspace. The first of the environment variables is "CONFIRM_DESTROY" with value 1; it allows a destroy to be done against the workspace. The second is "TF_CLI_ARGS" with value "-no-color"; it supresses color codes from the apply log output. You can edit this file to add as many variables as you want and then add it to your repository.
26+
1. Adds Terraform and environment variables from the file variables.csv that was included in the cloned repository if it exists or from the local copy in the same directory as the script. That local version adds one Terraform variable called "name" with value "Roger" and one Environment variable called "TF_CLI_ARGS" with value "-no-color" to the workspace. This supresses color codes from the apply log output. You can edit this file to add as many variables as you want and then add it to your repository.
2727
1. Determines the number of Sentinel policies so that it knows whether it needs to check them.
2828
1. Starts a new run.
2929
1. Enters a loop to check the run results periodically.
@@ -33,10 +33,14 @@ The script does the following steps:
3333
- If $run_status is "policy_override" and $override is "yes", it overrides the failed policy checks and does an Apply. In this case, one or more Sentinel policies failed, but they were marked "advisory" or "soft-mandatory" and the script was configured to override the failure.
3434
- If $run_status is "policy_override" and $override is "no", it prints out a message indicating that some policies failed and are not being overridden.
3535
- If $run_status is "errored", either the plan failed or a Sentinel policy marked "hard-mandatory" failed. The script terminates.
36+
- If $run_status is "planned_and_finished", the plan had no changes to apply. The script terminates.
37+
- If $run_status is "canceled", a user canceled the run. The script terminates.
38+
- If $run_status is "force_canceled", a user forcefully canceled the run. The script terminates.
39+
- If $run_status is "discarded", a user discarded the run. The script terminates.
3640
- Other values of $run_status cause the loop to repeat after a brief sleep.
3741
1. If $save_plan was set to "true" in the above loop, the script outputs and saves the plan log.
38-
1. If any apply was done, the script goes into a second loop to wait for it to finish.
39-
1. When the apply is finished, the script downloads the apply log and the state files from before and after the apply.
42+
1. If any apply was done, the script goes into a second loop to wait for the apply to finish, error, or be canceled.
43+
1. If and when the apply finishes, the script downloads the apply log and the state files from before and after the apply.
4044

4145
In addition to the loadAndRunWorkspace.sh script, this example includes the following files:
4246

operations/automation-script/loadAndRunWorkspace.sh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ while [ $continue -ne 0 ]; do
291291
# exist or are applicable to the workspace
292292

293293
# Run is planning - get the plan
294+
# Note that we use "True" rather than "true" because python converts the
295+
# boolean "true" in json responses to "True" and "false" to "False"
294296
if [[ "$run_status" == "planned" ]] && [[ "$is_confirmable" == "True" ]] && [[ "$override" == "no" ]]; then
295297
continue=0
296298
echo "There are " $sentinel_policy_count "policies, but none of them are applicable to this workspace."
@@ -344,6 +346,19 @@ while [ $continue -ne 0 ]; do
344346
echo "Plan errored or hard-mandatory policy failed"
345347
save_plan="true"
346348
continue=0
349+
elif [[ "$run_status" == "planned_and_finished" ]]; then
350+
echo "Plan indicates no changes to apply."
351+
save_plan="true"
352+
continue=0
353+
elif [[ "run_status" == "canceled" ]]; then
354+
echo "The run was canceled."
355+
continue=0
356+
elif [[ "run_status" == "force_canceled" ]]; then
357+
echo "The run was canceled forcefully."
358+
continue=0
359+
elif [[ "run_status" == "discarded" ]]; then
360+
echo "The run was discarded."
361+
continue=0
347362
else
348363
# Sleep and then check status again in next loop
349364
echo "We will sleep and try again soon."
@@ -392,6 +407,12 @@ if [[ "$applied" == "true" ]]; then
392407
if [[ "$apply_status" == "finished" ]]; then
393408
echo "Apply finished."
394409
continue=0
410+
elif [[ "$apply_status" == "errored" ]]; then
411+
echo "Apply errored."
412+
continue=0
413+
elif [[ "$apply_status" == "canceled" ]]; then
414+
echo "Apply was canceled."
415+
continue=0
395416
else
396417
# Sleep and then check apply status again in next loop
397418
echo "We will sleep and try again soon."
@@ -451,6 +472,6 @@ rm run.json
451472
rm variable.template.json
452473
rm variable.json
453474
rm workspace.template.json
454-
rm workspace.json
475+
rm workspace.json
455476

456477
echo "Finished"
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
name,Roger,terraform,false,false
2-
CONFIRM_DESTROY,1,env,false,false
32
TF_CLI_ARGS,-no-color,env,false,false

0 commit comments

Comments
 (0)