#!/usr/bin/env bash # Usage: ssh into VPS, then: sudo bash /opt/lichen/deploy/setup.sh set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" APP_USER="lichen" APP_DIR="/opt/lichen" echo "==> Updating system" apt-get update && apt-get upgrade -y apt-get install -y curl unzip git rsync podman sqlite3 echo "==> Installing Bun" if ! command -v bun &>/dev/null; then curl -fsSL https://bun.sh/install | bash cp ~/.bun/bin/bun /usr/local/bin/bun fi bun --version echo "==> Installing Caddy" if ! command -v caddy &>/dev/null; then curl -o /usr/bin/caddy "https://caddyserver.com/api/download?os=linux&arch=amd64&p=github.com%2Fcaddy-dns%2Fcloudflare" chmod +x /usr/bin/caddy fi caddy version echo "==> Installing Litestream" if ! command -v litestream &>/dev/null; then curl -fsSL "https://github.com/benbjohnson/litestream/releases/download/v0.3.13/litestream-v0.3.13-linux-amd64.deb" -o /tmp/litestream.deb dpkg -i /tmp/litestream.deb rm /tmp/litestream.deb fi litestream version echo "==> Creating app user and directories" # The lichen user is only for the staging host units now; prod runs in containers. if ! id "$APP_USER" &>/dev/null; then useradd --system --create-home --shell /usr/sbin/nologin "$APP_USER" fi mkdir -p "$APP_DIR" ENV_FILE="/etc/lichen/env" if [ ! -f "$ENV_FILE" ]; then echo "==> Creating environment file (edit CLOUDFLARE_API_TOKEN before starting)" mkdir -p /etc/lichen cat > "$ENV_FILE" <<'EOF' PUBLIC_URL=https://lichen.wiki OAUTH_PRIVATE_KEY_PATH= RELAY_URL=wss://bsky.network DB_PATH=/var/lib/lichen/lichen.db REGISTRY_DB_PATH=/var/lib/lichen/registry.db LITESTREAM_R2_ACCOUNT_ID= LITESTREAM_R2_ACCESS_KEY_ID= LITESTREAM_R2_SECRET_ACCESS_KEY= EOF chmod 600 "$ENV_FILE" fi echo "==> Bootstrapping staging instance" STAGING_APP_DIR="/opt/lichen-staging" STAGING_DATA_DIR="/var/lib/lichen-staging" mkdir -p "$STAGING_APP_DIR" "$STAGING_DATA_DIR" chown "$APP_USER:$APP_USER" "$STAGING_DATA_DIR" STAGING_KEY_PATH="$STAGING_DATA_DIR/oauth-private-key.json" if [ ! -f "$STAGING_KEY_PATH" ] && podman image exists localhost/lichen:latest; then # Generated from the image: /opt/lichen has no node_modules since prod moved to # containers, so host `bun run` cannot resolve @atcute/oauth-node-client. echo "==> Generating staging ES256 client-assertion JWK" podman run --rm localhost/lichen:latest bun run deploy/gen-oauth-jwk.ts > "$STAGING_KEY_PATH" chown "$APP_USER:$APP_USER" "$STAGING_KEY_PATH" chmod 600 "$STAGING_KEY_PATH" fi STAGING_ENV_FILE="/etc/lichen-staging/env" if [ ! -f "$STAGING_ENV_FILE" ]; then echo "==> Creating staging environment file" mkdir -p /etc/lichen-staging cat > "$STAGING_ENV_FILE" <<'EOF' PUBLIC_URL=https://staging.lichen.wiki OAUTH_PRIVATE_KEY_PATH=/var/lib/lichen-staging/oauth-private-key.json RELAY_URL=wss://bsky.network DB_PATH=/var/lib/lichen-staging/lichen.db REGISTRY_DB_PATH=/var/lib/lichen-staging/registry.db PORT=3001 EOF chmod 600 "$STAGING_ENV_FILE" fi echo "==> Installing lichen's own config files" # Prod is Quadlet. Do NOT install lichen-appview.service / lichen-firehose.service: # a host unit in /etc/systemd/system shadows the Quadlet unit of the same name, and # the box would silently run the old host process instead. Those two files are kept # in this directory as a rollback path only. mkdir -p /etc/containers/systemd cp "$SCRIPT_DIR/quadlet/lichen.network" "$SCRIPT_DIR/quadlet/lichen-data.volume" /etc/containers/systemd/ cp "$SCRIPT_DIR/quadlet/lichen-appview.container" /etc/containers/systemd/ cp "$SCRIPT_DIR/quadlet/lichen-firehose.container" /etc/containers/systemd/ cp "$SCRIPT_DIR/quadlet/lichen-litestream.container" /etc/containers/systemd/ cp "$SCRIPT_DIR/lichen-appview-staging.service" /etc/systemd/system/ cp "$SCRIPT_DIR/lichen-firehose-staging.service" /etc/systemd/system/ cp "$SCRIPT_DIR/litestream.yml" /etc/litestream.yml mkdir -p /etc/caddy/conf.d cp "$SCRIPT_DIR/Caddyfile.snippet" /etc/caddy/conf.d/lichen.caddy systemctl daemon-reload echo "==> Creating the data volume" systemctl start lichen-data-volume.service MOUNTPOINT=$(podman volume inspect systemd-lichen-data --format '{{.Mountpoint}}') KEY_PATH="$MOUNTPOINT/oauth-private-key.json" if [ ! -f "$KEY_PATH" ]; then if podman image exists localhost/lichen:latest; then echo "==> Generating ES256 client-assertion JWK into the volume" podman run --rm localhost/lichen:latest bun run deploy/gen-oauth-jwk.ts > "$KEY_PATH" chown 1000:1000 "$KEY_PATH" chmod 600 "$KEY_PATH" else echo "!! No lichen image yet — run ./deploy/deploy.sh first, then re-run this script" echo " to generate the OAuth key (the app will not start without it)." fi fi echo "" echo "==> This script does not install Caddy itself — set that up separately." echo "" echo "==> Setup complete!" echo " 1. Edit /etc/lichen/env — set OAUTH_PRIVATE_KEY_PATH=/var/lib/lichen/oauth-private-key.json" echo " (the in-container path) and the R2 keys." echo " (CLOUDFLARE_API_TOKEN goes in /etc/caddy/cloudflare.env)" echo " 2. Register a separate ATProto OAuth client for https://staging.lichen.wiki" echo " 3. Deploy code: ./deploy/deploy.sh (prod — builds the image)" echo " ./deploy/deploy.sh --target=staging" echo " 4. Re-run this script if it could not generate the OAuth key above." echo " 5. Start prod: sudo systemctl start lichen-appview lichen-firehose lichen-litestream" echo " (Quadlet handles boot start via [Install]; no 'enable' needed.)" echo " 6. Staging is on-demand — do NOT enable. Seed it from the prod volume:" echo " MP=\$(sudo podman volume inspect systemd-lichen-data --format '{{.Mountpoint}}')" echo " sudo sqlite3 \"file:\$MP/lichen.db?mode=ro\" \".backup /var/lib/lichen-staging/lichen.db\"" echo " sudo chown lichen:lichen /var/lib/lichen-staging/lichen.db" echo " sudo systemctl start lichen-appview-staging lichen-firehose-staging" echo " ...test, then:" echo " sudo systemctl stop lichen-appview-staging lichen-firehose-staging"