feat: add bounded operator terminal command runner
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { dockerExecInOpenShell, OPENSHELL_NAMESPACE, probeSandboxShell, validateSandboxId } from '../../../../lib/openshellHost'
|
||||
|
||||
interface PodSummary {
|
||||
containers: string[]
|
||||
images: string[]
|
||||
runningStatuses: string[]
|
||||
phase?: string
|
||||
readyCondition?: string
|
||||
}
|
||||
|
||||
const EMPTY_POD_SUMMARY: PodSummary = {
|
||||
containers: [],
|
||||
images: [],
|
||||
runningStatuses: [],
|
||||
}
|
||||
|
||||
async function inspectPod(sandboxId: string): Promise<PodSummary> {
|
||||
const result = await dockerExecInOpenShell(
|
||||
`kubectl -n ${OPENSHELL_NAMESPACE} get pod ${sandboxId} -o json`
|
||||
)
|
||||
|
||||
if (result.exitCode !== 0) {
|
||||
const failureText = `${result.stderr}\n${result.stdout}`.toLowerCase()
|
||||
if (failureText.includes('(notfound)') || failureText.includes('not found')) {
|
||||
return EMPTY_POD_SUMMARY
|
||||
}
|
||||
throw new Error(result.stderr.trim() || result.stdout.trim() || `kubectl get pod failed with exit code ${result.exitCode}`)
|
||||
}
|
||||
|
||||
if (!result.stdout.trim()) {
|
||||
throw new Error('kubectl get pod returned empty stdout')
|
||||
}
|
||||
|
||||
const pod = JSON.parse(result.stdout)
|
||||
return {
|
||||
containers: Array.isArray(pod?.spec?.containers) ? pod.spec.containers.map((c: any) => c.name) : [],
|
||||
images: Array.isArray(pod?.spec?.containers) ? pod.spec.containers.map((c: any) => c.image) : [],
|
||||
runningStatuses: Array.isArray(pod?.status?.containerStatuses) ? pod.status.containerStatuses.map((c: any) => c.name) : [],
|
||||
phase: pod?.status?.phase,
|
||||
readyCondition: Array.isArray(pod?.status?.conditions)
|
||||
? pod.status.conditions.find((c: any) => c.type === 'Ready')?.status
|
||||
: undefined,
|
||||
}
|
||||
}
|
||||
|
||||
function buildAttachContract(sandboxId: string) {
|
||||
const alias = `openshell-${sandboxId}`
|
||||
return {
|
||||
alias,
|
||||
aliasCommand: `ssh ${alias}`,
|
||||
fallbackCommand: `ssh -i ~/.ssh/id_ed25519_openclaw_labmac ${alias}`,
|
||||
loginShellCommand: `env PATH=/opt/homebrew/bin:$PATH zsh -l -c 'ssh ${alias}'`,
|
||||
shellHint: 'If a non-interactive shell misses Homebrew tooling, retry from a login shell or prepend PATH=/opt/homebrew/bin:$PATH before ssh.',
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const sandboxId = validateSandboxId(searchParams.get('sandboxId') || '')
|
||||
|
||||
const attach = buildAttachContract(sandboxId)
|
||||
const pod = await inspectPod(sandboxId)
|
||||
|
||||
let shellProbe: {
|
||||
ok: boolean
|
||||
output?: string
|
||||
stderr?: string
|
||||
error?: string
|
||||
} = { ok: false }
|
||||
|
||||
const podExists = Boolean(pod.containers.length || pod.images.length || pod.phase)
|
||||
|
||||
if (podExists) {
|
||||
try {
|
||||
const probe = await probeSandboxShell(sandboxId)
|
||||
shellProbe = {
|
||||
ok: true,
|
||||
output: probe.stdout.trim(),
|
||||
stderr: probe.stderr.trim(),
|
||||
}
|
||||
} catch (error) {
|
||||
shellProbe = {
|
||||
ok: false,
|
||||
error: error instanceof Error ? error.message : 'Shell probe failed',
|
||||
}
|
||||
}
|
||||
} else {
|
||||
shellProbe = {
|
||||
ok: false,
|
||||
error: `Sandbox pod ${sandboxId} was not found in ${OPENSHELL_NAMESPACE}.`,
|
||||
}
|
||||
}
|
||||
|
||||
const podReady = pod.readyCondition === 'True' || pod.phase === 'Running'
|
||||
const sshReachable = shellProbe.ok
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
sandboxId,
|
||||
podExists,
|
||||
podReady,
|
||||
sshReachable,
|
||||
degraded: podExists && !sshReachable,
|
||||
attach,
|
||||
pod,
|
||||
shellProbe,
|
||||
note: !podExists
|
||||
? `Sandbox pod ${sandboxId} is not present in ${OPENSHELL_NAMESPACE}.`
|
||||
: sshReachable
|
||||
? 'Pod introspection and bounded shell probe both succeeded.'
|
||||
: 'Pod introspection succeeded, but bounded shell probe did not. Treat this as degraded dashboard reachability until direct SSH is confirmed.',
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to build readiness contract'
|
||||
const status = message === 'sandboxId is required'
|
||||
|| message.includes('sandboxId must be a valid lowercase DNS-style identifier')
|
||||
? 400
|
||||
: 500
|
||||
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
podExists: false,
|
||||
podReady: false,
|
||||
sshReachable: false,
|
||||
degraded: false,
|
||||
error: message,
|
||||
}, { status })
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { DEFAULT_EXEC_TIMEOUT_MS, MAX_COMMAND_LENGTH, runSandboxCommand, validateSandboxId, validateTerminalCommand } from '../../../lib/openshellHost'
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url)
|
||||
@@ -8,7 +9,63 @@ export async function GET(request: Request) {
|
||||
ok: true,
|
||||
sandboxId,
|
||||
attached: false,
|
||||
note: 'Embedded terminal transport is not live yet. This route is the integration point for attaching to an OpenShell terminal session.',
|
||||
suggestedCommand: `openshell sandbox exec ${sandboxId} -- /bin/sh`
|
||||
interactive: false,
|
||||
mode: 'bounded-command-runner',
|
||||
note: 'Embedded PTY transport is still not live. This route now supports bounded one-shot command execution via POST while interactive terminal attach remains pending.',
|
||||
suggestedCommand: `openshell sandbox exec ${sandboxId} -- /bin/sh`,
|
||||
limits: {
|
||||
timeoutMs: DEFAULT_EXEC_TIMEOUT_MS,
|
||||
maxCommandLength: MAX_COMMAND_LENGTH,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const sandboxInput = typeof body?.sandboxId === 'string' ? body.sandboxId : ''
|
||||
const commandInput = typeof body?.command === 'string' ? body.command : ''
|
||||
|
||||
const sandboxId = validateSandboxId(sandboxInput)
|
||||
const command = validateTerminalCommand(commandInput)
|
||||
const result = await runSandboxCommand(sandboxId, command)
|
||||
|
||||
const ok = result.exitCode === 0
|
||||
|
||||
return NextResponse.json({
|
||||
ok,
|
||||
sandboxId,
|
||||
interactive: false,
|
||||
mode: 'bounded-command-runner',
|
||||
executionMode: result.mode,
|
||||
command,
|
||||
exitCode: result.exitCode,
|
||||
stdout: result.stdout.trim(),
|
||||
stderr: result.stderr.trim(),
|
||||
note: ok
|
||||
? result.mode === 'shell'
|
||||
? 'Bounded sandbox command execution succeeded. Interactive PTY transport is still pending.'
|
||||
: 'Sandbox reachability fallback succeeded, but command execution still requires a shell-capable image.'
|
||||
: `Bounded sandbox command exited non-zero (${result.exitCode}). Interactive PTY transport is still pending, but stderr/stdout below reflect the real sandbox response.`,
|
||||
limits: {
|
||||
timeoutMs: DEFAULT_EXEC_TIMEOUT_MS,
|
||||
maxCommandLength: MAX_COMMAND_LENGTH,
|
||||
},
|
||||
}, { status: ok ? 200 : 502 })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to run sandbox command'
|
||||
const status = message === 'sandboxId is required'
|
||||
|| message === 'command is required'
|
||||
|| message.includes('sandboxId must be a valid lowercase DNS-style identifier')
|
||||
|| message.includes('command exceeds')
|
||||
? 400
|
||||
: 500
|
||||
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
interactive: false,
|
||||
mode: 'bounded-command-runner',
|
||||
error: message,
|
||||
}, { status })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user