summaryrefslogtreecommitdiff
path: root/olamic-worker
diff options
context:
space:
mode:
Diffstat (limited to 'olamic-worker')
-rwxr-xr-xolamic-worker83
1 files changed, 83 insertions, 0 deletions
diff --git a/olamic-worker b/olamic-worker
new file mode 100755
index 0000000..942151a
--- /dev/null
+++ b/olamic-worker
@@ -0,0 +1,83 @@
1#!/bin/bash
2
3set -eu -o pipefail
4
5OLAMIC_EMAIL="${OLAMIC_EMAIL:-}"
6OLAMIC_QUEUE_HOST="${OLAMIC_QUEUE_HOST:-}"
7OLAMIC_QUEUE_DIR="${OLAMIC_QUEUE_DIR:-/srv/olamic/queue}"
8OLAMIC_WORKER_PERIOD="${OLAMIC_WORKER_PERIOD:-1m}"
9
10OLAMIC_WORKER_DIR="$(pwd -P)"
11
12while true; do
13 if [[ "$OLAMIC_QUEUE_HOST" == "" ]]; then
14 TASK_INFO="$(/usr/share/olamic/pick-task "$OLAMIC_QUEUE_DIR")"
15 else
16 TASK_INFO="$(ssh "$OLAMIC_QUEUE_HOST" /usr/share/olamic/pick-task "$OLAMIC_QUEUE_DIR")"
17 fi
18
19 if [[ "$TASK_INFO" == "" ]]; then
20 sleep "$OLAMIC_WORKER_PERIOD"
21 continue
22 fi
23
24 (
25 eval "$TASK_INFO"
26 printf "Running task %q\n" "$TASK_ID"
27
28 if [[ ! -e cache.git ]]; then
29 git init --bare cache.git
30 fi
31
32 set +e
33 (
34 set -ex
35
36 GIT_DIR=cache.git git fetch "$GIT_REPO" "$GIT_OBJECT"
37
38 rm -rf work
39 mkdir work
40 cd work
41
42 git clone --shared "$OLAMIC_WORKER_DIR"/cache.git checkout
43 cd checkout
44 git config advice.detachedHead false
45 git checkout -f "$GIT_OBJECT"
46
47 (
48 eval "$TASK_SCRIPT"
49 )
50 ) 2>&1 | tee log
51 STATUS="$?"
52
53 set -e
54
55 if [[ "$STATUS" != "0" ]]; then
56 echo "Task failed with exit code $STATUS"
57 if [[ "$OLAMIC_EMAIL" != "" ]]; then
58 echo "Sending email to $OLAMIC_EMAIL"
59 sendmail -t <<END
60To: $OLAMIC_EMAIL
61Subject: olamic failure $GIT_REPO $TASK_ID
62Content-type: text/plain
63
64Olamic task $TASK_ID failed with exit code $STATUS.
65
66Environment for the build:
67$(env | sort)
68
69Log from the build:
70$(cat log)
71END
72 fi
73 else
74 echo "Task completed successfully"
75 fi
76
77 if [[ "$OLAMIC_QUEUE_HOST" == "" ]]; then
78 rm -f "$OLAMIC_QUEUE_DIR"/in-progress/"$TASK_ID"
79 else
80 ssh "$OLAMIC_QUEUE_HOST" rm -f "$OLAMIC_QUEUE_DIR"/in-progress/"$TASK_ID"
81 fi
82 )
83done