Files
loki/.github/workflows/sync-sites.yml
CatoandClaude Opus 5.5 c9044a8151 feat(sites): sites that are not live yet sleep when idle (#1066)
* wip(sites): sleep-when-idle building blocks (not wired in yet)

Adds the status list and idle time (_box-env.sh) and the pure generators
(lib.sh: sleeps_when_idle, wake_inner_port, sleep_app_unit, wake_socket_unit,
wake_proxy_unit). Nothing calls them yet: sync-infra.sh is unchanged, so no
site's units change. Wiring them in waits on the owner's go-ahead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0191FbipnTCBDKHiKftp9W7B

* feat(sites): sites that are not live yet sleep when idle

Every site ran around the clock with up to 1 GB, which capped the 16 GB box at
roughly 50 sites however rarely they were visited. A site whose register status
is prospect, demo, validating or unverified now sleeps: systemd listens on its
port, the first request starts it (a second or two), and it stops after 15
idle minutes. Live sites (clients, products, evig, the 4001-4004 services) are
untouched; a row that becomes live is switched back to always on on its next
sync.

- sync-infra.sh: for a sleeping row, the app listens on port+20000 and is bound
  to <name>-wake.service (systemd-socket-proxyd --exit-idle-time) behind
  <name>-wake.socket on the public port. Caddy and deploy.sh keep talking to
  127.0.0.1:$PORT, so neither changes; a deploy's restart pulls the proxy up.
  Falls back to always on, out loud, if the box's proxyd lacks idle exit.
- install-watchdog.sh: a sleeping site is checked by its socket, not by a
  request every 5 minutes (which would keep it awake forever).
- uptime-sweep.sh: sleeping sites are health-probed on the first sweep of
  00/06/12/18 UTC instead of every 15 minutes, so botsmann's kind of outage
  is still caught within hours; certificates are still checked daily.
- Sync sites workflow: re-applies sync-infra.sh to named sites and checks each
  one answers — the rollout for sites already on the box.

Tests: test-sleep-when-idle.sh runs sync-infra and the watchdog installer end
to end against a recording ssh; test-uptime-sweep.sh covers the 6-hourly probe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0191FbipnTCBDKHiKftp9W7B

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-07 07:01:59 -07:00

69 lines
2.4 KiB
YAML

name: Sync sites
# Re-apply scripts/hetzner/sync-infra.sh to sites on the box: systemd units,
# launch script, Caddy vhost and the watchdog's target list, from the register
# on main. Registration runs this for a NEW site; this is the button for the
# ones already there — after a change to how sites are run (sleep when idle,
# 2026-10-07) or to a row's status (a site that goes live stays on again).
#
# Manual on purpose: it changes what is running. Name the sites: a sync of
# every row would also generate a vhost for the handcrafted services whose
# Caddy blocks are hand-written (evig), and two blocks for one host fail
# caddy validate for the whole box.
on:
workflow_dispatch:
inputs:
apps:
description: "Register names, space-separated"
required: true
permissions:
contents: read
# One writer of the box's units at a time, and never alongside a Loki deploy.
concurrency:
group: deploy-prod
cancel-in-progress: false
jobs:
sync:
runs-on: ubuntu-latest
timeout-minutes: 20
env:
HETZNER_IP: ${{ vars.HETZNER_IP }}
APPS: ${{ inputs.apps }}
steps:
- uses: actions/checkout@v7
- name: SSH setup
run: |
mkdir -p ~/.ssh
printf '%s\n' "${{ secrets.HETZNER_SSH_KEY }}" > ~/.ssh/id_deploy
chmod 600 ~/.ssh/id_deploy
ssh-keyscan -H "$HETZNER_IP" >> ~/.ssh/known_hosts 2>/dev/null
- name: Sync
env:
DEPLOY_KEY_PATH: /home/runner/.ssh/id_deploy
run: |
[ -n "${APPS// /}" ] || { echo "::error::name the sites to sync"; exit 1; }
# Word-split on purpose: APPS is a list of register names.
# shellcheck disable=SC2086
bash scripts/hetzner/sync-infra.sh $APPS
- name: Every synced site still answers
run: |
# A sleeping site is woken by this request; give it its cold start.
source scripts/hetzner/lib.sh
set +e
bad=""
for n in $APPS; do
app_lookup "$n" || continue
[ "$DOMAINS" != "-" ] || continue
host="${DOMAINS%%,*}"
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 45 "https://$host/")
echo "$n https://$host HTTP $code"
[[ "$code" =~ ^[1-4][0-9][0-9]$ ]] || bad="$bad $n"
done
[ -z "$bad" ] || { echo "::error::not answering:$bad"; exit 1; }