fix: use OpenShell sandbox exec for MCP sync
This commit is contained in:
@@ -1,26 +1,31 @@
|
||||
import { spawn } from "node:child_process"
|
||||
import { HOST_PATH } from "./hostCommands"
|
||||
|
||||
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"
|
||||
import { OPENSHELL_BIN, hostCommandEnv } from "./hostCommands"
|
||||
|
||||
function shellQuote(value: string) {
|
||||
return `'${value.replace(/'/g, `'\\''`)}'`
|
||||
}
|
||||
|
||||
function runDockerKubectl(args: string[], input?: Buffer | string) {
|
||||
function runSandboxShell(sandboxName: string, script: string, input?: Buffer | 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()
|
||||
})
|
||||
@@ -38,10 +43,7 @@ export async function writeSandboxFilePrivileged(
|
||||
`chmod ${shellQuote(mode)} ${shellQuote(targetPath)}`,
|
||||
`chown root:root ${shellQuote(targetPath)} 2>/dev/null || true`,
|
||||
].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 ${targetPath}`)
|
||||
return {
|
||||
sandboxName,
|
||||
@@ -71,9 +73,7 @@ export async function repairOpenClawExecApprovalsFile(sandboxName: string) {
|
||||
`chown sandbox:sandbox /sandbox/.openclaw/exec-approvals.json 2>/dev/null || chown 998:998 /sandbox/.openclaw/exec-approvals.json`,
|
||||
`chmod 0600 /sandbox/.openclaw/exec-approvals.json`,
|
||||
].join(" && ")
|
||||
const result = await runDockerKubectl(
|
||||
["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script],
|
||||
)
|
||||
const result = await runSandboxShell(sandboxName, script)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw exec approvals file")
|
||||
return {
|
||||
sandboxName,
|
||||
@@ -104,9 +104,7 @@ export async function stabilizeOpenClawGatewayConfig(sandboxName: string) {
|
||||
`openclaw gateway stop >/dev/null 2>&1 || true`,
|
||||
`if command -v gosu >/dev/null 2>&1; then nohup gosu sandbox openclaw gateway run >/sandbox/.openclaw-data/logs/gateway.log 2>&1 & else nohup openclaw gateway run >/sandbox/.openclaw-data/logs/gateway.log 2>&1 & fi`,
|
||||
].join(" && ")
|
||||
const result = await runDockerKubectl(
|
||||
["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script],
|
||||
)
|
||||
const result = await runSandboxShell(sandboxName, script)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "failed to stabilize OpenClaw gateway config")
|
||||
return {
|
||||
sandboxName,
|
||||
@@ -146,9 +144,7 @@ export async function repairOpenClawWorkspacePermissions(sandboxName: string) {
|
||||
`chown sandbox:sandbox /sandbox/.openclaw /sandbox/.openclaw/exec-approvals.json 2>/dev/null || chown 998:998 /sandbox/.openclaw /sandbox/.openclaw/exec-approvals.json`,
|
||||
`chmod 0755 /sandbox/.openclaw && chmod 0600 /sandbox/.openclaw/exec-approvals.json`,
|
||||
].join(" && ")
|
||||
const result = await runDockerKubectl(
|
||||
["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script],
|
||||
)
|
||||
const result = await runSandboxShell(sandboxName, script)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw mutable paths")
|
||||
return {
|
||||
sandboxName,
|
||||
|
||||
Reference in New Issue
Block a user