Skip to content

Commit f72da47

Browse files
authored
Merge pull request hashicorp#127 from hashicorp/jray-update-automation-script
update scripts to use TFE_ORG, TFE_ADDR env vars, updated API rate limit to match current limit, add provision for import-sentinel for CLI argument of policy set ID, change gitignore to ignore an JSON created, update scripts to delete files automatically created during runs, and README updates to reflect all changes.
2 parents a813e9f + 81a7281 commit f72da47

16 files changed

+433
-156
lines changed

.gitignore

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,18 @@
2323
.DS_Store
2424

2525
# Ignored vscode files
26-
.vscode/
26+
.vscode/
27+
28+
# Ignore Any Generated JSON Files
29+
operations/automation-script/apply.json
30+
operations/automation-script/configversion.json
31+
operations/automation-script/run.template.json
32+
operations/automation-script/run.json
33+
operations/automation-script/variable.template.json
34+
operations/automation-script/variable.json
35+
operations/automation-script/workspace.template.json
36+
operations/automation-script/workspace.json
37+
operations/sentinel-policies-scripts/create-policy.template.json
38+
operations/sentinel-policies-scripts/create-policy.json
39+
operations/variable-scripts/variable.template.json
40+
operations/variable-scripts/variable.json

operations/automation-script/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Three arguments can be provided on the command line when calling the script:
1515

1616
If you only want to set override to "yes" without passing values for the first two arguments, please use `./loadAndRunWorkspace.sh "" "" yes` to run the script.
1717

18-
The script uses several json templates which must be placed in the same directory as the script itself.
18+
The script uses several json templates which are written out to the file system and then deleted.
1919

2020
The script does the following steps:
2121
1. Clones a git repository containing Terraform configuration code or uses the code in the config directory if no git URL was provided.
@@ -38,21 +38,20 @@ The script does the following steps:
3838
1. If any apply was done, the script goes into a second loop to wait for it to finish.
3939
1. When the apply is finished, the script downloads the apply log and the state files from before and after the apply.
4040

41-
*Note* that some json template files are included from which other json files are generated so that they can be passed to the curl commands.
42-
4341
In addition to the loadAndRunWorkspace.sh script, this example includes the following files:
4442

45-
1. [config/main.tf](./config/main.tf): the file with some Terraform code that says "Hello" to the person whose name is given and generates a random number. This is used if no git URL is provided to the script.
46-
1. [workspace.template.json](./workspace.template.json) which is used to generate workspace.json which is used when creating the workspace. If you wish to add or modify the API commands that are included in _@workspace.json_ payload, add them to _workspace.template.json_ and be sure to check the Terraform Enterprise API [syntax](https://www.terraform.io/docs/enterprise/api/workspaces.html#update-a-workspace). Update or modify `"terraform-version": "0.11.14"` within _workspace.template.json_ to set a specific workspace version of Terraform OSS binary.
47-
1. [configversion.json](./configversion.json) which is used to generate a new configuration version.
48-
1. [variable.template.json](./variable.template.json) which is used to generate variable.json which is used when creating a variable called "name" in the workspace.
49-
1. [run.template.json](./run.template.json) which is used to generate run.json which is used when triggering a run against the workspace.
50-
1. [apply.json](./apply.json) which is used when doing the apply against the workspace.
51-
1. variables.csv which contains the variables that are uploaded to the workspace if no file with the same name is found in the root directory of the cloned repository. The columns are key, value, category, hcl, and sensitive with the last two corresponding to the hcl and sensitive checkboxes of TFE variables.
43+
1. [config/main.tf](./config/main.tf) which is a file with some Terraform code that says "Hello" to the person whose name is given and generates a random number. This is used if no git URL is provided to the script.
44+
1. [variables.csv](./variables.csv) which contains the variables that are uploaded to the workspace if no file with the same name is found in the root directory of the cloned repository. The columns are key, value, category, hcl, and sensitive with the last two corresponding to the hcl and sensitive checkboxes of TFE variables. This should be in the same directory as the script unless you include a file with the same name in your git repository.
5245
1. [deleteWorkspace.sh](./deleteWorkspace.sh): a script that can be used to delete the workspace.
5346
1. [restrict-name-variable.sentinel](./restrict-name-variable.sentinel): a Sentinel policy you can add to your TFE organization in order to see how the script can check Sentinel policies and even override soft-mandatory failures.
5447

55-
*Note* that the json templates file need to be in the same directory as the script itself. The variables.csv file should also be in the same directory as the script unless you include a file with the same name in your git repository.
48+
The following files are embedded inside the script:
49+
50+
1. **workspace.template.json** which is used to generate _workspace.json_ which is used when creating the workspace. If you wish to add or modify the settings that are included in the _@workspace.json_ payload, add them to _workspace.template.json_ inside the script and be sure to check the Terraform Enterprise API [syntax](https://www.terraform.io/docs/enterprise/api/workspaces.html#update-a-workspace). Update or modify `"terraform-version": "0.11.14"` within _workspace.template.json_ to set a specific workspace version of the Terraform OSS binary.
51+
1. **configversion.json** which is used to generate a new configuration version.
52+
1. **variable.template.json** which is used to generate _variable.json_ which is used when creating a variable called "name" in the workspace.
53+
1. **run.template.json** which is used to generate _run.json_ which is used when triggering a run against the workspace.
54+
1. **apply.json** which is used when doing the apply against the workspace.
5655

5756
## Preparation
5857
Do the following before using this script:
@@ -72,11 +71,11 @@ If you use this script with a Private Terraform Enterprise (PTFE) server that us
7271
## Instructions
7372
Follow these instructions to run the script with with the included main.tf and variables.csv files or with your own git repository:
7473

75-
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 "app.terraform.io" which is the address of the SaaS Terraform Enterprise server.
76-
1. Edit the script and set the organization variable to the name of your Terraform Enterprise organization.
7774
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.
7875
1. `export TFE_TOKEN=<owners_token>` where \<owners_token\> is the token generated in the previous step.
79-
1. If you want, you can also change the name of the workspace that will be created by editing the workspace variable. Note that you can also pass the workspace as the second argument to the script.
76+
1. `export TFE_ORG=<your_organization>` where \<your_organization\> is the name of your target TFE organization.
77+
1. `export TFE_ADDR=<your_address>` where \<your_address\> is the custom address of your target TFE server in the format server.domain.tld. If you do not set this environment variable it will default to the Terraform Enterprise Cloud/SaaS address of app.terraform.io.
78+
1. If you want, edit _loadAndRunWorkspace.sh_ to change the name of the workspace that will be created by editing the workspace variable. *Note* that you can also pass the workspace as the second argument to the script.
8079
1. If you want, you can change the sleep_duration variable which controls how often the script checks the status of the triggered run (in seconds). Setting a longer value would make sense if using Terraform code that takes longer to apply.
8180
1. If you are providing a URL to clone a git repository, you can add Terraform and environment variables needed by your Terraform code to [variables.csv](./variables.csv) and remove the "name" variable. You can also add the edited variables.csv file to your repository.
8281
1. If you want to use the sample main.tf or other code you place in the config directory, run `./loadAndRunWorkspace.sh` or `./loadAndRunWorkspace.sh "" "" <override>` where \<override\> is "yes" or "no". (The empty quotes are needed in the second case so that override is the third variable.) If you do not specify a value for \<override\>, the script will set it to "no".

operations/automation-script/apply.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

operations/automation-script/configversion.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

operations/automation-script/deleteWorkspace.sh

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
#!/bin/bash
22
# Script to delete the workspace created by the loadAndRunWorkspace.sh script
33

4-
# Make sure TFE_TOKEN environment variable is set
5-
# to owners team token for organization
6-
7-
# Set address if using private Terraform Enterprise server.
8-
# Set organization and workspace to create.
9-
# You should edit these before running.
10-
address="app.terraform.io"
11-
organization="<your_organization>"
4+
# Make sure TFE_TOKEN and TFE_ORG environment variables are set
5+
# to owners team token and organization name for the respective
6+
# TFE environment. TFE_ADDR should be set to the FQDN/URL of the private
7+
# TFE server or if unset it will default to TF Cloud/SaaS address.
8+
9+
if [ ! -z "$TFE_TOKEN" ]; then
10+
token=$TFE_TOKEN
11+
echo "TFE_TOKEN environment variable was found."
12+
else
13+
echo "TFE_TOKEN environment variable was not set."
14+
echo "You must export/set the TFE_TOKEN environment variable."
15+
echo "It should be a user or team token that has write or admin"
16+
echo "permission on the workspace."
17+
echo "Exiting."
18+
exit
19+
fi
20+
21+
# Evaluate $TFE_ORG environment variable
22+
# If not set, give error and exit
23+
if [ ! -z "$TFE_ORG" ]; then
24+
organization=$TFE_ORG
25+
echo "TFE_ORG environment variable was set to ${TFE_ORG}."
26+
echo "Using organization, ${organization}."
27+
else
28+
echo "You must export/set the TFE_ORG environment variable."
29+
echo "Exiting."
30+
exit
31+
fi
32+
33+
# Evaluate $TFE_ADDR environment variable if it exists
34+
# Otherwise, use "app.terraform.io"
35+
# You should edit these before running the script.
36+
if [ ! -z "$TFE_ADDR" ]; then
37+
address=$TFE_ADDR
38+
echo "TFE_ADDR environment variable was set to ${TFE_ADDR}."
39+
echo "Using address, ${address}"
40+
else
41+
address="app.terraform.io"
42+
echo "TFE_ADDR environment variable was not set."
43+
echo "Using Terraform Cloud (TFE SaaS) address, app.terraform.io."
44+
echo "If you want to use a private TFE server, export/set TFE_ADDR."
45+
fi
46+
1247
workspace="workspace-from-api"
1348

1449
# Set workspace if provided as the second argument

operations/automation-script/loadAndRunWorkspace.sh

Lines changed: 131 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,52 @@
88
# If an apply is done, the script waits for it to finish and then
99
# downloads the apply log and the before and after state files.
1010

11-
# Make sure TFE_TOKEN environment variable is set
12-
# to owners team token for organization
13-
14-
# Set address if using private Terraform Enterprise server.
15-
# Set organization and workspace to create.
16-
# You should edit these before running.
17-
address="app.terraform.io"
18-
organization="<your_organization>"
19-
# workspace name should not have spaces
11+
# Make sure TFE_TOKEN and TFE_ORG environment variables are set
12+
# to owners team token and organization name for the respective
13+
# TFE environment. TFE_ADDR should be set to the FQDN/URL of the private
14+
# TFE server or if unset it will default to TF Cloud/SaaS address.
15+
16+
if [ ! -z "$TFE_TOKEN" ]; then
17+
token=$TFE_TOKEN
18+
echo "TFE_TOKEN environment variable was found."
19+
else
20+
echo "TFE_TOKEN environment variable was not set."
21+
echo "You must export/set the TFE_TOKEN environment variable."
22+
echo "It should be a user or team token that has write or admin"
23+
echo "permission on the workspace."
24+
echo "Exiting."
25+
exit
26+
fi
27+
28+
# Evaluate $TFE_ORG environment variable
29+
# If not set, give error and exit
30+
if [ ! -z "$TFE_ORG" ]; then
31+
organization=$TFE_ORG
32+
echo "TFE_ORG environment variable was set to ${TFE_ORG}."
33+
echo "Using organization, ${organization}."
34+
else
35+
echo "You must export/set the TFE_ORG environment variable."
36+
echo "Exiting."
37+
exit
38+
fi
39+
40+
# Evaluate $TFE_ADDR environment variable if it exists
41+
# Otherwise, use "app.terraform.io"
42+
# You should edit these before running the script.
43+
if [ ! -z "$TFE_ADDR" ]; then
44+
address=$TFE_ADDR
45+
echo "TFE_ADDR environment variable was set to ${TFE_ADDR}."
46+
echo "Using address, ${address}"
47+
else
48+
address="app.terraform.io"
49+
echo "TFE_ADDR environment variable was not set."
50+
echo "Using Terraform Cloud (TFE SaaS) address, app.terraform.io."
51+
echo "If you want to use a private TFE server, export/set TFE_ADDR."
52+
fi
53+
54+
# workspace name should not have spaces and should be set as second
55+
# argument from CLI
56+
2057
workspace="workspace-from-api"
2158

2259
# You can change sleep duration if desired
@@ -72,6 +109,81 @@ fi
72109
echo "Tarring configuration directory."
73110
tar -czf ${config_dir}.tar.gz -C ${config_dir} --exclude .git .
74111

112+
# Write out workspace.template.json
113+
cat > workspace.template.json <<EOF
114+
{
115+
"data":
116+
{
117+
"attributes": {
118+
"name":"placeholder",
119+
"terraform-version": "0.11.14"
120+
},
121+
"type":"workspaces"
122+
}
123+
}
124+
EOF
125+
126+
# Write out configversion.json
127+
cat > configversion.json <<EOF
128+
{
129+
"data": {
130+
"type": "configuration-versions",
131+
"attributes": {
132+
"auto-queue-runs": false
133+
}
134+
}
135+
}
136+
EOF
137+
138+
# Write out variable.template.json
139+
cat > variable.template.json <<EOF
140+
{
141+
"data": {
142+
"type":"vars",
143+
"attributes": {
144+
"key":"my-key",
145+
"value":"my-value",
146+
"category":"my-category",
147+
"hcl":my-hcl,
148+
"sensitive":my-sensitive
149+
}
150+
},
151+
"filter": {
152+
"organization": {
153+
"username":"my-organization"
154+
},
155+
"workspace": {
156+
"name":"my-workspace"
157+
}
158+
}
159+
}
160+
EOF
161+
162+
# Write out run.template.json
163+
cat > run.template.json <<EOF
164+
{
165+
"data": {
166+
"attributes": {
167+
"is-destroy":false
168+
},
169+
"type":"runs",
170+
"relationships": {
171+
"workspace": {
172+
"data": {
173+
"type": "workspaces",
174+
"id": "workspace_id"
175+
}
176+
}
177+
}
178+
}
179+
}
180+
EOF
181+
182+
# Write out apply.json
183+
cat > apply.json <<EOF
184+
{"comment": "apply via API"}
185+
EOF
186+
75187
#Set name of workspace in workspace.json
76188
sed "s/placeholder/${workspace}/" < workspace.template.json > workspace.json
77189

@@ -321,4 +433,14 @@ if [[ "$applied" == "true" ]]; then
321433

322434
fi
323435

436+
# Remove json files
437+
rm apply.json
438+
rm configversion.json
439+
rm run.template.json
440+
rm run.json
441+
rm variable.template.json
442+
rm variable.json
443+
rm workspace.template.json
444+
rm workspace.json
445+
324446
echo "Finished"

operations/automation-script/run.template.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

operations/automation-script/variable.template.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

operations/automation-script/workspace.template.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)