feat(docker): agent-client helper, Docker container list page
- lib/agent-client.ts: server-side execAgent() that calls the ops-agent directly via OPS_AGENT_URL/OPS_AGENT_SECRET and streams SSE output - lib/parse-docker.ts: pure parser for fixed-width docker ps table output - app/docker/page.tsx: server component that fetches initial container list and passes it to the client component - app/docker/_components/docker-table.tsx: client component with 5s auto-refresh via useEffect, status badge, and Link to /docker/[name] - app/docker/[name]/page.tsx: placeholder detail page (logs in Story 3) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
92d450609c
commit
90eacc963d
5 changed files with 335 additions and 0 deletions
58
lib/agent-client.ts
Normal file
58
lib/agent-client.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import 'server-only'
|
||||
|
||||
const AGENT_URL = process.env.OPS_AGENT_URL ?? 'http://127.0.0.1:3099'
|
||||
const AGENT_SECRET = process.env.OPS_AGENT_SECRET ?? ''
|
||||
|
||||
export async function execAgent(
|
||||
commandKey: string,
|
||||
args: string[] = [],
|
||||
onChunk?: (chunk: string) => void,
|
||||
): Promise<string> {
|
||||
const response = await fetch(`${AGENT_URL}/agent/v1/exec`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${AGENT_SECRET}`,
|
||||
},
|
||||
body: JSON.stringify({ command_key: commandKey, args }),
|
||||
cache: 'no-store',
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
throw new Error(`agent error ${response.status}: ${text}`)
|
||||
}
|
||||
|
||||
const reader = response.body?.getReader()
|
||||
if (!reader) throw new Error('no response body')
|
||||
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = ''
|
||||
let output = ''
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
|
||||
buffer += decoder.decode(value, { stream: true })
|
||||
const lines = buffer.split('\n')
|
||||
buffer = lines.pop() ?? ''
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.slice(5).trim()
|
||||
try {
|
||||
const parsed = JSON.parse(jsonStr) as { data?: string }
|
||||
if (parsed.data !== undefined) {
|
||||
output += parsed.data
|
||||
onChunk?.(parsed.data)
|
||||
}
|
||||
} catch {
|
||||
// skip malformed SSE data
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
40
lib/parse-docker.ts
Normal file
40
lib/parse-docker.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
export type Container = {
|
||||
id: string
|
||||
image: string
|
||||
command: string
|
||||
created: string
|
||||
status: string
|
||||
ports: string
|
||||
name: string
|
||||
}
|
||||
|
||||
// Parse the fixed-width table output of `docker ps --format table`
|
||||
export function parseDockerPs(output: string): Container[] {
|
||||
const lines = output.trim().split('\n').filter(Boolean)
|
||||
if (lines.length < 2) return []
|
||||
|
||||
const header = lines[0]
|
||||
|
||||
const COLS = ['CONTAINER ID', 'IMAGE', 'COMMAND', 'CREATED', 'STATUS', 'PORTS', 'NAMES'] as const
|
||||
const positions = COLS.map((col) => header.indexOf(col))
|
||||
|
||||
// Must find at least NAMES (last) to produce useful output
|
||||
if (positions[6] === -1) return []
|
||||
|
||||
const extract = (line: string, i: number): string => {
|
||||
const start = positions[i]
|
||||
if (start === -1) return ''
|
||||
const nextIdx = positions.slice(i + 1).find((p) => p !== -1)
|
||||
return line.slice(start, nextIdx).trim()
|
||||
}
|
||||
|
||||
return lines.slice(1).map((line) => ({
|
||||
id: extract(line, 0),
|
||||
image: extract(line, 1),
|
||||
command: extract(line, 2),
|
||||
created: extract(line, 3),
|
||||
status: extract(line, 4),
|
||||
ports: extract(line, 5),
|
||||
name: extract(line, 6),
|
||||
}))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue