-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·311 lines (239 loc) · 9.13 KB
/
deploy.sh
File metadata and controls
executable file
·311 lines (239 loc) · 9.13 KB
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
#!/bin/bash
##############################################################################
##############################################################################
##
## this script reads in an env file that should have the following
## exported environment variables:
##
## STACK_NAME = the name of the stack to be deployed
## REGION = the region to deploy the stack within
## STAGE_TEMPLATE = should cloudformation templates be staged in S3 (1|0)
## WORKING_DIR = directory the deployment should occur within
## INFRA_PATH = path to IaC template file directory
## PARAM_PATH = path to IaC parameter file directory
## CF_TEMPLATE_PATH = path to specific IaC template file
## CF_PARAM_PATH = path to specific IaC parameter file
## LOG_PATH = path where logs should be stored
##
##############################################################################
##############################################################################
##############################################################################
##############################################################################
##
## MAGIC NUMBERS
##
##############################################################################
##############################################################################
ARG_NUMBER=1
##############################################################################
##############################################################################
##
## USAGE
##
##############################################################################
##############################################################################
usage(){
if [ $# -lt $ARG_NUMBER ]; then
echo "Usage: "
echo "$0 \ "
echo " --env-file [ENV_FILE_PATH] \ "
exit 1
fi
}
##############################################################################
##############################################################################
##
## PARSE ARGS
##
##############################################################################
##############################################################################
parse_args(){
while [[ $# > 1 ]];
do
key="$1"
case $key in
--env-file)
ENV_FILE="$2"
shift # past argument
;;
*)
# unknown option
;;
esac
shift
done
}
##############################################################################
##############################################################################
##
## Prep logs
##
##############################################################################
##############################################################################
prep_logs(){
ts=$(date +"%Y_%m_%d_%T" | sed -e 's/:/_/g')
LP="${LOG_PATH}/${STACK_NAME}/${ts}"
mkdir -p $LP
VALIDATION_LOG="${LP}/template_validation.txt"
EVENT_LOG="${LP}/event_log.txt"
OUTPUT_LOG="${LP}/output_log.txt"
ERR_VALIDATION_LOG="${LP}/err_template_validation.txt"
ERR_EVENT_LOG="${LP}/err_event_log.txt"
ERR_OUTPUT_LOG="${LP}/err_output_log.txt"
}
##############################################################################
##############################################################################
##
## STAGE
##
##############################################################################
##############################################################################
stage_template(){
KEY="cloudformation/$CF_TEMPLATE_PATH"
export S3_URL="https://s3.amazonaws.com/${STAGING_BUCKET_NAME}/${KEY}"
echo "[+] $(date) - Staging CloudFormation template ... $S3_URL"
aws s3api put-object --bucket $STAGING_BUCKET_NAME --key $KEY --body $CF_TEMPLATE_PATH
}
##############################################################################
##############################################################################
##
## VALIDATE
##
##############################################################################
##############################################################################
validate_template(){
echo "[+] $(date) - Validating CloudFormation template ... $CF_TEMPLATE_PATH"
echo "[+] $(date) - Writing validation log to ${VALIDATION_LOG}"
if [ ${STAGE_TEMPLATE} -gt 0 ];
then
aws cloudformation validate-template \
--template-url $S3_URL \
--region $REGION > $VALIDATION_LOG 2> $ERR_VALIDATION_LOG
else
aws cloudformation validate-template \
--template-body 'file://'$CF_TEMPLATE_PATH \
--region $REGION > $VALIDATION_LOG 2> $ERR_VALIDATION_LOG
fi
if [ $? -gt 0 ]
then
cat $VALIDATION_LOG;
cat $ERR_VALIDATION_LOG;
fi
}
##############################################################################
##############################################################################
##
## DEPLOY STACK
##
##############################################################################
##############################################################################
deploy_stack(){
##
## set CAPABILITY_NAMED_IAM to avoid possible
## failures if IAM resources need to be created ... this setting
## could be considered low security and removed if IAM resources
## are not needed (the intention here is to genearlize)
##
## and setting CAPABILITY_AUTO_EXPAND to support use of nested
## cloudformation modules
##
echo "[+] $(date) - Deploying CloudFormation stack ... $STACK_NAME"
if [ ${STAGE_TEMPLATE} -gt 0 ];
then
aws cloudformation create-stack \
--template-url $S3_URL \
--parameter 'file://'$CF_PARAM_PATH \
--stack-name $STACK_NAME \
--capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
--region $REGION
else
aws cloudformation create-stack \
--template-body 'file://'$CF_TEMPLATE_PATH \
--parameter 'file://'$CF_PARAM_PATH \
--stack-name $STACK_NAME \
--capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
--region $REGION
fi
}
##############################################################################
##############################################################################
##
## WAIT on STACK CREATE
##
##############################################################################
##############################################################################
wait_stack_create(){
echo "[+] $(date) - Waiting for stack-create-complete ... $STACK_NAME"
aws cloudformation wait stack-create-complete \
--stack-name $STACK_NAME \
--region $REGION
}
##############################################################################
##############################################################################
##
## DESCRIBE EVENTS
##
##############################################################################
##############################################################################
describe_events(){
echo "[+] $(date) - Describing stack events ... $STACK_NAME"
echo "[+] $(date) - Writing event log to ${EVENT_LOG}"
aws cloudformation describe-stack-events \
--stack-name $STACK_NAME \
--region $REGION > $EVENT_LOG 2> $ERR_EVENT_LOG
}
##############################################################################
##############################################################################
##
## GET STACK OUTPUTS
##
##############################################################################
##############################################################################
get_stack_outputs(){
echo "[+] $(date) - Describing stack outputs ... $STACK_NAME"
echo "[+] $(date) - Writing output log to ${OUTPUT_LOG}"
aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs" \
--region $REGION \
--output json > $OUTPUT_LOG 2> $ERR_OUTPUT_LOG
}
##############################################################################
##############################################################################
##
## MAIN
##
##############################################################################
##############################################################################
main(){
prep_logs
if [ ${STAGE_TEMPLATE} -gt 0 ];
then
stage_template
fi
if ! validate_template; then
echo "[-] $(date) - Invalid CloudFormation template ... exiting"
return 1
else
deploy_stack
if ! wait_stack_create; then
echo "[-] $(date) - Stack create failed ... "
describe_events
##
## this scenario leaves an orphaned failed stack
## either manually teardown, or run the teardown.sh
##
return 1
else
echo "[+] $(date) - Stack create complete ... "
describe_events
get_stack_outputs
echo "[+] $(date) - SUCCESS! Stack deployment complete."
return 0
fi
fi
}
usage $@
parse_args $@
source $ENV_FILE
main