Files
loki/scripts/hetzner/launch.sh.tmpl
CatoandClaude 9ad520a6cd fix(ops): an empty app .env line no longer erases a fleet key (#1488)
Every app template ships `RESEND_API_KEY=`. launch.sh sourced
/opt/fleet/env and then the app's .env, so that blank line overrode the
fleet's mail key with nothing, and password recovery stayed hidden on
diplodoctor, solon and heidi while the fleet held the key.

Now, at runtime (launch.sh.tmpl) and at build (load-build-env.sh):
- an empty app line keeps the fleet value;
- a non-empty app value still wins;
- FLEET_KEYS_OFF="KEY ..." in an app's .env switches named fleet keys
  off, the explicit replacement for "blank means off".

No app documents a blank line as its way to switch a fleet key off: the
empty lines come from the fleet and loki app templates, and aoz-begleitung
and hirnli, whose examples say "leave empty", describe the fleet key as
their production transport.

test-fleet-env-merge.sh runs the rendered template and the build loader
against one fixture, with a negative control for the old order.


Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-31 19:43:49 +01:00

54 lines
3.3 KiB
Cheetah

#!/usr/bin/env bash
# Generated onto the box by sync-infra.sh. __PORT__ is substituted per app.
# This template is the ONE definition of the launch script — do not paste a
# second copy into deploy.sh or patch the generated file after the fact.
set -euo pipefail
# `pwd -P`, not `pwd`: /opt/<name>/app is a SYMLINK into releases/, and find(1)
# does not descend a logical symlink path. With a plain `pwd` the server.js
# search below returns nothing and the app crash-loops on every deploy.
#
# This has now cost two outages. vitareba hit it on its first release-deploy
# (2026-07-17) and the fix was applied to deploy.sh — which sed-patched the
# already-generated file — instead of to the generator that keeps emitting the
# broken version. sbb-fundbuero hit the identical failure on 2026-09-08 the
# moment sync-infra.sh ran without a subsequent deploy. Fixing the output of a
# generator does not fix the generator.
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
# The fleet's shared keys first (AI vendors, web search, the shared mail
# account; scripts/hetzner/fleet-env.sh), then the app's own .env, so an app
# that holds its own key for any of them keeps it. An EMPTY line in the app's
# file does not erase a fleet key: the app templates ship `RESEND_API_KEY=`, and
# that blank kept password-recovery mail off on diplodoctor, solon and heidi
# while the fleet held the key (2026-10-31). To switch fleet keys off for one
# app, name them in its .env: FLEET_KEYS_OFF="RESEND_API_KEY GROQ_API_KEY".
# load-build-env.sh does the same for the build; test-fleet-env-merge.sh runs both.
__fleet_keys=""
if [ -f /opt/fleet/env ]; then
set -a; source /opt/fleet/env; set +a
for __fk in $(sed -nE 's/^[[:space:]]*(export[[:space:]]+)?([A-Za-z_][A-Za-z0-9_]*)=.*/\2/p' /opt/fleet/env); do
printf -v "__fleet_$__fk" '%s' "${!__fk-}"; __fleet_keys="$__fleet_keys $__fk"
done
fi
if [ -f "$HERE/.env" ]; then set -a; source "$HERE/.env"; set +a; fi
for __fk in $__fleet_keys; do
__fv="__fleet_$__fk"; [ -n "${!__fk-}" ] || export "$__fk=${!__fv}"; unset "$__fv"
done
for __fk in ${FLEET_KEYS_OFF-}; do unset "$__fk"; done
unset __fleet_keys __fk __fv
# HOSTNAME defaults to loopback; an app sets BIND_HOST in its .env (sourced
# just above, so it wins) to bind elsewhere. Solon needed BIND_HOST=0.0.0.0
# (2026-09-25, ~30 min of 500s): Next 16 standalone plus any middleware rewrite
# (next-intl as-needed: /hire -> /en/hire) behind Caddy's X-Forwarded-Proto:
# https failed every rewritten page with "Failed to proxy
# https://localhost:4018/en ... EPROTO". Next builds the rewrite URL with host
# `localhost` but compares it to an origin built from HOSTNAME, and it
# normalises 127.0.0.1 -> localhost, so the app cannot fix this in its own
# code. 0.0.0.0 is safe ONLY because ufw on the box allows 22/80/443 alone:
# the app ports are not reachable publicly (verified for :4004 and :4018).
# Open an app port in ufw and this override becomes a public bind.
export NODE_ENV=production PORT=__PORT__ HOSTNAME="${BIND_HOST:-127.0.0.1}"
SERVER_JS=$(find "$HERE" -maxdepth 4 -name server.js -not -path "*node_modules*" | head -1)
if [ -n "$SERVER_JS" ]; then exec /usr/bin/node "$SERVER_JS"; fi
# Non-Next services define their own entrypoint via START_CMD in .env
exec bash -c "${START_CMD:?no server.js found and START_CMD unset}"