diff --git a/scripts/deploy-s3-cf.sh b/scripts/deploy-s3-cf.sh index 6d99771..b51b1a1 100755 --- a/scripts/deploy-s3-cf.sh +++ b/scripts/deploy-s3-cf.sh @@ -1,29 +1,193 @@ #!/bin/sh +help() { + echo "Usage: $0 [-h] [-v] [-c] -s -u -i " + echo + echo "Options:" + echo " -s SOURCE_DIR" + echo " Local source directory" + echo " REQUIRED (Can be also provided via environment variable)" + echo " -u AWS_S3_BUCKET_URI" + echo " AWS S3 bucket URI in the following format: 's3:///[]'" + echo " REQUIRED (Can be also provided via environment variable or" + echo " constructed from optional variables - see below)" + echo " -i AWS_CF_DISTRIBUTION_ID" + echo " AWS CloudFront Distribution ID" + echo " REQUIRED (Can be also provided via environment variable)" + echo " -c Enable color output. Defaults to autodetection from TERM." + echo " -v Enable verbose output (print API commands being executed)." + echo " -h Display this help message" + echo + echo "Other environment variables:" + echo " AWS_S3_BUCKET Can be used to construct the S3 bucket URI." + echo " If used, AWS_S3_BUCKET_URI doesn't have to be set." + echo " AWS_S3_PATH Can be used to further specify bucket path in the S3 URI." + echo " Has to start with '/'. Works only when AWS_S3_BUCKET is set." + echo " Defaults to '/'." + echo " COLORIZE Set to 1 to enable color output (or use -c). Set to 0 to disable." + echo " By default it's autodetected from TERM." + echo " VERBOSE Set to 1 to enable verbose output (or use -v). Defaults to 0." +} + +# Format functions +log() { + if [ $# -eq 2 ]; then + _fmt="\033[32;3m--> \033[2m\033[32;2m%-28s\033[0m \033[34;5m%s\033[0m\n" + test "${COLORIZE}" -eq 0 && _fmt="--> %-28s %s\n" + printf -- "${_fmt}" "${1}:" "$2" >&2 + else + _fmt="\033[31;3m%s\033[0m\n" + test "${COLORIZE}" -eq 0 && _fmt="%s\n" + printf -- "${_fmt}" "${@}" >&2 + fi +} + +banner() { + _fmt="\033[32;1m>>> \033[3m%s\033[0m\n" + test "${COLORIZE}" -eq 0 && _fmt=">>> %s\n" + printf -- "${_fmt}" "${@}" >&2 +} + +fail() { + _fmt="\033[31;1m==> \033[3m%s\033[0m\n" + test "${COLORIZE}" -eq 0 && _fmt="==> %s\n" + printf -- "${_fmt}" "${@}" >&2 + return 1 +} + +# S3: Deploy +# ========== +# Wrapper to recursively copy the source directory to +# the S3 bucket URI +# +# Requires: +# - source directory +# (-s / $SOURCE_DIR) +# - AWS S3 Bucket URI +# (-u / $AWS_S3_BUCKET_URI) +# +s3_deploy() { + src_dir="${1}" + s3_uri="${2}" + s3_bucket=$(echo "${s3_uri}" | awk -F/ '{print $3}') + s3_dir=/$(echo "${s3_uri}" \ + | awk -F/ 'BEGIN { OFS="/"; ORS=""; } { for (i=4; i<=NF; i++) printf "%s%s", $i, (i/[]'" + false fi -aws s3 cp --recursive "${SOURCE_DIR}" "s3://${AWS_S3_BUCKET}/" -AWS_CF_INVALIDATION_ID="$(aws cloudfront create-invalidation --distribution-id ${AWS_CF_DISTRIBUTION_ID} --paths '/*' | jq -r .Invalidation.Id)" -echo "CloudFront Invalidation ID - ${AWS_CF_INVALIDATION_ID}" -aws cloudfront wait invalidation-completed --distribution-id ${AWS_CF_DISTRIBUTION_ID} --id ${AWS_CF_INVALIDATION_ID} +############ +### Main ### +############ + +banner "Deploy to S3 (running)" +s3_deploy "${SOURCE_DIR}" "${AWS_S3_BUCKET_URI}" \ + || fail "Deploy to S3 (failed)" +banner "Deploy to S3 (finished)" + +banner "Invalidate in CloudFront (running)" +AWS_CF_INVALIDATION_ID=$(cf_invalidate "${AWS_CF_DISTRIBUTION_ID}" \ + || fail "Invalidate in CloudFront (failed on request)") +cf_wait "${AWS_CF_DISTRIBUTION_ID}" "${AWS_CF_INVALIDATION_ID}" \ + || fail "Invalidate in CloudFront (failed on wait)" +banner "Invalidate in CloudFront (finished)"