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

192 lines
8.3 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 PLATFORM_RUN_RELEASE_URL="${PLATFORM_RUN_RELEASE_URL:-https://scum.npc0.com/}"
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:-http://127.0.0.1:$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_SOURCE_DIR}"
export RUN_BOOTSTRAP_BIN="${RUN_BOOTSTRAP_BIN:-$RUN_BUILD_BUCKET_ROOT/bootstrap/bin/run}"
export PLATFORM_BUILDER_DOCKER_BINARY="${PLATFORM_BUILDER_DOCKER_BINARY:-docker}"
export PLATFORM_BUILDER_IMAGE="${PLATFORM_BUILDER_IMAGE:-browser-platform-distribution-builder:1.0.0}"
export RUN_SOURCE_REPOSITORY="${RUN_SOURCE_REPOSITORY:-git@git.npc0.com:admin343/run.git}"
export RUN_SOURCE_REVISION="${RUN_SOURCE_REVISION:-main}"
export PLATFORM_BUILDER_SOURCE_DIR="${PLATFORM_BUILDER_SOURCE_DIR:-$RUN_SOURCE_DIR}"
export PLATFORM_BUILDER_SOURCE_REPOSITORY="${PLATFORM_BUILDER_SOURCE_REPOSITORY:-$RUN_SOURCE_REPOSITORY}"
export PLATFORM_BUILDER_SOURCE_REVISION="${PLATFORM_BUILDER_SOURCE_REVISION:-$RUN_SOURCE_REVISION}"
export PLATFORM_BUILDER_WORKSPACE_DIR="${PLATFORM_BUILDER_WORKSPACE_DIR:-$PLATFORM_DATA_DIR/distribution-builds}"
export PLATFORM_BUILDER_CACHE_DIR="${PLATFORM_BUILDER_CACHE_DIR:-$PLATFORM_DATA_DIR/distribution-build-cache}"
export PLATFORM_BUILDER_TIMEOUT_SECONDS="${PLATFORM_BUILDER_TIMEOUT_SECONDS:-1800}"
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
local executable_key
while IFS= read -r executable_key; do
[[ -n "$executable_key" ]] || continue
local executable_target="$scope/$executable_key"
mkdir -p "$(dirname "$executable_target")"
cp "$true_binary" "$executable_target"
chmod +x "$executable_target"
done < <(node - "$scope/actions" <<'NODE'
const fs = require("fs");
const path = require("path");
const actionsDir = process.argv[2];
for (const entry of fs.readdirSync(actionsDir)) {
if (!entry.endsWith(".json")) continue;
const action = JSON.parse(fs.readFileSync(path.join(actionsDir, entry), "utf8"));
const key = action.executableKey;
if (typeof key !== "string" || key.length === 0) continue;
if (path.posix.isAbsolute(key) || key.includes("\\") || key.split("/").includes("..")) {
throw new Error(`unsafe local-debug executableKey: ${key}`);
}
console.log(key);
}
NODE
)
}
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_prepare_run_build_source() {
if [[ -e "$RUN_SOURCE_DIR" && ! -d "$RUN_SOURCE_DIR/.git" ]]; then
printf 'run source path is not a git checkout: %s\n' "$RUN_SOURCE_DIR" >&2
return 1
fi
if [[ ! -d "$RUN_SOURCE_DIR/.git" ]]; then
mkdir -p "$(dirname "$RUN_SOURCE_DIR")"
git clone --no-checkout "$RUN_SOURCE_REPOSITORY" "$RUN_SOURCE_DIR"
fi
git -C "$RUN_SOURCE_DIR" fetch --depth 1 origin "$RUN_SOURCE_REVISION"
}
local_debug_prepare_distribution_builder() {
local_debug_prepare_run_build_source
if ! command -v "$PLATFORM_BUILDER_DOCKER_BINARY" >/dev/null 2>&1; then
printf 'platform distribution builder requires %s\n' "$PLATFORM_BUILDER_DOCKER_BINARY" >&2
return 1
fi
if [[ "$PLATFORM_BUILDER_IMAGE" == "browser-platform-distribution-builder:1.0.0" ]]; then
"$PLATFORM_BUILDER_DOCKER_BINARY" build \
-t "$PLATFORM_BUILDER_IMAGE" \
"$LOCAL_DEBUG_ROOT_DIR/platform/distribution-builder"
return
fi
if ! "$PLATFORM_BUILDER_DOCKER_BINARY" image inspect "$PLATFORM_BUILDER_IMAGE" >/dev/null 2>&1; then
printf 'configured platform builder image is not present: %s\n' "$PLATFORM_BUILDER_IMAGE" >&2
return 1
fi
}
local_debug_build_bootstrap_run() {
local_debug_prepare_run_build_source
mkdir -p "$(dirname "$RUN_BOOTSTRAP_BIN")"
(
cd "$RUN_SOURCE_DIR"
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)'
}