Files
loki/scripts/hetzner/launch.sh.tmpl
CatoandClaude Fable 5.2 8b3727e464 ops(fleet-env): retire a key everywhere in one command; GEMINI_API_KEY is no longer a fleet key; ai-kit 1.26 (#1704)
* ops(fleet-env): retire a key everywhere in one command; GEMINI_API_KEY is no longer a fleet key; ai-kit 1.26

GEMINI_API_KEY sat in /opt/fleet/env as a free tier. The Google project had
billing and every app read the key: about $567 on 10-11 Nov 2026. On
2026-11-13 it was still set there, with eight apps on an ai-kit that puts
Google in the free chain.

- fleet-env.sh where KEY: every env file on the box that sets KEY, by
  fingerprint. retire KEY: its line in /opt/fleet/env and in every app env
  becomes a dated comment (the value is kept), and the app units restart one
  at a time so none keeps the value it started with.
- fleet-keys.conf drops GEMINI_API_KEY, so seed never brings it back.
- fleet-env.yml offers where and retire with a key input.
- @bitbaum/ai-kit ^1.26.0: Google out of the free chain, paid AI off unless
  an app opts in, daily caps.

test-fleet-env.sh covers where, retire (value kept, other lines untouched,
no value printed, seed does not restore) and the refusal of a non-credential.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq

* test(ops): four .env cases that need an unreadable file are not run as root

chmod 000 is how the section makes a .env unreadable, and root reads any
file. In an agent's sandbox (uid 0) the four cases could only fail, on main
too, and stopped every push that touched scripts/hetzner. They print as not
run; CI and a developer's machine run them as an ordinary user.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq

* ops(launch): every app on the box writes to one AI usage log; ai-kit 1.27.0

launch.sh.tmpl exports AI_USAGE_DIR=/opt/fleet/ai-usage where that
directory exists and AI_APP_ID from the app's directory name, unless the
app's own .env sets them. @bitbaum/ai-kit 1.27.0 then writes one line per
model call there (app, vendor, model, outcome; never content). fleet-env.yml
creates the directory. Kivvi canaries 1.27.0, so the rollout gate no longer
passes 1.26.0.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-13 11:26:30 +01:00

69 lines
4.1 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
# One usage log for the whole box (@bitbaum/ai-kit `usage-log`): each model
# call an app makes is one line under AI_USAGE_DIR, with the app's name, the
# vendor, the model and what came of it, never the content. About $567 went
# to Google on 10-11 Nov 2026 and nobody could say which app spent it. An
# app's own .env may point elsewhere or name itself; neither is set where the
# directory is missing, so a box without it runs exactly as before.
__usage_dir="${FLEET_USAGE_DIR:-/opt/fleet/ai-usage}"
if [ -z "${AI_USAGE_DIR-}" ] && [ -d "$__usage_dir" ] && [ -w "$__usage_dir" ]; then
export AI_USAGE_DIR="$__usage_dir"
fi
if [ -z "${AI_APP_ID-}" ]; then
__app="${HERE%/releases/*}"; __app="${__app%/app}"; __app="${__app##*/}"
[ -n "$__app" ] && export AI_APP_ID="$(printf '%s' "$__app" | tr 'a-z-' 'A-Z_')"
fi
unset __usage_dir __app
# 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}"