Menu
Public repository / master
AkurAI-Monitor / deploy.sh
Latest change b6bab45601e68904ff4963fa8a736b0148effda1 - Deploy to titan, not the retired EC2 copy by Ólafur Búi Ólafsson
#!/usr/bin/env bash
# Deploy rust-monitor to titan: build, install binary + UI (keeping .prev copies), restart
# rust-monitor-titan, wait for a healthy /api/health, roll back on failure, then validate.
# Run from an operator shell: forge jobs are sandboxed (NoNewPrivileges, read-only home) and
# cannot restart the service. The EC2 copy is retired; never release to it.
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
ROOT="${MONITOR_ROOT:-$HOME/.local/opt/rust-monitor}"
SERVICE="${MONITOR_SERVICE:-rust-monitor-titan}"
BIN="${MONITOR_BIN:-target/x86_64-unknown-linux-musl/release/rust-monitor}"
SYSTEMCTL="${SYSTEMCTL:-sudo systemctl}"
HEALTH_URL="${HEALTH_URL:-http://127.0.0.1:8800/api/health}"
HEALTH_TRIES="${HEALTH_TRIES:-20}"
HEALTH_DELAY="${HEALTH_DELAY:-1}"
log() { printf 'deploy: %s\n' "$*" >&2; }
healthy() {
curl -fsS --max-time 5 "$HEALTH_URL" 2>/dev/null | python3 -c 'import json,sys; d=json.load(sys.stdin); assert d["app"]=="rust-monitor" and d["status"]=="ok"' 2>/dev/null
}
wait_healthy() {
local i
for ((i = 0; i < HEALTH_TRIES; i++)); do
healthy && return 0
sleep "$HEALTH_DELAY"
done
return 1
}
install_release() {
cp -p "$ROOT/bin/rust-monitor" "$ROOT/bin/rust-monitor.prev"
install -m 755 "$BIN" "$ROOT/bin/rust-monitor.new"
mv -f "$ROOT/bin/rust-monitor.new" "$ROOT/bin/rust-monitor"
rm -rf "$ROOT/app.prev"
cp -a "$ROOT/app" "$ROOT/app.prev"
cp -a ui/. "$ROOT/app/"
}
roll_back() {
log "health check failed; rolling back to the previous binary and UI"
mv -f "$ROOT/bin/rust-monitor.prev" "$ROOT/bin/rust-monitor"
rm -rf "$ROOT/app"
mv "$ROOT/app.prev" "$ROOT/app"
$SYSTEMCTL restart "$SERVICE"
}
if [ -z "${SKIP_BUILD:-}" ]; then
nice -n 10 cargo build --release --locked --target x86_64-unknown-linux-musl
fi
[ -x "$BIN" ] || { log "no built binary at $BIN"; exit 1; }
install_release
$SYSTEMCTL restart "$SERVICE"
if ! wait_healthy; then
roll_back
exit 1
fi
log "healthy at $HEALTH_URL"
if [ -z "${SKIP_VALIDATE:-}" ]; then
scripts/validate.sh
fi