Something went wrong. Try again.
A terminal UI for Libby libbui.vinnymac.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145#!/usr/bin/env bash# Redeploys libbui.vinnymac.dev when main has a new commit that passed CI.## Run by libbui-web-update.timer. It pulls main over SSH with this machine's# key, asks the CI host's API (read only) whether the `ci` workflow passed for# that exact commit, then builds in its own clone and runs deploy.sh. Nothing# can reach this machine from outside, and no secrets live in the repository.## Where to pull from lives outside the repository, in# ~/.config/libbui-web/update.env:# LIBBUI_WEB_REMOTE=git@host:owner/libbui.git (SSH URL to clone and fetch)# LIBBUI_WEB_REPO=owner/libbui (owner/name for `gh api`)## update.sh deploy if main moved, is green and has not failed here# update.sh --force rebuild and deploy main even if it is live or failed## deploy.sh replaces this file while it runs. bash reads scripts as it goes,# so everything lives in main() and the last line exits: the whole body is# parsed before deploy.sh can touch it.set -Eeuo pipefailumask 077
base="$HOME/.local/share/libbui-web"source_dir="$base/source"state="$base/state"config="$HOME/.config/libbui-web/update.env"[ -r "$config" ] || { echo "error: $config is missing; see the top of this script" >&2; exit 1; }# shellcheck source=/dev/null. "$config"repo="${LIBBUI_WEB_REPO:?set LIBBUI_WEB_REPO in $config}"remote="${LIBBUI_WEB_REMOTE:?set LIBBUI_WEB_REMOTE in $config}"# The live unit runs this node; the candidate check must use the same one.node=/usr/bin/nodemin_free_kib=$((4 * 1024 * 1024))export PATH="$base/tools:$HOME/.cargo/bin:$HOME/.volta/bin:/home/linuxbrew/.linuxbrew/bin:/usr/bin:/bin"export GIT_TERMINAL_PROMPT=0 GIT_SSH_COMMAND="ssh -o BatchMode=yes"export CARGO_TARGET_DIR="$base/target" CARGO_TERM_COLOR=never# A binary that was never run must not go live.export QEMU=qemu-aarch64-static REQUIRE_QEMU=1
# Waits for `url` to answer with exactly `want`.serves() { local url=$1 want=$2 got for _ in $(seq 1 15); do got="$(curl -fsS "$url" 2>/dev/null || true)" [ "$got" = "$want" ] && return 0 sleep 1 done echo "error: $url answered '${got:-nothing}', expected $want" >&2 return 1}
main() { mkdir -p "$state" exec 9>"$state/update.lock" flock -n 9 || { echo "another update is running"; return 0; }
if [ ! -d "$source_dir/.git" ]; then git clone --quiet --no-checkout "$remote" "$source_dir" fi [ "$(git -C "$source_dir" remote get-url origin)" = "$remote" ] || { echo "error: $source_dir does not track $remote" >&2; return 1; } git -C "$source_dir" fetch --quiet --prune origin main sha="$(git -C "$source_dir" rev-parse origin/main)"
if [ "${1:-}" != --force ]; then [ "$(cat "$state/deployed" 2>/dev/null)" = "$sha" ] && return 0 # A commit that failed to build or deploy here is not retried every five # minutes; push a fix or run update.sh --force. [ "$(cat "$state/failed" 2>/dev/null)" = "$sha" ] && return 0 fi
# Only a commit whose push run of the `ci` workflow succeeded may go live. conclusion="$(gh api "repos/$repo/actions/runs?head_sha=$sha&event=push&per_page=20" \ --jq '[.workflow_runs[] | select(.name == "ci")] | sort_by(.run_attempt) | last | if . == null then "missing" elif .status != "completed" then "pending" else .conclusion end')" case "$conclusion" in success) ;; missing | pending) echo "main is $sha; waiting for CI ($conclusion)" return 0 ;; *) if [ "$(cat "$state/rejected" 2>/dev/null)" != "$sha" ]; then echo "main is $sha but CI ended in $conclusion; keeping the live site" >&2 echo "$sha" > "$state/rejected" fi return 0 ;; esac
free="$(df --output=avail -k "$base" | tail -1 | tr -d ' ')" if [ "$free" -lt "$min_free_kib" ]; then echo "error: only $((free / 1024)) MiB free under $base; not building" >&2 return 1 fi
echo "deploying $sha" # From here on, any failure marks this commit so the timer leaves it alone. candidate="" trap 'status=$?; [ -n "$candidate" ] && kill "$candidate" 2>/dev/null; if [ "$status" -ne 0 ]; then echo "$sha" > "$state/failed"; echo "error: $sha failed; it will not be retried until main moves" >&2; fi' EXIT
git -C "$source_dir" checkout --quiet --force --detach "$sha" git -C "$source_dir" clean -fdxq -e web/node_modules
cd "$source_dir" scripts/static-linux.sh aarch64-unknown-linux-musl binary="$CARGO_TARGET_DIR/aarch64-unknown-linux-musl/release/libbui"
cd web # No install scripts: they would run with this user's keys and tokens. npm ci --no-audit --no-fund --ignore-scripts LIBBUI_BINARY="$binary" npm run binary npm run build printf '%s\n' "$sha" > dist/version.txt
# Health-check the build on a spare port before it replaces the live one. LIBBUI_WEB_ROOT="$source_dir/web/dist" PORT=5181 "$node" server.mjs & candidate=$! serves http://127.0.0.1:5181/version.txt "$sha" curl -fsS -o /dev/null http://127.0.0.1:5181/ for asset in dist/assets/*; do case "$asset" in *.br | *.gz) continue ;; esac curl -fsS -o /dev/null "http://127.0.0.1:5181/assets/${asset##*/}" done # Another process on 5181 could have answered; make sure ours is alive. kill -0 "$candidate" kill "$candidate" candidate=""
previous="$(readlink "$base/current" || true)" deploy/deploy.sh if ! serves http://127.0.0.1:5179/version.txt "$sha"; then echo "error: live check failed after deploying $sha" >&2 if [ -n "$previous" ]; then ln -sfn "$previous" "$base/current" systemctl --user restart libbui-web.service echo "restored $previous" >&2 fi return 1 fi echo "$sha" > "$state/deployed" rm -f "$state/failed" echo "deployed $sha"}
main "$@"exit $?