Files
otunes/docker/entrypoint.sh
T

116 lines
3.4 KiB
Bash

#!/usr/bin/env bash
# Runs the Convex backend and the oTunes web server in one container.
#
# Every secret is generated on first boot and kept in /convex/data:
# - the Convex instance secret (by the upstream read_credentials.sh)
# - the Convex Auth signing keys (stored as Convex environment variables)
# The admin key is derived from the instance secret on each boot, used to deploy
# the bundled functions, and never exported to the web server.
set -euo pipefail
NODE=/opt/node/bin/node
CONVEX_DATA_DIR=/convex/data
FUNCTIONS_DIR=/opt/otunes/convex/packages/convex
BACKEND_URL=http://127.0.0.1:3210
: "${CONVEX_CLOUD_ORIGIN:=http://127.0.0.1:3210}"
: "${CONVEX_SITE_ORIGIN:=http://127.0.0.1:3211}"
: "${APPLICATION_MAX_CONCURRENT_MUTATIONS:=16}"
: "${APPLICATION_MAX_CONCURRENT_NODE_ACTIONS:=16}"
: "${APPLICATION_MAX_CONCURRENT_QUERIES:=16}"
: "${APPLICATION_MAX_CONCURRENT_V8_ACTIONS:=16}"
: "${DISABLE_BEACON:=true}"
: "${DISABLE_METRICS_ENDPOINT:=true}"
: "${DOCUMENT_RETENTION_DELAY:=172800}"
: "${DO_NOT_REQUIRE_SSL:=true}"
: "${REDACT_LOGS_TO_CLIENT:=true}"
: "${RUST_LOG:=info}"
export CONVEX_CLOUD_ORIGIN CONVEX_SITE_ORIGIN \
APPLICATION_MAX_CONCURRENT_MUTATIONS APPLICATION_MAX_CONCURRENT_NODE_ACTIONS \
APPLICATION_MAX_CONCURRENT_QUERIES APPLICATION_MAX_CONCURRENT_V8_ACTIONS \
DISABLE_BEACON DISABLE_METRICS_ENDPOINT DOCUMENT_RETENTION_DELAY \
DO_NOT_REQUIRE_SSL REDACT_LOGS_TO_CLIENT RUST_LOG
backend_pid=""
web_pid=""
log() {
echo "[otunes] $*"
}
stop() {
trap - TERM INT
if [ -n "$web_pid" ]; then kill -TERM "$web_pid" 2>/dev/null || true; fi
if [ -n "$backend_pid" ]; then kill -INT "$backend_pid" 2>/dev/null || true; fi
wait || true
}
trap 'stop; exit 0' TERM INT
convex_cli() {
(
cd "$FUNCTIONS_DIR"
unset CONVEX_DEPLOYMENT CONVEX_DEPLOY_KEY
CONVEX_SELF_HOSTED_URL="$BACKEND_URL" \
CONVEX_SELF_HOSTED_ADMIN_KEY="$admin_key" \
"$NODE" node_modules/convex/bin/main.js "$@"
)
}
# Only root (the backend) may read the database and instance secret.
mkdir -p "$CONVEX_DATA_DIR"
chmod 700 "$CONVEX_DATA_DIR"
log "Starting Convex backend"
(cd /convex && umask 077 && exec ./run_backend.sh) &
backend_pid=$!
for _ in $(seq 1 120); do
if curl -fsS "$BACKEND_URL/version" >/dev/null 2>&1; then
break
fi
if ! kill -0 "$backend_pid" 2>/dev/null; then
log "Convex backend exited during startup"
exit 1
fi
sleep 0.5
done
if ! curl -fsS "$BACKEND_URL/version" >/dev/null 2>&1; then
log "Convex backend did not become ready"
stop
exit 1
fi
admin_key=$(cd /convex && ./generate_admin_key.sh)
log "Deploying Convex functions"
convex_cli deploy --typecheck disable --codegen disable
if [ -z "$(convex_cli env get JWKS 2>/dev/null || true)" ]; then
log "Generating Convex Auth signing keys"
"$NODE" /opt/otunes/generate-auth-keys.mjs | convex_cli env set --force
fi
unset admin_key
log "Starting web server"
(
cd /app
export HOME=/app NODE_ENV=production PORT=3000 HOSTNAME=0.0.0.0
export CONVEX_INTERNAL_URL="$BACKEND_URL"
# Server-side Convex helpers fall back to this at runtime. The browser bundle
# is built without it, so browsers still derive their own URL.
export NEXT_PUBLIC_CONVEX_URL="$BACKEND_URL"
exec setpriv --reuid=otunes --regid=otunes --init-groups \
"$NODE" apps/web/server.js
) &
web_pid=$!
set +e
wait -n "$backend_pid" "$web_pid"
status=$?
set -e
log "A process exited with status $status, shutting down"
stop
exit "$status"