peppa/scripts/install-launchd.sh
Асель 03aee432fb PEP-18: separate Peppa prod — release dir, PM button, DB backup, auto-rollback
- scripts/peppa-release.sh: ~/peppa/{repo.git,releases/<sha>,current,shared/.env};
  install → next build → checked DB backup → migrate → switch current → kickstart →
  health (/api/health commit + fresh heartbeats) → rollback; --queue, --check, --no-restart
- runner/platform-release.ts: deployProd for the platform project (PEPPA_PLATFORM_KEY=PEP)
  queues a request for launchd quest.telecom.peppa.release; offer after green main
- CLI: peppa db backup [file], peppa release report; backupDb verifies the copy
- install-launchd.sh: wait for unload after bootout (no "Bootstrap failed: 5"),
  release agent (QueueDirectories), PEPPA_ROOT in agents
- bin wrappers: logical root so hooks/credential.helper point at ~/peppa/current
- mock-launchctl, mock-pnpm; e2e platform-release.test.ts; OpenSpec pep-18-peppa-prod

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 17:58:34 +05:00

92 lines
4.4 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`)
# 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.
# 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}"
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' start -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