Skip to content

Instantly share code, notes, and snippets.

@mgreau
Last active September 28, 2017 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgreau/6e4cb2cf56865d57c39d5ae0cd04b4cb to your computer and use it in GitHub Desktop.
Save mgreau/6e4cb2cf56865d57c39d5ae0cd04b4cb to your computer and use it in GitHub Desktop.
./generate-sha1.sh BUCKET_NAME RELEASE_VERSION
#!/bin/bash -e
S3_BUCKET=$1
RELEASE_VERSION=$2
readonly S3_BUCKET_FOLDER='downloads'
sync_get_release_files_from_s3(){
# sync locally only ${RELEASE_VERSION} files from downloads
echo "****"
echo "Get files from S3 bucket $S3_BUCKET_PATH..."
echo "****"
aws s3 sync s3://${S3_BUCKET}/${S3_BUCKET_FOLDER}/ ./downloads --exclude "*" --include "*${RELEASE_VERSION}*"
}
sync_push_files_to_s3(){
# push sha1 files to S3
echo "****"
echo "Push .sha1 files..."
echo "****"
aws s3 sync --dryrun ./downloads s3://${S3_BUCKET}/${S3_BUCKET_FOLDER}/ --exclude "*" --include "*${RELEASE_VERSION}*.sha1"
}
docker_s3_access(){
echo " (command $1)"
docker run --rm --user 1000:1000 -v $(pwd):/srv:rw --workdir /srv \
-e AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
-e AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
-e AWS_DEFAULT_REGION=us-west-2 \
jess/awscli "$1"
}
# generate SHA1 files for each artifacts that have a SHA512 files
generate_sha1_files(){
echo "****"
echo "Generate .sha1 files..."
echo "****"
# for each .sha512 files, generate a .sha1 file from the related source archive
FILES=$(find downloads -type f -name '*.sha512')
for f in $FILES ;
do
if [[ -f "${f%.sha512}" ]];
then
# generate sha1 hash
hash=$(shasum -a 1 "${f%.sha512}" | awk 'NR==1 { print $1 }')
# write sha1 content to .sha1 file
if [[ $? ]] && [[ -n "${hash}" ]]; then
echo "$hash" > "${f%.sha512}.sha1"
fi
if [ -f "${f%.sha512}.sha1" ];
then
echo "SHA1 file generated for ${f%.sha512}"
fi
else
echo "ERROR: ${f%.sha512} doesn't exist, can't generate SHA1 file."
fi
done
}
### MAIN
# all sync and generated files will be sored in this folder
mkdir -p downloads
# get files from S3
sync_get_release_files_from_s3
# generate missing .sha1 files
generate_sha1_files
# push .sha1 files to S3
sync_push_files_to_s3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment