Last active
August 29, 2015 13:56
-
-
Save stefanoverna/9179228 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
# Usage: cached-bundle install --deployment | |
# | |
# After running `bundle`, caches the `vendor/bundle` directory to S3. | |
# On the next run, restores the cached directory before running `bundle`. | |
# When `Gemfile.lock` changes, the cache gets rebuilt. | |
# | |
# Requirements: | |
# - Gemfile.lock | |
# - REPO_SLUG | |
# - .ruby-version | |
# - S3_BUILD_CACHE_BUCKET | |
# - bundle | |
# - curl | |
# | |
# Author: Mislav Marohnić | |
set -e | |
compute_md5() { | |
local output="$(openssl md5)" | |
echo "${output##* }" | |
} | |
download() { | |
curl --tcp-nodelay -qsfL "$1" -o "$2" | |
} | |
script_dir=$(dirname "${BASH_SOURCE[0]}") | |
bundle_path="vendor/bundle" | |
cache_buster="${BUNDLE_GEMFILE:-Gemfile}.lock" | |
ruby_version=$(cat .ruby-version) | |
if [ ! -f $cache_buster ]; then | |
cache_buster="$GEMSPEC" | |
fi | |
cache_busting_hash="$(compute_md5 <"$cache_buster")" | |
cache_name="bundler-${ruby_version}-${cache_busting_hash}.tgz" | |
fetch_url="http://${S3_BUILD_CACHE_BUCKET}.s3.amazonaws.com/${REPO_SLUG}/${cache_name}" | |
if download "$fetch_url" "$cache_name"; then | |
echo "Reusing cached bundle ${cache_name}" | |
tar xzf "$cache_name" | |
fi | |
echo "Running 'bundle $@'..." | |
bundle "$@" | |
if [ ! -f "$cache_name" ]; then | |
if [ -z "$S3_SECRET_ACCESS_KEY" ] || [ -z "$S3_ACCESS_KEY_ID" ] | |
then | |
echo "Enviroment variables not set. Exiting..." | |
exit 0 | |
fi | |
echo "Caching \`${bundle_path}' to S3" | |
tar czf "$cache_name" "$bundle_path" | |
curl -s 'https://gist.githubusercontent.com/stefanoverna/9179228/raw/s3-put' | bash /dev/stdin "$cache_name" "${S3_BUILD_CACHE_BUCKET}:${REPO_SLUG}/${cache_name}" | |
fi | |
# vim: filetype=sh |
This file contains 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
#!/usr/bin/env bash | |
# Usage: s3-put <FILE> <S3_BUCKET>[:<PATH>] [<CONTENT_TYPE>] | |
# | |
# Uploads a file to the Amazon S3 service. | |
# Outputs the URL for the newly uploaded file. | |
# | |
# Requirements: | |
# - S3_ACCESS_KEY_ID | |
# - S3_SECRET_ACCESS_KEY | |
# - openssl | |
# - curl | |
# | |
# Author: Mislav Marohnić | |
set -e | |
authorization() { | |
local signature="$(string_to_sign | hmac_sha1 | base64)" | |
echo "AWS ${S3_ACCESS_KEY_ID?}:${signature}" | |
} | |
hmac_sha1() { | |
openssl dgst -binary -sha1 -hmac "${S3_SECRET_ACCESS_KEY?}" | |
} | |
base64() { | |
openssl enc -base64 | |
} | |
bin_md5() { | |
openssl dgst -binary -md5 | |
} | |
string_to_sign() { | |
echo "$http_method" | |
echo "$content_md5" | |
echo "$content_type" | |
echo "$date" | |
echo "x-amz-acl:$acl" | |
printf "/$bucket/$remote_path" | |
} | |
date_string() { | |
LC_TIME=C date "+%a, %d %h %Y %T %z" | |
} | |
file="$1" | |
bucket="${2%%:*}" | |
remote_path="${2#*:}" | |
content_type="$3" | |
if [ -z "$remote_path" ] || [ "$remote_path" = "$bucket" ]; then | |
remote_path="${file##*/}" | |
fi | |
http_method=PUT | |
acl="public-read" | |
content_md5="$(bin_md5 < "$file" | base64)" | |
date="$(date_string)" | |
url="https://$bucket.s3.amazonaws.com/$remote_path" | |
curl -qsSf -T "$file" \ | |
-H "Authorization: $(authorization)" \ | |
-H "x-amz-acl: $acl" \ | |
-H "Date: $date" \ | |
-H "Content-MD5: $content_md5" \ | |
-H "Content-Type: $content_type" \ | |
"$url" | |
echo "$url" | |
# vim: filetype=sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment