Created
February 22, 2018 17:29
-
-
Save akutz/7f9b3e5c80f5da96d8d514f2e6142f17 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/bash | |
# Must set BINTRAY_USER and BINTRAY_KEY to owner of both orgs and that | |
# owner's API key | |
# Set to stable, staged, or unstable | |
REPO=${REPO:-$1} | |
REPO=${REPO:-stable} | |
# It's possible to pass in a file with the | |
# versions for the provided repo. | |
VERSIONS_FILE=$2 | |
src_get_versions() { | |
curl -sSL https://dl.bintray.com/emccode/rexray/$REPO/ | \ | |
grep 0. | \ | |
awk -F \" '{print substr($2, 1, length($2)-1)}' | \ | |
sort --version-sort | |
} | |
src_get_file_names_for_version() { | |
curl -sSL https://dl.bintray.com/emccode/rexray/$REPO/$1/ | \ | |
grep 'rexray' | \ | |
awk -F \" '{print $2}' | |
} | |
# Creates a new version in the rexray org. Args include: | |
# 1 - version | |
# 2 - description | |
# 3 - vcs_tag | |
# 4 - released date | |
tgt_new_version() { | |
mkdir -p $REPO/$1 | |
json_file=$REPO/$1/version.json | |
# Check to see if the version exists | |
if ! curl -sSL \ | |
-u $BINTRAY_USER:$BINTRAY_KEY \ | |
-o $json_file \ | |
https://api.bintray.com/packages/rexray/rexray/$REPO/versions/$1; then | |
exit $? | |
fi | |
# If the version does not exist then create it | |
if grep -q "Version '$1' was not found" $json_file; then | |
if ! curl -sSL -X POST \ | |
-u $BINTRAY_USER:$BINTRAY_KEY \ | |
-o $json_file \ | |
-H "Content-Type: application/json" \ | |
-d "{\"name\":\"$1\",\"desc\":\"$2\",\"vcs_tag\":\"$3\",\"released\":\"$4\"}" \ | |
https://api.bintray.com/packages/rexray/rexray/$REPO/versions; then | |
exit $? | |
fi | |
fi | |
# Verify the existing version has a tag set. | |
if [ "$(jq -r '.vcs_tag' $json_file)" == "null" ]; then | |
if ! curl -sSL -X PATCH \ | |
-u $BINTRAY_USER:$BINTRAY_KEY \ | |
-H "Content-Type: application/json" \ | |
-d "{\"vcs_tag\":\"$3\"}" \ | |
https://api.bintray.com/packages/rexray/rexray/$REPO/versions/$1 \ | |
1> /dev/null; then | |
exit $? | |
fi | |
if ! curl -sSL \ | |
-u $BINTRAY_USER:$BINTRAY_KEY \ | |
-o $json_file \ | |
https://api.bintray.com/packages/rexray/rexray/$REPO/versions/$1; then | |
exit $? | |
fi | |
fi | |
# Format the file | |
json=$(jq '.' $json_file) | |
echo $json > $json_file | |
} | |
if [ -e "$VERSIONS_FILE" ]; then | |
repo_versions=$(cat $VERSIONS_FILE) | |
else | |
repo_versions=$(src_get_versions) | |
fi | |
for v in $repo_versions; do | |
echo "VERSION: $v" | |
v_json=$(curl -sSL -u $BINTRAY_USER:$BINTRAY_KEY \ | |
-s https://api.bintray.com/packages/emccode/rexray/$REPO/versions/$v) | |
name=$v | |
desc=$(echo $v_json | jq -r '.desc') | |
vcs_tag=$(echo $v_json | jq -r '.vcs_tag') | |
released=$(echo $v_json | jq -r '.released' | \ | |
sed -e 's/0015-/2015-/g' \ | |
-e 's/0016-/2016-/g' \ | |
-e 's/0017-/2017-/g' \ | |
-e 's/0018-/2018-/g') | |
if [ "$vcs_tag" == "null" ]; then vcs_tag=v$v; fi | |
echo " NAME: $name" | |
echo " DESC: $desc" | |
echo " TAG: $vcs_tag" | |
echo " DATE: $released" | |
# Create the version in the rexray org | |
tgt_new_version "$name" "$desc" "$vcs_tag" "$released" | |
# Copy all of this version's files to the new location. | |
echo | |
for f in $(src_get_file_names_for_version $v); do | |
file_path=$REPO/$v/$f | |
echo " FILE: $f" | |
if [ -e "$file_path" ]; then continue; fi | |
echo " DOWNLOAD" | |
if ! curl --progress-bar -L \ | |
-o $file_path \ | |
https://dl.bintray.com/emccode/rexray/$REPO/$v/$f; then | |
exit 1 | |
fi | |
echo " UPLOAD" | |
curl -L \ | |
--progress-bar \ | |
--data-binary "@$file_path" \ | |
-X PUT \ | |
-u $BINTRAY_USER:$BINTRAY_KEY \ | |
-H "X-Bintray-Package: $REPO" \ | |
-H "X-Bintray-Version: $v" \ | |
-H "X-Bintray-Override: 1" \ | |
-H "X-Bintray-Publish: 1" \ | |
-H "X-Bintray-Explode: 0" \ | |
-H "X-Checksum-Sha2: $(shasum -a 256 $file_path | awk '{print $1}')" \ | |
-H "Content-Type: application/octet-stream" \ | |
https://api.bintray.com/content/rexray/rexray/$REPO/$v/$f > $file_path.log | |
upload_exit_code=$? | |
rm -f $file_path | |
if [ "$upload_exit_code" -ne "0" ]; then | |
cat $file_path.log | |
exit $upload_exit_code | |
fi | |
touch $file_path | |
rm -f $file_path.log | |
echo | |
done | |
echo | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment