Skip to content

Commit d681a28

Browse files
committed
Bugfix to ensure that if no application source exists the exit code is
1. Removing trailing whitespace.
1 parent e3e4d64 commit d681a28

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

bin/aws-code-deploy.sh

+50-49
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ blue="\e[34m"
2525
h1() {
2626
printf "\n${bold}${underline}%s${reset}\n" "$(echo "$@" | sed '/./,$!d')"
2727
}
28-
h2() {
28+
h2() {
2929
printf "\n${bold}%s${reset}\n" "$(echo "$@" | sed '/./,$!d')"
3030
}
3131
info() {
@@ -92,7 +92,7 @@ installAwsCli() {
9292
runCommand "sudo apt-get install -y python-pip"
9393
success "Installing PIP (`pip --version`) succeeded"
9494
fi
95-
95+
9696
h2 "Installing AWS CLI"
9797
runCommand "sudo pip install awscli"
9898
}
@@ -104,13 +104,13 @@ vercomp() {
104104
fi
105105
local IFS=.
106106
local i ver1=($1) ver2=($2)
107-
107+
108108
# fill empty fields in ver1 with zeros
109109
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
110110
do
111111
ver1[i]=0
112112
done
113-
113+
114114
for ((i=0; i<${#ver1[@]}; i++))
115115
do
116116
if [[ -z ${ver2[i]} ]]
@@ -162,13 +162,13 @@ if ! typeExists "aws"; then
162162
else
163163
# aws-cli 1.9.8 is required for proper SSE syntax
164164
AWS_FULL_VER=$(aws --version 2>&1)
165-
AWS_VER=$(echo $AWS_FULL_VER | sed -e 's/aws-cli\///' | sed -e 's/ Python.*//')
165+
AWS_VER=$(echo $AWS_FULL_VER | sed -e 's/aws-cli\///' | sed -e 's/ Python.*//')
166166
vercomp $AWS_VER "1.9.8"
167167
if [[ $? == 2 ]]; then
168168
h2 "Installing updated AWS CLI version ($AWS_VER < 1.9.8)"
169169
installAwsCli
170-
fi
171-
170+
fi
171+
172172
success "Dependencies met $(aws --version 2>&1)"
173173
fi
174174

@@ -184,7 +184,7 @@ if [ -z "$AWS_CODE_DEPLOY_KEY" ]; then
184184
# Ensure an access key has already been set
185185
if [ $(aws configure list | grep access_key | wc -l) -lt 1 ]; then
186186
error "No AWS_CODE_DEPLOY_KEY specified and AWS cli is not configured with an access key via env, config, or shared credentials"
187-
exit 1
187+
exit 1
188188
fi
189189
success "AWS Access Key already configured."
190190
else
@@ -196,7 +196,7 @@ if [ -z "$AWS_CODE_DEPLOY_SECRET" ]; then
196196
# Ensure an access key secret has already been set
197197
if [ $(aws configure list | grep secret_key | wc -l) -lt 1 ]; then
198198
error "No AWS_CODE_DEPLOY_SECRET specified and AWS cli is not configured with an access secret via env, config, or shared credentials"
199-
exit 1
199+
exit 1
200200
fi
201201
success "AWS Secret Access Key already configured."
202202
else
@@ -208,7 +208,7 @@ if [ -z "$AWS_CODE_DEPLOY_REGION" ]; then
208208
# Ensure AWS region has already been set
209209
if [ $(aws configure list | grep region | wc -l) -lt 1 ]; then
210210
error "No AWS_CODE_DEPLOY_REGION specified and AWS cli is not configured with an existing default region via env, config, or shared credentials"
211-
exit 1
211+
exit 1
212212
fi
213213
success "AWS Region already configured."
214214
else
@@ -304,7 +304,7 @@ if [ $? -ne 0 ]; then
304304
if [ -n "$EC2_TAG_FILTERS" ]; then
305305
DEPLOYMENT_GROUP_CREATE="$DEPLOYMENT_GROUP_CREATE --ec2-tag-filters $EC2_TAG_FILTERS"
306306
fi
307-
307+
308308
runCommand "$DEPLOYMENT_GROUP_CREATE" \
309309
"Creating deployment group \"$DEPLOYMENT_GROUP\" for application \"$APPLICATION_NAME\" failed" \
310310
"Creating deployment group \"$DEPLOYMENT_GROUP\" for application \"$APPLICATION_NAME\" succeeded"
@@ -318,10 +318,11 @@ h1 "Step 6: Checking Application Source"
318318
APP_SOURCE=$(readlink -f "${AWS_CODE_DEPLOY_APP_SOURCE:-.}")
319319

320320
if [ ! -d "$APP_SOURCE" -a ! -e "$APP_SOURCE" ]; then
321-
error "The specified application source \"${APP_SOURCE}\" does not exist."
322-
exit
321+
# Note: Use original variable for output as the readlink can potentially evaluate to ""
322+
error "The specified application source \"${AWS_CODE_DEPLOY_APP_SOURCE}\" does not exist."
323+
exit 1
323324
fi
324-
if [ -d "$APP_SOURCE" ]; then
325+
if [ -d "$APP_SOURCE" ]; then
325326
if [ ! -e "$APP_SOURCE/appspec.yml" ]; then
326327
error "The specified source directory \"${APP_SOURCE}\" does not contain an \"appspec.yml\" in the application root."
327328
exit 1
@@ -334,17 +335,17 @@ if [ -d "$APP_SOURCE" ]; then
334335
DEPLOYMENT_COMPRESS_ORIG_DIR_SIZE=$(du -hs $APP_SOURCE | awk '{ print $1}')
335336
APP_LOCAL_FILE="${AWS_CODE_DEPLOY_S3_FILENAME%.*}.zip"
336337
APP_LOCAL_TEMP_FILE="/tmp/$APP_LOCAL_FILE"
337-
338+
338339
runCommand "cd \"$APP_SOURCE\" && zip -rq \"${APP_LOCAL_TEMP_FILE}\" ." \
339-
"Unable to compress \"$APP_SOURCE\""
340+
"Unable to compress \"$APP_SOURCE\""
340341
DEPLOYMENT_COMPRESS_FILESIZE=$(ls -lah "${APP_LOCAL_TEMP_FILE}" | awk '{ print $5}')
341342
BUNDLE_TYPE="zip"
342343
success "Successfully compressed \"$APP_SOURCE\" ($DEPLOYMENT_COMPRESS_ORIG_DIR_SIZE) into \"$APP_LOCAL_FILE\" ($DEPLOYMENT_COMPRESS_FILESIZE)"
343344
else
344345
APP_SOURCE_BASENAME=$(basename "$APP_SOURCE")
345346
APP_SOURCE_FILESIZE=$(ls -lah "${APP_SOURCE}" | awk '{ print $5}')
346347
EXTENSION="${APP_SOURCE#*.}"
347-
348+
348349
if [ $EXTENSION == "tar" ]; then
349350
BUNDLE_TYPE="tar"
350351
elif [ $EXTENSION == "tar.gz" ]; then
@@ -355,10 +356,10 @@ else
355356
error "Unsupported bundle type for application source file: ${APP_SOURCE_BASENAME} - Must be tar, zip or tgz"
356357
exit 1
357358
fi
358-
359+
359360
APP_LOCAL_FILE=$APP_SOURCE_BASENAME
360361
APP_LOCAL_TEMP_FILE=$APP_SOURCE
361-
362+
362363
success "Valid source file: $APP_SOURCE_BASENAME ($BUNDLE_TYPE) ($APP_SOURCE_FILESIZE)"
363364
fi
364365

@@ -403,24 +404,24 @@ else
403404
"Unable to list directory contents \"$S3_BUCKET/\"" \
404405
"" \
405406
S3_LS_OUTPUT
406-
407+
407408
# Sort the output by date first
408409
S3_LS_OUTPUT=$(echo "$S3_LS_OUTPUT" | sort)
409410

410411
# Filter out S3 prefixes (These do not count, especially useful in root bucket location)
411412
S3_FILES=()
412413
IFS=$'\n';
413-
for line in $S3_LS_OUTPUT; do
414+
for line in $S3_LS_OUTPUT; do
414415
if [[ ! $line =~ ^[[:space:]]+PRE[[:space:]].*$ ]]; then
415416
S3_FILES+=("$line")
416417
fi
417418
done
418-
419+
419420
S3_TOTAL_FILES=${#S3_FILES[@]}
420421
S3_NUMBER_FILES_TO_CLEAN=$(($S3_TOTAL_FILES-$S3_DEPLOY_LIMIT))
421422
if [ $S3_NUMBER_FILES_TO_CLEAN -gt 0 ]; then
422423
h2 "Removing oldest $S3_NUMBER_FILES_TO_CLEAN file(s) ..."
423-
for line in "${S3_FILES[@]}"; do
424+
for line in "${S3_FILES[@]}"; do
424425
if [ $S3_NUMBER_FILES_TO_CLEAN -le 0 ]; then
425426
success "Successfuly removed $(($S3_TOTAL_FILES-$S3_DEPLOY_LIMIT)) file(s)"
426427
break
@@ -490,12 +491,12 @@ note "You can follow your deployment at: https://console.aws.amazon.com/codedepl
490491
DEPLOYMENT_OVERVIEW=${AWS_CODE_DEPLOY_DEPLOYMENT_OVERVIEW:-true}
491492
if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
492493
h1 "Deployment Overview"
493-
494-
DEPLOYMENT_GET="aws deploy get-deployment --output json --deployment-id \"$DEPLOYMENT_ID\""
494+
495+
DEPLOYMENT_GET="aws deploy get-deployment --output json --deployment-id \"$DEPLOYMENT_ID\""
495496
h2 "Monitoring deployment \"$DEPLOYMENT_ID\" for \"$APPLICATION_NAME\" on deployment group $DEPLOYMENT_GROUP ..."
496497
info "$DEPLOYMENT_GET"
497498
printf "\n"
498-
499+
499500
while :
500501
do
501502
DEPLOYMENT_GET_OUTPUT="$(eval $DEPLOYMENT_GET 2>&1)"
@@ -504,7 +505,7 @@ if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
504505
error "Deployment of application \"$APPLICATION_NAME\" on deployment group \"$DEPLOYMENT_GROUP\" failed"
505506
exit 1
506507
fi
507-
508+
508509
# Deployment Overview
509510
IN_PROGRESS=$(echo "$DEPLOYMENT_GET_OUTPUT" | jsonValue "InProgress" | tr -d "\r\n ")
510511
PENDING=$(echo "$DEPLOYMENT_GET_OUTPUT" | jsonValue "Pending" | tr -d "\r\n ")
@@ -517,18 +518,18 @@ if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
517518
if [ "$SKIPPED" == "" ]; then SKIPPED="-"; fi
518519
if [ "$SUCCEEDED" == "" ]; then SUCCEEDED="-"; fi
519520
if [ "$FAILED" == "" ]; then FAILED="-"; fi
520-
521+
521522
# Deployment Status
522523
STATUS=$(echo "$DEPLOYMENT_GET_OUTPUT" | jsonValue "status" | tr -d "\r\n" | tr -d " ")
523524
ERROR_MESSAGE=$(echo "$DEPLOYMENT_GET_OUTPUT" | jsonValue "message")
524-
525+
525526
printf "\r${bold}${blink}Status${reset} | In Progress: $IN_PROGRESS | Pending: $PENDING | Skipped: $SKIPPED | Succeeded: $SUCCEEDED | Failed: $FAILED | "
526527

527528
# Print Failed Details
528529
if [ "$STATUS" == "Failed" ]; then
529530
printf "\r${bold}Status${reset} | In Progress: $IN_PROGRESS | Pending: $PENDING | Skipped: $SKIPPED | Succeeded: $SUCCEEDED | Failed: $FAILED |\n"
530531
error "Deployment failed: $ERROR_MESSAGE"
531-
532+
532533
# Retrieve failed instances. Use text output here to easier retrieve array. Output format:
533534
# INSTANCESLIST i-1497a9e2
534535
# INSTANCESLIST i-23a541eb
@@ -538,22 +539,22 @@ if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
538539
"" \
539540
"" \
540541
LIST_INSTANCES_OUTPUT
541-
542+
542543
INSTANCE_IDS=($(echo "$LIST_INSTANCES_OUTPUT" | sed -r 's/INSTANCESLIST\s+//g'))
543544
INSTANCE_IDS_JOINED=$(printf ", %s" "${INSTANCE_IDS[@]}")
544545
success "Found ${#INSTANCE_IDS[@]} failed instance(s) [ ${INSTANCE_IDS_JOINED:2} ]"
545-
546+
546547
# Enumerate over each failed instance
547548
for i in "${!INSTANCE_IDS[@]}"; do
548549
FAILED_INSTANCE_OUTPUT=$(aws deploy get-deployment-instance --deployment-id $DEPLOYMENT_ID --instance-id ${INSTANCE_IDS[$i]} --output text)
549550
printf "\n${bold}Instance: ${INSTANCE_IDS[$i]}${reset}\n"
550-
551+
551552
echo "$FAILED_INSTANCE_OUTPUT" | while read -r line; do
552-
553+
553554
case "$(echo $line | awk '{ print $1; }')" in
554-
555+
555556
INSTANCESUMMARY)
556-
557+
557558
printf " Instance ID: %s\n" "$(echo $line | awk '{ print $3; }')"
558559
printf " Status: %s\n" "$(echo $line | awk '{ print $5; }')"
559560
printf "Last Updated At: %s\n\n" "$(date -d @$(echo $line | awk '{ print $4; }'))"
@@ -566,56 +567,56 @@ if [ "true" = "$DEPLOYMENT_OVERVIEW" ]; then
566567
LIFECYCLEEVENTS)
567568
# For now, lets just strip off start/stop times. Also convert tabs to spaces
568569
lineModified=$(echo "$line" | sed -r 's/[0-9]+\.[0-9]+//g' | sed 's/\t/ /g')
569-
570+
570571
# Bugfix: Ubuntu 12.04 has some weird issues with spacing as seen on CircleCI. We fix this
571572
# by just condensing down to single spaces and ensuring the proper separator.
572573
IFS=$' '
573574
ARGS=($(echo "$lineModified" | sed -r 's/\s+/ /g'))
574-
575+
575576
if [ ${#ARGS[@]} == 3 ]; then
576577
case "${ARGS[2]}" in
577578
Succeeded)
578579
printf "${bold}${green}✔ [%s]${reset}\t%s\n" "${ARGS[2]}" "${ARGS[1]}"
579580
;;
580-
581+
581582
Skipped)
582583
printf "${bold} [%s]${reset}\t%s\n" "${ARGS[2]}" "${ARGS[1]}"
583584
;;
584-
585+
585586
Failed)
586587
printf "${bold}${red}✖ [%s]${reset}\t%s\n" "${ARGS[2]}" "${ARGS[1]}"
587588
;;
588589
esac
589-
590+
590591
else
591592
echo "[UNKNOWN] (${#ARGS[@]}) $lineModified"
592593
fi
593594
;;
594-
595+
595596
DIAGNOSTICS)
596597
# Skip diagnostics if we have "DIAGNOSTICS Success Succeeded"
597598
if [ "$(echo $line | awk '{ print $2; }')" == "Success" ] && [ "$(echo $line | awk '{ print $3; }')" == "Succeeded" ]; then
598599
continue
599600
fi
600-
601+
601602
# Just pipe off the DIAGNOSTICS
602603
printf "${red}%s${reset}\n" "$(echo $line | sed -r 's/^DIAGNOSTICS\s*//g')"
603-
;;
604-
604+
;;
605+
605606
*)
606607
printf "${red}${line}${reset}\n"
607608
;;
608-
609+
609610
esac
610611

611612
done # end: while
612-
613+
613614
done # ~ end: instance
614-
615+
615616
printf "\n\n"
616617
exit 1
617618
fi
618-
619+
619620
# Deployment succeeded
620621
if [ "$STATUS" == "Succeeded" ]; then
621622
printf "\r${bold}Status${reset} | In Progress: $IN_PROGRESS | Pending: $PENDING | Skipped: $SKIPPED | Succeeded: $SUCCEEDED | Failed: $FAILED |\n"

0 commit comments

Comments
 (0)