Changeset 5462bab in Klonkt


Ignore:
Timestamp:
07/31/2026 01:52:04 PM (6 weeks ago)
Author:
Robin <roboburr@…>
Branches:
main
Children:
f434294
Parents:
ccaa530
Message:

Fix 500 op de Uiterlijk-pagina na een halve update (boiert.eu)

Bart kreeg een 500 op /admin/sites/<slug>/edit. Oorzaak was een gemengde
staat: zijn klonkt-update was gegenereerd voor de oude single-service-layout
en herstart nog klonkt.service, de unit die de migratie juist heeft
uitgezet. De code op schijf werd dus wel bijgewerkt, maar het draaiende
proces nooit herstart. De oude route rendert dan het nieuwe template, dat
een variabele verwacht die de oude route niet meegeeft: ReferenceError, en
Express cachet het gecompileerde template tot de volgende herstart.

Drie lagen gefixt:

  1. Het aliasveld degradeert bij een ontbrekende variabele naar leeg in plaats van de hele pagina mee te nemen (typeof-guard, zelfde patroon als auth-register). Een deploy-moment mag nooit een 500 opleveren.
  2. De nieuw-site-form gaf dezelfde ReferenceError ook met volledig nieuwe code: de /new-route rendert hetzelfde template maar gaf apAliases niet mee. Lokaal gereproduceerd en bevestigd gefixt (beide pagina's 200).
  3. De wortel: een gedeeld script dat /usr/local/bin/klonkt-update herschrijft voor de actuele layout. De migratie draait het voortaan zelf, install.sh genereert de updater er ook mee (kan nooit meer uiteenlopen), en al gemigreerde servers repareren het met een los commando.

Changed files:
src/routes/admin-sites.js

  • /new geeft apAliases mee aan het template

src/views/pages/admin-site-edit.ejs

  • typeof-guard op apAliases met uitleg waarom

scripts/klonkt-migrate-data.sh

  • herschrijft de updater na de unit-omschakeling; waarschuwt als het script in een oudere checkout ontbreekt

scripts/install.sh

  • inline updater-generatie vervangen door het gedeelde script

deploy/MULTI-INSTANCE.md

  • reparatie-instructie voor servers die voor deze fix zijn gemigreerd

New file:
scripts/klonkt-refresh-updater.sh

  • idempotent; detecteert de branch uit de checkout; herstart alle klonkt@<slug>-instances, of klonkt.service als er geen zijn

-robo
Co-Authored-By: Claude Fable 5 <noreply@…>

Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • deploy/MULTI-INSTANCE.md

    rccaa530 r5462bab  
    133133```
    134134
     135**Migrated before August 2026?** Then your `klonkt-update` still restarts the
     136retired `klonkt.service`: the code updates but the running process never
     137follows, and after the next update the site can 500 on pages whose template
     138and route no longer match. Repair it once:
     139
     140```bash
     141sudo systemctl restart klonkt@<slug>                        # load the current code now
     142sudo bash /opt/klonkt/scripts/klonkt-refresh-updater.sh     # fix the updater for good
     143```
     144
     145The migration script does this by itself nowadays.
     146
    135147That pulls the code once into `/opt/klonkt`, reinstalls dependencies only when
    136148`package-lock.json` changed, and restarts every instance it finds under
  • scripts/install.sh

    rccaa530 r5462bab  
    305305
    306306log "Update command 'klonkt-update'…"
    307 cat > /usr/local/bin/klonkt-update <<EOF
    308 #!/usr/bin/env bash
    309 set -euo pipefail
    310 D="${KLONKT_DIR}"
    311 B=\$(runuser -u ${KLONKT_USER} -- git -C "\$D" rev-parse HEAD 2>/dev/null || true)
    312 runuser -u ${KLONKT_USER} -- git -C "\$D" fetch --depth 1 origin ${KLONKT_BRANCH}
    313 runuser -u ${KLONKT_USER} -- git -C "\$D" checkout -qf -B ${KLONKT_BRANCH} FETCH_HEAD
    314 A=\$(runuser -u ${KLONKT_USER} -- git -C "\$D" rev-parse HEAD)
    315 if [ "\$B" = "\$A" ]; then
    316   echo "Klonkt is already up to date (\$A) — nothing to do."
    317   exit 0
    318 fi
    319 if ! runuser -u ${KLONKT_USER} -- git -C "\$D" diff --quiet "\$B" "\$A" -- package-lock.json 2>/dev/null; then
    320   runuser -u ${KLONKT_USER} -- env HOME="\$D" bash -c "cd '\$D' && npm ci --omit=dev"
    321 fi
    322 # Restart every instance. Each directory under the data root with an .env is one
    323 # instance sharing this checkout. An install that has not been split yet has no
    324 # such directories and still runs the single klonkt.service.
    325 N=0
    326 for d in ${KLONKT_DATA_ROOT}/*/; do
    327   [ -f "\$d/.env" ] || continue
    328   s=\$(basename "\$d")
    329   systemctl restart "klonkt@\$s" && N=\$((N+1))
    330 done
    331 if [ "\$N" = 0 ]; then
    332   systemctl restart klonkt
    333   echo "Klonkt updated (\$A) + restarted."
    334 else
    335   echo "Klonkt updated (\$A) + restarted \$N instance(s)."
    336 fi
    337 EOF
    338 chmod +x /usr/local/bin/klonkt-update
     307# Generated by the shared script so an install and a later layout migration
     308# can never drift apart on what the updater restarts.
     309KLONKT_DIR="$KLONKT_DIR" KLONKT_USER="$KLONKT_USER" \
     310KLONKT_DATA_ROOT="$KLONKT_DATA_ROOT" KLONKT_BRANCH="$KLONKT_BRANCH" \
     311  bash "$KLONKT_DIR/scripts/klonkt-refresh-updater.sh"
    339312ok "klonkt-update"
    340313
  • scripts/klonkt-migrate-data.sh

    rccaa530 r5462bab  
    135135run "systemctl enable --now 'klonkt@$SLUG'"
    136136
     137step "Rewriting klonkt-update for the new layout"
     138# The installer generated an updater that restarts klonkt.service — which we
     139# just retired. Left alone it would keep updating the code while never
     140# restarting the real process: half old, half new, and a 500 with no obvious
     141# cause. Rewrite it so it restarts every klonkt@<slug> instead.
     142if [ -f "$KLONKT_DIR/scripts/klonkt-refresh-updater.sh" ]; then
     143  run "KLONKT_DIR='$KLONKT_DIR' KLONKT_USER='$KLONKT_USER' KLONKT_DATA_ROOT='$DATA_ROOT' bash '$KLONKT_DIR/scripts/klonkt-refresh-updater.sh'"
     144else
     145  say "WARNING: scripts/klonkt-refresh-updater.sh missing in this checkout."
     146  say "         Update the code and run it once by hand, or every klonkt-update"
     147  say "         from now on will update code WITHOUT restarting the process."
     148fi
     149
    137150step "Verifying"
    138151if [ "$DRY" = 1 ]; then
  • src/routes/admin-sites.js

    rccaa530 r5462bab  
    211211    platforms: listPlatforms(),
    212212    parsedLinks: [],
     213    apAliases: '',
    213214    error: null,
    214215  });
  • src/views/pages/admin-site-edit.ejs

    rccaa530 r5462bab  
    217217      <p class="form-hint"><%= t('asite.aliases_hint') %></p>
    218218      <label>
    219         <textarea name="ap_aliases" rows="3" placeholder="@oud@mastodon.social&#10;https://andere-klonkt.example/ap/users/naam"><%= apAliases %></textarea>
     219<%# typeof-guard on purpose: during a deploy an old route can render this new
     220    template for one request (view cache fills on first hit). A missing local
     221    must degrade to an empty field, not take the whole page down with a 500. %>
     222        <textarea name="ap_aliases" rows="3" placeholder="@oud@mastodon.social&#10;https://andere-klonkt.example/ap/users/naam"><%= typeof apAliases !== 'undefined' ? apAliases : '' %></textarea>
    220223      </label>
    221224    </fieldset>
Note: See TracChangeset for help on using the changeset viewer.