2
0

latest.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #! /bin/sh
  2. # vim:sw=4 ts=4 et:
  3. BUCKET=releases.wagtail.io
  4. REGION=eu-west-1
  5. CF_DISTRIBUTION=E283SZ5CB4MDM0
  6. # Find the location of the AWS CLI binary. MacPorts sometimes put it in a
  7. # weird place, so to be helpful we check those locations as well.
  8. if [ -z "${AWS_CLI}" ]; then
  9. for d in $(echo "${PATH}" | tr ':' ' ') \
  10. /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin \
  11. /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin \
  12. ; do
  13. if [ -x "${d}/aws" ]; then
  14. AWS_CLI="${d}/aws"
  15. break
  16. fi
  17. done
  18. if [ -z "${AWS_CLI}" ]; then
  19. printf >&2 -- '%s: cannot find AWS CLI binary "aws"\n' "$0"
  20. printf >&2 -- '%s: please install AWS from https://aws.amazon.com/documentation/cli/\n' "$0"
  21. exit 1
  22. fi
  23. fi
  24. # CloudFront support in the CLI is still in beta.
  25. $AWS_CLI configure set preview.cloudfront true
  26. _usage() {
  27. printf >&2 -- 'usage: %s get [output-filename]\n' "$0"
  28. printf >&2 -- ' %s put [input-filename]\n' "$0"
  29. printf >&2 -- ' %s <vi|edit>\n' "$0"
  30. }
  31. if [ "$#" -lt 1 ]; then
  32. _usage
  33. exit 1
  34. fi
  35. _get() {
  36. if ! $AWS_CLI s3 cp --region "${REGION}" "s3://${BUCKET}/latest.txt" "$1"; then
  37. printf >&2 -- "%s: failed to download latest.txt; see above messages\\n" "$0"
  38. exit 1
  39. fi
  40. }
  41. _put() {
  42. if ! $AWS_CLI s3 cp --acl public-read --region "${REGION}" "$1" "s3://${BUCKET}/latest.txt"; then
  43. printf >&2 -- "%s: failed to upload latest.txt; see above messages\\n" "$0"
  44. exit 1
  45. fi
  46. $AWS_CLI >/dev/null cloudfront create-invalidation \
  47. --distribution-id "$CF_DISTRIBUTION" \
  48. --invalidation-batch \
  49. '{
  50. "Paths": {
  51. "Items": [
  52. "/latest.txt"
  53. ],
  54. "Quantity": 1
  55. },
  56. "CallerReference": "latest.sh"
  57. }'
  58. }
  59. if [ "$1" = "get" ]; then
  60. if [ "$#" -lt 2 ]; then
  61. _usage
  62. exit 1
  63. fi
  64. shift
  65. if [ -e "$2" ]; then
  66. printf >&2 -- "%s: \"%s\": already exists, won't overwrite\\n" "$0"
  67. exit 1
  68. fi
  69. _get "$@"
  70. elif [ "$1" = "put" ]; then
  71. if [ "$#" -lt 2 ]; then
  72. _usage
  73. exit 1
  74. fi
  75. shift
  76. _put "$@"
  77. elif [ "$1" = "edit" -o "$1" = "vi" ]; then
  78. LTMP=$(mktemp "${TMPDIR:-/tmp}/latest.XXXXXX")
  79. if [ "$?" -ne 0 ]; then
  80. printf >&2 -- '%s: cannot create temporary file\n' "$0"
  81. exit 1
  82. fi
  83. trap 'rm -f "${LTMP}"' 0 TERM INT
  84. LTMP2=$(mktemp "${TMPDIR:-/tmp}/latest.XXXXXX")
  85. if [ "$?" -ne 0 ]; then
  86. printf >&2 -- '%s: cannot create temporary file\n' "$0"
  87. exit 1
  88. fi
  89. trap 'rm -f "${LTMP2}"' 0 TERM INT
  90. if ! _get "${LTMP}"; then
  91. exit 1
  92. fi
  93. cp "${LTMP}" "${LTMP2}"
  94. if [ ! -z "${VISUAL}" ]; then
  95. editor="${VISUAL}"
  96. elif [ ! -z "${EDITOR}" ]; then
  97. editor="${EDITOR}"
  98. else
  99. editor='vi'
  100. fi
  101. $editor "${LTMP}"
  102. if cmp "${LTMP}" "${LTMP2}" >/dev/null; then
  103. printf >&2 -- '%s: no changes; exiting\n' "$0"
  104. exit
  105. fi
  106. diff -u "${LTMP2}" "${LTMP}"
  107. _put "${LTMP}"
  108. fi