peppa/scripts/install-launchd.sh
Асель b1c13e739e PEP-165: the live site runs next start; deploy builds before restarting web
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:50:47 +05:00

99 lines
5.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Installs Peppa's launchd agents (start at login, restart on crash):
# quest.telecom.peppa.watchdog — session watchdog under caffeinate -i (no idle sleep)
# quest.telecom.peppa.web — Next.js platform on :3100 (needs `pnpm --filter @peppa/web build`);
# PEPPA_WEB_MODE=dev runs `next dev` instead (HMR: edits in this
# checkout go live without build or release — the pre-launch setup)
# quest.telecom.peppa.bot — Telegram bot (TELEGRAM_BOT_TOKEN in .env)
# quest.telecom.peppa.release — Peppa's own prod releases (PEP-18): started by launchd while
# ~/.peppa/release-queue has requests (the PM's button)
# Prod runs from the release directory: ~/peppa/current/scripts/install-launchd.sh, so every
# path below (and PEPPA_ROOT for hooks and git config) is ~/peppa/current, not a release.
# Pre-launch (PEP-165): scripts/install-launchd.sh watchdog web bot from the working copy — web in
# `start` mode (build first: `next build` in apps/web), no release agent. Never `next dev` on the live
# site: its rebuilds made every open tab reload in a loop (16.11).
# Usage: scripts/install-launchd.sh [watchdog] [web] [bot] [release] (default: all)
# Remove: launchctl bootout gui/$(id -u)/<label> && rm ~/Library/LaunchAgents/<label>.plist
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
PEPPA_HOME="${PEPPA_HOME:-$HOME/.peppa}"
WEB_MODE="${PEPPA_WEB_MODE:-start}"
case "$WEB_MODE" in start|dev) ;; *) echo "PEPPA_WEB_MODE must be start or dev" >&2; exit 1 ;; esac
mkdir -p "$HOME/Library/LaunchAgents" "$PEPPA_HOME"
PATH_ENV="$HOME/.local/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin"
# Bot and watchdog run TypeScript straight from source with Node's type stripping (PEP-36):
# pin the Node that was checked here, so launchd does not depend on what PATH finds later.
NODE_BIN="${PEPPA_NODE:-$(PATH="$PATH_ENV" command -v node || true)}"
[ -x "$NODE_BIN" ] || { echo "node not found (set PEPPA_NODE)" >&2; exit 1; }
"$NODE_BIN" --import "$ROOT/scripts/lib/require-node.mjs" -e 0 || exit 1
# `bootout` returns before launchd has unloaded the agent; a bootstrap right after it fails
# with "Bootstrap failed: 5: Input/output error". Wait until `print` no longer finds it.
wait_unloaded() { # label
local i tries=$(( ${PEPPA_LAUNCHD_WAIT_SEC:-10} * 5 ))
for (( i = 0; i < tries; i++ )); do
launchctl print "gui/$(id -u)/$1" >/dev/null 2>&1 || return 0
sleep 0.2
done
echo "warning: $1 is still loaded after ${PEPPA_LAUNCHD_WAIT_SEC:-10}s" >&2
}
# Extra plist keys for the next install_agent (default: start at login, restart on exit).
LIFECYCLE=' <key>RunAtLoad</key><true/>
<key>KeepAlive</key><true/>'
install_agent() { # label log workdir args...
local label=$1 log=$2 workdir=$3; shift 3
local plist="$HOME/Library/LaunchAgents/$label.plist" args=""
for a in "$@"; do args+=" <string>$a</string>"$'\n'; done
cat > "$plist" <<PLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>$label</string>
<key>ProgramArguments</key>
<array>
$args </array>
<key>WorkingDirectory</key><string>$workdir</string>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key><string>$PATH_ENV</string>
<key>PEPPA_NODE</key><string>$NODE_BIN</string>
<key>PEPPA_ROOT</key><string>$ROOT</string>
</dict>
$LIFECYCLE
<key>StandardOutPath</key><string>$PEPPA_HOME/$log</string>
<key>StandardErrorPath</key><string>$PEPPA_HOME/$log</string>
</dict>
</plist>
PLIST
launchctl bootout "gui/$(id -u)/$label" 2>/dev/null && wait_unloaded "$label"
# Still "5" (unloaded by something else meanwhile): one more try after a second.
launchctl bootstrap "gui/$(id -u)" "$plist" || { sleep 1; launchctl bootstrap "gui/$(id -u)" "$plist"; }
echo "installed $label (log: $PEPPA_HOME/$log)"
}
SERVICES=("$@")
[ ${#SERVICES[@]} -eq 0 ] && SERVICES=(watchdog web bot release)
for s in "${SERVICES[@]}"; do
case "$s" in
watchdog)
install_agent quest.telecom.peppa.watchdog watchdog.log "$ROOT" \
/usr/bin/caffeinate -i "$ROOT/packages/cli/bin/peppa" watchdog ;;
web)
install_agent quest.telecom.peppa.web web.log "$ROOT/apps/web" \
/bin/bash -c "set -a; . '$ROOT/.env'; set +a; exec '$ROOT/apps/web/node_modules/.bin/next' $WEB_MODE -p 3100" ;;
bot)
install_agent quest.telecom.peppa.bot bot.log "$ROOT" "$ROOT/apps/bot/bin/peppa-bot" ;;
release)
mkdir -p "$PEPPA_HOME/release-queue" && chmod 700 "$PEPPA_HOME/release-queue"
LIFECYCLE=" <key>QueueDirectories</key><array><string>$PEPPA_HOME/release-queue</string></array>"
install_agent quest.telecom.peppa.release release.log "$HOME" \
/bin/bash "$ROOT/scripts/peppa-release.sh" --queue
LIFECYCLE=' <key>RunAtLoad</key><true/>
<key>KeepAlive</key><true/>' ;;
*) echo "unknown service $s" >&2; exit 1 ;;
esac
done