-
Notifications
You must be signed in to change notification settings - Fork 3
/
exec_code_api.sh
executable file
·185 lines (147 loc) · 5.04 KB
/
exec_code_api.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash
# Make sure tfc_token environment variable is set
# to owners team token for organization
# Set address if using private Terraform Enterprise server.
# Set organization and workspace to create.
# You should edit these before running.
tfc_token=`cat tfe_team_token`
address="app.terraform.io"
organization="<MY_ORG>"
workspace="<MY_WORKSPACE>"
########################
# 01) CREATE WORKSPACE #
########################
#Set name of workspace in workspace.json (create a payload.json)
sed -e "s/placeholder/$workspace/" < workspace.template.json > workspace.json
# Create workspace
workspace_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @workspace.json \
"https://${address}/api/v2/organizations/${organization}/workspaces"
)
workspace_id=$(
echo $workspace_result | jq -r ".data | select (.attributes.name == \"$workspace\") | .id "
)
echo "Workspace created. WorkspaceID: $workspace_id" && echo
read -n 1 -r -p "Press any key to continue with STEP 02) Variables"
#####################################
# 02) ASSIGN VARIABLES TO WORKSPACE #
#####################################
# Add variables to workspace
while IFS=',' read -r key value category hcl sensitive
do
sed -e "s/my-organization/$organization/" \
-e "s/my-workspace/$workspace_id/" \
-e "s/my-key/$key/" \
-e "s/my-value/$value/" \
-e "s/my-category/$category/" \
-e "s/my-hcl/$hcl/" \
-e "s/my-sensitive/$sensitive/" < variable.template.json > variable.json
echo "Adding variable $key in category $category "
upload_variable_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
--data @variable.json \
"https://${address}/api/v2/vars?filter%5Borganization%5D%5Bname%5D=${organization}&filter%5Bworkspace%5D%5Bname%5D=${workspace}"
)
done < variables.csv
echo
read -n 1 -r -p "Press any key to continue with STEP 03) Config-Version"
####################################
# 03) Create configuration version #
####################################
configuration_version_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @configversion.json \
"https://${address}/api/v2/workspaces/${workspace_id}/configuration-versions"
)
upload_url=$(
echo $configuration_version_result | jq -r '.data.attributes."upload-url"'
)
configversion_id=$(
echo $configuration_version_result | jq -r '.data.id'
)
echo "URL: $upload_url"
echo "configversion_id: $configversion_id" && echo
read -n 1 -r -p "Press any key to continue with STEP 04) Upload Configuration"
############################
# 04) Upload Configuration #
############################
#build myconfig.tar.gz
create_config=$(
cd terraform_project
tar -cvf myconfig.tar .
gzip myconfig.tar
mv myconfig.tar.gz ../.
cd ..
)
echo "Config tar.gz created and ready for upload"
echo "imagine this code could also be cloned from a repository"
# Upload configuration
upload_config=$(
curl -Ss \
--header "Content-Type: application/octet-stream" \
--request PUT \
--data-binary @myconfig.tar.gz \
"$upload_url"
)
echo "config uploaded..." && echo
read -n 1 -r -p "Press any key to continue with STEP 05) Run a plan"
##################
# 05) Run a Plan #
##################
sed -e "s/workspace_id/$workspace_id/" \
-e "s/configversion_id/$configversion_id/" < run-plan.template.json > run-plan.json
run_plan=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @run-plan.json \
"https://${address}/api/v2/runs"
)
run_id=$(
echo $run_plan | jq -r '.data.id'
)
echo "Run-ID: $run_id" && echo
read -n 1 -r -p "Press any key to continue with STEP 06 and 07) Check state and do apply"
#######################################################
# 06 & 07) Apply the plan if it is in the right state #
#######################################################
continue=1
while [ $continue -ne 0 ]
do
# check status
check_status=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
"https://${address}/api/v2/runs/${run_id}" |\
jq -r '.data.attributes.status'
)
if [[ "$check_status" == "cost_estimated" ]] ; then
continue=0
# Do the apply
echo "cost estimated. Doing apply..."
apply_result=$(
curl -Ss \
--header "Authorization: Bearer $tfc_token" \
--header "Content-Type: application/vnd.api+json" \
--data @apply.json \
"https://${address}/api/v2/runs/${run_id}/actions/apply"
)
elif [[ "$check_status" == "errored" ]]; then
echo "Plan errored or hard-mandatory policy failed"
continue=0
else
echo "current status: $check_status"
sleep 5
fi
done