Skip to content

Commit

Permalink
Merge pull request #14 from meirshal/exit-with-1-on-error
Browse files Browse the repository at this point in the history
Exit with code 1 when command returned error or failed to execute
  • Loading branch information
dnorth98 authored May 4, 2021
2 parents 33a693c + 3d9e68e commit f676f54
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions aws-connect
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ while getopts "a:d:c:f:g:w:n:t:r:p:o:x:shvl" OPTION; do
;;
g)
github_token_location=$OPTARG
;;
;;
c)
cloudwatch_group=$OPTARG
;;
Expand Down Expand Up @@ -281,7 +281,7 @@ elif [ "${action}" == "document" ]; then
cloudwatch_group="aws-connect"
fi

echo "Running ${document_name} on instance ${instance_id}"
echo "Running ${document_name} on instance ${instance_id}"

# create the send-command as a string so that parameters can be appended on if needed
aws_command="aws ssm send-command \
Expand All @@ -305,11 +305,11 @@ elif [ "${action}" == "document" ]; then

if [ "$?" != "0" ]; then
echo "Unable to execute command!"
exit 0
exit 1
fi

echo "Command ID: $sh_command_id"

# get the status of the command
status=$(aws ssm list-command-invocations \
--command-id "${sh_command_id}" \
Expand All @@ -336,21 +336,13 @@ elif [ "${action}" == "document" ]; then

echo "Command still running..."
sleep 5

status=$(aws ssm list-command-invocations \
--command-id "${sh_command_id}" \
--details \
--output text \
--no-paginate \
--query "CommandInvocations[0].Status")

# check if the command has failed
for (( i = 0; i < "${#ssm_failed_statuses[@]}"; i++ )); do
if [ "${ssm_failed_statuses[$i]}" == "${status}" ]; then
echo "Failure Status: ${ssm_failed_statuses[$i]}!"
endLoop="true"
fi
done
--query "CommandInvocations[0].Status")

# check if the command has succeeded. If so then get the command output
if [ "Success" == "${status}" ]; then
Expand All @@ -370,6 +362,15 @@ elif [ "${action}" == "document" ]; then
endLoop="true"
fi

# check if the command has failed
for (( i = 0; i < "${#ssm_failed_statuses[@]}"; i++ )); do
if [ "${ssm_failed_statuses[$i]}" == "${status}" ]; then
echo "Failure Status: ${ssm_failed_statuses[$i]}!"
endLoop="true"
exit 1
fi
done

done

else
Expand Down

0 comments on commit f676f54

Please sign in to comment.