summaryrefslogtreecommitdiff
path: root/olamic-worker
blob: c9a1bd031ff8fc794e49879b0cc183eca8e01d3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/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 -e

            (
                set -x

                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"
            )

            (
                cd work/checkout
                eval "set -x; $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 <<END
To: $OLAMIC_EMAIL
Subject: olamic failure $GIT_REPO $TASK_ID
Content-type: text/plain

Olamic task $TASK_ID failed with exit code $STATUS.

Environment for the build:
$(env | sort)

Log from the build:
$(cat log)
END
            fi
        else
            echo "Task completed successfully"
        fi

        if [[ "$OLAMIC_QUEUE_HOST" == "" ]]; then
            rm -f "$OLAMIC_QUEUE_DIR"/in-progress/"$TASK_ID"
        else
            ssh "$OLAMIC_QUEUE_HOST" rm -f "$OLAMIC_QUEUE_DIR"/in-progress/"$TASK_ID"
        fi
    )
done