Skip to content

Commit e49d669

Browse files
committed
Fixing color issues. Updates for Ubuntu 12.04 and CircleCI
compatibility. Full instance reporting with color coded details.
1 parent 43400ce commit e49d669

File tree

1 file changed

+121
-89
lines changed

1 file changed

+121
-89
lines changed

aws-code-deploy.sh

Lines changed: 121 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,58 @@ set -o noglob
77
# Set Colors
88
#
99

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"
1318

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)
1919

2020
#
21-
# Headers and Logging
21+
# Common Output Styles
2222
#
2323

24-
underline() { printf "${underline}${bold}%s${reset}\n" "$@"
24+
h1() {
25+
printf "\n${bold}${underline}%s${reset}\n" "$(echo "$@" | sed '/./,$!d')"
2526
}
26-
h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@"
27+
h2() {
28+
printf "\n${bold}%s${reset}\n" "$(echo "$@" | sed '/./,$!d')"
2729
}
28-
h2() { printf "\n${bold}%s${reset}\n" "$@"
30+
info() {
31+
printf "${dim}➜ %s${reset}\n" "$(echo "$@" | sed '/./,$!d')"
2932
}
30-
debug() { printf "${white}%s${reset}\n" "$@"
33+
success() {
34+
printf "${green}✔ %s${reset}\n" "$(echo "$@" | sed '/./,$!d')"
3135
}
32-
info() { printf "${white}➜ %s${reset}\n" "$@"
36+
error() {
37+
printf "${red}${bold}✖ %s${reset}\n" "$(echo "$@" | sed '/./,$!d')"
3338
}
34-
success() { printf "${green}✔ %s${reset}\n" "$@"
39+
warnError() {
40+
printf "${red}✖ %s${reset}\n" "$(echo "$@" | sed '/./,$!d')"
3541
}
36-
error() { printf "${red}✖ %s${reset}\n" "$@"
42+
warnNotice() {
43+
printf "${blue}✖ %s${reset}\n" "$(echo "$@" | sed '/./,$!d')"
3744
}
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')"
4347
}
4448

4549
# 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
5054
runCommand() {
5155
command="$1"
5256
info "$1"
53-
local output="$(eval $command 2>&1)"
57+
output="$(eval $command 2>&1)"
5458
ret_code=$?
5559

5660
if [ $ret_code != 0 ]; then
57-
warn "$output"
61+
warnError "$output"
5862
if [ ! -z "$2" ]; then
5963
error "$2"
6064
fi
@@ -108,6 +112,7 @@ if [ -z "$AWS_CODE_DEPLOY_S3_BUCKET" ]; then
108112
fi
109113

110114

115+
111116
# ----- Install AWS Cli -----
112117
# see documentation http://docs.aws.amazon.com/cli/latest/userguide/installing.html
113118
# ---------------------------
@@ -129,6 +134,7 @@ else
129134
fi
130135

131136

137+
132138
# ----- Configure -----
133139
# see documentation
134140
# http://docs.aws.amazon.com/cli/latest/reference/configure/index.html
@@ -149,6 +155,7 @@ if [ -n "$AWS_CODE_DEPLOY_REGION" ]; then
149155
fi
150156

151157

158+
152159
# ----- Application -----
153160
# see documentation
154161
# 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}}
160167

161168
# Check application exists
162169
h1 "Step 3: Checking Application"
163-
h2 "Checking application \"$APPLICATION_NAME\" exists"
164-
165170
APPLICATION_EXISTS="aws deploy get-application --application-name $APPLICATION_NAME"
166171
info "$APPLICATION_EXISTS"
167172
APPLICATION_EXISTS_OUTPUT=$($APPLICATION_EXISTS 2>&1)
168173

169174
if [ $? -ne 0 ]; then
170-
warn "$APPLICATION_EXISTS_OUTPUT"
175+
warnNotice "$APPLICATION_EXISTS_OUTPUT"
171176
h2 "Creating application \"$APPLICATION_NAME\""
172177

173178
# Create application
@@ -179,6 +184,7 @@ else
179184
fi
180185

181186

187+
182188
# ----- Deployment Config (optional) -----
183189
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment-config.html
184190
# ----------------------
@@ -187,14 +193,12 @@ MINIMUM_HEALTHY_HOSTS=${AWS_CODE_DEPLOY_MINIMUM_HEALTHY_HOSTS:-type=FLEET_PERCEN
187193

188194
# Check deployment config exists
189195
h1 "Step 4: Checking Deployment Config"
190-
h2 "Checking deployment config \"$DEPLOYMENT_CONFIG_NAME\" exists"
191-
192196
DEPLOYMENT_CONFIG_EXISTS="aws deploy get-deployment-config --deployment-config-name $DEPLOYMENT_CONFIG_NAME"
193197
info "$DEPLOYMENT_CONFIG_EXISTS"
194198
DEPLOYMENT_CONFIG_EXISTS_OUTPUT=$($DEPLOYMENT_CONFIG_EXISTS 2>&1)
195199

196200
if [ $? -ne 0 ]; then
197-
warn "$DEPLOYMENT_CONFIG_EXISTS_OUTPUT"
201+
warnNotice "$DEPLOYMENT_CONFIG_EXISTS_OUTPUT"
198202
h2 "Creating deployment config \"$DEPLOYMENT_CONFIG_NAME\""
199203

200204
# Create application
@@ -206,6 +210,7 @@ else
206210
fi
207211

208212

213+
209214
# ----- Deployment Group -----
210215
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment-config.html
211216
# ----------------------
@@ -217,14 +222,12 @@ SERVICE_ROLE_ARN="$AWS_CODE_DEPLOY_SERVICE_ROLE_ARN"
217222

218223
# Check deployment group exists
219224
h1 "Step 5: Checking Deployment Group"
220-
h2 "Checking deployment group \"$DEPLOYMENT_GROUP\" exists for application \"$APPLICATION_NAME\""
221-
222225
DEPLOYMENT_GROUP_EXISTS="aws deploy get-deployment-group --application-name $APPLICATION_NAME --deployment-group-name $DEPLOYMENT_GROUP"
223226
info "$DEPLOYMENT_GROUP_EXISTS"
224227
DEPLOYMENT_GROUP_EXISTS_OUTPUT=$($DEPLOYMENT_GROUP_EXISTS 2>&1)
225228

226229
if [ $? -ne 0 ]; then
227-
warn "$DEPLOYMENT_GROUP_EXISTS_OUTPUT"
230+
warnNotice "$DEPLOYMENT_GROUP_EXISTS_OUTPUT"
228231
h2 "Creating deployment group \"$DEPLOYMENT_GROUP\" for application \"$APPLICATION_NAME\""
229232

230233
# Create deployment group
@@ -248,6 +251,7 @@ else
248251
fi
249252

250253

254+
251255
# ----- Compressing Source -----
252256
APP_SOURCE=$(readlink -f "${AWS_CODE_DEPLOY_APP_SOURCE:-.}")
253257
APP_LOCAL_FILE="${AWS_CODE_DEPLOY_S3_FILENAME%.*}.zip"
@@ -274,6 +278,7 @@ DEPLOYMENT_COMPRESS_FILESIZE=$(ls -lah "${APP_LOCAL_TEMP_FILE}" | awk '{ print $
274278
success "Successfully compressed \"$APP_SOURCE\" ($DEPLOYMENT_COMPRESS_ORIG_DIR_SIZE) into \"$APP_LOCAL_FILE\" ($DEPLOYMENT_COMPRESS_FILESIZE)"
275279

276280

281+
277282
# ----- Push Bundle to S3 -----
278283
# see documentation http://docs.aws.amazon.com/cli/latest/reference/s3/cp.html
279284
# ----------------------
@@ -298,6 +303,7 @@ runCommand "$S3_CP \"$APP_LOCAL_TEMP_FILE\" \"s3://$S3_FULL_BUCKET/$APP_LOCAL_FI
298303
"Copying bundle \"$APP_LOCAL_FILE\" to S3 succeeded"
299304

300305

306+
301307
# ----- Limit Deploy Revisions per Bucket/Key -----
302308
# see documentation http://docs.aws.amazon.com/cli/latest/reference/s3/cp.html
303309
# ----------------------
@@ -344,6 +350,7 @@ else
344350
fi
345351

346352

353+
347354
# ----- Register Revision -----
348355
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/register-application-revision.html
349356
# ----------------------
@@ -369,6 +376,7 @@ runCommand "$REGISTER_APP_CMD" \
369376
"Registering revision succeeded"
370377

371378

379+
372380
# ----- Create Deployment -----
373381
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment.html
374382
# ----------------------
@@ -389,21 +397,17 @@ runCommand "$DEPLOYMENT_CMD" \
389397
DEPLOYMENT_OUTPUT
390398

391399
DEPLOYMENT_ID=$(echo $DEPLOYMENT_OUTPUT | jsonValue 'deploymentId' | tr -d ' ')
400+
success "Successfully created deployment: \"$DEPLOYMENT_ID\""
392401
note "You can follow your deployment at: https://console.aws.amazon.com/codedeploy/home#/deployments/$DEPLOYMENT_ID"
393402

394403

404+
395405
# ----- Monitor Deployment -----
396406
# see documentation http://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment.html
397407
# ----------------------
398408
if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
399409
h1 "Deployment Overview"
400410

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-
407411
DEPLOYMENT_GET="aws deploy get-deployment --deployment-id \"$DEPLOYMENT_ID\""
408412
h2 "Monitoring deployment \"$DEPLOYMENT_ID\" for \"$APPLICATION_NAME\" on deployment group $DEPLOYMENT_GROUP ..."
409413
info "$DEPLOYMENT_GET"
@@ -417,7 +421,7 @@ if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
417421
error "Deployment of application \"$APPLICATION_NAME\" on deployment group \"$DEPLOYMENT_GROUP\" failed"
418422
exit 1
419423
fi
420-
424+
421425
# Deployment Overview
422426
IN_PROGRESS=$(echo "$DEPLOYMENT_GET_OUTPUT" | jsonValue "InProgress" | tr -d "\r\n ")
423427
PENDING=$(echo "$DEPLOYMENT_GET_OUTPUT" | jsonValue "Pending" | tr -d "\r\n ")
@@ -434,78 +438,106 @@ if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
434438
# Deployment Status
435439
STATUS=$(echo "$DEPLOYMENT_GET_OUTPUT" | jsonValue "status" | tr -d "\r\n" | tr -d " ")
436440
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 | "
439443

440444
# Print Failed Details
441445
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"
443447
error "Deployment failed: $ERROR_MESSAGE"
444448

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
446452
LIST_INSTANCES_OUTPUT=""
447453
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" \
449455
"" \
450456
"" \
451457
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+
456463
# Enumerate over each failed instance
457464
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)
465466
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
469471

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; }'))"
486477
;;
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+
;;
487497

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
491506

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+
;;
494527

495528
esac
496529

497-
done
498-
# ~ end: instance-events
499-
500-
done
501-
# ~ end: instance
530+
done # end: while
531+
532+
done # ~ end: instance
502533

534+
printf "\n\n"
503535
exit 1
504536
fi
505537

506538
# Deployment succeeded
507539
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"
509541
success "Deployment of application \"$APPLICATION_NAME\" on deployment group \"$DEPLOYMENT_GROUP\" succeeded"
510542
break
511543
fi

0 commit comments

Comments
 (0)