#!/bin/sh
# Updates of a DUR build node (package dur-worker), when the node started
# and after each task it finished (dur-node-events), not on a timer: the
# server's only way to change a node is a new package in the archive,
# signed as every other; nothing else on the machine is touched.  Only
# while no build runs (an update restarts the worker, which would cut a
# build off):
#  - new versions of dur-worker and debusine-worker
#    (AUTO_UPDATE=no in /etc/dur/worker.conf leaves that to the owner);
#    a new debusine-worker restarts the worker
#  - a new dur-worker applied: setup-worker.sh again (its postinst only
#    marks it, apt cannot run inside dpkg)
#  - the isolation image built again when the node's Devuan indices changed
#    (security and point updates), not weekly
set -eu
AUTO_UPDATE=yes
[ ! -r /etc/dur/worker.conf ] || . /etc/dur/worker.conf
pending=/var/lib/dur/worker-update-pending
exec 9> /run/dur-worker-update.lock
flock -n 9 || exit 0
[ -s /etc/debusine/worker/token ] || exit 0  # not enrolled yet
# a build in progress: a process works in or has files open below the
# work directory (the task, or the worker uploading its results); a
# directory a crashed task left behind does not count
workdir=/var/lib/debusine/worker/workdir
for p in /proc/[0-9]*; do
    for f in "$p/cwd" "$p"/fd/*; do
        case $(readlink "$f" 2> /dev/null) in "$workdir"/*) exit 0 ;; esac
    done
done

# 9>&-: the lock stays here; services these start or restart would keep
# it open and block every later update (apt-cacher-ng did)
apt-get update -qq > /dev/null 2>&1 9>&- || true
if [ "$AUTO_UPDATE" = yes ]; then
    before=$(dpkg-query -W -f '${Version}' debusine-worker 2> /dev/null || true)
    DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --only-upgrade \
        dur-worker debusine-worker > /dev/null 2>&1 9>&- || true
    # with init's respawn (dur-worker-run) the package's own restart is
    # skipped: end the old worker, init starts the new one
    if [ "$(dpkg-query -W -f '${Version}' debusine-worker 2> /dev/null || true)" != "$before" ] &&
        grep -q dur-worker-run /etc/inittab 2> /dev/null; then
        kill "$(cat /run/debusine-worker.pid)" 2> /dev/null || true
    fi
fi
if [ -e "$pending" ]; then
    api=$(sed -n 's/^api-url *= *//p' /etc/debusine/worker/config.ini)
    if sh /usr/share/dur/worker/scripts/setup-worker.sh "$api" \
            > /var/log/dur-worker-update.log 2>&1 9>&-; then
        rm -f "$pending"
    fi
fi
# the image has the node's release: built again once its indices changed
# (the DUR's own do not go into it)
stamp=/var/lib/dur/isolation-image.indices
indices=$(ls /var/lib/apt/lists/*InRelease 2> /dev/null | grep -v _dur_dur_ |
    xargs -r sed -n 's/^Date: //p' | sort | sha256sum)
if [ -e "$stamp" ] && [ "$(cat "$stamp")" != "$indices" ]; then
    /usr/share/dur/build-isolation-image.sh > /var/log/dur-isolation-image.log 2>&1 9>&- &&
        echo "$indices" > "$stamp"
elif [ ! -e "$stamp" ]; then
    echo "$indices" > "$stamp"
fi
