Created
November 16, 2021 20:50
-
-
Save grevych/7f210910f6877ed1fe92768346cba6c9 to your computer and use it in GitHub Desktop.
Upload vcr cassettes to s3 based on hash strategy
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 | |
set -ex | |
summary () { | |
# MacOS - local testing | |
# echo "$(stat -f '%z' "$1") $(md5sum "$1")" | |
# Linux | |
echo "$(stat -c '%s' "$1") $(md5sum "$1")" | |
} | |
export -f summary | |
BIN_DIR=$(cd "$(dirname "$0")"; pwd) | |
BASE_DIR=$(dirname "${BIN_DIR}") | |
CASSETTES_DIR=$BASE_DIR/spec/cassettes/upload | |
BUCKET=cassettes | |
HASH=`find $CASSETTES_DIR -type f -exec bash -c 'summary "$0"' {} \; | LC_ALL=C sort | md5sum | cut -d " " -f1` | |
BUCKET_PATH="s3://$BUCKET" | |
BUCKET_HASH_PATH="$BUCKET_PATH/$HASH" | |
# ==================================== # | |
# VERIFY # | |
# ==================================== # | |
echo "Verifying cassettes hash" | |
current_version=`aws s3 ls $BUCKET_HASH_PATH || echo ""` | |
if [ -n "$current_version" ]; then | |
echo "Cassettes up to date" | |
exit 0 | |
fi | |
# ==================================== # | |
# UPLOAD # | |
# ==================================== # | |
echo "Uploading cassettes with hash $HASH" | |
# Get hashes for old versions | |
old_hashes=`aws s3 ls $BUCKET_PATH | sed '/PRE/d' | tr -s " " | cut -d " " -f4 | xargs` | |
# Upload cassettes for current version | |
aws s3 cp --recursive --include="*" "$CASSETTES_DIR" "$BUCKET_HASH_PATH/" | |
# Mark current version as uploaded | |
touch "$HASH" | |
aws s3 cp "$HASH" "$BUCKET_PATH" | |
# On successful upload, delete old versions | |
for old_hash in $old_hashes | |
do | |
aws s3 rm --recursive "$BUCKET_PATH/$old_hash/" | |
aws s3 rm "$BUCKET_PATH/$old_hash" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment