Skip to content

Commit 19b749d

Browse files
committed
tidy things up a bit
1 parent f5672ab commit 19b749d

4 files changed

Lines changed: 337 additions & 313 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import { x } from "tinyexec";
2+
3+
function stringToLines(str: string): string[] {
4+
return str.split("\n").filter(Boolean);
5+
}
6+
7+
function lineToWords(line: string): string[] {
8+
return line.trim().split(/\s+/);
9+
}
10+
11+
async function getDockerNetworks(): Promise<string[]> {
12+
try {
13+
const result = await x("docker", ["network", "ls" /* , "--no-trunc" */]);
14+
return stringToLines(result.stdout);
15+
} catch (error) {
16+
console.error(error);
17+
return ["error: check additional logs for more details"];
18+
}
19+
}
20+
21+
async function getDockerContainers(): Promise<string[]> {
22+
try {
23+
const result = await x("docker", ["ps", "-a" /* , "--no-trunc" */]);
24+
return stringToLines(result.stdout);
25+
} catch (error) {
26+
console.error(error);
27+
return ["error: check additional logs for more details"];
28+
}
29+
}
30+
31+
type DockerResource = { id: string; name: string };
32+
33+
type DockerNetworkAttachment = DockerResource & {
34+
containers: string[];
35+
};
36+
37+
export async function getDockerNetworkAttachments(): Promise<DockerNetworkAttachment[]> {
38+
let attachments: DockerNetworkAttachment[] = [];
39+
let networks: DockerResource[] = [];
40+
41+
try {
42+
const result = await x("docker", [
43+
"network",
44+
"ls",
45+
"--format",
46+
'{{.ID | printf "%.12s"}} {{.Name}}',
47+
]);
48+
49+
const lines = stringToLines(result.stdout);
50+
51+
networks = lines.map((line) => {
52+
const [id, name] = lineToWords(line);
53+
return { id, name };
54+
});
55+
} catch (err) {
56+
console.error("Failed to list docker networks:", err);
57+
}
58+
59+
for (const { id, name } of networks) {
60+
try {
61+
// Get containers, one per line: id name\n
62+
const containersResult = await x("docker", [
63+
"network",
64+
"inspect",
65+
"--format",
66+
'{{range $k, $v := .Containers}}{{$k | printf "%.12s"}} {{$v.Name}}\n{{end}}',
67+
id,
68+
]);
69+
70+
const containers = stringToLines(containersResult.stdout);
71+
72+
attachments.push({ id, name, containers });
73+
} catch (err) {
74+
console.error(`Failed to inspect network ${id}:`, err);
75+
attachments.push({ id, name, containers: [] });
76+
}
77+
}
78+
79+
return attachments;
80+
}
81+
82+
type DockerContainerNetwork = DockerResource & {
83+
networks: string[];
84+
};
85+
86+
export async function getDockerContainerNetworks(): Promise<DockerContainerNetwork[]> {
87+
let results: DockerContainerNetwork[] = [];
88+
let containers: DockerResource[] = [];
89+
90+
try {
91+
const result = await x("docker", [
92+
"ps",
93+
"-a",
94+
"--format",
95+
'{{.ID | printf "%.12s"}} {{.Names}}',
96+
]);
97+
98+
const lines = stringToLines(result.stdout);
99+
100+
containers = lines.map((line) => {
101+
const [id, name] = lineToWords(line);
102+
return { id, name };
103+
});
104+
} catch (err) {
105+
console.error("Failed to list docker containers:", err);
106+
}
107+
108+
for (const { id, name } of containers) {
109+
try {
110+
const inspectResult = await x("docker", [
111+
"inspect",
112+
"--format",
113+
'{{ range $k, $v := .NetworkSettings.Networks }}{{ $k | printf "%.12s" }} {{ $v.Name }}\n{{ end }}',
114+
id,
115+
]);
116+
117+
const networks = stringToLines(inspectResult.stdout);
118+
119+
results.push({ id, name, networks });
120+
} catch (err) {
121+
console.error(`Failed to inspect container ${id}:`, err);
122+
results.push({ id, name: String(err), networks: [] });
123+
}
124+
}
125+
126+
return results;
127+
}
128+
129+
export type DockerDiagnostics = {
130+
containers?: string[];
131+
networks?: string[];
132+
containerNetworks?: DockerContainerNetwork[];
133+
networkAttachments?: DockerNetworkAttachment[];
134+
};
135+
136+
export async function getDockerDiagnostics(): Promise<DockerDiagnostics> {
137+
const [containers, networks, networkAttachments, containerNetworks] = await Promise.all([
138+
getDockerContainers(),
139+
getDockerNetworks(),
140+
getDockerNetworkAttachments(),
141+
getDockerContainerNetworks(),
142+
]);
143+
144+
return {
145+
containers,
146+
networks,
147+
containerNetworks,
148+
networkAttachments,
149+
};
150+
}

0 commit comments

Comments
 (0)