screen-locker/scripts/deploy.sh
Krzysztof kuhy Rudnicki 71aca9740f
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (flutter, flutter-analyze, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,flutter-analyze,dependency-freshness) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / lock-popup-safety (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (0) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (1) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (2) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (3) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (4) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (5) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (6) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (7) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (8) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (9) (push) Waiting to run
Tests / flutter-coverage (push) Blocked by required conditions
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Rebuild web/dist on deploy when web/ changed
The server serves the git-ignored web/dist, so a deploy that only pulled
and restarted kept the last hand-built bundle live; the kuhy credit footer
never reached :8770. deploy.sh now rebuilds it when the web/ tree differs
from the last built one (--web-only runs just that step).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 14:53:04 +01:00

184 lines
7.6 KiB
Bash
Executable File

#!/bin/bash
# Deploy screen-locker for real: update the live clone, reinstall, restart the
# long-running user units, prove they are healthy, then ship the workout app.
#
# Every path is anchored on the LIVE clone (~/src/screen-locker), never on this
# script's own directory: install.sh installs editable from wherever it sits,
# so running a worktree copy of it would point the services at scratch code.
#
# workout-locker.service is deliberately NOT restarted: it is the login-time
# gate and starting it can lock the display. Its timer is only re-enabled.
#
# Phone: phone_deploy.sh refuses com.kuhy.workout_app (exit 46) unless kuhy
# runs this himself with PHONE_PROTECT_ALLOW=com.kuhy.workout_app and the app
# closed. This script never sets that variable; it passes the caller's
# environment through and fails loudly when the guard refuses.
#
# The phone step is skipped when the workout app's source is unchanged since
# the last successful phone install: a PC-only change (CI config, a Python pin)
# then deploys with the phone unplugged. The key is the git tree of APP_DIR,
# written to PHONE_APP_MARKER after each install; no marker means the phone is
# required (fail closed). Any app change gives a new tree, so it is never skipped.
#
# Web: the server serves web/dist, which git ignores, so a pull alone left the
# last hand-built bundle live. The bundle is rebuilt (pnpm install + build,
# which stamps /version) whenever the git tree of web/ differs from the one in
# WEB_BUILD_MARKER, before the restart; a failed build fails the deploy.
#
# Usage: bash scripts/deploy.sh [--health-only | --phone-only | --web-only]
#
# Seams: SCREEN_LOCKER_LIVE (live clone), DEPLOY_WEB_PORT (health port),
# PHONE_DEPLOY (installer), PHONE_APP_MARKER (last installed app tree),
# WEB_PNPM (pnpm binary), WEB_BUILD_MARKER (last built web tree).
# Exit: 0 healthy and installed everywhere | 1 any step or check failed.
set -euo pipefail
readonly LIVE="${SCREEN_LOCKER_LIVE:-$HOME/src/screen-locker}"
readonly PY="/usr/bin/python3"
readonly APP_DIR="stronglift_replacement/workout_app"
readonly PHONE_DEPLOY="${PHONE_DEPLOY:-$HOME/.claude/scripts/phone_deploy.sh}"
readonly PHONE_APP_MARKER="${PHONE_APP_MARKER:-$HOME/.local/state/screen-locker/phone_app_tree}"
readonly WEB_PNPM="${WEB_PNPM:-pnpm}"
readonly WEB_BUILD_MARKER="${WEB_BUILD_MARKER:-$HOME/.local/state/screen-locker/web_tree}"
readonly UNITS=(screen-locker-web.service workout-poke.service runnerup-watch.service)
die() { echo "deploy: FAIL: $*" >&2; exit 1; }
ok() { echo "deploy: ok: $*"; }
main_pid() { systemctl --user show -p MainPID --value "$1"; }
web_port() {
if [[ -n "${DEPLOY_WEB_PORT:-}" ]]; then
echo "$DEPLOY_WEB_PORT"
else
(cd /tmp && "$PY" -c 'from screen_locker._web_server import DEFAULT_PORT; print(DEFAULT_PORT)')
fi
}
# /api/health answers and reports enforcement armed; retried while the
# freshly restarted server binds its port.
check_web() {
local port body i
port="$(web_port)"
for i in $(seq 1 15); do
if body="$(curl -fsS --max-time 2 "http://127.0.0.1:$port/api/health" 2>/dev/null)"; then
jq -e '.armed == true' >/dev/null <<<"$body" \
|| die "web health on :$port says enforcement is NOT armed"
ok "web /api/health on :$port armed (try $i)"
return 0
fi
sleep 1
done
die "web /api/health on :$port did not answer within 15 s"
}
health() {
local unit
for unit in "${UNITS[@]}"; do
systemctl --user is-active --quiet "$unit" || die "$unit is not active"
ok "$unit active"
done
check_web
"$PY" "$LIVE/scripts/check_enforcement_armed.py" --quiet \
|| die "check_enforcement_armed.py: enforcement is not armed"
ok "enforcement armed"
"$PY" "$LIVE/scripts/poke_selftest.py" sandbox >/dev/null \
|| die "workout-poke sandbox self-test failed"
ok "workout-poke signed sandbox round trip"
local mod
mod="$(cd /tmp && "$PY" -c 'import screen_locker; print(screen_locker.__file__)')"
[[ "$mod" == "$LIVE"/* ]] || die "screen_locker imports from $mod, not $LIVE"
ok "screen_locker imports from the live clone"
}
desktop() {
local unit
declare -A before=()
git -C "$LIVE" pull --ff-only || die "git pull --ff-only in $LIVE (dirty or diverged?)"
[[ "$(git -C "$LIVE" rev-parse HEAD)" == "$(git -C "$LIVE" rev-parse '@{u}')" ]] \
|| die "$LIVE HEAD is not its upstream after the pull"
ok "live clone at $(git -C "$LIVE" rev-parse --short HEAD)"
bash "$LIVE/install.sh" || die "install.sh failed"
web
for unit in "${UNITS[@]}"; do before[$unit]="$(main_pid "$unit")"; done
systemctl --user restart "${UNITS[@]}" || die "restart of ${UNITS[*]} failed"
for unit in "${UNITS[@]}"; do
[[ "$(main_pid "$unit")" != "${before[$unit]}" ]] \
|| die "$unit kept PID ${before[$unit]}: restart did not take"
done
ok "restarted ${UNITS[*]}"
}
# Rebuild web/dist when web/ changed since the last good build (see header).
web() {
local tree
tree="$(git -C "$LIVE" rev-parse HEAD:web)" || die "no web tree at $LIVE HEAD"
if [[ "$(cat "$WEB_BUILD_MARKER" 2>/dev/null || true)" == "$tree" ]]; then
ok "web unchanged since the last build (tree ${tree:0:10}); build skipped"
return 0
fi
"$WEB_PNPM" -C "$LIVE/web" install --frozen-lockfile || die "web: pnpm install failed"
"$WEB_PNPM" -C "$LIVE/web" run build || die "web: pnpm run build failed"
mkdir -p "$(dirname "$WEB_BUILD_MARKER")"
printf '%s\n' "$tree" > "$WEB_BUILD_MARKER"
ok "web/dist rebuilt (tree ${tree:0:10})"
}
# The app's one-time gap deload backfill saves the pre-cut exercise_state on
# its first start after install; keep a PC copy so restore works without the
# phone's dir. The app needs a few seconds after launch, hence the retries;
# a miss only warns, `restore_exercise_state.py pull` fetches it later.
deload_export() {
local tries
[[ -f "$HOME/.local/state/screen-locker/exercise_state_pre_gap_deload.json" ]] && return 0
for tries in 1 2 3 4 5 6 7 8 9 10; do
if python3 "$LIVE/scripts/restore_exercise_state.py" pull >/dev/null 2>&1; then
ok "pre-gap-deload export copied to ~/.local/state/screen-locker/"
return 0
fi
sleep 2
done
echo "deploy: WARN: no pre-gap-deload export pulled after $tries tries;" \
"run: python3 $LIVE/scripts/restore_exercise_state.py pull" >&2
}
phone() {
local rc=0 tree
tree="$(git -C "$LIVE" rev-parse "HEAD:$APP_DIR")" || die "no $APP_DIR tree at $LIVE HEAD"
if [[ "$(cat "$PHONE_APP_MARKER" 2>/dev/null || true)" == "$tree" ]]; then
ok "workout app unchanged since the last phone install (tree ${tree:0:10}); phone step skipped"
return 0
fi
bash "$PHONE_DEPLOY" "$LIVE/$APP_DIR" --release || rc=$?
case "$rc" in
0)
mkdir -p "$(dirname "$PHONE_APP_MARKER")"
printf '%s\n' "$tree" > "$PHONE_APP_MARKER"
ok "workout app release build installed on the phone (tree ${tree:0:10})"
deload_export
;;
46) die "phone_deploy protect guard (exit 46) refused com.kuhy.workout_app;" \
"kuhy runs: PHONE_PROTECT_ALLOW=com.kuhy.workout_app bash $LIVE/scripts/deploy.sh" \
"with the workout app closed" ;;
*) die "phone_deploy.sh exited $rc (see its exit-code table)" ;;
esac
}
if [[ "${1:-}" == "--phone-only" ]]; then
phone
exit 0
fi
if [[ "${1:-}" == "--web-only" ]]; then
web
exit 0
fi
if [[ "${1:-}" == "--health-only" ]]; then
health
exit 0
fi
desktop
health
phone
ok "deployed"