Files
browser/scripts/local-debug/env.sh
T

178 lines
7.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Shared configuration for the first-party local debug workspace.
set -euo pipefail
LOCAL_DEBUG_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOCAL_DEBUG_ROOT_DIR="$(cd "$LOCAL_DEBUG_SCRIPT_DIR/../.." && pwd)"
export LOCAL_DEBUG_ROOT="${LOCAL_DEBUG_ROOT:-$LOCAL_DEBUG_ROOT_DIR/.local-debug}"
export LOCAL_DEBUG_LOG_DIR="${LOCAL_DEBUG_LOG_DIR:-$LOCAL_DEBUG_ROOT/logs}"
export LOCAL_DEBUG_PID_DIR="${LOCAL_DEBUG_PID_DIR:-$LOCAL_DEBUG_ROOT/pids}"
export GOCACHE="${GOCACHE:-$LOCAL_DEBUG_ROOT/go-build-cache}"
export LOCAL_DEBUG_PLATFORM_PORT="${LOCAL_DEBUG_PLATFORM_PORT:-18080}"
export LOCAL_DEBUG_WEB_PORT="${LOCAL_DEBUG_WEB_PORT:-5173}"
export PLATFORM_ADDR="${PLATFORM_ADDR:-127.0.0.1:$LOCAL_DEBUG_PLATFORM_PORT}"
export PLATFORM_STORAGE_BACKEND="${PLATFORM_STORAGE_BACKEND:-file}"
export PLATFORM_DATA_DIR="${PLATFORM_DATA_DIR:-$LOCAL_DEBUG_ROOT/platform}"
export PLATFORM_METADATA_PATH="${PLATFORM_METADATA_PATH:-$PLATFORM_DATA_DIR/metadata.json}"
export PLATFORM_LOG_BODY_BACKEND="${PLATFORM_LOG_BODY_BACKEND:-file}"
export PLATFORM_LOG_DIR="${PLATFORM_LOG_DIR:-$PLATFORM_DATA_DIR/logs}"
export PLATFORM_ARTIFACT_DIR="${PLATFORM_ARTIFACT_DIR:-$PLATFORM_DATA_DIR/artifacts}"
export PLATFORM_BOOTSTRAP_ADMIN_EMAIL="${PLATFORM_BOOTSTRAP_ADMIN_EMAIL:-operator.local@example.test}"
export PLATFORM_BOOTSTRAP_ADMIN_PASSWORD="${PLATFORM_BOOTSTRAP_ADMIN_PASSWORD:-operator-local}"
export PLATFORM_SECRET_ENVELOPE_KEY="${PLATFORM_SECRET_ENVELOPE_KEY:-local-debug-secret-envelope-key-change-me}"
export PLATFORM_AI_PROVIDER_MODE="${PLATFORM_AI_PROVIDER_MODE:-mock}"
export RUN_SOURCE_DIR="${RUN_SOURCE_DIR:-${RUN_REPO_DIR:-$LOCAL_DEBUG_ROOT_DIR/run}}"
export RUN_REPO_DIR="$RUN_SOURCE_DIR"
export RUN_MODE="${RUN_MODE:-worker}"
export RUN_PLATFORM_URL="${RUN_PLATFORM_URL:-https://scum.npc0.com:$LOCAL_DEBUG_PLATFORM_PORT}"
export RUN_ENDPOINT_ID="${RUN_ENDPOINT_ID:-run-local-debug}"
export RUN_DISPLAY_NAME="${RUN_DISPLAY_NAME:-Local Debug Run}"
export RUN_VERSION="${RUN_VERSION:-0.1.0-local-debug}"
export RUN_REGISTRATION_TOKEN="${RUN_REGISTRATION_TOKEN:-local-debug-registration}"
export RUN_WORKSPACE_ROOT="${RUN_WORKSPACE_ROOT:-$LOCAL_DEBUG_ROOT/run/workspace}"
export RUN_BUILD_BUCKET_ROOT="${RUN_BUILD_BUCKET_ROOT:-$LOCAL_DEBUG_ROOT/run/build-buckets}"
export RUN_BUILD_SOURCE_ROOT="${RUN_BUILD_SOURCE_ROOT:-$RUN_BUILD_BUCKET_ROOT/source/current}"
export RUN_BOOTSTRAP_BIN="${RUN_BOOTSTRAP_BIN:-$RUN_BUILD_BUCKET_ROOT/bootstrap/bin/run}"
export RUN_SPOOL_ROOT="${RUN_SPOOL_ROOT:-$LOCAL_DEBUG_ROOT/run/spool}"
export RUN_MAX_JOBS="${RUN_MAX_JOBS:-1}"
export RUN_HEARTBEAT_INTERVAL_MS="${RUN_HEARTBEAT_INTERVAL_MS:-2000}"
export RUN_POLL_INTERVAL_MS="${RUN_POLL_INTERVAL_MS:-1000}"
export RUN_RETRY_BACKOFF_MS="${RUN_RETRY_BACKOFF_MS:-500}"
export PLATFORM_API_PROXY="${PLATFORM_API_PROXY:-http://127.0.0.1:$LOCAL_DEBUG_PLATFORM_PORT}"
export VITE_PLATFORM_API_BASE_URL="${VITE_PLATFORM_API_BASE_URL:-/api/v1}"
export VITE_ENABLE_LOCAL_AUTH_FALLBACK="${VITE_ENABLE_LOCAL_AUTH_FALLBACK:-false}"
local_debug_platform_url() {
printf 'http://127.0.0.1:%s' "$LOCAL_DEBUG_PLATFORM_PORT"
}
local_debug_web_url() {
printf 'http://0.0.0.0:%s' "$LOCAL_DEBUG_WEB_PORT"
}
local_debug_plugin_source_dir() {
case "$1" in
game.example)
printf '%s/plugins/examples/dev-game-plugin' "$LOCAL_DEBUG_ROOT_DIR"
;;
game.scum)
printf '%s/plugins/examples/scum-server-plugin' "$LOCAL_DEBUG_ROOT_DIR"
;;
*)
printf 'unknown local-debug plugin id: %s\n' "$1" >&2
return 1
;;
esac
}
local_debug_seed_lifecycle_scope() {
local source_dir="$1"
local scope="$2"
local true_binary="/usr/bin/true"
if [[ ! -x "$true_binary" ]]; then
printf 'required local true executable not found at %s\n' "$true_binary" >&2
return 1
fi
mkdir -p "$scope/actions" "$scope/bin"
local action
for action in install start stop restart status; do
if [[ -f "$source_dir/actions/$action.json" ]]; then
cp "$source_dir/actions/$action.json" "$scope/actions/$action.json"
fi
done
cp "$true_binary" "$scope/bin/install-server"
cp "$true_binary" "$scope/bin/game-server"
chmod +x "$scope/bin/install-server" "$scope/bin/game-server"
}
local_debug_seed_plugin_lifecycle_template() {
local plugin_id="$1"
local profile_key="${2:-run-local}"
local source_dir
source_dir="$(local_debug_plugin_source_dir "$plugin_id")"
local_debug_seed_lifecycle_scope "$source_dir" "$RUN_WORKSPACE_ROOT/plugins/$plugin_id/$profile_key"
}
local_debug_seed_server_lifecycle_workspace() {
local server_id="$1"
local plugin_id="$2"
local profile_key="${3:-run-local}"
local source_dir
source_dir="$(local_debug_plugin_source_dir "$plugin_id")"
local_debug_seed_lifecycle_scope "$source_dir" "$RUN_WORKSPACE_ROOT/$server_id"
local_debug_seed_lifecycle_scope "$source_dir" "$RUN_WORKSPACE_ROOT/instances/$server_id/$profile_key"
}
local_debug_prepare_run_lifecycle_templates() {
mkdir -p "$RUN_WORKSPACE_ROOT"
local_debug_seed_plugin_lifecycle_template game.example run-local
local_debug_seed_plugin_lifecycle_template game.scum run-local
}
local_debug_require_bucket_path() {
local target="$1"
local parent
parent="$(dirname "$target")"
mkdir -p "$parent"
local resolved_parent
resolved_parent="$(cd "$parent" && pwd -P)"
local resolved="$resolved_parent/$(basename "$target")"
case "$resolved" in
"$LOCAL_DEBUG_ROOT"/* | /private/tmp/browser-local-debug-*/* | /tmp/browser-local-debug-*/*)
return 0
;;
esac
printf 'refusing to clear non-local-debug build bucket path: %s\n' "$target" >&2
printf 'set RUN_BUILD_BUCKET_ROOT/RUN_BUILD_SOURCE_ROOT under %s or an approved browser-local-debug temp root\n' "$LOCAL_DEBUG_ROOT" >&2
return 1
}
ensure_local_run_repo() {
if [[ ! -f "$RUN_SOURCE_DIR/go.mod" ]]; then
printf 'run source repo not found at %s\n' "$RUN_SOURCE_DIR" >&2
printf 'clone git@git.npc0.com:admin343/run.git into %s/run or set RUN_SOURCE_DIR/RUN_REPO_DIR to another independent run checkout\n' "$LOCAL_DEBUG_ROOT_DIR" >&2
return 1
fi
}
local_debug_prepare_run_build_source() {
ensure_local_run_repo
local_debug_require_bucket_path "$RUN_BUILD_SOURCE_ROOT"
rm -rf "$RUN_BUILD_SOURCE_ROOT"
mkdir -p "$RUN_BUILD_SOURCE_ROOT"
rsync -a --delete \
--exclude '.git/' \
--exclude '.local-debug/' \
--exclude '.run-workspace/' \
--exclude '.tmp/' \
--exclude 'node_modules/' \
"$RUN_SOURCE_DIR"/ "$RUN_BUILD_SOURCE_ROOT"/
cat >"$RUN_BUILD_SOURCE_ROOT/.local-debug-source.json" <<EOF
{
"sourceDir": "$RUN_SOURCE_DIR",
"buildSourceRoot": "$RUN_BUILD_SOURCE_ROOT",
"createdBy": "scripts/local-debug/env.sh"
}
EOF
}
local_debug_build_bootstrap_run() {
local_debug_prepare_run_build_source
mkdir -p "$(dirname "$RUN_BOOTSTRAP_BIN")"
(
cd "$RUN_BUILD_SOURCE_ROOT"
env GOCACHE="$GOCACHE" go build -trimpath -o "$RUN_BOOTSTRAP_BIN" ./cmd/run
)
chmod +x "$RUN_BOOTSTRAP_BIN"
}
local_debug_forbidden_pattern() {
printf '%s' '(/Users/|/private/|unix://|tcp://|Bearer |sk-|password=|apiKeyRef|rawApiKey|sessionToken|run session token|direct run URL|plugin-owned transport)'
}