From 3e2d71d7b135cc6980cc10a4108130236734551e Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Fri, 30 Oct 2020 16:04:16 -0700 Subject: Initial version. --- olamic-worker | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 olamic-worker (limited to 'olamic-worker') diff --git a/olamic-worker b/olamic-worker new file mode 100755 index 0000000..942151a --- /dev/null +++ b/olamic-worker @@ -0,0 +1,83 @@ +#!/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 <