+current_git_branch() {
+ # git rev-parse --abbrev-ref HEAD # outputs HEAD for detached head
+ # outputs nothing for detached HEAD, which is fine for ``git fetch``
+ git describe --all --exact-match 2>/dev/null | sed 's=.*/=='
+}
+
+check_url() {
+ if [[ $DOWNLOADER_NAME == 'wget' ]]; then
+ if [[ $(wget -S --spider $1 2>&1 | grep 'HTTP/1.1 200 OK') ]]; then
+ return 0
+ else
+ return 1
+ fi
+ elif [[ $DOWNLOADER_NAME == 'curl' ]]; then
+ local code=$(curl -sL -w "%{http_code}\\n" "$1" -o /dev/null)
+ if [[ $code -eq 200 ]]; then return 0; else return 1; fi
+ else
+ $ECHO "error: wget or curl required in check_url"
+ exit_script 12
+ fi
+}
+
+# If we are on a branch, first try to get a boot image for that branch.
+# Otherwise, just use `master`
+set_boot_image_vars() {
+ set_current_branch
+ local url="https://downloads.factorcode.org/images/${CURRENT_BRANCH}/checksums.txt"
+ check_url $url
+ if [[ $? -eq 0 ]]; then
+ CHECKSUM_URL="$url"
+ BOOT_IMAGE_URL="https://downloads.factorcode.org/images/${CURRENT_BRANCH}/${BOOT_IMAGE}"
+ else
+ $ECHO "boot image for branch \`${CURRENT_BRANCH}\` is not on server, trying master instead"
+ $ECHO " tried nonexistent url: ${url}"
+ CHECKSUM_URL="https://downloads.factorcode.org/images/master/checksums.txt"
+ BOOT_IMAGE_URL="https://downloads.factorcode.org/images/master/${BOOT_IMAGE}"
+ fi
+}
+
+set_current_branch() {
+ if [ -n "${CI_BRANCH}" ]; then
+ CURRENT_BRANCH="${CI_BRANCH}"
+ else
+ CURRENT_BRANCH=$(current_git_branch)
+ fi
+}
+
+update_boot_image() {
+ set_boot_image_vars