All checks were successful
Docker image / publish (push) Successful in 59s
Update the shadcn preset and visual Compose editor. Add deployment previews, history, attention notices, and cleanup safeguards. Use the supplied icon in the app header and authentication screens.
76 lines
2.8 KiB
TypeScript
76 lines
2.8 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { promisify } from "node:util";
|
|
import { ValidationError } from "./util-server";
|
|
|
|
const exec = promisify(execFile);
|
|
const idPattern = /^[0-9a-f]{64}$/;
|
|
|
|
export function containerId(id: unknown): string {
|
|
if (typeof id !== "string" || !idPattern.test(id)) {
|
|
throw new ValidationError("containerInvalidId");
|
|
}
|
|
return id;
|
|
}
|
|
|
|
export function docker(args: string[], timeout = 15000, maxBuffer = 4 * 1024 * 1024) {
|
|
return exec("docker", args, { timeout,
|
|
maxBuffer,
|
|
encoding: "utf8",
|
|
windowsHide: true });
|
|
}
|
|
|
|
interface ContainerRow {
|
|
id: string;
|
|
name: string;
|
|
image: string;
|
|
state: string;
|
|
project: string;
|
|
service: string;
|
|
ports: string;
|
|
status: string;
|
|
}
|
|
|
|
const listFields = [
|
|
"\"id\":{{json .ID}}",
|
|
"\"name\":{{json .Names}}",
|
|
"\"image\":{{json .Image}}",
|
|
"\"state\":{{json .State}}",
|
|
"\"project\":{{json (.Label \"com.docker.compose.project\")}}",
|
|
"\"service\":{{json (.Label \"com.docker.compose.service\")}}",
|
|
"\"ports\":{{json .Ports}}",
|
|
"\"status\":{{json .Status}}",
|
|
];
|
|
|
|
const detailFields = [
|
|
"\"name\":{{json .Name}}",
|
|
"\"imageId\":{{json .Image}}",
|
|
"\"created\":{{json .Created}}",
|
|
"\"started\":{{json .State.StartedAt}}",
|
|
"\"finished\":{{json .State.FinishedAt}}",
|
|
"\"state\":{{json .State.Status}}",
|
|
"\"health\":{{with index .State \"Health\"}}{{json .Status}}{{else}}\"\"{{end}}",
|
|
"\"exitCode\":{{json .State.ExitCode}}",
|
|
"\"restartCount\":{{json .RestartCount}}",
|
|
"\"restartPolicy\":{{json .HostConfig.RestartPolicy.Name}}",
|
|
"\"platform\":{{json .Platform}}",
|
|
"\"memoryBytes\":{{json .HostConfig.Memory}}",
|
|
"\"nanoCpus\":{{json .HostConfig.NanoCpus}}",
|
|
"\"mounts\":[{{range $i, $mount := .Mounts}}{{if $i}},{{end}}" +
|
|
"{\"type\":{{json $mount.Type}},\"name\":{{json (index $mount \"Name\")}},\"target\":{{json $mount.Destination}},\"writable\":{{json $mount.RW}}}{{end}}]",
|
|
"\"networks\":{ {{$sep := \"\"}}{{range $name, $network := index .NetworkSettings \"Networks\"}}{{$sep}}{{json $name}}:" +
|
|
"{\"ipv4\":{{json (index $network \"IPAddress\")}},\"ipv6\":{{json (index $network \"GlobalIPv6Address\")}}}{{$sep = \",\"}}{{end}} }",
|
|
];
|
|
|
|
export class ContainerManagement {
|
|
async list(): Promise<ContainerRow[]> {
|
|
const { stdout } = await docker([ "container", "ls", "--all", "--no-trunc", "--format", `{${listFields.join(",")}}` ]);
|
|
return stdout.trim().split(/\r?\n/).filter(Boolean).map(row => JSON.parse(row) as ContainerRow);
|
|
}
|
|
|
|
async details(rawId: unknown) {
|
|
const id = containerId(rawId);
|
|
const { stdout } = await docker([ "container", "inspect", "--format", `{${detailFields.join(",")}}`, "--", id ]);
|
|
return JSON.parse(stdout);
|
|
}
|
|
}
|