-
Notifications
You must be signed in to change notification settings - Fork 24
/
cfn-promote.sh
executable file
·367 lines (283 loc) · 13.4 KB
/
cfn-promote.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/bin/bash
# This script is called from Jenkins to promote the service and deploy using CloudFormation
ENVIRONMENT="$1"
BUILD_PATH="$2"
# image name is optional
IMAGE_NAME="$3"
CFN_DEPLOY_RULES="$(readlink -f "${BUILD_PATH}/deploy/${ENVIRONMENT}.cfn.yaml")"
echo "************************************************************************"
echo "************************ Checking Status of CFN ************************"
echo "************************************************************************"
RETCODE=0
echo
echo "*** Checking if shyaml is installed"
command -v shyaml >/dev/null 2>&1 || { echo >&2 "I require shyaml but it's not installed. Aborting."; exit 1; }
if [ -e "${CFN_DEPLOY_RULES}" ]; then
echo "*** CFN deployment rules found (${CFN_DEPLOY_RULES})"
else
echo "*** ERROR - CFN deployment rules not found (${CFN_DEPLOY_RULES})"
RETCODE=1
fi
if [ $RETCODE -eq 0 ]; then
cd "${BUILD_PATH}"
echo "*** Executing from $(pwd)"
read-0() {
while [ "$1" ]; do
IFS=$'\0' read -r -d '' "$1" || return 1
shift
done
}
echo "*** Retrieving values from $CFN_DEPLOY_RULES"
STACKNAME=$(cat $CFN_DEPLOY_RULES | shyaml get-value cloudformation.stack_name)
echo "Stackname set to $STACKNAME"
REGION=$(cat $CFN_DEPLOY_RULES | shyaml get-value cloudformation.region)
echo "Region set to $REGION"
CFN_TEMPLATE_NAME=$(cat $CFN_DEPLOY_RULES | shyaml get-value cloudformation.cfn_template)
echo "CFN Template name set to $CFN_TEMPLATE_NAME"
CFN_TEMPLATE_FILE=${BUILD_PATH}/CFN/$CFN_TEMPLATE_NAME
echo "Template path set to $CFN_TEMPLATE_FILE"
# In the next few sections, we try to find default values for things we need
# but may be omitted from the YAML file. If they're NOT omitted, then they
# will just be entered with the rest of the YAML parameters.
# We have some microservices that create ECS task defs only
# In these cases, there is no ELB to query or set a value on
if [ ! -z ${TASK_ONLY} ] && [ ${TASK_ONLY,,} == 'true' ]; then
echo "Task only service - not querying cluster for LB params"
else
CLUSTERPARAM=""
CLUSTERVAL=$(cat $CFN_DEPLOY_RULES | shyaml get-value cloudformation.parameters.CLUSTERNAME 2>/dev/null)
if [ $? -eq 1 ]; then
echo "Could not find ClusterName within parameters, constructing using ${CFN_DEPLOY_RULES}"
CLUSTERVAL=$(cat $CFN_DEPLOY_RULES | shyaml get-value cloudformation.clusterName)
echo "Cluster name set to ${CLUSTERVAL}"
CLUSTERPARAM="ParameterKey=CLUSTERNAME,ParameterValue=$CLUSTERVAL,UsePreviousValue=false"
fi
SUBNETSPARAM=""
$(cat $CFN_DEPLOY_RULES | shyaml get-value cloudformation.parameters.LOADBALANCERSUBNETS &>/dev/null)
if [ $? -eq 1 ]; then
echo "Could not find Subnets within parameters, constructing based on cluster ${CLUSTERVAL}"
RETVAL=$(aws cloudformation describe-stacks --stack-name ${CLUSTERVAL} --region $REGION --output text --query 'Stacks[0].Parameters[?ParameterKey==`Subnets`].ParameterValue')
if [ -z "$RETVAL" ]; then
echo "*** ERROR - Could not retrieve subnets from the cluster ${CLUSTERVAL}"
exit 1
fi
echo "Subnets set to ${RETVAL}"
SUBNETSPARAM="ParameterKey=LOADBALANCERSUBNETS,ParameterValue='${RETVAL}',UsePreviousValue=false"
fi
LBSECPARAM=""
$(cat $CFN_DEPLOY_RULES | shyaml get-value cloudformation.parameters.LOADBALANCERSECURITYGROUP &>/dev/null)
if [ $? -eq 1 ]; then
echo "Could not find Load Balancer Security Group within parameters, constructing based on cluster ${CLUSTERVAL}"
RETVAL=$(aws cloudformation describe-stack-resources --stack-name ${CLUSTERVAL} --region $REGION --output text --query 'StackResources[?LogicalResourceId==`LbSecurityGroup`].PhysicalResourceId')
if [ -z "$RETVAL" ]; then
echo "*** ERROR - Could not retrieve Load Balancer Security Group from the cluster ${CLUSTERVAL}"
exit 1
fi
echo "Security group set to ${RETVAL}"
LBSECPARAM="ParameterKey=LOADBALANCERSECURITYGROUP,ParameterValue=$RETVAL,UsePreviousValue=false"
fi
fi
IMAGEPARAM=""
if [ -n "$IMAGE_NAME" ]; then
echo "Docker image set to ${IMAGE_NAME}"
IMAGEPARAM="ParameterKey=IMAGENAME,ParameterValue=$IMAGE_NAME,UsePreviousValue=false"
fi
if [ -z "$STACKNAME" ] || [ -z "$REGION" ] || [ -z "$CFN_TEMPLATE_NAME" ]; then
echo "*** ERROR - Could not extract variables from $CFN_DEPLOY_RULES"
exit 1
fi
echo "*** Checking if a stack exists with name $STACKNAME"
STACKSTATUS=$(aws cloudformation describe-stacks --stack-name $STACKNAME --region $REGION --output text --query 'Stacks[0].StackStatus' 2> /dev/null)
STATUS=$?
COMMAND="create-stack"
if [ $STATUS -eq 0 ]; then
echo "*** Stack with name $STACKNAME does exist, updating instead of creating"
COMMAND="update-stack"
echo "*** Checking if update-stack can be performed on stack $STACKNAME ..."
echo $STACKSTATUS | grep -q "COMPLETE"
if [ $? -eq 1 ]; then
echo "*** ERROR ***"
echo "*** Stack is NOT in an updatable state"
echo "*** Current stack status is $STACKSTATUS"
exit 1
fi
echo $STACKSTATUS | grep -q "PROGRESS"
if [ $? -eq 0 ]; then
echo "*** ERROR ***"
echo "*** Stack is NOT in an updatable state"
echo "*** Current stack status is $STACKSTATUS"
exit 1
fi
fi
echo "*** Running $COMMAND on stack $STACKNAME"
if [ ! -z ${TASK_ONLY} ] && [ ${TASK_ONLY,,} == 'true' ]; then
STACKID=$(aws cloudformation $COMMAND --stack-name $STACKNAME --region $REGION --template-body file://${CFN_TEMPLATE_FILE} \
--parameters \
$(cat $CFN_DEPLOY_RULES | shyaml key-values-0 cloudformation.parameters |
while read-0 key value; do
echo -n "ParameterKey=$key,ParameterValue=$value,UsePreviousValue=false "
done) --output text 2>&1)
else
STACKID=$(aws cloudformation $COMMAND --stack-name $STACKNAME --region $REGION --template-body file://${CFN_TEMPLATE_FILE} \
--parameters ${CLUSTERPARAM} ${SUBNETSPARAM} ${LBSECPARAM} \
$(cat $CFN_DEPLOY_RULES | shyaml key-values-0 cloudformation.parameters |
while read-0 key value; do
echo -n "ParameterKey=$key,ParameterValue=$value,UsePreviousValue=false "
done) --output text 2>&1)
fi
echo $STACKID | grep -q "ValidationError"
STATUS=$?
if [ $STATUS -eq 0 ]; then
echo $STACKID | grep -q "No updates"
if [ $? -eq 1 ]; then
echo "*** ERROR - Command failed with code $STATUS"
exit 1
fi
echo "*** No updates performed, continuing ..."
fi
echo "*** Command completed with return code $STATUS"
echo "*** Waiting to make sure the command $COMMAND completed successfully"
NEXT_WAIT_TIME=0
MAX_WAIT_TIMES=10
SLEEP_SECONDS=60
echo "*** This may take up to $(( $MAX_WAIT_TIMES * $SLEEP_SECONDS )) seconds..."
while [ $NEXT_WAIT_TIME -le $MAX_WAIT_TIMES ]; do
STATUS=$(aws cloudformation describe-stacks --stack-name $STACKNAME --region $REGION --query 'Stacks[0].StackStatus')
echo $STATUS | grep "ROLLBACK"
if [ $? -eq 0 ]; then
RETCODE=1
echo "*** ERROR - $COMMAND failed"
echo "*** Waiting for 5 minutes to make sure stack rolled back successfuly..."
sleep 5m
STATUS=`aws cloudformation describe-stacks --stack-name $STACKNAME --region $REGION --query 'Stacks[0].StackStatus'`
echo $STATUS | grep "FAILED"
if [ $? -eq 0 ]; then
RETCODE=1
echo "*** CRITICAL ERROR - rollback has failed"
else
echo "*** Stack rolled back"
fi
echo "*** Stack operation failed. Printing events ..."
aws cloudformation describe-stack-events --stack-name $STACKID --region $REGION --output text --query 'StackEvents[].{Time:Timestamp,Resource:ResourceType,Status:ResourceStatus,Reason:ResourceStatusReason}'
if [ $COMMAND = "create-stack" ]; then
echo "*** Removing unstable stack ..."
aws cloudformation delete-stack --stack-name $STACKID --region $REGION
fi
exit 1
fi
echo $STATUS | grep "COMPLETE"
if [ $? -eq 0 ]; then
echo "*** Operation $COMMAND completed successfully"
break
else
echo "Current stack status: $STATUS"
fi
(( NEXT_WAIT_TIME++ )) && sleep $SLEEP_SECONDS
done
fi
if [ $NEXT_WAIT_TIME -ge $MAX_WAIT_TIMES ] && [ $STATUS != "COMPLETE" ]; then
# We have reached the max wait time and we are not yet complete
echo "*** Stack has not yet stabilized in $(( $MAX_WAIT_TIMES * $SLEEP_SECONDS )) seconds - check the cloudformation console or the ECS events tab for more detail"
RETCODE=1
fi
if [ ! -z "$SERVICE_ALARM_ENDPOINT" ] && [ $RETCODE -ne 1 ]; then
echo "************************************************************************"
echo "********************** Creating CloudWatch Alarms **********************"
echo "************************************************************************"
SERVICE_ALARM_TEMPLATE=${BUILD_PATH}/CFN/alarm-template.json
echo "Found CloudWatch Alarms template, creating stack ..."
CPULIMIT=$(cat $CFN_DEPLOY_RULES | shyaml get-value cloudformation.cpuLimit)
echo "CPU Threshold set to $CPULIMIT"
MEMORYLIMIT=$(cat $CFN_DEPLOY_RULES | shyaml get-value cloudformation.memoryLimit)
echo "Memory Threshold set to $MEMORYLIMIT"
CLUSTERNAME=$(cat $CFN_DEPLOY_RULES | shyaml get-value cloudformation.clusterName)
echo "Cluster name set to $CLUSTERNAME"
SERVICENAME=$(aws cloudformation describe-stacks --stack-name $STACKNAME --region $REGION --output text --query 'Stacks[0].Outputs[].OutputValue' | cut -f 2 -d "/")
echo "Extracted Service name: $SERVICENAME"
COMMAND="create-stack"
echo "*** Checking if the stack exists with name $STACKNAME-alarm"
STACKSTATUS=$(aws cloudformation describe-stacks --stack-name ${STACKNAME}-alarm --region $REGION --output text --query 'Stacks[0].StackStatus' 2> /dev/null)
STATUS=$?
COMMAND="create-stack"
if [ $STATUS -eq 0 ]; then
echo "*** Stack with name $STACKNAME does exist, updating instead of creating"
COMMAND="update-stack"
echo "*** Checking if update-stack can be performed on stack ${STACKNAME}-alarm ..."
echo $STACKSTATUS | grep -q "COMPLETE"
if [ $? -eq 1 ]; then
echo "*** ERROR ***"
echo "*** Stack is NOT in an updatable state"
echo "*** Current stack status is $STACKSTATUS"
exit 1
fi
echo $STACKSTATUS | grep -q "PROGRESS"
if [ $? -eq 0 ]; then
echo "*** ERROR ***"
echo "*** Stack is NOT in an updatable state"
echo "*** Current stack status is $STACKSTATUS"
exit 1
fi
fi
echo "*** Running $COMMAND on stack ${STACKNAME}-alarm"
STACKID=$(aws cloudformation $COMMAND --stack-name ${STACKNAME}-alarm --region ${REGION} --template-body file://${SERVICE_ALARM_TEMPLATE} \
--parameters "ParameterKey=ServiceName,ParameterValue=$SERVICENAME,UsePreviousValue=false" \
"ParameterKey=AlarmCpuUtilization,ParameterValue=$CPULIMIT,UsePreviousValue=false" \
"ParameterKey=AlarmMemoryUtilization,ParameterValue=$MEMORYLIMIT,UsePreviousValue=false" \
"ParameterKey=SNSSubscription,ParameterValue=$SERVICE_ALARM_ENDPOINT,UsePreviousValue=false" \
"ParameterKey=ECSClusterName,ParameterValue=${CLUSTERNAME},UsePreviousValue=false" \
"ParameterKey=StackName,ParameterValue=${STACKNAME},UsePreviousValue=false" --output text 2>&1)
echo $STACKID | grep -q "ValidationError"
STATUS=$?
if [ $STATUS -eq 0 ]; then
echo $STACKID | grep -q "No updates"
if [ $? -eq 1 ]; then
echo "*** ERROR - Command failed with code $STATUS"
exit 1
fi
echo "*** No updates performed, continuing ..."
fi
echo "*** Command completed with return code $STATUS"
echo "*** Waiting to make sure the command $COMMAND completed successfully"
NEXT_WAIT_TIME=0
MAX_WAIT_TIMES=10
SLEEP_SECONDS=30
echo "*** This may take up to $(( $MAX_WAIT_TIMES * $SLEEP_SECONDS )) seconds..."
while [ $NEXT_WAIT_TIME -le $MAX_WAIT_TIMES ]; do
STATUS=$(aws cloudformation describe-stacks --stack-name ${STACKNAME}-alarm --region $REGION --query 'Stacks[0].StackStatus')
echo $STATUS | grep "ROLLBACK"
if [ $? -eq 0 ]; then
RETCODE=1
echo "*** ERROR - $COMMAND failed"
echo "*** Waiting for 3 minutes to make sure stack rolled back successfuly..."
sleep 3m
STATUS=`aws cloudformation describe-stacks --stack-name ${STACKNAME}-alarm --region $REGION --query 'Stacks[0].StackStatus'`
echo $STATUS | grep "FAILED"
if [ $? -eq 0 ]; then
echo "*** CRITICAL ERROR - rollback has failed"
else
echo "*** Stack rolled back"
fi
echo "*** Stack operation failed. Printing events ..."
aws cloudformation describe-stack-events --stack-name ${STACKNAME}-alarm --region $REGION
if [ $COMMAND = "create-stack" ]; then
echo "*** Removing unstable stack ..."
aws cloudformation delete-stack --stack-name ${STACKNAME}-alarm --region $REGION
fi
exit 1
fi
echo $STATUS | grep "COMPLETE"
if [ $? -eq 0 ]; then
echo "*** Operation $COMMAND completed successfully"
break
else
echo "Current stack status: $STATUS"
fi
(( NEXT_WAIT_TIME++ )) && sleep $SLEEP_SECONDS
done
if [ $NEXT_WAIT_TIME -ge $MAX_WAIT_TIMES ] && [ $STATUS != "COMPLETE" ]; then
# We have reached the max wait time and we are not yet complete
echo "*** Alarm Stack has not yet stabilized in $(( $MAX_WAIT_TIMES * $SLEEP_SECONDS )) seconds - check the cloudformation console or the ECS events tab for more detail"
RETCODE=1
fi
fi
exit $RETCODE