Launch per-sandbox OpenClaw dashboards
This commit is contained in:
@@ -10,6 +10,9 @@ export type OpenClawInstanceRecord = {
|
||||
const DEFAULT_OPENCLAW_DASHBOARD_URL = 'http://127.0.0.1:18789/'
|
||||
const DEFAULT_TERMINAL_SERVER_URL = process.env.TERMINAL_SERVER_URL || 'http://127.0.0.1:3011'
|
||||
const DEFAULT_INSTANCE_ID = 'default'
|
||||
const SANDBOX_INSTANCE_PREFIX = 'sandbox'
|
||||
const SANDBOX_DASHBOARD_PORT_BASE = Number.parseInt(process.env.OPENCLAW_SANDBOX_DASHBOARD_PORT_BASE || '19000', 10)
|
||||
const SANDBOX_DASHBOARD_PORT_RANGE = 2000
|
||||
|
||||
const DEFAULT_INSTANCE: OpenClawInstanceRecord = {
|
||||
id: DEFAULT_INSTANCE_ID,
|
||||
@@ -72,9 +75,9 @@ function parseRegistryFromEnv(): OpenClawInstanceRecord[] {
|
||||
}
|
||||
|
||||
const OPENCLAW_INSTANCE_REGISTRY = parseRegistryFromEnv()
|
||||
const DEFAULT_SANDBOX_INSTANCE_MAP = process.env.MY_ASSISTANT_OPENCLAW_INSTANCE_ID?.trim()
|
||||
const DEFAULT_SANDBOX_INSTANCE_MAP: Record<string, string> = process.env.MY_ASSISTANT_OPENCLAW_INSTANCE_ID?.trim()
|
||||
? { 'my-assistant': process.env.MY_ASSISTANT_OPENCLAW_INSTANCE_ID.trim() }
|
||||
: { 'my-assistant': DEFAULT_INSTANCE_ID }
|
||||
: {}
|
||||
|
||||
function parseSandboxInstanceMapFromEnv(): Record<string, string> {
|
||||
const raw = process.env.OPENCLAW_SANDBOX_INSTANCE_MAP_JSON?.trim()
|
||||
@@ -105,6 +108,54 @@ function parseSandboxInstanceMapFromEnv(): Record<string, string> {
|
||||
|
||||
const OPENCLAW_SANDBOX_INSTANCE_MAP = parseSandboxInstanceMapFromEnv()
|
||||
|
||||
function normalizeSandboxId(value?: string | null) {
|
||||
if (typeof value !== 'string') return ''
|
||||
return value.trim()
|
||||
}
|
||||
|
||||
function hashSandboxId(value: string) {
|
||||
let hash = 0
|
||||
for (let index = 0; index < value.length; index += 1) {
|
||||
hash = ((hash << 5) - hash + value.charCodeAt(index)) | 0
|
||||
}
|
||||
return Math.abs(hash)
|
||||
}
|
||||
|
||||
export function getOpenClawDashboardPortForSandbox(sandboxId?: string | null) {
|
||||
const normalized = normalizeSandboxId(sandboxId)
|
||||
if (!normalized) return null
|
||||
return SANDBOX_DASHBOARD_PORT_BASE + (hashSandboxId(normalized) % SANDBOX_DASHBOARD_PORT_RANGE)
|
||||
}
|
||||
|
||||
export function buildSandboxOpenClawInstanceId(sandboxId?: string | null) {
|
||||
const normalized = normalizeSandboxId(sandboxId)
|
||||
const port = getOpenClawDashboardPortForSandbox(normalized)
|
||||
if (!normalized || !port) return null
|
||||
return `${SANDBOX_INSTANCE_PREFIX}-${port}-${normalized}`
|
||||
}
|
||||
|
||||
function parseSandboxOpenClawInstanceId(instanceId: string) {
|
||||
const match = instanceId.match(/^sandbox-(\d+)-(.+)$/)
|
||||
if (!match) return null
|
||||
const port = Number.parseInt(match[1], 10)
|
||||
const sandboxId = match[2]
|
||||
if (!Number.isFinite(port) || !sandboxId) return null
|
||||
return { port, sandboxId }
|
||||
}
|
||||
|
||||
function resolveSandboxOpenClawInstance(instanceId: string): OpenClawInstanceRecord | null {
|
||||
const parsed = parseSandboxOpenClawInstanceId(instanceId)
|
||||
if (!parsed) return null
|
||||
return {
|
||||
id: instanceId,
|
||||
label: `OpenClaw for ${parsed.sandboxId}`,
|
||||
dashboardUrl: `http://127.0.0.1:${parsed.port}/`,
|
||||
terminalServerUrl: DEFAULT_TERMINAL_SERVER_URL,
|
||||
loopbackOnly: true,
|
||||
default: false,
|
||||
}
|
||||
}
|
||||
|
||||
export function listOpenClawInstances() {
|
||||
return OPENCLAW_INSTANCE_REGISTRY.map((entry) => ({ ...entry }))
|
||||
}
|
||||
@@ -116,13 +167,15 @@ export function getDefaultOpenClawInstance() {
|
||||
export function resolveOpenClawInstance(instanceId?: string | null) {
|
||||
const requested = typeof instanceId === 'string' ? instanceId.trim() : ''
|
||||
if (!requested) return getDefaultOpenClawInstance()
|
||||
const sandboxInstance = resolveSandboxOpenClawInstance(requested)
|
||||
if (sandboxInstance) return sandboxInstance
|
||||
return OPENCLAW_INSTANCE_REGISTRY.find((entry) => entry.id === requested) || getDefaultOpenClawInstance()
|
||||
}
|
||||
|
||||
export function getOpenClawInstanceIdForSandbox(sandboxId?: string | null) {
|
||||
const requestedSandboxId = typeof sandboxId === 'string' ? sandboxId.trim() : ''
|
||||
const requestedSandboxId = normalizeSandboxId(sandboxId)
|
||||
if (!requestedSandboxId) return null
|
||||
return OPENCLAW_SANDBOX_INSTANCE_MAP[requestedSandboxId] || null
|
||||
return OPENCLAW_SANDBOX_INSTANCE_MAP[requestedSandboxId] || buildSandboxOpenClawInstanceId(requestedSandboxId)
|
||||
}
|
||||
|
||||
export function resolveOpenClawInstanceForSandbox(sandboxId?: string | null) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { execFile, spawn } from "node:child_process"
|
||||
import { existsSync } from "node:fs"
|
||||
import { promisify } from "node:util"
|
||||
import { getDefaultOpenClawInstance, resolveOpenClawInstance } from "./openclawInstances"
|
||||
import { getDefaultOpenClawInstance, getOpenClawDashboardPortForSandbox, resolveOpenClawInstance } from "./openclawInstances"
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
|
||||
@@ -30,6 +30,7 @@ const OPENCLAW_BIN =
|
||||
OPENCLAW_BIN_CANDIDATES.find((candidate) => existsSync(candidate)) ?? OPENCLAW_BIN_CANDIDATES[0] ?? "openclaw"
|
||||
const OPENSHELL_GATEWAY = process.env.OPENSHELL_GATEWAY || "openshell"
|
||||
const OPENSHELL_NAMESPACE = "agent-sandbox-system"
|
||||
const SANDBOX_DASHBOARD_REMOTE_PORT = Number.parseInt(process.env.OPENCLAW_SANDBOX_DASHBOARD_REMOTE_PORT || "18789", 10)
|
||||
|
||||
export type HostTelemetry = {
|
||||
cpu: number
|
||||
@@ -324,6 +325,8 @@ export async function resolveOpenClawDashboardBootstrap(instanceId?: string | nu
|
||||
async function ensureOpenClawDashboardListener(instanceId: string, port: number) {
|
||||
const instance = resolveOpenClawInstance(instanceId)
|
||||
const defaultInstance = getDefaultOpenClawInstance()
|
||||
const sandboxInstance = resolveSandboxInstanceId(instance.id)
|
||||
if (sandboxInstance) return ensureSandboxOpenClawDashboardTunnel(sandboxInstance.sandboxId)
|
||||
if (instance.id !== defaultInstance.id) return inspectListeningPort(port)
|
||||
|
||||
const initial = await inspectListeningPort(port)
|
||||
@@ -339,6 +342,79 @@ async function ensureOpenClawDashboardListener(instanceId: string, port: number)
|
||||
return waitForListeningPort(port)
|
||||
}
|
||||
|
||||
function resolveSandboxInstanceId(instanceId: string) {
|
||||
const match = instanceId.match(/^sandbox-(\d+)-(.+)$/)
|
||||
if (!match) return null
|
||||
const port = Number.parseInt(match[1], 10)
|
||||
const sandboxId = match[2]
|
||||
if (!Number.isFinite(port) || !sandboxId) return null
|
||||
return { port, sandboxId }
|
||||
}
|
||||
|
||||
function buildSandboxSshArgs(sandboxName: string, extraArgs: string[]) {
|
||||
return [
|
||||
"-o", "BatchMode=yes",
|
||||
"-o", "StrictHostKeyChecking=no",
|
||||
"-o", "UserKnownHostsFile=/dev/null",
|
||||
"-o", "GlobalKnownHostsFile=/dev/null",
|
||||
"-o", "LogLevel=ERROR",
|
||||
"-o", `ProxyCommand=${OPENSHELL_BIN} ssh-proxy --gateway-name nemoclaw --name ${sandboxName}`,
|
||||
`sandbox@openshell-${sandboxName}`,
|
||||
...extraArgs,
|
||||
]
|
||||
}
|
||||
|
||||
async function execSandboxSsh(sandboxName: string, command: string, timeoutMs = 10000) {
|
||||
return execFileAsync("ssh", buildSandboxSshArgs(sandboxName, [command]), {
|
||||
timeout: timeoutMs,
|
||||
maxBuffer: 2 * 1024 * 1024,
|
||||
env: buildOpenClawEnv(),
|
||||
})
|
||||
}
|
||||
|
||||
async function ensureRemoteSandboxOpenClawDashboard(sandboxName: string) {
|
||||
const command = [
|
||||
`curl -fsS --max-time 2 http://127.0.0.1:${SANDBOX_DASHBOARD_REMOTE_PORT}/ >/dev/null 2>&1`,
|
||||
"||",
|
||||
`(nohup /usr/local/bin/openclaw gateway run --allow-unconfigured --bind loopback --port ${SANDBOX_DASHBOARD_REMOTE_PORT} >/tmp/gateway.log 2>&1 &)`
|
||||
].join(" ")
|
||||
|
||||
await execSandboxSsh(sandboxName, command).catch(() => null)
|
||||
|
||||
for (let attempt = 0; attempt < 16; attempt += 1) {
|
||||
try {
|
||||
await execSandboxSsh(sandboxName, `curl -fsS --max-time 2 http://127.0.0.1:${SANDBOX_DASHBOARD_REMOTE_PORT}/ >/dev/null`, 5000)
|
||||
return true
|
||||
} catch {
|
||||
await sleep(500)
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
async function ensureSandboxOpenClawDashboardTunnel(sandboxName: string) {
|
||||
const port = getOpenClawDashboardPortForSandbox(sandboxName)
|
||||
if (!port) return inspectListeningPort(SANDBOX_DASHBOARD_REMOTE_PORT)
|
||||
|
||||
const initial = await inspectListeningPort(port)
|
||||
if (initial.listenerPresent) return initial
|
||||
|
||||
await ensureRemoteSandboxOpenClawDashboard(sandboxName)
|
||||
|
||||
const child = spawn("ssh", buildSandboxSshArgs(sandboxName, [
|
||||
"-N",
|
||||
"-L", `127.0.0.1:${port}:127.0.0.1:${SANDBOX_DASHBOARD_REMOTE_PORT}`,
|
||||
]), {
|
||||
detached: true,
|
||||
env: buildOpenClawEnv(),
|
||||
stdio: "ignore",
|
||||
})
|
||||
child.unref()
|
||||
|
||||
return waitForListeningPort(port, 8000)
|
||||
}
|
||||
|
||||
export async function probeOpenClawDashboard(instanceId?: string | null): Promise<DashboardProbe> {
|
||||
const instance = resolveOpenClawInstance(instanceId)
|
||||
const target = new URL(instance.dashboardUrl)
|
||||
|
||||
Reference in New Issue
Block a user