diff options
| author | Julian Blake Kongslie | 2020-10-30 16:04:16 -0700 |
|---|---|---|
| committer | Julian Blake Kongslie | 2020-10-30 17:33:31 -0700 |
| commit | 3e2d71d7b135cc6980cc10a4108130236734551e (patch) | |
| tree | ec52715eab33236c10f01ab3f47c913f110ec032 /olamic-worker | |
| download | olamic-3e2d71d7b135cc6980cc10a4108130236734551e.tar.xz | |
Initial version.release/1
Diffstat (limited to '')
| -rwxr-xr-x | olamic-worker | 83 |
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 | |||
| 3 | set -eu -o pipefail | ||
| 4 | |||
| 5 | OLAMIC_EMAIL="${OLAMIC_EMAIL:-}" | ||
| 6 | OLAMIC_QUEUE_HOST="${OLAMIC_QUEUE_HOST:-}" | ||
| 7 | OLAMIC_QUEUE_DIR="${OLAMIC_QUEUE_DIR:-/srv/olamic/queue}" | ||
| 8 | OLAMIC_WORKER_PERIOD="${OLAMIC_WORKER_PERIOD:-1m}" | ||
| 9 | |||
| 10 | OLAMIC_WORKER_DIR="$(pwd -P)" | ||
| 11 | |||
| 12 | while 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 | ||
| 60 | To: $OLAMIC_EMAIL | ||
| 61 | Subject: olamic failure $GIT_REPO $TASK_ID | ||
| 62 | Content-type: text/plain | ||
| 63 | |||
| 64 | Olamic task $TASK_ID failed with exit code $STATUS. | ||
| 65 | |||
| 66 | Environment for the build: | ||
| 67 | $(env | sort) | ||
| 68 | |||
| 69 | Log from the build: | ||
| 70 | $(cat log) | ||
| 71 | END | ||
| 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 | ) | ||
| 83 | done | ||
