#!/usr/bin/env bash # Start/stop/status the local atproto-pds used by the e2e scripts. set -euo pipefail SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) REPO_DIR=$(cd "$SCRIPT_DIR/.." && pwd) ATPROTO_CRATES_DIR=${ATPROTO_CRATES_DIR:-/Users/johnandersen777/src/atproto-crates} PINNED_REV=b80a318ccadd910c4a66cbe3bbab3212702312ed PDS_PORT=${PDS_PORT:-4800} PDS_BIND=${PDS_BIND:-127.0.0.1} PDS_HOSTNAME=${PDS_HOSTNAME:-$PDS_BIND:$PDS_PORT} PDS_URL=${PDS_URL:-http://$PDS_HOSTNAME} PDS_HEALTH_URL=${PDS_HEALTH_URL:-http://127.0.0.1:$PDS_PORT/xrpc/_health} PLC_URL=${PLC_URL:-http://127.0.0.1:2587} PDS_DID_PLC_URL=${PDS_DID_PLC_URL:-$PLC_URL} STATE_ROOT=${STATE_ROOT:-$REPO_DIR/.state} STATE_DIR=${STATE_DIR:-$STATE_ROOT/pds} PDS_DATA_DIRECTORY=${PDS_DATA_DIRECTORY:-$STATE_DIR/data} PDS_LEXICON_DIR=${PDS_LEXICON_DIR:-$REPO_DIR/lexicons} PDS_BIN=${PDS_BIN:-$ATPROTO_CRATES_DIR/target/release/pds} PDS_FEATURES=${PDS_FEATURES:-clap} PID_FILE=$STATE_DIR/pds.pid LOG_FILE=$STATE_DIR/pds.log log() { printf '%s\n' "$*" >&2; } die() { printf 'error: %s\n' "$*" >&2; exit 1; } random_hex() { od -An -N32 -tx1 /dev/urandom | tr -d ' \n' } pid_alive() { [ -f "$PID_FILE" ] || return 1 local pid pid=$(cat "$PID_FILE" 2>/dev/null) || return 1 [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null } healthy() { curl -fsS --max-time 2 -o /dev/null "$PDS_HEALTH_URL" 2>/dev/null } verify_rev() { [ -d "$ATPROTO_CRATES_DIR" ] || die "no atproto-crates checkout at $ATPROTO_CRATES_DIR (set ATPROTO_CRATES_DIR)" local actual actual=$(git -C "$ATPROTO_CRATES_DIR" rev-parse HEAD 2>/dev/null) || die "cannot read git HEAD in $ATPROTO_CRATES_DIR" if [ "$actual" != "$PINNED_REV" ]; then if [ "${ALLOW_ATPROTO_REV_MISMATCH:-0}" = 1 ]; then log "warning: $ATPROTO_CRATES_DIR HEAD is $actual, expected $PINNED_REV; continuing because ALLOW_ATPROTO_REV_MISMATCH=1" else die "$ATPROTO_CRATES_DIR HEAD is $actual, expected $PINNED_REV (set ALLOW_ATPROTO_REV_MISMATCH=1 to override)" fi fi } build_pds() { if [ -x "$PDS_BIN" ] && [ "${PDS_REBUILD:-0}" != 1 ]; then return 0 fi command -v cargo >/dev/null 2>&1 || die "cargo is required to build pds" log "building pds from $ATPROTO_CRATES_DIR (features: $PDS_FEATURES)" ( cd "$ATPROTO_CRATES_DIR" || exit 1 # sqlx macros need the stable toolchain on this host; 1.97 emits a corrupt dylib. cargo +stable build --release --bin pds --features "$PDS_FEATURES" ) [ -x "$PDS_BIN" ] || die "expected pds binary at $PDS_BIN after build" } start() { if pid_alive; then log "atproto-pds already running (pid $(cat "$PID_FILE"))" printf '%s\n' "$PDS_URL" return 0 fi verify_rev if healthy; then die "something is already serving $PDS_HEALTH_URL (port $PDS_PORT is in use)" fi build_pds mkdir -p "$PDS_DATA_DIRECTORY" "$STATE_DIR" : >"$LOG_FILE" local jwt_secret admin_password jwt_secret=$(random_hex) admin_password=$(random_hex) log "starting atproto-pds on $PDS_URL (data $PDS_DATA_DIRECTORY, log $LOG_FILE)" ( cd "$ATPROTO_CRATES_DIR" || exit 1 exec env \ PDS_PORT="$PDS_PORT" \ PDS_BIND="$PDS_BIND" \ PDS_DATA_DIRECTORY="$PDS_DATA_DIRECTORY" \ PDS_DID_PLC_URL="$PDS_DID_PLC_URL" \ PDS_SERVICE_DID=did:web:localhost \ PDS_HOSTNAME="$PDS_HOSTNAME" \ PDS_ALLOW_DEV_DEFAULTS=true \ PDS_INVITE_REQUIRED=false \ PDS_JWT_SECRET="$jwt_secret" \ PDS_ADMIN_PASSWORD="$admin_password" \ PDS_LEXICON_DIR="$PDS_LEXICON_DIR" \ PDS_RATE_LIMIT_BYPASS_IPS=127.0.0.1 \ nohup "$PDS_BIN" ) >>"$LOG_FILE" 2>&1 & local pid=$! printf '%s\n' "$pid" >"$PID_FILE" local i for i in $(seq 1 480); do if healthy; then log "atproto-pds ready (pid $pid, log $LOG_FILE)" printf '%s\n' "$PDS_URL" return 0 fi if ! kill -0 "$pid" 2>/dev/null; then rm -f "$PID_FILE" log "atproto-pds exited during startup; last log lines:" tail -n 40 "$LOG_FILE" >&2 || true die "startup failed" fi sleep 0.25 done kill -TERM "$pid" 2>/dev/null || true rm -f "$PID_FILE" die "timed out after 120s waiting for $PDS_HEALTH_URL (see $LOG_FILE)" } stop() { if ! pid_alive; then rm -f "$PID_FILE" log "atproto-pds is not running" return 0 fi local pid i pid=$(cat "$PID_FILE") kill -TERM "$pid" 2>/dev/null || true for i in $(seq 1 120); do kill -0 "$pid" 2>/dev/null || break sleep 0.25 done if kill -0 "$pid" 2>/dev/null; then log "atproto-pds did not exit after SIGTERM; sending SIGKILL" kill -KILL "$pid" 2>/dev/null || true fi wait "$pid" 2>/dev/null || true rm -f "$PID_FILE" log "stopped atproto-pds pid $pid" } status() { if pid_alive; then local health=unhealthy if healthy; then health=healthy; fi printf 'running pid=%s url=%s health=%s data=%s\n' \ "$(cat "$PID_FILE")" "$PDS_URL" "$health" "$PDS_DATA_DIRECTORY" return 0 fi printf 'not running (state dir %s)\n' "$STATE_DIR" return 1 } usage() { printf 'usage: %s start|stop|status\n' "$0" >&2 exit 2 } [ $# -eq 1 ] || usage case "$1" in start) start ;; stop) stop ;; status) status ;; *) usage ;; esac