#!/bin/bash set -eu -o pipefail OLAMIC_EMAIL="${OLAMIC_EMAIL:-}" OLAMIC_QUEUE_HOST="${OLAMIC_QUEUE_HOST:-}" OLAMIC_QUEUE_DIR="${OLAMIC_QUEUE_DIR:-/srv/olamic/queue}" OLAMIC_WORKER_PERIOD="${OLAMIC_WORKER_PERIOD:-1m}" OLAMIC_WORKER_DIR="$(pwd -P)" while true; do if [[ "$OLAMIC_QUEUE_HOST" == "" ]]; then TASK_INFO="$(/usr/share/olamic/pick-task "$OLAMIC_QUEUE_DIR")" else TASK_INFO="$(ssh "$OLAMIC_QUEUE_HOST" /usr/share/olamic/pick-task "$OLAMIC_QUEUE_DIR")" fi if [[ "$TASK_INFO" == "" ]]; then sleep "$OLAMIC_WORKER_PERIOD" continue fi ( eval "$TASK_INFO" printf "Running task %q\n" "$TASK_ID" if [[ ! -e cache.git ]]; then git init --bare cache.git fi set +e ( set -ex GIT_DIR=cache.git git fetch "$GIT_REPO" "$GIT_OBJECT" rm -rf work mkdir work cd work git clone --shared "$OLAMIC_WORKER_DIR"/cache.git checkout cd checkout git config advice.detachedHead false git checkout -f "$GIT_OBJECT" ( eval "$TASK_SCRIPT" ) ) 2>&1 | tee log STATUS="$?" set -e if [[ "$STATUS" != "0" ]]; then echo "Task failed with exit code $STATUS" if [[ "$OLAMIC_EMAIL" != "" ]]; then echo "Sending email to $OLAMIC_EMAIL" sendmail -t <