Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"}
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-${ARCH}.tar.gz")
NIGHTLY_RELEASE=$(echo "$DOWNLOAD_URL" | grep -o -P 'crystal-nightly-\d{8}')
echo -e "Downloading ${NIGHTLY_RELEASE} ${ARCH} to ${INSTALL_DIR}..."
curl "${DOWNLOAD_URL}" | tar -xz -C "${INSTALL_DIR}"
echo -e "Installing binaries..."
install_binary crystal
install_binary shards
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment