forked from emvaldes/terraform-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.functions
311 lines (304 loc) · 15 KB
/
action.functions
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
function configure_terraform_template () {
## echo -e "\nManaged Scripts: ${managed_scripts[@]}" ;
if [[ ${#managed_scripts[@]} -eq 0 ]]; then
echo -e "\nWarning: Managed Scripts set is not defined! \n" ;
exit 1 ;
else if [[ ! -e ${manage_terraform_template} ]]; then
if [[ ${result} -gt 0 ]]; then
echo -e "\nWarning: Manage Terraform template was not found! \n" ;
exit 2 ;
fi ;
fi ;
for managed_script in ${managed_scripts[@]}; do
cat /dev/null > ${managed_script} ;
cat ${manage_terraform_template} \
| sed -e "s|{{ console.Restore_Folder }}|${restore_folder}|g" \
-e "s|{{ console.Remote_Origin }}|${remote_origin/https:/git:}|g" \
-e "s|{{ console.Commit_SHAID }}|${commit_shaid}|g" \
-e "s|{{ console.S3Bucket }}|${terraform_s3bucket}|g" \
-e "s|{{ console.Remote_Path }}|${target_location}|g" \
-e "s|{{ console.Verbosity }}|${terraform_verbosity}|g" \
-e "s|{{ console.Target_Workspace }}|${target_workspace}|g" \
-e "s|{{ console.Region }}|${aws_default_region}|g" \
-e "s|{{ github.workspace }}|\${HOME}|g" \
> ${managed_script};
done ;
sed -i -e "s|{{ console.Terraform_Action }}|apply -auto-approve \${terraform_restore}/terraform.tfstate.d/${target_workspace}/terraform.tfplan|g" ${restore_script} ;
sed -i -e "s|{{ console.Terraform_Action }}|destroy -auto-approve|g" ${destroy_script} ;
fi ;
return 0 ;
} ;
function setup_dynamodb_github () {
table_exists="$(
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb list-tables \
| jq '.TableNames[]|select(.=="'${dynamodb_github_table}'")' \
--raw-output
)" ;
if [[ ${#table_exists} -eq 0 ]]; then
echo -e "\nWarning: Creating GitHub-Pipelines DynamoDB Table ...\n" ;
curl --silent ${usercontent_repository}/service/dynamodb/github/pipelines/create.shell \
| sed -e "s|{{ console.Profile }}|${aws_default_profile}|g" \
-e "s|{{ console.Region }}|${aws_default_region}|g" \
-e "s|{{ console.Table_Name }}|${dynamodb_github_table}|g" \
| bash - ;
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb wait table-exists \
--table-name ${dynamodb_github_table} ;
fi; unset table_exists ;
dynamodb_github="${usercontent_repository}/service/dynamodb/github/pipelines/template.json" ;
dynamodb_github_template="${github_workspace}/${dynamodb_github_table}.json" ;
cat /dev/null > ${dynamodb_github_template} ;
wget --quiet --output-document=${dynamodb_github_template} ${dynamodb_github} ;
sed -i -e "s|{{ console.Repo_SHAIndex }}|${commit_shaid}|g" \
-e "s|{{ console.Repository }}|${github_repository}|g" \
-e "s|{{ console.Date_Time }}|${target_timestamp}|g" \
-e "s|{{ console.Author_Name }}|${github_author_name}|g" \
-e "s|{{ console.Repo_Branch }}|${github_repository_branch}|g" \
-e "s|{{ console.Author_Contact }}|???|g" \
-e "s|{{ console.Organization }}|${github_repository_owner}|g" \
-e "s|{{ console.Repo_Version }}|???|g" \
${dynamodb_github_template} ;
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb put-item \
--table-name ${dynamodb_github_table} \
--item file://${dynamodb_github_template} ;
echo -e "\nListing DynamoDB GitHub Table-Record: ...\n"
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb get-item \
--table-name ${dynamodb_github_table} \
--key '{ "shaindex": {"S": "'${commit_shaid}'"}, "repository": { "S": "'${github_repository}'" } }'
return 0 ;
} ;
function setup_dynamodb_terraform () {
table_exists="$(
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb list-tables \
| jq '.TableNames[]|select(.=="'${dynamodb_terraform_table}'")' \
--raw-output
)" ;
if [[ ${#table_exists} -eq 0 ]]; then
echo -e "\nWarning: Creating Terraform-Pipelines DynamoDB Table ...\n" ;
curl --silent ${usercontent_repository}/service/dynamodb/terraform/pipelines/create.shell \
| sed -e "s|{{ console.Profile }}|${aws_default_profile}|g" \
-e "s|{{ console.Region }}|${aws_default_region}|g" \
-e "s|{{ console.Table_Name }}|${dynamodb_terraform_table}|g" \
| bash - ;
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb wait table-exists \
--table-name ${dynamodb_terraform_table} ;
fi; unset table_exists ;
dynamodb_terraform="${usercontent_repository}/service/dynamodb/terraform/pipelines/template.json" ;
dynamodb_terraform_template="${github_workspace}/${dynamodb_terraform_table}.json" ;
cat /dev/null > ${dynamodb_terraform_template} ;
wget --quiet --output-document=${dynamodb_terraform_template} ${dynamodb_terraform} ;
declare -a terraform_variables=();
oIFS="${IFS}"; IFS='_' ;
for param in ${input_params[@]}; do
terraform_variables+=("{ \"S\": \"$(
echo -en ${param} \
| base64 --decode \
| sed -e 's|"|\\\\"|g' \
-e "s|^\(.*\)\(=\)\(.*\)$|\1\2'\3'|g"
)\" },") ;
done ; IFS="${oIFS}" ;
sed -i -e "s|{{ console.Repo_SHAIndex }}|${commit_shaid}|g" \
-e "s|{{ console.Repository }}|${github_repository}|g" \
-e "s|{{ console.Date_Time }}|${target_timestamp}|g" \
-e "s|{{ console.Restore_Location }}|${target_location}|g" \
-e "s|{{ console.Custom_Configs }}|${target_custom_tfvars}|g" \
-e "s|{{ console.Target_LongID }}|???|g" \
-e "s|{{ console.Target_ShortID }}|${target_workspace}|g" \
-e "s|{{ console.Identity_Access }}|${DEVOPS_ACCESS_ROLE}|g" \
-e "s|{{ console.Identity_Account }}|${aws_default_account}|g" \
-e "s|{{ console.Identity_Profile }}|${DEVOPS_ACCOUNT_NAME}|g" \
-e "s|{{ console.Identity_UserID }}|???|g" \
-e "s|{{ console.Source_Pipeline }}|${target_container}|g" \
-e "s|{{ console.Custom_Variables }}|$(
echo ${terraform_variables[@]} | sed -e 's|,$||'
)|g" \
${dynamodb_terraform_template} ;
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb put-item \
--table-name ${dynamodb_terraform_table} \
--item file://${dynamodb_terraform_template} ;
echo -e "\nListing DynamoDB Terraform Table-Record: ...\n"
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb get-item \
--table-name ${dynamodb_terraform_table} \
--key '{ "shaindex": {"S": "'${commit_shaid}'"}, "repository": { "S": "'${github_repository}'" } }'
return 0 ;
} ;
function update_dynamodb_terraform () {
IFS="${IFS}" ; IFS=':' ;
declare -a outputs=($(
terraform output \
| cut -d '=' -f1 \
| tr '[[:space:]]' ':' \
| sed -e 's|\(:\)\{1,\}$||g' -e 's|\(:\)\{1,\}|:|g'
)); IFS="${oIFS}" ;
declare -a terraform_outputs=();
for xitem in ${outputs[@]}; do
output="$(
terraform output ${xitem} | sed -e 's|"|\\\\"|g'
)" ;
terraform_outputs+=("{ \"S\": \"${xitem}='${output}'\" },") ;
done ;
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb update-item \
--table-name ${dynamodb_terraform_table} \
--key '{ "shaindex": { "S": "'${commit_shaid}'" }, "repository": { "S": "'${github_repository}'" } }' \
--update-expression "SET outputs = :outputs" \
--expression-attribute-values '{ ":outputs": { "L": [ '$(
echo ${terraform_outputs[@]} | sed -e 's|,$||'
)' ] } }' ;
return 0 ;
} ;
function backup_terraform_state () {
## tracking_process ${FUNCNAME} "${@}" ;
oIFS="${IFS}" ;
for xitem in "${@}"; do
IFS='='; set `echo -e "${xitem}" | sed -e '1s|^\(-\)\{1,\}||'`
[[ ${1#*\--} = "source-dataset" ]] && export source_dataset="${2}" ;
[[ ${1#*\--} = "restore-path" ]] && export target_restorepath="${2}" ;
[[ ${1#*\--} = "s3bucket-container" ]] && export s3bucket_container="${2}" ;
## [[ ${1#*\--} = "dry-run" ]] && export dry_run="${2}" ;
[[ ${1#*\--} = "verbose" ]] && export verbose='true' ;
[[ ${1#*\--} = "help" ]] && export display_help='true' ;
done; IFS="${oIFS}" ;
## Define custom-parameter(s):
[[ ${#source_dataset} -eq 0 ]] && export target_builset='false' ;
[[ ${#target_restorepath} -eq 0 ]] && export target_restorepath='' ;
[[ ${#s3bucket_container} -eq 0 ]] && export s3bucket_container='' ;
## [[ ${#dry_run} -eq 0 ]] && export dry_run='false' ;
[[ ${#verbose} -eq 0 ]] && export verbose='false' ;
if [[ ${source_dataset} != false ]]; then
echo -e "\nWorking Directory: $(pwd)" ;
export target_s3path="$(
echo -e "${s3bucket_container}/${target_restorepath}" \
| sed -e 's|\(/\)\{2,\}|/|g'
)" ;
echo -e "\nS3Bucket Path: ${target_s3path}" ;
echo -e "\nTransferring Terraform State @ Local-Storage ... ${terraform_s3bucket}" ;
echo aws --profile ${aws_default_profile} \
--region ${aws_default_region} \
s3 cp ${source_dataset} s3://${terraform_s3bucket}/${target_s3path} \
--recursive --only-show-errors ;
aws --profile ${aws_default_profile} \
--region ${aws_default_region} \
s3 cp ${source_dataset} s3://${terraform_s3bucket}/${target_s3path} \
--recursive --only-show-errors ;
echo -e "\nListing Terraform State @ AWS S3 Bucket ... ${terraform_s3bucket}" ;
echo aws --profile ${aws_default_profile} \
--region ${aws_default_region} \
s3 ls s3://${terraform_s3bucket}/${target_s3path} \
--recursive ; echo -e ;
aws --profile ${aws_default_profile} \
--region ${aws_default_region} \
s3 ls s3://${terraform_s3bucket}/${target_s3path} \
--recursive ;
else echo -e "\nWarning: Target Buildset is invalid! \n" ;
exit 1 ;
fi ;
return 0 ;
};
function backup_terraform_configs () {
terraform_modules="${github_workspace}/.terraform" ;
echo -e "\nCreating Target Build-Set Container ... [ ${target_buildset} ]" ;
mkdir -p ${target_buildset}/scripts ;
echo -e "\nTransferring terraform configurations to container ...\n" ;
cp -r ${terraform_tfstate} ${target_buildset}/ | cut -d ' ' -f2- ;
touch ${target_buildset}/${commit_shaid} ;
cp ${restore_script} ${target_buildset}/scripts/ ;
cp ${destroy_script} ${target_buildset}/scripts/ ;
tree ${target_buildset} ;
backup_terraform_state --source-dataset="${target_buildset}" \
--s3bucket-container="${S3BUCKET_CONTAINER}/${target_buildset}" \
;
return 0 ;
} ;
function restore_terraform_state () {
restore_timestamp="$(date +"%y%m%d%H%M%S")";
if [[ "${#restore_shaindex}" -eq 0 ]]; then
echo -e "\nWarning: Git SHA Index is invalid! \n";
exit 1 ;
fi ;
eval $(
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb get-item \
--table-name ${dynamodb_github_table} \
--key '{ "shaindex": {"S": "'${restore_shaindex}'"}, "repository": { "S": "'${restore_project}'" } }' \
--query 'Item.{organization:organization.S,repository:repository.S}' \
| jq -r "to_entries|map(\"export \(.key)=\(.value|tostring)\")|.[]"
) ;
if [[ ( ${#organization} -eq 0 ) || ( ${organization} == 'null' ) ]]; then
echo -e "\nWarning: Source Organization is invalid! \n";
exit 2;
fi;
if [[ ( ${#repository} -eq 0 ) || ( ${repository} == 'null' ) ]]; then
echo -e "\nWarning: Source Repository is invalid! \n";
exit 3;
fi;
eval $(
aws --profile ${aws_default_profile} \
--region ${dynamodb_default_region} \
dynamodb get-item \
--table-name ${dynamodb_terraform_table} \
--key '{ "shaindex": {"S": "'${restore_shaindex}'"}, "repository": { "S": "'${restore_project}'" } }' \
--query 'Item.{location:location.S,workspace:environment.M.target.S}' \
| jq -r "to_entries|map(\"export target_\(.key)=\(.value|tostring)\")|.[]"
) ;
if [[ ( ${#target_workspace} -eq 0 ) || ( ${target_workspace} == 'null' ) ]]; then
echo -e "\nWarning: Restore Environment is invalid! \n";
exit 4;
else echo -e "\nRestore Environment: '${target_workspace}'" ;
fi;
if [[ ( ${#target_location} -eq 0 ) || ( ${target_location} == 'null' ) ]]; then
echo -e "\nWarning: Restore Location is invalid! \n";
exit 5;
else echo -e "\nTarget Location: '${target_location}'" ;
fi;
echo -e "\nRestoring Terraform State @ ${organization}/${repository} -> ${restore_shaindex} [${target_workspace}] = ${target_location} :\n" ;
export target_path="${github_workspace}";
export restore_folder="${target_path}/restore/${restore_shaindex:0:7}" ;
mkdir -p ${restore_folder} ;
export restore_script="${target_path}/restore.shell" ;
declare -a managed_scripts=(${restore_script}) ;
configure_terraform_template ;
terraform_planfile="\${terraform_restore}/terraform.tfstate.d/${target_workspace}/terraform.tfplan";
terraform_action="apply -auto-approve ${terraform_planfile}";
sed -i -e "s|{{ console.Terraform_Action }}|${terraform_action}|g" \
-e "s|\${aws_default_profile}|${AWS_DEFAULT_PROFILE}|g" \
-e "s|\${aws_default_region}|${AWS_DEFAULT_REGION}|g" \
-e "s|\${HOME}|${github_workspace}|g" \
${restore_script} ;
sed -i -e '/^read -p.*$/d' ${restore_script} ;
sed -i -e '/./b' \
-e :n \
-e 'N;s/\n$//;tn' \
${restore_script} ;
echo -e "Restore Terraform Script:" ;
cat ${restore_script};
echo -e ;
bash ${restore_script} ;
cd ${restore_folder} ;
echo -e "\nExecuting Terraform Show ...\n" ;
terraform_states="${restore_folder}/terraform.tfstate.d/${target_workspace}" ;
terraform show -no-color | tee ${terraform_states}/terraform.tfstate.yaml ;
echo -e "\nBacking-Up Terraform Restore-State ..." ;
target_restorepath="restored/${AWS_DEFAULT_ACCOUNT}/${AWS_DEFAULT_REGION}/${restore_timestamp}/terraform.tfstate.d/${target_workspace}" ;
backup_terraform_state --source-dataset="${terraform_states}" \
--restore-path="${target_location}/${target_restorepath}" \
;
return 0 ;
} ;