#!/usr/bin/env bash # Starts a lore-forge deployment: lore-forge, then the pinned loreserver configured to trust it. # # scripts/start.sh # # Everything the deployment keeps lives in run/ (git-ignored; set LORE_FORGE_RUN_DIR to move it): # # run/deployment.env settings, written with local defaults on the first run; edit it # run/secret.key the key that encrypts token signing keys; back it up apart from the data # run/forge-data/ lore-forge's database # run/lore-config/ the loreserver configuration this script generates on every start # run/lore-store/ loreserver's repository storage # run/logs/ both processes' logs # run/downloads/ desktop client archives offered at /download (scripts/build-desktop-dist.sh) # # Needs Rust and protoc to build lore-forge, and curl and tar to fetch Lore. Ctrl-C stops both. set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" RUN="${LORE_FORGE_RUN_DIR:-$ROOT/run}" ENV_FILE="$RUN/deployment.env" say() { echo "start: $*" >&2; } die() { say "$*"; exit 1; } mkdir -p "$RUN/logs" if [ ! -f "$ENV_FILE" ]; then cat >"$ENV_FILE" <<'EOF' # lore-forge deployment settings, read by scripts/start.sh. # The defaults run everything on this machine for trying lore-forge out. # lore-forge's canonical public URL. It is the token issuer that loreserver trusts and Lore's # user directory, so it must not change once people have logged in. Behind a reverse proxy, # this is the proxy's https URL; the proxy must speak HTTP/2 to lore-forge so gRPC passes. PUBLIC_URL=http://localhost:8080 # The address lore-forge listens on. LISTEN=127.0.0.1:8080 # The URL identifying the loreserver deployment: the tokens' audience. Its host must be the # host Lore clients connect to. LORE_URL=https://localhost # How people address the Lore server from the CLI, shown on the forge's pages. lore:// skips # certificate checks and sends tokens over plaintext gRPC, so use it only on this machine; # with a certificate below, use lores://:41337. LORE_REMOTE=lore://localhost:41337 # TLS certificate and key for loreserver's QUIC and gRPC endpoints. Empty: QUIC uses a # self-signed certificate made on every start and gRPC is plaintext, fine only locally. For a # real deployment on Linux, see deploy/README.md instead of this script. LORE_TLS_CERT= LORE_TLS_KEY= # 1 when a proxy in front ends TLS on TCP 41337 and forwards gRPC to loreserver over a private # link: the certificate above then serves QUIC only, and gRPC stays plaintext behind the proxy. LORE_GRPC_TLS_AT_PROXY= EOF say "wrote $ENV_FILE with local defaults" fi # shellcheck source=/dev/null . "$ENV_FILE" : "${PUBLIC_URL:?set PUBLIC_URL in $ENV_FILE}" "${LISTEN:?}" "${LORE_URL:?}" "${LORE_REMOTE:?}" if [ ! -s "$RUN/secret.key" ]; then say "building lore-forge to generate the secret key" cargo build --quiet --release --manifest-path "$ROOT/Cargo.toml" -p lore-forge (umask 077 && "$ROOT/target/release/lore-forge" generate-secret-key >"$RUN/secret.key") say "generated $RUN/secret.key; back it up somewhere other than run/" fi say "building lore-forge" cargo build --quiet --release --manifest-path "$ROOT/Cargo.toml" -p lore-forge FORGE="$ROOT/target/release/lore-forge" LORE_BIN_DIR="$("$ROOT/scripts/fetch-lore.sh")" export LORE_FORGE_SECRET_KEY="$(cat "$RUN/secret.key")" export LORE_FORGE_PUBLIC_URL="$PUBLIC_URL" export LORE_FORGE_LORE_URL="$LORE_URL" export LORE_FORGE_DATA_DIR="$RUN/forge-data" export LORE_FORGE_LORE_REMOTE="$LORE_REMOTE" # Desktop client archives for /download, from scripts/build-desktop-dist.sh. export LORE_FORGE_DOWNLOADS_DIR="$RUN/downloads" TLS_ARGS=() if [ -n "${LORE_TLS_CERT:-}" ]; then TLS_ARGS=(--tls-cert "$LORE_TLS_CERT" --tls-key "${LORE_TLS_KEY:?set LORE_TLS_KEY beside LORE_TLS_CERT}") if [ "${LORE_GRPC_TLS_AT_PROXY:-}" = 1 ]; then # A proxy ends gRPC's TLS, so loreserver's gRPC stays plaintext and lore-forge reads it # here, at the default http://127.0.0.1:41337. TLS_ARGS+=(--grpc-tls-at-proxy) else # With a certificate gRPC is TLS too, reached by the name the certificate is for. REMOTE_HOST="${LORE_REMOTE#*://}" export LORE_FORGE_LORE_GRPC_URL="https://${REMOTE_HOST%%/*}" fi fi FORGE_PID="" LORE_PID="" stop() { [ -n "$LORE_PID" ] && kill "$LORE_PID" 2>/dev/null || true [ -n "$FORGE_PID" ] && kill "$FORGE_PID" 2>/dev/null || true wait 2>/dev/null || true } trap stop EXIT trap 'exit 130' INT TERM say "starting lore-forge at $PUBLIC_URL (logs: run/logs/lore-forge.log)" "$FORGE" serve --listen "$LISTEN" >>"$RUN/logs/lore-forge.log" 2>&1 & FORGE_PID=$! for _ in $(seq 1 100); do curl -fsS "http://$LISTEN/.well-known/openid-configuration" >/dev/null 2>&1 && break kill -0 "$FORGE_PID" 2>/dev/null || { tail -20 "$RUN/logs/lore-forge.log" >&2; die "lore-forge exited"; } sleep 0.2 done curl -fsS "http://$LISTEN/.well-known/openid-configuration" >/dev/null 2>&1 || die "lore-forge did not answer on $LISTEN" # loreserver fetches lore-forge's keys at startup, so its configuration is written and it is # started only once lore-forge answers. mkdir -p "$RUN/lore-config" "$FORGE" loreserver-config --store "$RUN/lore-store" ${TLS_ARGS[@]+"${TLS_ARGS[@]}"} \ --output "$RUN/lore-config/forge.toml" say "starting loreserver $("$LORE_BIN_DIR/loreserver" --version | awk '{print $2}') (logs: run/logs/loreserver.log)" "$LORE_BIN_DIR/loreserver" --config "$RUN/lore-config" --env forge >>"$RUN/logs/loreserver.log" 2>&1 & LORE_PID=$! for _ in $(seq 1 150); do curl -fsS http://127.0.0.1:41339/health_check >/dev/null 2>&1 && break kill -0 "$LORE_PID" 2>/dev/null || { tail -20 "$RUN/logs/loreserver.log" >&2; die "loreserver exited"; } sleep 0.2 done curl -fsS http://127.0.0.1:41339/health_check >/dev/null 2>&1 || die "loreserver did not answer its health check" cat >&2 </ Ctrl-C stops both. EOF while kill -0 "$FORGE_PID" 2>/dev/null && kill -0 "$LORE_PID" 2>/dev/null; do sleep 1 done kill -0 "$FORGE_PID" 2>/dev/null || say "lore-forge stopped; see run/logs/lore-forge.log" kill -0 "$LORE_PID" 2>/dev/null || say "loreserver stopped; see run/logs/loreserver.log" exit 1