Skip to content

Instantly share code, notes, and snippets.

@straight-shoota
Last active May 5, 2020 21:39
Show Gist options
  • Save straight-shoota/a2f4b29203936c04c578d6f6a1ad5f9b to your computer and use it in GitHub Desktop.
Save straight-shoota/a2f4b29203936c04c578d6f6a1ad5f9b to your computer and use it in GitHub Desktop.
install-crystal-nightly.sh
#! /usr/bin/env bash
#
# This script looks up the latest successfull nightly build of crystal on
# Circle CI, extracts the build number of `dist_artifacts` job and retrieves
# it's artifacts list.
#
# From this artifacts list, the specified tar package is downloaded and
# extracted in $INSTALL_DIR.
# The binaries `bin/crystal` and `bin/shards` are linked as `crystal-nightly`
# and `shards-nightly` in `/usr/local/bin`.
set -e
ARCH=${ARCH:-"x86_64"}
SYSTEM=${SYSTEM:-"linux"}
INSTALL_DIR=${INSTALL_DIR:-"/opt/crystal-nightlies"}
PROJECT_URL="https://circleci.com/api/v1.1/project/github/crystal-lang/crystal"
function build_num_request() {
curl "${PROJECT_URL}?filter=successful&limit=${1}&branch=master&offset=${2}" | \
python -c 'import json; import sys; print(next(x["build_num"] for x in json.load(sys.stdin) if x["build_parameters"]["CIRCLE_JOB"] == "dist_artifacts"))'
}
function last_build_num() {
python <<EOF
import json
import requests
limit = 100
for i in range(0, 5):
offset = i * limit
url = "${PROJECT_URL}?filter=successful&branch=master&limit=" + str(limit) + "&offset=" + str(offset)
for entry in requests.get(url).json():
if entry["build_parameters"]["CIRCLE_JOB"] == "dist_artifacts" and entry["branch"] == "master":
print(entry["build_num"])
exit(0)
exit(1)
EOF
}
function list_artifacts() {
curl "${PROJECT_URL}/$1/artifacts" | grep -o 'https://[^"]*'
}
function install_binary(){
TARGET_PATH="/usr/local/bin/$1-nightly"
[ -L "$TARGET_PATH" ] && rm "$TARGET_PATH"
ln -s "${INSTALL_DIR}/${NIGHTLY_RELEASE}/bin/$1" "$TARGET_PATH"
echo -e " $TARGET_PATH"
}
# try to create directory first to fail early if missing rights
mkdir -p "${INSTALL_DIR}"
if [ ! -w "${INSTALL_DIR}" ]; then
echo "${INSTALL_DIR} not writeable"
exit
fi
echo -e "Looking for latest succesfull nightly build..."
BUILD_NUM=${BUILD_NUM:-$(last_build_num)}
if [ -z "$BUILD_NUM" ]; then
echo "No valid build found in circleci results."
exit 1
fi
echo -e "Loading artifacts list for build number ${BUILD_NUM}..."
DOWNLOAD_URL=$(list_artifacts $BUILD_NUM | grep -e "-1-${SYSTEM}-${ARCH}.tar.gz")
NIGHTLY_RELEASE=$(echo "$DOWNLOAD_URL" | grep -o -P 'crystal-nightly-\d{8}')
echo -e "Downloading ${NIGHTLY_RELEASE} ${ARCH} to ${INSTALL_DIR}..."
mkdir -p "${INSTALL_DIR}/${NIGHTLY_RELEASE}"
curl -L "${DOWNLOAD_URL}" | tar -xz -C "${INSTALL_DIR}/${NIGHTLY_RELEASE}" --strip-components=1
echo -e "Installing binaries..."
install_binary crystal
install_binary shards
@oprypin
Copy link

oprypin commented May 5, 2020

@@ -12,6 +12,7 @@
 set -e
 
 ARCH=${ARCH:-"x86_64"}
+SYSTEM=${SYSTEM:-"linux"}
 INSTALL_DIR=${INSTALL_DIR:-"/opt/crystal-nightlies"}
 PROJECT_URL="https://circleci.com/api/v1.1/project/github/crystal-lang/crystal"
 
@@ -65,13 +66,13 @@ if [ -z "$BUILD_NUM" ]; then
 fi
 
 echo -e "Loading artifacts list for build number ${BUILD_NUM}..."
-DOWNLOAD_URL=$(list_artifacts $BUILD_NUM | grep -e "-1-${ARCH}.tar.gz")
+DOWNLOAD_URL=$(list_artifacts $BUILD_NUM | grep -e "-1-${SYSTEM}-${ARCH}.tar.gz")
 
 NIGHTLY_RELEASE=$(echo "$DOWNLOAD_URL" | grep -o -P 'crystal-nightly-\d{8}')
 
 echo -e "Downloading ${NIGHTLY_RELEASE} ${ARCH} to ${INSTALL_DIR}..."
 mkdir -p "${INSTALL_DIR}/${NIGHTLY_RELEASE}"
-curl "${DOWNLOAD_URL}" | tar -xz -C "${INSTALL_DIR}/${NIGHTLY_RELEASE}" --strip-components=1
+curl -L "${DOWNLOAD_URL}" | tar -xz -C "${INSTALL_DIR}/${NIGHTLY_RELEASE}" --strip-components=1
 
 echo -e "Installing binaries..."
 install_binary crystal

@straight-shoota
Copy link
Author

Thanks @oprypin! I applied the patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment