Files
loki/scripts/hetzner/gen-env.sh
CatoandClaude 4c78b0dc3b fix(previews): a preview address must answer, and is kept answering (#1195)
francesco.orangecat.ch showed ERR_SSL_PROTOCOL_ERROR: wildcard DNS sends every
label to the box, but a label with no Caddy vhost has no certificate. Found on
a phone four times.

- createPreview waits until the address answers over HTTPS and says plainly
  when it does not, instead of reporting success on hope
- previews.yml + ensure-preview.sh: `create` makes a preview as the operator on
  the box and fails unless its address answers; `heal` (every six hours)
  re-attaches and re-checks every preview folder so a lost vhost is restored
  before anyone opens the link. No access code goes through a public repo's
  Actions
- A preview with no code yet answers 503 ("switched off"): the address and
  certificate work, the code is set in the app
- set_preview_code chat tool ("set the preview code to cat"); create_website
  still insists on a code
- Tests: ensure scripts parse, fail loudly, run on a clock, carry no code


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

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-14 10:58:18 -07:00

58 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build <repo>/<app_dir>/.env.selfhost.local for an app from its captured
# env backup ($ENV_BACKUP_DIR, default ~/dev/env-backup — where the one-time
# env snapshots were captured). Strips host-runner vars, rewrites DB URL to the
# box-local Postgres, points auth/site URLs at the new domain, adds
# AUTH_TRUST_HOST for proxied Auth.js.
# Usage: gen-env.sh <app>
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
app_lookup "${1:?usage: gen-env.sh <app>}"
SRC_DIR="$REPO/$APP_DIR"
OUT="$SRC_DIR/.env.selfhost.local"
DOMAIN="${DOMAINS%%,*}"
# Source env: the captured per-app backup file.
RAW=$(mktemp); trap 'rm -f "$RAW"' EXIT
BACKUP="${ENV_BACKUP_DIR:-$HOME/dev/env-backup}/$NAME.env"
[ -f "$BACKUP" ] && cp "$BACKUP" "$RAW"
[ -s "$RAW" ] || { echo "WARN: no env backup found for $NAME at $BACKUP — writing minimal env"; : > "$RAW"; }
DB_URL=""
# `supabase:<schema>` names a schema inside the supabase-db container, not a
# host database — those apps reach it over SUPABASE_URL and have no row in
# ~/.db-credentials, so building a host DATABASE_URL for them would fail here.
case "$DB" in supabase:*) DB="-";; esac
if [ "$DB" != "-" ]; then
pass=$(box "grep '^$DB|' ~/.db-credentials | cut -d'|' -f2")
[ -z "$pass" ] && { echo "ERROR: no credentials for db $DB on box"; exit 1; }
DB_URL="postgresql://$DB:$pass@localhost:5432/$DB"
fi
{
echo "# Generated by gen-env.sh $(date +%F) — runtime env for $DOMAIN"
while IFS= read -r line; do
case "$line" in \#*|"") continue;; esac
key="${line%%=*}"
val="${line#*=}"
val="${val%\"}"; val="${val#\"}" # strip surrounding quotes
# Strip ALL literal \n / \r sequences and real CR/LF anywhere in the value —
# not just one trailing token. A prior incident corrupted secrets with
# multiple embedded \n, which broke all email + the private-zone PIN.
val="${val//\\n/}"; val="${val//\\r/}"
val="${val//$'\n'/}"; val="${val//$'\r'/}"
case "$key" in
VERCEL|VERCEL_*|NX_DAEMON|TURBO_*) continue ;;
DATABASE_URL|DIRECT_URL|POSTGRES_URL|POSTGRES_PRISMA_URL|POSTGRES_URL_NON_POOLING)
[ -n "$DB_URL" ] && val="$DB_URL" ;;
NEXTAUTH_URL|AUTH_URL|NEXT_PUBLIC_APP_URL|NEXT_PUBLIC_BASE_URL|NEXT_PUBLIC_SITE_URL)
val="https://$DOMAIN" ;;
esac
printf '%s="%s"\n' "$key" "${val//\"/\\\"}"
done < "$RAW"
[ -n "$DB_URL" ] && grep -qE '^(DATABASE_URL)=' "$RAW" || { [ -n "$DB_URL" ] && echo "DATABASE_URL=$DB_URL"; }
echo "AUTH_TRUST_HOST=true"
} > "$OUT"
chmod 600 "$OUT"
echo "wrote $OUT ($(grep -c '=' "$OUT") vars, db=$DB)"