Files
otunes/docker/entrypoint.sh
T
joshuafhigginsandClaude Opus 5.5 Verified 86235a11c4 feat: playlist cover artwork
Playlists draw a 2x2 of their first albums automatically. Owners can pick
from random grids, reshuffle, or upload an image on web and mobile.
Covers are rendered once into Convex storage and redrawn only when the
playlist's albums change. Web and iPad sidebars show playlist covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 18:11:59 -05:00

141 lines
4.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=/usr/local/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
as_convex=(setpriv --reuid=convex --regid=convex --init-groups env HOME=/home/convex)
convex_cli() {
(
cd "$FUNCTIONS_DIR"
unset CONVEX_DEPLOYMENT CONVEX_DEPLOY_KEY
CONVEX_SELF_HOSTED_URL="$BACKEND_URL" \
CONVEX_SELF_HOSTED_ADMIN_KEY="$admin_key" \
"${as_convex[@]}" "$NODE" node_modules/convex/bin/main.js "$@"
)
}
# Only the convex user may read the database and instance secret. Volumes from
# images that ran the backend as root are still owned by root, and
# read_credentials.sh generates a new instance secret if it cannot read the old one.
mkdir -p "$CONVEX_DATA_DIR"
find "$CONVEX_DATA_DIR" \( ! -user convex -o ! -group convex \) \
-exec chown -h convex:convex {} +
chmod 700 "$CONVEX_DATA_DIR"
# The web server writes HLS streams here and a Convex cron deletes stale ones.
mkdir -p /tmp/otunes
chown otunes:otunes /tmp/otunes
chmod -R g+rwX /tmp/otunes
chmod g+s,o-rwx /tmp/otunes
log "Starting Convex backend"
(cd /convex && umask 077 && exec "${as_convex[@]}" ./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 && umask 077 && "${as_convex[@]}" ./generate_admin_key.sh)
log "Deploying Convex functions"
convex_cli deploy --typecheck disable --codegen disable
log "Running data migrations"
convex_cli run migrations:migrateUsers
convex_cli run migrations:migratePlaylists
# `env get` exits 0 with no output when the variable is unset. Any other failure
# must not be read as "no keys": regenerating them signs every user out.
if ! jwks=$(convex_cli env get JWKS); then
log "Could not read Convex Auth signing keys"
stop
exit 1
fi
if [ -z "$jwks" ]; then
log "Generating Convex Auth signing keys"
"$NODE" /opt/otunes/generate-auth-keys.mjs | convex_cli env set --force
fi
unset admin_key jwks
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"
# Lets the convex group member delete stale HLS streams.
umask 0002
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"