Skip to content

Instantly share code, notes, and snippets.

@xuqingfeng
Created February 23, 2017 04:19
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 xuqingfeng/2924048f541ee66bd437ec4ce7a5720a to your computer and use it in GitHub Desktop.
Save xuqingfeng/2924048f541ee66bd437ec4ce7a5720a to your computer and use it in GitHub Desktop.
# remove old
rm -rf /opt/wcl/monitoring
mkdir -p /opt/wcl/monitoring
chmod 755 /opt/wcl
cd /opt/wcl/monitoring
# https://gist.github.com/maxim/6e15aa45ba010ab030c4
TOKEN=$1
VERSION=$2 # tag name or the word "latest"
FILE=$3 # the name of your release asset file, e.g. build.tar.gz
# TODO: check from different aspects
if [ -z "$1" ]; then
echo "[E] Invalid TOKEN"
exit 1
fi
if [ -z "$2" ]; then
echo "[E] Invalid VERSION"
exit 1
fi
if [ -z "$3" ]; then
echo "[E] Invalid FILE"
exit 1
fi
REPO="Wiredcraft/products"
GITHUB="https://api.github.com"
function gh_curl() {
curl -H "Authorization: token $TOKEN" \
-H "Accept: application/vnd.github.v3.raw" \
$@
}
if [ "$VERSION" = "latest" ]; then
# Github should return the latest release first.
parser=".[0].assets | map(select(.name == \"$FILE\"))[0].id"
else
parser=". | map(select(.tag_name == \"$VERSION\"))[0].assets | map(select(.name == \"$FILE\"))[0].id"
fi
CONTENT=`gh_curl -s $GITHUB/repos/$REPO/releases`
TYPE=`echo "$CONTENT" | jq 'type'`
if [ "$TYPE" = "object" ]; then
MESSAGE=`echo "$CONTENT" | jq '.message'`
echo "[E] curl github fail: $MESSAGE"
exit 1
else
# TYPE = "array"
ASSET_ID=`echo "$CONTENT" | jq "$parser"`
if [ "$ASSET_ID" = "null" ]; then
echo "[E] version not found: $VERSION"
exit 1
fi
fi
wget -q --auth-no-challenge --header='Accept:application/octet-stream' \
https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$ASSET_ID \
-O $3
tar xzvf $3
chown -R wcladmin:wcladmin /opt/wcl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment