mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 19:08:13 +01:00
sync-infra's live branch stopped <app>-wake.service and deleted its unit files, but stopping a failed unit does not clear its failed state. systemd then keeps it as a not-found, failed unit that nothing ever starts again, and host-check's failed-unit sweep reports it as still failing. signin-wake.service failed on 2026-10-31 (Result: resources, #1491). signin went live minutes later (#1490). On 2026-11-01 an older retire-site.sh that did not yet know about wake units retired signin. The ghost unit was still dispatching an incident on 2026-11-05. The live branch now runs reset-failed on both wake units before it removes their files. retire-site.sh --mode delete already does the same. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
171 lines
7.7 KiB
Bash
Executable File
171 lines
7.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Generate + push systemd units, launch scripts, and Caddy vhosts for every
|
|
# app in apps.conf. Idempotent. Does NOT touch the four pre-existing services
|
|
# (bridge, loki, orangecat, evig) or any /opt/<app>/.env.
|
|
# Usage: sync-infra.sh [app ...] (no args = all)
|
|
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
|
|
|
|
apps=("$@")
|
|
[ ${#apps[@]} -eq 0 ] && mapfile -t apps < <(app_names)
|
|
|
|
# One-time: Caddy import dir + import line
|
|
box 'sudo mkdir -p /etc/caddy/apps.d
|
|
grep -q "import apps.d" /etc/caddy/Caddyfile || \
|
|
echo "import apps.d/*.caddy" | sudo tee -a /etc/caddy/Caddyfile >/dev/null'
|
|
|
|
# Sleep when idle needs systemd-socket-proxyd with --exit-idle-time (systemd
|
|
# 246+). Asked once; without it every site stays always on, as before.
|
|
CAN_SLEEP=0
|
|
proxyd_help=$(box "/usr/lib/systemd/systemd-socket-proxyd --help 2>&1" 2>/dev/null || true)
|
|
if [[ "$proxyd_help" == *--exit-idle-time* ]]; then
|
|
CAN_SLEEP=1
|
|
else
|
|
echo "note: systemd-socket-proxyd --exit-idle-time unavailable on the box; no site will sleep"
|
|
fi
|
|
|
|
for app in "${apps[@]}"; do
|
|
app_lookup "$app"
|
|
echo "=== sync $NAME (port $PORT) ==="
|
|
|
|
# A sleeping site's app listens on an inner port; systemd holds $PORT for it
|
|
# (lib.sh, sleep when idle). Everything else listens on $PORT itself.
|
|
SLEEP=0; LISTEN="$PORT"
|
|
if [ "$CAN_SLEEP" = 1 ] && sleeps_when_idle "$PORT" "$DOMAINS" "$STATUS"; then
|
|
SLEEP=1; LISTEN=$(wake_inner_port "$PORT")
|
|
echo " sleeps when idle (status $STATUS): socket :$PORT -> app :$LISTEN"
|
|
fi
|
|
|
|
launch=$(sed "s|__PORT__|$LISTEN|g" "$(dirname "${BASH_SOURCE[0]}")/launch.sh.tmpl")
|
|
|
|
# Unquoted heredoc on purpose ($NAME expands). That makes every backtick in
|
|
# it a command substitution run HERE: two comments once quoted "systemctl
|
|
# reset-failed" and "systemctl restart" in backticks, and every sync ran both
|
|
# on the machine doing the sync — polkit refusals on the box, silent on a
|
|
# laptop. test-register-site.sh keeps this file backtick-free.
|
|
unit=$(cat <<EOF
|
|
[Unit]
|
|
Description=$NAME application server (self-hosted, generated by sync-infra.sh)
|
|
After=network-online.target postgresql.service
|
|
Wants=network-online.target
|
|
# Never enter the start-limit "failed" lockout: a crash-looping app should keep
|
|
# retrying (Restart=on-failure) instead of staying down until a manual
|
|
# systemctl reset-failed.
|
|
StartLimitIntervalSec=0
|
|
# Any failure fires an instant Telegram (see install-host-alerts.sh). Wired here
|
|
# so a newly-synced app is alertable from its first boot, not only after a
|
|
# separate install-host-alerts run.
|
|
#
|
|
# MUST be in [Unit]. systemd ignores OnFailure in [Service] — it logs "Unknown
|
|
# key 'OnFailure' in section [Service], ignoring" and carries on, so the unit
|
|
# looks fine and simply never alerts. It sat in [Service] until 2026-09-11, and
|
|
# the effect was exactly what the comment above promises it prevents: 10 of 28
|
|
# app units had NO failure alerting, every one of them created after the last
|
|
# install-host-alerts run, which is the only other thing that wires it.
|
|
OnFailure=notify-failure@%n.service
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=ubuntu
|
|
WorkingDirectory=/opt/$NAME/app
|
|
ExecStart=/opt/$NAME/app/launch.sh
|
|
Restart=on-failure
|
|
RestartSec=3
|
|
# Bound the stop phase so a systemctl restart can't block on a slow-draining
|
|
# Next process (default 90s). KillMode=mixed SIGTERMs the main proc then SIGKILLs
|
|
# the cgroup, freeing the port fast for the new process.
|
|
#
|
|
# 3s, not 15: a Next standalone server closes its listener on SIGTERM and then
|
|
# waits on keep-alive connections the proxy never closes, so the drain never
|
|
# finishes and the whole timeout is served as 502s. Measured on loki
|
|
# (2026-08-14): ~13s of refused connections per deploy. Cut the wait; the
|
|
# matching lb_try_duration below hides what is left.
|
|
TimeoutStopSec=3
|
|
KillMode=mixed
|
|
# Memory ceiling: a leaking/runaway Next process is OOM-killed (and restarted)
|
|
# alone instead of taking the whole small box down. MemoryHigh throttles first.
|
|
MemoryMax=1G
|
|
MemoryHigh=768M
|
|
StandardOutput=journal
|
|
StandardError=journal
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
)
|
|
|
|
if [ "$SLEEP" = 1 ]; then
|
|
unit=$(sleep_app_unit "$unit" "$NAME" "$LISTEN")
|
|
# Stop the always-on app only when the socket is not already holding the
|
|
# port (the first switch); a re-sync of a sleeping site stops nothing.
|
|
# Stop the wake proxy then too: a deploy that ran before the first sync
|
|
# finished starts the app, which pulls in the proxy, and the proxy keeps
|
|
# the inherited listener on the public port, so the new socket cannot bind
|
|
# it (signin, 2026-10-31: Result: resources on every sync).
|
|
activate="sudo tee /etc/systemd/system/$NAME-wake.socket >/dev/null <<'SOCK_EOF'
|
|
$(wake_socket_unit "$NAME" "$PORT")
|
|
SOCK_EOF
|
|
sudo tee /etc/systemd/system/$NAME-wake.service >/dev/null <<'PROXY_EOF'
|
|
$(wake_proxy_unit "$NAME" "$LISTEN")
|
|
PROXY_EOF
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl disable $NAME-app >/dev/null 2>&1 || true
|
|
if ! systemctl is-active --quiet $NAME-wake.socket; then
|
|
sudo systemctl stop $NAME-wake.service $NAME-app >/dev/null 2>&1 || true
|
|
sudo systemctl reset-failed $NAME-wake.socket >/dev/null 2>&1 || true
|
|
fi
|
|
if ! sudo systemctl enable --now $NAME-wake.socket >/dev/null 2>&1; then
|
|
echo \" $NAME-wake.socket did not start (it holds 127.0.0.1:$PORT for the app):\" >&2
|
|
systemctl status --no-pager --lines=15 $NAME-wake.socket >&2 || true
|
|
echo \" listening on :$PORT now:\" >&2
|
|
sudo ss -ltnpH | grep -E ':$PORT[[:space:]]' >&2 || echo ' (nothing)' >&2
|
|
exit 1
|
|
fi"
|
|
else
|
|
# A site that went live is always on again: drop its wake units and
|
|
# restart the app on its own port. Otherwise exactly what it always was.
|
|
# Clear the wake units' failed state as well. Stopping a failed unit does
|
|
# not do that, and once its file is gone systemd keeps it as a not-found,
|
|
# failed unit that nothing ever starts again. host-check then reports it
|
|
# as still failing (signin-wake.service, failed on 2026-10-31 when signin
|
|
# went live, was still dispatching an incident on 2026-11-05).
|
|
activate="if [ -f /etc/systemd/system/$NAME-wake.socket ]; then
|
|
sudo systemctl disable --now $NAME-wake.socket >/dev/null 2>&1 || true
|
|
sudo systemctl stop $NAME-wake.service >/dev/null 2>&1 || true
|
|
sudo systemctl reset-failed $NAME-wake.socket $NAME-wake.service >/dev/null 2>&1 || true
|
|
sudo rm -f /etc/systemd/system/$NAME-wake.socket /etc/systemd/system/$NAME-wake.service
|
|
sudo systemctl daemon-reload && sudo systemctl enable $NAME-app >/dev/null 2>&1 && sudo systemctl restart $NAME-app
|
|
else
|
|
sudo systemctl daemon-reload && sudo systemctl enable $NAME-app >/dev/null 2>&1
|
|
fi"
|
|
fi
|
|
|
|
box "sudo mkdir -p /opt/$NAME/app && sudo chown -R ubuntu:ubuntu /opt/$NAME
|
|
cat > /opt/$NAME/app/launch.sh <<'LAUNCH_EOF'
|
|
$launch
|
|
LAUNCH_EOF
|
|
chmod +x /opt/$NAME/app/launch.sh
|
|
sudo tee /etc/systemd/system/$NAME-app.service >/dev/null <<'UNIT_EOF'
|
|
$unit
|
|
UNIT_EOF
|
|
$activate"
|
|
|
|
if [ "$DOMAINS" != "-" ]; then
|
|
# One rule for what the domains field means — including a site that has
|
|
# moved to its own domain — lives in lib.sh (caddy_vhost).
|
|
vhost=$(caddy_vhost "$NAME" "$PORT" "$DOMAINS" "$SITES_BASE_DOMAIN")
|
|
box "sudo tee /etc/caddy/apps.d/$NAME.caddy >/dev/null <<'CADDY_EOF'
|
|
$vhost
|
|
CADDY_EOF
|
|
sudo mkdir -p /opt/$NAME/uploads && sudo chown ubuntu:ubuntu /opt/$NAME/uploads"
|
|
fi
|
|
done
|
|
|
|
box 'sudo caddy validate --config /etc/caddy/Caddyfile >/dev/null && sudo systemctl reload caddy && echo "caddy reloaded OK"'
|
|
|
|
# Keep the watchdog's monitored targets in lockstep with apps.conf. targets.conf
|
|
# is SSOT-derived from apps.conf, but by a SEPARATE script — so a newly-synced
|
|
# public app was NOT monitored until someone re-ran install-watchdog by hand.
|
|
# solon was deployed but never added to targets → externally dark for weeks with
|
|
# no alert (2026-07). Refreshing here makes "provisioned" imply "monitored".
|
|
bash "$HERE/install-watchdog.sh"
|