AkurAI Build
Menu

Public repository / main

AkurAI-Build / deploy/forge-swap.sh

Latest change a0c9d4ff8c1ee8397ccd5287465783b1f0f23d04 - deploy: path-activated root forge swap; CI job stages binary without sudo (OLI-279) by Ólafur Búi Ólafsson

#!/bin/sh
# Root-side forge binary swap, run by akurai-build-swap.service when the CI
# deploy job drops a request into the staging directory (see service-deploy.sh).
# Installed root-owned at /usr/local/libexec/akurai-build-swap by deploy/systemd/install.sh.
# Result: `journalctl -u akurai-build-swap`.
set -eu

owner="${AKURAI_SWAP_OWNER:-olibuijr}"
host_home="${AKURAI_HOST_HOME:-/home/$owner}"
dest_dir="${AKURAI_BUILD_LIB:-$host_home/.local/lib/akurai-build}"
stage_dir="${AKURAI_DEPLOY_STAGING:-$host_home/.local/share/akurai-build/deploy-staging}"
unit="${AKURAI_BUILD_UNIT:-akurai-build}"
health="${AKURAI_BUILD_HEALTH:-http://127.0.0.1:3210/api/health}"
settle_seconds="${AKURAI_SWAP_SETTLE:-5}"
health_seconds=20
dest="$dest_dir/akurai"
staged="$stage_dir/akurai"
request="$stage_dir/request"

test -f "$request" || { echo "no deploy request"; exit 0; }
rm -f "$request"
test -x "$staged"
test -x "$dest"

# Let the deploy job finish persisting its result before the forge restarts.
sleep "$settle_seconds"

install -o "$owner" -m 755 "$dest" "$dest.prev"
install -o "$owner" -m 755 "$staged" "$dest.new"
mv -f "$dest.new" "$dest"
rm -f "$staged"
systemctl restart "$unit"

i=0
while [ "$i" -lt "$health_seconds" ]; do
  if curl -fsS -o /dev/null "$health"; then
    echo "akurai-build healthy"
    exit 0
  fi
  i=$((i + 1))
  sleep 1
done
echo "akurai-build unhealthy after ${health_seconds}s; rolling back" >&2
install -o "$owner" -m 755 "$dest.prev" "$dest"
systemctl restart "$unit"
exit 1