Skip to content

Commit 94b06c7

Browse files
committed
documented need for python3 and added comments to script
1 parent 6434d5a commit 94b06c7

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

operations/automation-script/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ Do the following before using this script:
3333

3434
1. `git clone https://github.com/hashicorp/terraform-guides.git`
3535
1. `cd operations/automation-script`
36+
1. Make sure [python3](https://www.python.org/downloads/) is installed on your machine and in your path since the script uses python to parse JSON documents returned by the Terraform Enterprise REST API.
3637

3738
## Instructions
3839
Follow these instructions to run the script:
3940

4041
1. If you are using a private Terraform Enterprise server, edit the script and set the address variable to the address of your server. Otherwise, you would leave the address set to "atlas.hashicorp.com" which is the address of the SaaS Terraform Enterprise server.
4142
1. Edit the script and set the organization variable to the name of your Terraform Enterprise organization.
43+
1. Generate a [team token](https://www.terraform.io/docs/enterprise/users-teams-organizations/service-accounts.html#team-service-accounts) for the owners team in your organization in the Terraform Enterprise UI by selecting your organization settings, then Teams, then owners, and then clicking the Generate button and saving the token that is displayed.
44+
1. `export ATLAS_TOKEN=<owners_token>` where \<owners_token\> is the token generated in the previous step.
4245
1. If you want, you can also change the name of the workspace that will be created and the sleep_duration variable which controls how often the script checks the status of the triggered run (in seconds).
4346
1. Run `./loadAndRunWorkspace.sh <name>` or `./loadAndRunWorkspace.sh <name> <override>` where \<name\> is any name (without spaces) and \<override\> is "yes" or "no". If you do not specify a value for \<override\>, the script will set it to "no".
4447

operations/automation-script/loadAndRunWorkspace.sh

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
# Make sure ATLAS_TOKEN environment variable is set
44
# to owners team token for organization
55

6-
# Set PTFE address, organization, and workspace to create. You should edit these before running.
6+
# Set address if using private Terraform Enterprise server.
7+
# Set organization and workspace to create.
8+
# You should edit these before running.
79
address="atlas.hashicorp.com"
810
organization="<your_organization>"
911
workspace="workspace-from-api"
@@ -12,11 +14,12 @@ workspace="workspace-from-api"
1214
sleep_duration=15
1315

1416
# name of person to set name variable to
17+
# first argument passed to script
1518
name=$1
1619

17-
# Override soft-mandatory policy checks that fail
18-
# Set to "yes" or "no"
19-
# if not specified, then we set to "no"
20+
# Override soft-mandatory policy checks that fail.
21+
# Set to "yes" or "no" in second argument passed to script.
22+
# If not specified, then this is set to "no"
2023
if [ ! -z $2 ]; then
2124
override=$2
2225
else
@@ -33,65 +36,63 @@ cd ..
3336
#Set name of workspace in workspace.json
3437
sed "s/placeholder/$workspace/" < workspace.template.json > workspace.json
3538

39+
# Create workspace
3640
workspace_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --request POST --data @workspace.json "https://${address}/api/v2/organizations/${organization}/workspaces")
3741

3842
# Parse workspace_id from workspace_result
3943
workspace_id=$(echo $workspace_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['id'])")
40-
4144
echo "Workspace ID: " $workspace_id
4245

43-
# Create configuration versions
46+
# Create configuration version
4447
configuration_version_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @configversion.json "https://${address}/api/v2/workspaces/${workspace_id}/configuration-versions")
4548

4649
# Parse configuration_version_id and upload_url
4750
config_version_id=$(echo $configuration_version_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['id'])")
4851
upload_url=$(echo $configuration_version_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['attributes']['upload-url'])")
49-
5052
echo "Config Version ID: " $config_version_id
5153
echo "Upload URL: " $upload_url
5254

5355
# Upload configuration
5456
curl --request PUT -F '[email protected]' "$upload_url"
5557

56-
# Add name variable
58+
# Add name variable to workspace
5759
sed -e "s/my-name/$name/" -e "s/my-organization/$organization/" -e "s/my-workspace/$workspace/" < variable.template.json > variable.json
58-
5960
upload_variable_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @variable.json "https://${address}/api/v2/vars?filter%5Borganization%5D%5Busername%5D=${organization}&filter%5Bworkspace%5D%5Bname%5D=${workspace}")
6061

6162
# Do a run
6263
sed "s/workspace_id/$workspace_id/" < run.template.json > run.json
63-
6464
run_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @run.json https://${address}/api/v2/runs)
6565

66-
# Parse run run_result
66+
# Parse run_result
6767
run_id=$(echo $run_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['id'])")
6868
echo "Run ID: " $run_id
6969

70-
# Check run run result
70+
# Check run result in loop
7171
continue=1
7272
while [ $continue -ne 0 ]; do
7373
# Sleep a bit
7474
sleep $sleep_duration
7575
echo "Checking run status"
7676

77-
# Check the status
77+
# Check the status of run
7878
check_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" https://${address}/api/v2/runs/${run_id})
7979

80-
# Parse out the startus
80+
# Parse out the run status
8181
run_status=$(echo $check_result | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['attributes']['status'])")
8282
echo "Run Status: " $run_status
8383

84-
# If status is "policy_checked" or "policy_override",
85-
# then do Apply. If "errored", exit loop.
86-
# Anything else, continue loop
84+
# Apply in some cases
85+
# policy_checked means all Sentinel policies passed
8786
if [[ "$run_status" == "policy_checked" ]] ; then
8887
continue=0
8988
# Do the apply
9089
echo "Policies passed. Doing Apply"
9190
apply_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @apply.json https://${address}/api/v2/runs/${run_id}/actions/apply)
91+
# policy_override means at least 1 Sentinel policy failed
92+
# but since $override is "yes", we will override and then apply
9293
elif [[ "$run_status" == "policy_override" ]] && [[ "$override" == "yes" ]]; then
9394
continue=0
94-
echo "Some policies failed, but will override"
95+
echo "Some policies failed, but overriding"
9596
# Get the policy check ID
9697
echo "Getting policy check ID"
9798
policy_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" https://${address}/api/v2/runs/${run_id}/policy-checks)
@@ -104,13 +105,19 @@ while [ $continue -ne 0 ]; do
104105
# Do the apply
105106
echo "Doing Apply"
106107
apply_result=$(curl --header "Authorization: Bearer $ATLAS_TOKEN" --header "Content-Type: application/vnd.api+json" --data @apply.json https://${address}/api/v2/runs/${run_id}/actions/apply)
108+
# policy_override means at least 1 Sentinel policy failed
109+
# but since $override is "no", we will not override
110+
# and will not apply
107111
elif [[ "$run_status" == "policy_override" ]] && [[ "$override" == "no" ]]; then
108112
echo "Some policies failed, but will not override. Check run in Terraform Enterprise UI."
109113
continue=0
114+
# errored means that plan had an error or that a hard-mandatory
115+
# policy failed
110116
elif [[ "$run_status" == "errored" ]]; then
111117
echo "Plan errored or hard-mandatory policy failed"
112118
continue=0
113119
else
114-
sleep $sleep_duration
120+
# Sleep a bit and then check status again in next loop
121+
sleep $sleep_duration
115122
fi
116123
done

0 commit comments

Comments
 (0)