Created
August 4, 2017 22:17
-
-
Save wbyoung/2acfc7ad48406d5cc0e402d62b1bd224 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
JQ="jq --raw-output --exit-status" | |
# @env region | |
# @env task_family | |
# @returns via stderr | |
locate-task() { | |
local task_definition_arn=$( | |
aws ecs list-task-definitions \ | |
--output json \ | |
--region "$region" \ | |
--family-prefix "$task_family" | | |
$JQ '.taskDefinitionArns[0]' | |
) | |
echo "Definition: ${task_definition_arn##*/}" | |
echo "$task_definition_arn" >&2 | |
} | |
# @env region | |
# @env task_definition_arn | |
# @env overrides | |
# @env commit_sha | |
# @returns via stderr | |
run-task() { | |
local task_arn=$( | |
aws ecs run-task \ | |
--output json \ | |
--region "$region" \ | |
--cluster default \ | |
--task-definition "$task_definition_arn" \ | |
--overrides "$overrides" \ | |
--started-by "deploy-${commit_sha:0:9}" | | |
$JQ '.tasks[] | .taskArn' | |
) | |
echo "Started: ${task_arn##*/}" | |
echo "$task_arn" >&2 | |
} | |
# @env region | |
# @env task_arn | |
wait-for-task() { | |
for attempt in {1..30}; do | |
if running=$( | |
aws ecs describe-tasks \ | |
--output json \ | |
--region "$region" \ | |
--cluster default \ | |
--tasks "$task_arn" | | |
$JQ ".tasks[] | select(.desiredStatus != \"STOPPED\") | .taskArn" | |
); then | |
echo "Waiting for task to finish:" | |
echo "$running" | |
sleep 5 | |
else | |
return 0 | |
fi | |
done | |
} | |
# @env region | |
# @env task_id | |
# @env log_group_name | |
# @env log_stream_name_template | |
print-task-logs() { | |
local log_stream_name=$( | |
echo "$log_stream_name_template" | | |
sed \ | |
-e "s/\${TASK_ID}/$task_id/g" | |
) | |
echo "Output:" | |
aws logs get-log-events \ | |
--output json \ | |
--region "$region" \ | |
--start-from-head \ | |
--log-group-name "$log_group_name" \ | |
--log-stream-name "$log_stream_name" | | |
$JQ ".events[] | .message" | |
} | |
usage() { | |
if [ $# -gt 1 ]; then | |
echo $@ | |
echo | |
fi | |
echo 'aws-run' | |
echo ' --account-id <value>' | |
echo ' --task-family <value>' | |
echo ' --region <value>' | |
echo ' --overrides <value>' | |
echo ' --log-group-name <value>' | |
echo ' --log-stream-name-template <value>' | |
echo | |
echo 'OPTIONS' | |
echo | |
echo ' --account-id (string)' | |
echo ' The AWS account ID' | |
echo | |
echo ' --task-family (string)' | |
echo ' The task family under which to register' | |
echo | |
echo ' --region (string)' | |
echo ' The region' | |
echo | |
echo ' --overrides (string)' | |
echo ' The task overrides in JSON format' | |
echo | |
echo ' --log-group-name (string)' | |
echo ' The log group name' | |
echo | |
echo ' --log-stream-name-template (string) the log stream name template' | |
echo ' The template may include the following placeholders in the style ${VAR}:' | |
echo | |
echo ' TASK_ID the task id' | |
} | |
aws-run() { | |
while [ $# -ne 0 ]; do | |
case "$1" in | |
--account-id) | |
shift; local account_id="$1" | |
shift | |
;; | |
--task-family) | |
shift; local task_family="$1" | |
shift | |
;; | |
--region) | |
shift; local region="$1" | |
shift | |
;; | |
--overrides) | |
shift; local overrides="$1" | |
shift | |
;; | |
--log-group-name) | |
shift; local log_group_name="$1" | |
shift | |
;; | |
--log-stream-name-template) | |
shift; local log_stream_name_template="$1" | |
shift | |
;; | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
*) | |
usage unknown option "$1" | |
exit 1 | |
;; | |
esac | |
done | |
[ -n "$account_id" ] || { usage missing --account-id; exit 1; } | |
[ -n "$task_family" ] || { usage missing --task-family; exit 1; } | |
[ -n "$region" ] || { usage missing --region; exit 1; } | |
[ -n "$overrides" ] || { usage missing --overrides; exit 1; } | |
[ -n "$log_group_name" ] || { usage missing --log-group-name; exit 1; } | |
[ -n "$log_stream_name_template" ] || { | |
usage missing --log-stream-name-template; exit 1; | |
} | |
local commit_sha=$(git rev-parse HEAD) | |
local task_definition_arn=$(locate-task 3>&2 2>&1 1>&3) | |
local task_arn=$(run-task 3>&2 2>&1 1>&3) | |
local task_id="${task_arn##*/}" | |
wait-for-task | |
print-task-logs | |
} | |
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | |
aws-run "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: