@@ -7,54 +7,58 @@ set -o noglob
7
7
# Set Colors
8
8
#
9
9
10
- bold=$( tput bold)
11
- underline=$( tput sgr 0 1)
12
- reset=$( tput sgr0)
10
+ bold=" \e[1m"
11
+ dim=" \e[2m"
12
+ underline=" \e[4m"
13
+ blink=" \e[5m"
14
+ reset=" \e[0m"
15
+ red=" \e[31m"
16
+ green=" \e[32m"
17
+ blue=" \e[34m"
13
18
14
- red=$( tput setaf 1)
15
- green=$( tput setaf 2)
16
- white=$( tput setaf 7)
17
- tan=$( tput setaf 3)
18
- blue=$( tput setaf 4)
19
19
20
20
#
21
- # Headers and Logging
21
+ # Common Output Styles
22
22
#
23
23
24
- underline () { printf " ${underline}${bold} %s${reset} \n" " $@ "
24
+ h1 () {
25
+ printf " \n${bold}${underline} %s${reset} \n" " $( echo " $@ " | sed ' /./,$!d' ) "
25
26
}
26
- h1 () { printf " \n${underline}${bold}${blue} %s${reset} \n" " $@ "
27
+ h2 () {
28
+ printf " \n${bold} %s${reset} \n" " $( echo " $@ " | sed ' /./,$!d' ) "
27
29
}
28
- h2 () { printf " \n${bold} %s${reset} \n" " $@ "
30
+ info () {
31
+ printf " ${dim} ➜ %s${reset} \n" " $( echo " $@ " | sed ' /./,$!d' ) "
29
32
}
30
- debug () { printf " ${white} %s${reset} \n" " $@ "
33
+ success () {
34
+ printf " ${green} ✔ %s${reset} \n" " $( echo " $@ " | sed ' /./,$!d' ) "
31
35
}
32
- info () { printf " ${white} ➜ %s${reset} \n" " $@ "
36
+ error () {
37
+ printf " ${red}${bold} ✖ %s${reset} \n" " $( echo " $@ " | sed ' /./,$!d' ) "
33
38
}
34
- success () { printf " ${green} ✔ %s${reset} \n" " $@ "
39
+ warnError () {
40
+ printf " ${red} ✖ %s${reset} \n" " $( echo " $@ " | sed ' /./,$!d' ) "
35
41
}
36
- error () { printf " ${red} ✖ %s${reset} \n" " $@ "
42
+ warnNotice () {
43
+ printf " ${blue} ✖ %s${reset} \n" " $( echo " $@ " | sed ' /./,$!d' ) "
37
44
}
38
- warn () { printf " ${tan} ➜ %s${reset} \n" " $@ "
39
- }
40
- bold () { printf " ${bold} %s${reset} \n" " $@ "
41
- }
42
- note () { printf " \n${underline}${bold}${blue} Note:${reset} ${blue} %s${reset} \n" " $@ "
45
+ note () {
46
+ printf " \n${bold}${blue} Note:${reset} ${blue} %s${reset} \n" " $( echo " $@ " | sed ' /./,$!d' ) "
43
47
}
44
48
45
49
# Runs the specified command and logs it appropriately.
46
- # $1 = command
47
- # $2 = (optional) error message
48
- # $3 = (optional) success message
49
- # $4 = (optional) global variable to assign the output to
50
+ # $1 = command
51
+ # $2 = (optional) error message
52
+ # $3 = (optional) success message
53
+ # $4 = (optional) global variable to assign the output to
50
54
runCommand () {
51
55
command=" $1 "
52
56
info " $1 "
53
- local output=" $( eval $command 2>&1 ) "
57
+ output=" $( eval $command 2>&1 ) "
54
58
ret_code=$?
55
59
56
60
if [ $ret_code != 0 ]; then
57
- warn " $output "
61
+ warnError " $output "
58
62
if [ ! -z " $2 " ]; then
59
63
error " $2 "
60
64
fi
@@ -108,6 +112,7 @@ if [ -z "$AWS_CODE_DEPLOY_S3_BUCKET" ]; then
108
112
fi
109
113
110
114
115
+
111
116
# ----- Install AWS Cli -----
112
117
# see documentation http://docs.aws.amazon.com/cli/latest/userguide/installing.html
113
118
# ---------------------------
129
134
fi
130
135
131
136
137
+
132
138
# ----- Configure -----
133
139
# see documentation
134
140
# http://docs.aws.amazon.com/cli/latest/reference/configure/index.html
@@ -149,6 +155,7 @@ if [ -n "$AWS_CODE_DEPLOY_REGION" ]; then
149
155
fi
150
156
151
157
158
+
152
159
# ----- Application -----
153
160
# see documentation
154
161
# http://docs.aws.amazon.com/cli/latest/reference/deploy/get-application.html
@@ -160,14 +167,12 @@ APPLICATION_VERSION=${AWS_CODE_DEPLOY_APPLICATION_VERSION:-${GIT_COMMIT:0:7}}
160
167
161
168
# Check application exists
162
169
h1 " Step 3: Checking Application"
163
- h2 " Checking application \" $APPLICATION_NAME \" exists"
164
-
165
170
APPLICATION_EXISTS=" aws deploy get-application --application-name $APPLICATION_NAME "
166
171
info " $APPLICATION_EXISTS "
167
172
APPLICATION_EXISTS_OUTPUT=$( $APPLICATION_EXISTS 2>&1 )
168
173
169
174
if [ $? -ne 0 ]; then
170
- warn " $APPLICATION_EXISTS_OUTPUT "
175
+ warnNotice " $APPLICATION_EXISTS_OUTPUT "
171
176
h2 " Creating application \" $APPLICATION_NAME \" "
172
177
173
178
# Create application
179
184
fi
180
185
181
186
187
+
182
188
# ----- Deployment Config (optional) -----
183
189
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment-config.html
184
190
# ----------------------
@@ -187,14 +193,12 @@ MINIMUM_HEALTHY_HOSTS=${AWS_CODE_DEPLOY_MINIMUM_HEALTHY_HOSTS:-type=FLEET_PERCEN
187
193
188
194
# Check deployment config exists
189
195
h1 " Step 4: Checking Deployment Config"
190
- h2 " Checking deployment config \" $DEPLOYMENT_CONFIG_NAME \" exists"
191
-
192
196
DEPLOYMENT_CONFIG_EXISTS=" aws deploy get-deployment-config --deployment-config-name $DEPLOYMENT_CONFIG_NAME "
193
197
info " $DEPLOYMENT_CONFIG_EXISTS "
194
198
DEPLOYMENT_CONFIG_EXISTS_OUTPUT=$( $DEPLOYMENT_CONFIG_EXISTS 2>&1 )
195
199
196
200
if [ $? -ne 0 ]; then
197
- warn " $DEPLOYMENT_CONFIG_EXISTS_OUTPUT "
201
+ warnNotice " $DEPLOYMENT_CONFIG_EXISTS_OUTPUT "
198
202
h2 " Creating deployment config \" $DEPLOYMENT_CONFIG_NAME \" "
199
203
200
204
# Create application
206
210
fi
207
211
208
212
213
+
209
214
# ----- Deployment Group -----
210
215
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment-config.html
211
216
# ----------------------
@@ -217,14 +222,12 @@ SERVICE_ROLE_ARN="$AWS_CODE_DEPLOY_SERVICE_ROLE_ARN"
217
222
218
223
# Check deployment group exists
219
224
h1 " Step 5: Checking Deployment Group"
220
- h2 " Checking deployment group \" $DEPLOYMENT_GROUP \" exists for application \" $APPLICATION_NAME \" "
221
-
222
225
DEPLOYMENT_GROUP_EXISTS=" aws deploy get-deployment-group --application-name $APPLICATION_NAME --deployment-group-name $DEPLOYMENT_GROUP "
223
226
info " $DEPLOYMENT_GROUP_EXISTS "
224
227
DEPLOYMENT_GROUP_EXISTS_OUTPUT=$( $DEPLOYMENT_GROUP_EXISTS 2>&1 )
225
228
226
229
if [ $? -ne 0 ]; then
227
- warn " $DEPLOYMENT_GROUP_EXISTS_OUTPUT "
230
+ warnNotice " $DEPLOYMENT_GROUP_EXISTS_OUTPUT "
228
231
h2 " Creating deployment group \" $DEPLOYMENT_GROUP \" for application \" $APPLICATION_NAME \" "
229
232
230
233
# Create deployment group
248
251
fi
249
252
250
253
254
+
251
255
# ----- Compressing Source -----
252
256
APP_SOURCE=$( readlink -f " ${AWS_CODE_DEPLOY_APP_SOURCE:- .} " )
253
257
APP_LOCAL_FILE=" ${AWS_CODE_DEPLOY_S3_FILENAME% .* } .zip"
@@ -274,6 +278,7 @@ DEPLOYMENT_COMPRESS_FILESIZE=$(ls -lah "${APP_LOCAL_TEMP_FILE}" | awk '{ print $
274
278
success " Successfully compressed \" $APP_SOURCE \" ($DEPLOYMENT_COMPRESS_ORIG_DIR_SIZE ) into \" $APP_LOCAL_FILE \" ($DEPLOYMENT_COMPRESS_FILESIZE )"
275
279
276
280
281
+
277
282
# ----- Push Bundle to S3 -----
278
283
# see documentation http://docs.aws.amazon.com/cli/latest/reference/s3/cp.html
279
284
# ----------------------
@@ -298,6 +303,7 @@ runCommand "$S3_CP \"$APP_LOCAL_TEMP_FILE\" \"s3://$S3_FULL_BUCKET/$APP_LOCAL_FI
298
303
" Copying bundle \" $APP_LOCAL_FILE \" to S3 succeeded"
299
304
300
305
306
+
301
307
# ----- Limit Deploy Revisions per Bucket/Key -----
302
308
# see documentation http://docs.aws.amazon.com/cli/latest/reference/s3/cp.html
303
309
# ----------------------
344
350
fi
345
351
346
352
353
+
347
354
# ----- Register Revision -----
348
355
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/register-application-revision.html
349
356
# ----------------------
@@ -369,6 +376,7 @@ runCommand "$REGISTER_APP_CMD" \
369
376
" Registering revision succeeded"
370
377
371
378
379
+
372
380
# ----- Create Deployment -----
373
381
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment.html
374
382
# ----------------------
@@ -389,21 +397,17 @@ runCommand "$DEPLOYMENT_CMD" \
389
397
DEPLOYMENT_OUTPUT
390
398
391
399
DEPLOYMENT_ID=$( echo $DEPLOYMENT_OUTPUT | jsonValue ' deploymentId' | tr -d ' ' )
400
+ success " Successfully created deployment: \" $DEPLOYMENT_ID \" "
392
401
note " You can follow your deployment at: https://console.aws.amazon.com/codedeploy/home#/deployments/$DEPLOYMENT_ID "
393
402
394
403
404
+
395
405
# ----- Monitor Deployment -----
396
406
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment.html
397
407
# ----------------------
398
408
if [ " true" = " $DEPLOYMENT_OVERVIEW " ]; then
399
409
h1 " Deployment Overview"
400
410
401
- if ! typeExists " jshon" ; then
402
- h2 " Installing Monitoring Dependency: \" jshon\" "
403
- runCommand " sudo apt-get install -y jshon"
404
- success " Installing jshon (Version: ` jshon --version` ) succeeded"
405
- fi
406
-
407
411
DEPLOYMENT_GET=" aws deploy get-deployment --deployment-id \" $DEPLOYMENT_ID \" "
408
412
h2 " Monitoring deployment \" $DEPLOYMENT_ID \" for \" $APPLICATION_NAME \" on deployment group $DEPLOYMENT_GROUP ..."
409
413
info " $DEPLOYMENT_GET "
@@ -417,7 +421,7 @@ if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
417
421
error " Deployment of application \" $APPLICATION_NAME \" on deployment group \" $DEPLOYMENT_GROUP \" failed"
418
422
exit 1
419
423
fi
420
-
424
+
421
425
# Deployment Overview
422
426
IN_PROGRESS=$( echo " $DEPLOYMENT_GET_OUTPUT " | jsonValue " InProgress" | tr -d " \r\n " )
423
427
PENDING=$( echo " $DEPLOYMENT_GET_OUTPUT " | jsonValue " Pending" | tr -d " \r\n " )
@@ -434,78 +438,106 @@ if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
434
438
# Deployment Status
435
439
STATUS=$( echo " $DEPLOYMENT_GET_OUTPUT " | jsonValue " status" | tr -d " \r\n" | tr -d " " )
436
440
ERROR_MESSAGE=$( echo " $DEPLOYMENT_GET_OUTPUT " | jsonValue " message" )
437
-
438
- printf " \r${bold} Status${reset} | In Progress: $IN_PROGRESS | Pending: $PENDING | Skipped: $SKIPPED | Succeeded: $SUCCEEDED | Failed: $FAILED | "
441
+
442
+ printf " \r${bold}${blink} Status${reset} | In Progress: $IN_PROGRESS | Pending: $PENDING | Skipped: $SKIPPED | Succeeded: $SUCCEEDED | Failed: $FAILED | "
439
443
440
444
# Print Failed Details
441
445
if [ " $STATUS " == " Failed" ]; then
442
- printf " \n"
446
+ printf " \r ${bold} Status ${reset} | In Progress: $IN_PROGRESS | Pending: $PENDING | Skipped: $SKIPPED | Succeeded: $SUCCEEDED | Failed: $FAILED |\ n"
443
447
error " Deployment failed: $ERROR_MESSAGE "
444
448
445
- # Retrieve failed instances
449
+ # Retrieve failed instances. Use text output here to easier retrieve array. Output format:
450
+ # INSTANCESLIST i-1497a9e2
451
+ # INSTANCESLIST i-23a541eb
446
452
LIST_INSTANCES_OUTPUT=" "
447
453
h2 " Retrieving failed instance details ..."
448
- runCommand " aws deploy list-deployment-instances --deployment-id $DEPLOYMENT_ID --instance-status-filter Failed" \
454
+ runCommand " aws deploy list-deployment-instances --deployment-id $DEPLOYMENT_ID --instance-status-filter Failed --output text " \
449
455
" " \
450
456
" " \
451
457
LIST_INSTANCES_OUTPUT
452
-
453
- INSTANCE_IDS=($( echo $LIST_INSTANCES_OUTPUT | jshon -e instancesList | tr -d ' ",[] ' ) )
454
- success " Found ${# INSTANCE_IDS[@]} failed instance(s) [ ${INSTANCE_IDS[@]} ]"
455
-
458
+
459
+ INSTANCE_IDS=($( echo " $LIST_INSTANCES_OUTPUT " | sed -r ' s/INSTANCESLIST\s+//g' ) )
460
+ INSTANCE_IDS_JOINED=$( printf " , %s" " ${INSTANCE_IDS[@]} " )
461
+ success " Found ${# INSTANCE_IDS[@]} failed instance(s) [ ${INSTANCE_IDS_JOINED: 2} ]"
462
+
456
463
# Enumerate over each failed instance
457
464
for i in " ${! INSTANCE_IDS[@]} " ; do
458
- FAILED_INSTANCE_OUTPUT=$( aws deploy get-deployment-instance --deployment-id $DEPLOYMENT_ID --instance-id ${INSTANCE_IDS[$i]} )
459
-
460
- I_ID=$( echo $FAILED_INSTANCE_OUTPUT | jshon -e instanceSummary -e instanceId | tr -d ' "' | sed ' s/\\\//\//g' )
461
- I_STATUS=$( echo $FAILED_INSTANCE_OUTPUT | jshon -e instanceSummary -e status | tr -d ' "' )
462
- I_LAST_UPDATED=$( echo $FAILED_INSTANCE_OUTPUT | jshon -e instanceSummary -e lastUpdatedAt | tr -d ' "' )
463
- EVENT_NAMES=($( echo $FAILED_INSTANCE_OUTPUT | jshon -e instanceSummary -e lifecycleEvents -a -e lifecycleEventName | tr -d ' "' ) )
464
-
465
+ FAILED_INSTANCE_OUTPUT=$( aws deploy get-deployment-instance --deployment-id $DEPLOYMENT_ID --instance-id ${INSTANCE_IDS[$i]} --output text)
465
466
printf " \n${bold} Instance: ${INSTANCE_IDS[$i]}${reset} \n"
466
- printf " Instance ID: %s\n" " $I_ID "
467
- printf " Status: %s\n" " $I_STATUS "
468
- printf " Last Updated At: %s\n\n" " $( date -d @$I_LAST_UPDATED ) "
467
+
468
+ echo " $FAILED_INSTANCE_OUTPUT " | while read -r line; do
469
+
470
+ case " $( echo $line | awk ' { print $1; }' ) " in
469
471
470
- for i in " ${! EVENT_NAMES[@]} " ; do
471
- E_STATUS=$( echo $FAILED_INSTANCE_OUTPUT | jshon -e instanceSummary -e lifecycleEvents -e $i -e status | tr -d ' "' )
472
-
473
- case " $E_STATUS " in
474
- Failed)
475
- I_E_ERR_CODE=$( echo $FAILED_INSTANCE_OUTPUT | jshon -e instanceSummary -e lifecycleEvents -e $i -e diagnostics -e errorCode | tr -d ' "' )
476
- I_E_SCRIPT_NAME=$( echo $FAILED_INSTANCE_OUTPUT | jshon -e instanceSummary -e lifecycleEvents -e $i -e diagnostics -e scriptName | tr -d ' "' | sed ' s/\\\//\//g' )
477
- I_MESSAGE=$( echo $FAILED_INSTANCE_OUTPUT | jshon -e instanceSummary -e lifecycleEvents -e $i -e diagnostics -e message | tr -d ' "' | sed ' s/\\\//\//g' )
478
- I_E_LOG_TAIL=$( echo $FAILED_INSTANCE_OUTPUT | jshon -e instanceSummary -e lifecycleEvents -e $i -e diagnostics -e logTail | tr -d ' "' | sed ' s/\\\//\//g' )
479
-
480
- printf " ${bold}${red} %s${reset} \t%s\n" " [$E_STATUS ]" " ${EVENT_NAMES[$i]} "
481
- printf " ${red} %s:\t\t%s${reset} \n" " Error Code" " $I_E_ERR_CODE "
482
- printf " ${red} %s:\t%s${reset} \n" " Script Name" " $I_E_SCRIPT_NAME "
483
- printf " ${red} %s:\t\t%s${reset} \n" " Message" " $I_MESSAGE "
484
- printf " ${red} === Log Tail =================================\n%b\n" " $I_E_LOG_TAIL "
485
- printf " ${red} ==============================================${reset} \n"
472
+ INSTANCESUMMARY)
473
+
474
+ printf " Instance ID: %s\n" " $( echo $line | awk ' { print $3; }' ) "
475
+ printf " Status: %s\n" " $( echo $line | awk ' { print $5; }' ) "
476
+ printf " Last Updated At: %s\n\n" " $( date -d @$( echo $line | awk ' { print $4; }' ) ) "
486
477
;;
478
+
479
+ # The text version should have either 3 or 5 arguments
480
+ # LIFECYCLEEVENTS ValidateService Skipped
481
+ # LIFECYCLEEVENTS 1434231363.6 BeforeInstall 1434231363.49 Failed
482
+ # LIFECYCLEEVENTS 1434231361.79 DownloadBundle 1434231361.34 Succeeded
483
+ LIFECYCLEEVENTS)
484
+ # For now, lets just strip off start/stop times. Also convert tabs to spaces
485
+ lineModified=$( echo " $line " | sed -r ' s/[0-9]+\.[0-9]+//g' | sed ' s/\t/ /g' )
486
+
487
+ # Bugfix: Ubuntu 12.04 has some weird issues with spacing as seen on CircleCI. We fix this
488
+ # by just condensing down to single spaces and ensuring the proper separator.
489
+ IFS=$' '
490
+ ARGS=($( echo " $lineModified " | sed -r ' s/\s+/ /g' ) )
491
+
492
+ if [ ${# ARGS[@]} == 3 ]; then
493
+ case " ${ARGS[2]} " in
494
+ Succeeded)
495
+ printf " ${bold}${green} ✔ [%s]${reset} \t%s\n" " ${ARGS[2]} " " ${ARGS[1]} "
496
+ ;;
487
497
488
- Succeeded)
489
- printf " ${bold}${green} %s${reset} \t%s\n" " [$E_STATUS ]" " ${EVENT_NAMES[$i]} "
490
- ;;
498
+ Skipped)
499
+ printf " ${bold} [%s]${reset} \t%s\n" " ${ARGS[2]} " " ${ARGS[1]} "
500
+ ;;
501
+
502
+ Failed)
503
+ printf " ${bold}${red} ✖ [%s]${reset} \t%s\n" " ${ARGS[2]} " " ${ARGS[1]} "
504
+ ;;
505
+ esac
491
506
492
- Skipped)
493
- printf " ${bold} %s${reset} \t%s\n" " [$E_STATUS ]" " ${EVENT_NAMES[$i]} "
507
+ else
508
+ echo " [UNKNOWN] (${# ARGS[@]} ) $lineModified "
509
+ fi
510
+ ;;
511
+
512
+ DIAGNOSTICS)
513
+ # Skip diagnostics if we have "DIAGNOSTICS Success Succeeded"
514
+ if [ " $( echo $line | awk ' { print $2; }' ) " == " Success" ] && [ " $( echo $line | awk ' { print $3; }' ) " == " Succeeded" ]; then
515
+ continue
516
+ fi
517
+
518
+ # Attempt to get error message
519
+ ERROR_CODE=$( echo $line | sed -r ' s/DIAGNOSTICS//g' | sed -r ' s/LifecycleEvent\s-\s[a-zA-Z]+//g' )
520
+ printf " ${red} Error Code: %s${reset} \n" $ERROR_CODE
521
+ printf " ${red} Error Log:${reset} \n"
522
+ ;;
523
+
524
+ * )
525
+ printf " ${red}${line}${reset} \n"
526
+ ;;
494
527
495
528
esac
496
529
497
- done
498
- # ~ end: instance-events
499
-
500
- done
501
- # ~ end: instance
530
+ done # end: while
531
+
532
+ done # ~ end: instance
502
533
534
+ printf " \n\n"
503
535
exit 1
504
536
fi
505
537
506
538
# Deployment succeeded
507
539
if [ " $STATUS " == " Succeeded" ]; then
508
- printf " \n"
540
+ printf " \r ${bold} Status ${reset} | In Progress: $IN_PROGRESS | Pending: $PENDING | Skipped: $SKIPPED | Succeeded: $SUCCEEDED | Failed: $FAILED |\ n"
509
541
success " Deployment of application \" $APPLICATION_NAME \" on deployment group \" $DEPLOYMENT_GROUP \" succeeded"
510
542
break
511
543
fi
0 commit comments