SPB Git

Repository Public

dev-up.sh 4.1 KB 114 lines shellscript

1#!/bin/bash2# Agent OS — local dev stack: real server + real worker + static console on http://localhost:8640.3#4#   scripts/dev-up.sh             build everything, create/migrate agent_os_dev, start worker + server5#   scripts/dev-up.sh --no-build  same without the build (dist/ and apps/web/out must exist)6#   scripts/dev-up.sh --worker    start ONLY a worker (after a crash / kill; refuses if one is alive)7#8# Provider keys come from ~/.config/agent-os/dev.env through AOS_ENV_FILE: the processes read the9# file themselves, this script never sources, prints or passes the values.10set -euo pipefail1112ROOT="$(cd "$(dirname "$0")/.." && pwd)"13DB_NAME="${AOS_DEV_DB:-agent_os_dev}"14DATA_DIR="${AOS_DEV_DATA_DIR:-/tmp/aos-dev-data}"15WORKSPACE_ROOT="${AOS_DEV_WORKSPACE_ROOT:-/tmp/aos-dev-workspace}"16PORT="${AOS_DEV_PORT:-8640}"17ORIGIN="http://localhost:${PORT}"18KEYS_FILE="${AOS_DEV_KEYS_FILE:-$HOME/.config/agent-os/dev.env}"19RUN_DIR="$DATA_DIR/run"20LOG_DIR="$DATA_DIR/logs"2122BUILD=123ONLY_WORKER=024for arg in "$@"; do25  case "$arg" in26    --no-build) BUILD=0 ;;27    --worker) ONLY_WORKER=1; BUILD=0 ;;28    *) echo "unknown option: $arg" >&2; exit 2 ;;29  esac30done3132alive() { # alive <name>: 0 when the recorded pid is a live process33  local f="$RUN_DIR/$1.pid" pid34  [ -f "$f" ] || return 135  pid="$(cat "$f")"36  [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null37}3839mkdir -p "$RUN_DIR" "$LOG_DIR" "$WORKSPACE_ROOT"40chmod 700 "$DATA_DIR"4142if [ ! -f "$KEYS_FILE" ]; then43  echo "missing $KEYS_FILE (0600, lines ANTHROPIC_API_KEY=… / OPENAI_API_KEY=…)" >&244  exit 145fi4647if [ "$ONLY_WORKER" = 1 ]; then48  if alive worker; then echo "a worker is already running (pid $(cat "$RUN_DIR/worker.pid"))" >&2; exit 1; fi49else50  for p in server worker; do51    if alive "$p"; then52      echo "the dev stack is already running ($p pid $(cat "$RUN_DIR/$p.pid")); stop it with scripts/dev-down.sh" >&253      exit 154    fi55  done56  if lsof -nP -iTCP:"$PORT" -sTCP:LISTEN >/dev/null 2>&1; then57    echo "port $PORT is already in use" >&258    exit 159  fi60fi6162cd "$ROOT"63if [ "$BUILD" = 1 ]; then64  echo "building…"65  pnpm -r --workspace-concurrency=1 build >"$LOG_DIR/build.log" 2>&1 || { tail -40 "$LOG_DIR/build.log" >&2; exit 1; }66fi67for f in apps/server/dist/main.js apps/worker/dist/main.js packages/kernel/dist/migrate-cli.js; do68  [ -f "$f" ] || { echo "missing $f — run without --no-build" >&2; exit 1; }69done7071export DATABASE_URL="postgres://localhost/${DB_NAME}"72export AOS_DATA_DIR="$DATA_DIR"73export AOS_WORKSPACE_ROOT="$WORKSPACE_ROOT"74export AOS_ENV_FILE="$KEYS_FILE"75export AOS_LOG_LEVEL="${AOS_LOG_LEVEL:-info}"76# Never inherit keys from the caller's shell: the only source is AOS_ENV_FILE.77unset ANTHROPIC_API_KEY OPENAI_API_KEY7879if [ "$ONLY_WORKER" = 0 ]; then80  if ! psql -h /tmp -d postgres -Atc "SELECT 1 FROM pg_database WHERE datname = '${DB_NAME}'" | grep -q 1; then81    createdb -h /tmp "$DB_NAME"82    echo "created database $DB_NAME"83  fi84  node packages/kernel/dist/migrate-cli.js >"$LOG_DIR/migrate.log" 2>&1 || { tail -20 "$LOG_DIR/migrate.log" >&2; exit 1; }85fi8687AOS_MAX_CONCURRENT_RUNS="${AOS_MAX_CONCURRENT_RUNS:-4}" nohup node apps/worker/dist/main.js >>"$LOG_DIR/worker.log" 2>&1 &88echo $! >"$RUN_DIR/worker.pid"89sleep 190if ! alive worker; then echo "the worker did not start:" >&2; tail -20 "$LOG_DIR/worker.log" >&2; exit 1; fi91echo "worker  pid $(cat "$RUN_DIR/worker.pid")  log $LOG_DIR/worker.log"92[ "$ONLY_WORKER" = 1 ] && exit 09394AOS_PORT="$PORT" AOS_HOST=127.0.0.1 AOS_PUBLIC_ORIGIN="$ORIGIN" AOS_WEB_DIR="$ROOT/apps/web/out" \95  nohup node apps/server/dist/main.js >>"$LOG_DIR/server.log" 2>&1 &96echo $! >"$RUN_DIR/server.pid"9798for _ in $(seq 1 60); do99  if curl -fsS "http://127.0.0.1:${PORT}/api/v1/health" >/dev/null 2>&1; then100    echo "server  pid $(cat "$RUN_DIR/server.pid")  log $LOG_DIR/server.log"101    echo "console $ORIGIN"102    if [ -f "$DATA_DIR/setup-token" ]; then103      echo "first run: the one-time setup token is in $DATA_DIR/setup-token (read it yourself; it is never printed)"104    fi105    exit 0106  fi107  alive server || break108  sleep 0.5109done110echo "the server did not become healthy:" >&2111tail -20 "$LOG_DIR/server.log" >&2112"$ROOT/scripts/dev-down.sh" >/dev/null 2>&1 || true113exit 1114
Files main
Menu

Theme