mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 06:17:23 +01:00
* 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>
157 lines
6.8 KiB
Bash
Executable File
157 lines
6.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Behavioural test for scripts/hetzner/launch.sh.tmpl.
|
|
#
|
|
# This does NOT grep the template for "pwd -P" — a gate that asserts a string is
|
|
# present passes for a hundred wrong reasons and proves nothing about behaviour.
|
|
# It builds the real /opt layout (a release directory plus an `app` SYMLINK into
|
|
# it, which is what the box actually has) and checks that the template's own
|
|
# server.js discovery finds the file through that symlink.
|
|
#
|
|
# Mutate `pwd -P` back to `pwd` in the template and this test must go red. That
|
|
# is the whole point: the plain-`pwd` version is what crash-looped vitareba
|
|
# (2026-07-17) and sbb-fundbuero (2026-09-08), and both times it was invisible
|
|
# until an app failed to boot in production.
|
|
set -euo pipefail
|
|
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
TMPL="$HERE/launch.sh.tmpl"
|
|
fails=0
|
|
|
|
check() {
|
|
local name="$1" expected="$2" actual="$3"
|
|
if [ "$expected" = "$actual" ]; then
|
|
echo " ✓ $name"
|
|
else
|
|
echo " ✗ $name" >&2
|
|
echo " expected: $expected" >&2
|
|
echo " actual: $actual" >&2
|
|
fails=$((fails + 1))
|
|
fi
|
|
}
|
|
|
|
echo "launch.sh.tmpl: behavioural checks"
|
|
|
|
[ -f "$TMPL" ] || { echo "✗ $TMPL missing" >&2; exit 1; }
|
|
|
|
# --- Fixture: the releases layout, exactly as deploy.sh builds it on the box ---
|
|
work="$(mktemp -d)"
|
|
trap 'rm -rf "$work"' EXIT
|
|
|
|
mkdir -p "$work/releases/r1/.next" "$work/shared" "$work/releases/r1/node_modules/next/dist"
|
|
echo '// real entrypoint' > "$work/releases/r1/server.js"
|
|
# A decoy inside node_modules: the search must skip it, or a deploy would boot
|
|
# the framework's own server instead of the app's.
|
|
echo '// decoy' > "$work/releases/r1/node_modules/next/dist/server.js"
|
|
ln -s "$work/releases/r1" "$work/app"
|
|
|
|
# Render the template the way sync-infra.sh does, and install it the way the box
|
|
# has it: the real file in shared/, reached through a symlink in the release.
|
|
sed "s|__PORT__|4099|g" "$TMPL" > "$work/shared/launch.sh"
|
|
chmod +x "$work/shared/launch.sh"
|
|
ln -s "$work/shared/launch.sh" "$work/releases/r1/launch.sh"
|
|
|
|
# --- 1. Does it find server.js at all, invoked through the app symlink? ---
|
|
# Run the template with node/exec stubbed out so it reports what it WOULD run.
|
|
stub="$work/stub"; mkdir -p "$stub"
|
|
cat > "$stub/node" <<'STUB'
|
|
#!/usr/bin/env bash
|
|
echo "WOULD_RUN=$1"
|
|
echo "AI_APP_ID=${AI_APP_ID-}"
|
|
echo "AI_USAGE_DIR=${AI_USAGE_DIR-}"
|
|
STUB
|
|
chmod +x "$stub/node"
|
|
|
|
# The template hardcodes /usr/bin/node, so rewrite just that for the test.
|
|
sed 's|/usr/bin/node|node|' "$work/shared/launch.sh" > "$work/shared/launch-test.sh"
|
|
chmod +x "$work/shared/launch-test.sh"
|
|
ln -sf "$work/shared/launch-test.sh" "$work/releases/r1/launch-test.sh"
|
|
|
|
resolved="$(PATH="$stub:$PATH" bash "$work/app/launch-test.sh" 2>&1 || true)"
|
|
check "finds server.js through the app symlink" \
|
|
"WOULD_RUN=$work/releases/r1/server.js" \
|
|
"$(echo "$resolved" | grep '^WOULD_RUN=' || echo "$resolved")"
|
|
|
|
# --- 1b. The box's usage log: named after the app, only where the directory exists ---
|
|
check "names the app after its directory, for the usage log" \
|
|
"AI_APP_ID=$(basename "$work" | tr 'a-z-' 'A-Z_')" "$(grep '^AI_APP_ID=' <<<"$resolved")"
|
|
check "sets no usage directory where the box has none" \
|
|
"AI_USAGE_DIR=" "$(FLEET_USAGE_DIR="$work/none" PATH="$stub:$PATH" bash "$work/app/launch-test.sh" 2>&1 | grep '^AI_USAGE_DIR=')"
|
|
mkdir -p "$work/usage"
|
|
check "points every app at the box's usage directory when it exists" \
|
|
"AI_USAGE_DIR=$work/usage" "$(FLEET_USAGE_DIR="$work/usage" PATH="$stub:$PATH" bash "$work/app/launch-test.sh" 2>&1 | grep '^AI_USAGE_DIR=')"
|
|
printf 'AI_APP_ID=MINE\nAI_USAGE_DIR=/elsewhere\n' > "$work/releases/r1/.env"
|
|
check "an app's own .env wins for both" \
|
|
"AI_APP_ID=MINE AI_USAGE_DIR=/elsewhere" "$(FLEET_USAGE_DIR="$work/usage" PATH="$stub:$PATH" bash "$work/app/launch-test.sh" 2>&1 | grep -E '^AI_(APP_ID|USAGE_DIR)=' | tr '\n' ' ' | sed 's/ $//')"
|
|
rm -f "$work/releases/r1/.env"
|
|
|
|
# --- 2. Does it skip the node_modules decoy? ---
|
|
if grep -q 'node_modules' <<<"$resolved"; then
|
|
echo " ✗ picked a server.js inside node_modules" >&2
|
|
fails=$((fails + 1))
|
|
else
|
|
echo " ✓ skips node_modules"
|
|
fi
|
|
|
|
# --- 3. Is the port substituted? ---
|
|
check "substitutes __PORT__" "1" \
|
|
"$(grep -c 'PORT=4099' "$work/shared/launch.sh")"
|
|
check "leaves no __PORT__ placeholder" "0" \
|
|
"$(grep -c '__PORT__' "$work/shared/launch.sh" || true)"
|
|
|
|
# --- 4. The negative control: prove this test can actually fail. ---
|
|
# Build the same fixture with the pre-fix `pwd` and assert discovery BREAKS. If
|
|
# this ever passes, the test has stopped testing anything and every check above
|
|
# is decorative.
|
|
sed 's|&& pwd -P)|\&\& pwd)|; s|/usr/bin/node|node|' "$TMPL" \
|
|
| sed "s|__PORT__|4099|g" > "$work/shared/launch-broken.sh"
|
|
chmod +x "$work/shared/launch-broken.sh"
|
|
ln -sf "$work/shared/launch-broken.sh" "$work/releases/r1/launch-broken.sh"
|
|
broken="$(PATH="$stub:$PATH" bash "$work/app/launch-broken.sh" 2>&1 || true)"
|
|
|
|
if grep -q '^WOULD_RUN=' <<<"$broken"; then
|
|
echo " ✗ negative control: plain 'pwd' still found server.js —" >&2
|
|
echo " this test cannot detect the regression it exists for" >&2
|
|
fails=$((fails + 1))
|
|
else
|
|
echo " ✓ negative control: plain 'pwd' fails to find server.js"
|
|
fi
|
|
|
|
# --- 5. HOSTNAME: loopback by default, BIND_HOST in .env overrides it. ---
|
|
# Run the rendered script for real and read the HOSTNAME the server would be
|
|
# exec'd with — the stub node reports it. Solon 500'd for ~30 min (2026-09-25)
|
|
# on a hard-coded 127.0.0.1 that its .env could not override.
|
|
cat > "$stub/node" <<'STUB'
|
|
#!/usr/bin/env bash
|
|
echo "WOULD_RUN=$1"
|
|
echo "HOSTNAME_SEEN=$HOSTNAME"
|
|
STUB
|
|
chmod +x "$stub/node"
|
|
|
|
hostname_seen() { # $1 = launch script name inside the release
|
|
env -u BIND_HOST -u HOSTNAME PATH="$stub:$PATH" bash "$work/app/$1" 2>&1 \
|
|
| sed -n 's/^HOSTNAME_SEEN=//p'
|
|
}
|
|
|
|
rm -f "$work/releases/r1/.env"
|
|
check "no BIND_HOST -> HOSTNAME=127.0.0.1" "127.0.0.1" "$(hostname_seen launch-test.sh)"
|
|
|
|
# On the box release/.env is a symlink to shared/.env; mirror that.
|
|
printf 'BIND_HOST=0.0.0.0\n' > "$work/shared/.env"
|
|
ln -sf "$work/shared/.env" "$work/releases/r1/.env"
|
|
check "BIND_HOST=0.0.0.0 in .env -> HOSTNAME=0.0.0.0" "0.0.0.0" "$(hostname_seen launch-test.sh)"
|
|
|
|
# Negative control: the pre-fix hard-coded line must IGNORE the override, or
|
|
# the check above cannot tell the fix from the bug.
|
|
sed 's|HOSTNAME="${BIND_HOST:-127.0.0.1}"|HOSTNAME=127.0.0.1|; s|/usr/bin/node|node|' "$TMPL" \
|
|
| sed "s|__PORT__|4099|g" > "$work/shared/launch-hardcoded.sh"
|
|
ln -sf "$work/shared/launch-hardcoded.sh" "$work/releases/r1/launch-hardcoded.sh"
|
|
check "negative control: hard-coded HOSTNAME ignores BIND_HOST" "127.0.0.1" \
|
|
"$(hostname_seen launch-hardcoded.sh)"
|
|
rm -f "$work/releases/r1/.env" "$work/shared/.env"
|
|
|
|
if [ "$fails" -gt 0 ]; then
|
|
echo "launch.sh.tmpl: $fails check(s) FAILED" >&2
|
|
exit 1
|
|
fi
|
|
echo "launch.sh.tmpl: ok"
|