fix: use OpenShell sandbox exec for MCP sync
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
import { spawn } from "node:child_process"
|
||||
import { HOST_PATH } from "./hostCommands"
|
||||
import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
|
||||
|
||||
export const OPENSHELL_CONTROL_MCP_SERVER_NAME = "openshell-control"
|
||||
export const OPENCLAW_CONFIG_PATH = "/sandbox/.openclaw/openclaw.json"
|
||||
const OPENCLAW_CONFIG_HASH_PATH = "/sandbox/.openclaw/.config-hash"
|
||||
|
||||
const DOCKER_BIN = process.env.DOCKER_BIN || "docker"
|
||||
const OPENSHELL_CLUSTER_CONTAINER = process.env.OPENSHELL_CLUSTER_CONTAINER || "openshell-cluster-nemoclaw"
|
||||
const OPENSHELL_NAMESPACE = process.env.OPENSHELL_SANDBOX_NAMESPACE || "openshell"
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value) && typeof value === "object" && !Array.isArray(value)
|
||||
}
|
||||
@@ -17,25 +13,34 @@ function normalizeBrokerBaseUrl(value: string) {
|
||||
return value.replace(/\/+$/, "")
|
||||
}
|
||||
|
||||
function runDockerKubectl(args: string[], input?: string) {
|
||||
function runSandboxShell(sandboxName: string, script: string, input?: string, timeoutMs = 60000) {
|
||||
return new Promise<{ stdout: string; stderr: string; code: number | null }>((resolve, reject) => {
|
||||
const child = spawn(DOCKER_BIN, ["exec", ...(input ? ["-i"] : []), OPENSHELL_CLUSTER_CONTAINER, "kubectl", ...args], {
|
||||
env: { ...process.env, PATH: HOST_PATH },
|
||||
const child = spawn(OPENSHELL_BIN, ["sandbox", "exec", "-n", sandboxName, "--", "sh", "-lc", script], {
|
||||
env: hostCommandEnv({
|
||||
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
|
||||
}),
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
})
|
||||
let stdout = ""
|
||||
let stderr = ""
|
||||
const timer = setTimeout(() => child.kill("SIGTERM"), timeoutMs)
|
||||
child.stdout.on("data", (chunk) => { stdout += String(chunk) })
|
||||
child.stderr.on("data", (chunk) => { stderr += String(chunk) })
|
||||
child.on("error", reject)
|
||||
child.on("close", (code) => resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code }))
|
||||
child.on("error", (error) => {
|
||||
clearTimeout(timer)
|
||||
reject(error)
|
||||
})
|
||||
child.on("close", (code) => {
|
||||
clearTimeout(timer)
|
||||
resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code })
|
||||
})
|
||||
if (input) child.stdin.end(input)
|
||||
else child.stdin.end()
|
||||
})
|
||||
}
|
||||
|
||||
async function readOpenClawConfig(sandboxName: string) {
|
||||
const result = await runDockerKubectl(["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "cat", OPENCLAW_CONFIG_PATH])
|
||||
const result = await runSandboxShell(sandboxName, `cat ${OPENCLAW_CONFIG_PATH}`)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "Failed to read OpenClaw config")
|
||||
const parsed = JSON.parse(result.stdout)
|
||||
if (!isRecord(parsed)) throw new Error("OpenClaw config must be a JSON object")
|
||||
@@ -46,22 +51,19 @@ async function writeOpenClawConfig(sandboxName: string, config: Record<string, u
|
||||
const payload = `${JSON.stringify(config, null, 2)}\n`
|
||||
const script = [
|
||||
`cat > ${OPENCLAW_CONFIG_PATH}`,
|
||||
`chmod 444 ${OPENCLAW_CONFIG_PATH}`,
|
||||
`chown root:root ${OPENCLAW_CONFIG_PATH}`,
|
||||
`chmod 0660 ${OPENCLAW_CONFIG_PATH}`,
|
||||
`chown sandbox:sandbox ${OPENCLAW_CONFIG_PATH} 2>/dev/null || chown 998:998 ${OPENCLAW_CONFIG_PATH}`,
|
||||
`sha256sum ${OPENCLAW_CONFIG_PATH} > ${OPENCLAW_CONFIG_HASH_PATH}`,
|
||||
`chmod 444 ${OPENCLAW_CONFIG_HASH_PATH}`,
|
||||
`chown root:root ${OPENCLAW_CONFIG_HASH_PATH}`,
|
||||
`chmod 0660 ${OPENCLAW_CONFIG_HASH_PATH}`,
|
||||
`chown sandbox:sandbox ${OPENCLAW_CONFIG_HASH_PATH} 2>/dev/null || chown 998:998 ${OPENCLAW_CONFIG_HASH_PATH}`,
|
||||
].join(" && ")
|
||||
const result = await runDockerKubectl(
|
||||
["exec", "-i", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script],
|
||||
payload,
|
||||
)
|
||||
const result = await runSandboxShell(sandboxName, script, payload)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "Failed to write OpenClaw MCP config")
|
||||
}
|
||||
|
||||
async function restartOpenClawGatewayIfRunning(sandboxName: string) {
|
||||
const script = "for p in /proc/[0-9]*; do cmd=$(tr '\\0' ' ' < \"$p/cmdline\" 2>/dev/null || true); case \"$cmd\" in *'openclaw gateway'*) kill \"${p##*/}\" 2>/dev/null || true;; esac; done"
|
||||
await runDockerKubectl(["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script])
|
||||
await runSandboxShell(sandboxName, script)
|
||||
}
|
||||
|
||||
export function buildOpenClawMcpServerConfig(brokerBaseUrl: string, token: string) {
|
||||
|
||||
Reference in New Issue
Block a user