@myrialabs/clopen 0.4.34 → 0.4.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/backend/auth/permissions.ts +7 -0
- package/backend/containers/actions.ts +316 -0
- package/backend/containers/disk-usage.test.ts +68 -0
- package/backend/containers/disk-usage.ts +170 -0
- package/backend/containers/logs.ts +335 -0
- package/backend/containers/monitor.ts +410 -0
- package/backend/containers/parse.test.ts +599 -0
- package/backend/containers/parse.ts +905 -0
- package/backend/containers/port-index.test.ts +91 -0
- package/backend/containers/port-index.ts +198 -0
- package/backend/containers/prune-job.test.ts +148 -0
- package/backend/containers/prune-job.ts +129 -0
- package/backend/containers/pty-backend.ts +337 -0
- package/backend/containers/pty.ts +114 -0
- package/backend/containers/runtime.ts +218 -0
- package/backend/containers/scan.ts +293 -0
- package/backend/files/file-watcher.test.ts +105 -0
- package/backend/files/file-watcher.ts +45 -1
- package/backend/git/git-service.test.ts +33 -0
- package/backend/git/git-service.ts +7 -3
- package/backend/git/nested-repos.test.ts +292 -1
- package/backend/git/nested-repos.ts +330 -17
- package/backend/{ports → host}/runner.ts +19 -10
- package/backend/index.ts +11 -2
- package/backend/ports/attribute.ts +1 -1
- package/backend/ports/kill.ts +1 -1
- package/backend/ports/monitor.ts +1 -1
- package/backend/ports/processes.ts +1 -1
- package/backend/ports/scan-host.ts +77 -8
- package/backend/ports/scan.test.ts +1 -1
- package/backend/ports/scan.ts +1 -1
- package/backend/ports/ssh-lineage.ts +1 -1
- package/backend/preview/browser/browser-video-capture.ts +84 -31
- package/backend/ws/containers/actions.ts +156 -0
- package/backend/ws/containers/index.ts +43 -0
- package/backend/ws/containers/list.ts +146 -0
- package/backend/ws/containers/logs.ts +43 -0
- package/backend/ws/containers/tunnel.ts +101 -0
- package/backend/ws/git/log.ts +3 -1
- package/backend/ws/git/status.ts +37 -27
- package/backend/ws/index.ts +2 -0
- package/backend/ws/message-rate-limiter.test.ts +45 -0
- package/backend/ws/message-rate-limiter.ts +34 -3
- package/backend/ws/ssh/connections.ts +8 -0
- package/backend/ws/system/device-info.ts +99 -43
- package/dist/assets/{index-vW2RpdHu.js → index-B0iIhSO9.js} +1 -1
- package/dist/assets/{index-gGTMFUa8.js → index-BRJqss6F.js} +1 -1
- package/dist/assets/main-Buko4D0t.css +1 -0
- package/dist/assets/{main-ZCojSzvW.js → main-C_iGUZyL.js} +467 -449
- package/dist/index.html +2 -2
- package/dist/manifest.json +9 -2
- package/package.json +1 -1
- package/shared/types/containers/container.ts +366 -0
- package/shared/types/containers/index.ts +5 -0
- package/shared/types/host/index.ts +18 -0
- package/shared/types/ports/port.ts +47 -9
- package/shared/utils/logger.ts +2 -0
- package/shared/utils/ws-client.ts +67 -12
- package/shared/utils/ws-server.ts +64 -13
- package/dist/assets/main-CYCMEbXB.css +0 -1
|
@@ -46,6 +46,13 @@ export const ADMIN_ONLY_ROUTES = new Set([
|
|
|
46
46
|
// Port manager — anyone signed in may see what is listening, but ending a
|
|
47
47
|
// process can take down another member's work, so stopping is admin-only.
|
|
48
48
|
'ports:kill',
|
|
49
|
+
// Containers — anyone signed in may see what is running, but starting or
|
|
50
|
+
// stopping one takes down whatever it serves for everyone else. Opening a
|
|
51
|
+
// shell inside a container is gated separately, in the PtyKit authorize.
|
|
52
|
+
'containers:action',
|
|
53
|
+
'containers:remove',
|
|
54
|
+
'containers:prune',
|
|
55
|
+
'containers:prune-dismiss',
|
|
49
56
|
// Stack — binary installation is an admin-only operation.
|
|
50
57
|
'stack:status',
|
|
51
58
|
'stack:status-all',
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Containers — starting, stopping, removing, and clearing up after.
|
|
3
|
+
*
|
|
4
|
+
* Every action addresses a container or an image by its full id, and a volume
|
|
5
|
+
* or a network by its name, and every one of those is checked against a
|
|
6
|
+
* pattern before it reaches a command line. Names arrive over a socket, and an
|
|
7
|
+
* argument that begins with a dash is a flag; `assertContainerId` and
|
|
8
|
+
* `assertResourceName` are the gate that stops one becoming the other. Nothing
|
|
9
|
+
* here ever runs through `sudo`: an account that may not talk to the runtime is
|
|
10
|
+
* told so.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type {
|
|
14
|
+
ContainerAction,
|
|
15
|
+
ContainerActionResult,
|
|
16
|
+
ContainerDetail,
|
|
17
|
+
ContainerResourceKind,
|
|
18
|
+
ContainerRuntime,
|
|
19
|
+
ContainerStats,
|
|
20
|
+
PruneKind,
|
|
21
|
+
PruneOutcome
|
|
22
|
+
} from '$shared/types/containers';
|
|
23
|
+
import { CONTAINER_TIMEOUTS } from '$shared/types/containers';
|
|
24
|
+
import { containerMonitor } from './monitor';
|
|
25
|
+
import { invalidateDiskUsage } from './disk-usage';
|
|
26
|
+
import { containerArgv, detectRuntime, firstProblem, tryRun } from './runtime';
|
|
27
|
+
import { parseInspect, parsePruneOutput, parseStats } from './parse';
|
|
28
|
+
import { debug } from '$shared/utils/logger';
|
|
29
|
+
|
|
30
|
+
/** Both runtimes identify a container or an image with hex. */
|
|
31
|
+
const CONTAINER_ID = /^[a-f0-9]{12,64}$/i;
|
|
32
|
+
/** An image id may carry the digest algorithm that produced it. */
|
|
33
|
+
const IMAGE_ID = /^(sha256:)?[a-f0-9]{12,64}$/i;
|
|
34
|
+
/**
|
|
35
|
+
* The character set both runtimes allow in a volume or network name. Anchored
|
|
36
|
+
* at both ends and required to start with an alphanumeric, so nothing that
|
|
37
|
+
* could be read as a flag or a path ever gets through.
|
|
38
|
+
*/
|
|
39
|
+
const RESOURCE_NAME = /^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,127}$/;
|
|
40
|
+
|
|
41
|
+
export function isContainerId(value: string): boolean {
|
|
42
|
+
return CONTAINER_ID.test(value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function assertContainerId(value: string): string {
|
|
46
|
+
if (!isContainerId(value)) throw new Error('That is not a container id.');
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Validate whatever identifies a resource of this kind, or refuse it. */
|
|
51
|
+
export function assertResourceId(kind: ContainerResourceKind, value: string): string {
|
|
52
|
+
const ok =
|
|
53
|
+
kind === 'container'
|
|
54
|
+
? CONTAINER_ID.test(value)
|
|
55
|
+
: kind === 'image'
|
|
56
|
+
? IMAGE_ID.test(value)
|
|
57
|
+
: RESOURCE_NAME.test(value);
|
|
58
|
+
if (!ok) throw new Error(`That is not a ${kind} Clopen can address.`);
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
/** The argv for each action, which is where `remove` stops being one word. */
|
|
64
|
+
function actionArgv(action: ContainerAction, containerId: string): string[] {
|
|
65
|
+
switch (action) {
|
|
66
|
+
case 'remove':
|
|
67
|
+
return ['rm', containerId];
|
|
68
|
+
case 'force-remove':
|
|
69
|
+
// The runtime stops it first. Offered separately from `remove` so the
|
|
70
|
+
// confirmation can say that outright rather than surprising anyone.
|
|
71
|
+
return ['rm', '--force', containerId];
|
|
72
|
+
default:
|
|
73
|
+
return [action, containerId];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function runContainerAction(
|
|
78
|
+
hostId: string,
|
|
79
|
+
containerId: string,
|
|
80
|
+
action: ContainerAction
|
|
81
|
+
): Promise<ContainerActionResult> {
|
|
82
|
+
assertContainerId(containerId);
|
|
83
|
+
|
|
84
|
+
const result = await containerMonitor.withHost(hostId, async (runner, platform) => {
|
|
85
|
+
const info = await detectRuntime(hostId, runner, platform);
|
|
86
|
+
if (info.problem !== 'none' || !info.runtime) {
|
|
87
|
+
return { ok: false, error: 'This host has no container runtime available right now.' };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const outcome = await tryRun(
|
|
91
|
+
runner,
|
|
92
|
+
containerArgv(info.runtime, platform, actionArgv(action, containerId)),
|
|
93
|
+
CONTAINER_TIMEOUTS.action
|
|
94
|
+
);
|
|
95
|
+
if (outcome.code === 0) return { ok: true, error: null };
|
|
96
|
+
|
|
97
|
+
const reason = firstProblem(outcome.stderr, outcome.stdout, outcome.code);
|
|
98
|
+
debug.warn('containers', `${action} failed for ${containerId} on ${hostId}: ${reason}`);
|
|
99
|
+
return { ok: false, error: reason };
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// The table is a second or two old at most, but an action is the one moment
|
|
103
|
+
// a user expects it to be instant.
|
|
104
|
+
containerMonitor.invalidate(hostId);
|
|
105
|
+
// Starting and stopping do not move the disk figures enough to be worth
|
|
106
|
+
// re-measuring for; removing a container does.
|
|
107
|
+
if (action === 'remove' || action === 'force-remove') invalidateDiskUsage(hostId);
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The argv that removes one resource of each kind. */
|
|
112
|
+
function removeArgv(kind: ContainerResourceKind, id: string, force: boolean): string[] {
|
|
113
|
+
switch (kind) {
|
|
114
|
+
case 'container':
|
|
115
|
+
return force ? ['rm', '--force', id] : ['rm', id];
|
|
116
|
+
case 'image':
|
|
117
|
+
return force ? ['image', 'rm', '--force', id] : ['image', 'rm', id];
|
|
118
|
+
case 'volume':
|
|
119
|
+
return force ? ['volume', 'rm', '--force', id] : ['volume', 'rm', id];
|
|
120
|
+
case 'network':
|
|
121
|
+
return ['network', 'rm', id];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Remove one image, volume or network.
|
|
127
|
+
*
|
|
128
|
+
* Deliberately not forgiving: the runtime refuses to remove anything still in
|
|
129
|
+
* use, and that refusal is passed straight through rather than being retried
|
|
130
|
+
* with `--force`. A volume a running container has mounted is not a volume the
|
|
131
|
+
* user meant to delete, whatever the button said.
|
|
132
|
+
*/
|
|
133
|
+
export async function removeResource(
|
|
134
|
+
hostId: string,
|
|
135
|
+
kind: ContainerResourceKind,
|
|
136
|
+
id: string,
|
|
137
|
+
force = false
|
|
138
|
+
): Promise<ContainerActionResult> {
|
|
139
|
+
assertResourceId(kind, id);
|
|
140
|
+
|
|
141
|
+
const result = await containerMonitor.withHost(hostId, async (runner, platform) => {
|
|
142
|
+
const info = await detectRuntime(hostId, runner, platform);
|
|
143
|
+
if (info.problem !== 'none' || !info.runtime) {
|
|
144
|
+
return { ok: false, error: 'This host has no container runtime available right now.' };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const outcome = await tryRun(
|
|
148
|
+
runner,
|
|
149
|
+
containerArgv(info.runtime, platform, removeArgv(kind, id, force)),
|
|
150
|
+
CONTAINER_TIMEOUTS.action
|
|
151
|
+
);
|
|
152
|
+
if (outcome.code === 0) return { ok: true, error: null };
|
|
153
|
+
|
|
154
|
+
const reason = firstProblem(outcome.stderr, outcome.stdout, outcome.code);
|
|
155
|
+
debug.warn('containers', `could not remove ${kind} ${id} on ${hostId}: ${reason}`);
|
|
156
|
+
return { ok: false, error: reason };
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
containerMonitor.invalidate(hostId);
|
|
160
|
+
invalidateDiskUsage(hostId);
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The argv for a sweep.
|
|
166
|
+
*
|
|
167
|
+
* `volumes` is the one place the two runtimes had to be reconciled rather than
|
|
168
|
+
* passed through. Podman's `volume prune` removes every unused volume; Docker's
|
|
169
|
+
* removes only the anonymous ones unless it is given `--all`, which older
|
|
170
|
+
* Docker does not have. So Docker is asked with `--all` and falls back without
|
|
171
|
+
* it, and both runtimes end up meaning the same thing — which is what the
|
|
172
|
+
* confirmation says they mean.
|
|
173
|
+
*/
|
|
174
|
+
function pruneArgv(kind: PruneKind, runtime: ContainerRuntime): string[] {
|
|
175
|
+
switch (kind) {
|
|
176
|
+
case 'containers':
|
|
177
|
+
return ['container', 'prune', '--force'];
|
|
178
|
+
case 'dangling-images':
|
|
179
|
+
return ['image', 'prune', '--force'];
|
|
180
|
+
case 'images':
|
|
181
|
+
return ['image', 'prune', '--all', '--force'];
|
|
182
|
+
case 'volumes':
|
|
183
|
+
return runtime === 'docker'
|
|
184
|
+
? ['volume', 'prune', '--all', '--force']
|
|
185
|
+
: ['volume', 'prune', '--force'];
|
|
186
|
+
case 'networks':
|
|
187
|
+
return ['network', 'prune', '--force'];
|
|
188
|
+
case 'build-cache':
|
|
189
|
+
return ['builder', 'prune', '--force'];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** True when a command failed because the runtime is too old for a flag. */
|
|
194
|
+
function unknownFlag(stderr: string): boolean {
|
|
195
|
+
return /unknown flag|unknown shorthand|flag provided but not defined|unrecognized option/i.test(
|
|
196
|
+
stderr
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Run a set of sweeps, one after another.
|
|
202
|
+
*
|
|
203
|
+
* Sequential rather than parallel: both runtimes serialise this work behind
|
|
204
|
+
* their own lock anyway, and on an SSH host every one of these is a command on
|
|
205
|
+
* a shared connection. A failure in one kind does not stop the rest — a host
|
|
206
|
+
* with no build cache should not cost the user their volume sweep.
|
|
207
|
+
*/
|
|
208
|
+
export async function pruneResources(hostId: string, kinds: PruneKind[]): Promise<PruneOutcome[]> {
|
|
209
|
+
const outcomes = await containerMonitor.withHost(hostId, async (runner, platform) => {
|
|
210
|
+
const info = await detectRuntime(hostId, runner, platform);
|
|
211
|
+
if (info.problem !== 'none' || !info.runtime) {
|
|
212
|
+
return kinds.map((kind) => ({
|
|
213
|
+
kind,
|
|
214
|
+
ok: false,
|
|
215
|
+
removed: 0,
|
|
216
|
+
reclaimed: null,
|
|
217
|
+
error: 'This host has no container runtime available right now.'
|
|
218
|
+
}));
|
|
219
|
+
}
|
|
220
|
+
const runtime = info.runtime;
|
|
221
|
+
|
|
222
|
+
const results: PruneOutcome[] = [];
|
|
223
|
+
for (const kind of kinds) {
|
|
224
|
+
let outcome = await tryRun(
|
|
225
|
+
runner,
|
|
226
|
+
containerArgv(runtime, platform, pruneArgv(kind, runtime)),
|
|
227
|
+
CONTAINER_TIMEOUTS.prune
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
// Docker before 23 has no `volume prune --all`, and removed every unused
|
|
231
|
+
// volume without it. Dropping the flag there keeps the meaning identical.
|
|
232
|
+
if (outcome.code !== 0 && kind === 'volumes' && unknownFlag(outcome.stderr)) {
|
|
233
|
+
outcome = await tryRun(
|
|
234
|
+
runner,
|
|
235
|
+
containerArgv(runtime, platform, ['volume', 'prune', '--force']),
|
|
236
|
+
CONTAINER_TIMEOUTS.prune
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (outcome.code !== 0) {
|
|
241
|
+
const error = firstProblem(outcome.stderr, outcome.stdout, outcome.code);
|
|
242
|
+
debug.warn('containers', `prune ${kind} failed on ${hostId}: ${error}`);
|
|
243
|
+
results.push({ kind, ok: false, removed: 0, reclaimed: null, error });
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const { removed, reclaimed } = parsePruneOutput(outcome.stdout);
|
|
248
|
+
results.push({ kind, ok: true, removed, reclaimed, error: null });
|
|
249
|
+
}
|
|
250
|
+
return results;
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
containerMonitor.invalidate(hostId);
|
|
254
|
+
// A sweep is exactly when these numbers move, and showing the figures it was
|
|
255
|
+
// meant to fix would read as the sweep having done nothing.
|
|
256
|
+
invalidateDiskUsage(hostId);
|
|
257
|
+
return outcomes;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* One sample of what a container is consuming, read when asked for.
|
|
262
|
+
*
|
|
263
|
+
* Not polled and not part of the listing: `stats` samples over a window and
|
|
264
|
+
* takes a second or more even with `--no-stream`, which is fine for a question
|
|
265
|
+
* someone asked and far too expensive for a table that ticks.
|
|
266
|
+
*/
|
|
267
|
+
export async function readStats(hostId: string, containerId: string): Promise<ContainerStats | null> {
|
|
268
|
+
assertContainerId(containerId);
|
|
269
|
+
|
|
270
|
+
return containerMonitor.withHost(hostId, async (runner, platform) => {
|
|
271
|
+
const info = await detectRuntime(hostId, runner, platform);
|
|
272
|
+
if (info.problem !== 'none' || !info.runtime) return null;
|
|
273
|
+
|
|
274
|
+
const result = await tryRun(
|
|
275
|
+
runner,
|
|
276
|
+
containerArgv(info.runtime, platform, [
|
|
277
|
+
'stats',
|
|
278
|
+
'--no-stream',
|
|
279
|
+
'--format',
|
|
280
|
+
info.runtime === 'docker' ? '{{json .}}' : 'json',
|
|
281
|
+
containerId
|
|
282
|
+
]),
|
|
283
|
+
CONTAINER_TIMEOUTS.stats
|
|
284
|
+
);
|
|
285
|
+
if (result.code !== 0) {
|
|
286
|
+
debug.log('containers', `stats failed for ${containerId}:`, result.stderr);
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
return parseStats(result.stdout);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Everything `inspect` knows, read only when a detail pane asks for it. */
|
|
294
|
+
export async function inspectContainer(
|
|
295
|
+
hostId: string,
|
|
296
|
+
containerId: string
|
|
297
|
+
): Promise<ContainerDetail | null> {
|
|
298
|
+
assertContainerId(containerId);
|
|
299
|
+
|
|
300
|
+
return containerMonitor.withHost(hostId, async (runner, platform) => {
|
|
301
|
+
const info = await detectRuntime(hostId, runner, platform);
|
|
302
|
+
if (info.problem !== 'none' || !info.runtime) return null;
|
|
303
|
+
|
|
304
|
+
const result = await tryRun(
|
|
305
|
+
runner,
|
|
306
|
+
containerArgv(info.runtime, platform, ['inspect', '--format', '{{json .}}', containerId]),
|
|
307
|
+
CONTAINER_TIMEOUTS.inspect
|
|
308
|
+
);
|
|
309
|
+
if (result.code !== 0) {
|
|
310
|
+
debug.log('containers', `inspect failed for ${containerId}:`, result.stderr);
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
return parseInspect(result.stdout);
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { isDiskUsageStale } from './disk-usage';
|
|
3
|
+
import {
|
|
4
|
+
CONTAINER_TIMEOUTS,
|
|
5
|
+
DISK_USAGE_STALE_MS,
|
|
6
|
+
TRANSPORT_GRACE_MS,
|
|
7
|
+
type ContainerDiskUsage
|
|
8
|
+
} from '$shared/types/containers';
|
|
9
|
+
|
|
10
|
+
const NOW = Date.parse('2026-09-01T12:00:00.000Z');
|
|
11
|
+
|
|
12
|
+
function reading(measuredAt: string | null): ContainerDiskUsage {
|
|
13
|
+
return { rows: [], error: null, measuredAt };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe('isDiskUsageStale', () => {
|
|
17
|
+
test('a reading taken just now is still good', () => {
|
|
18
|
+
expect(isDiskUsageStale(reading(new Date(NOW - 1_000).toISOString()), NOW)).toBe(false);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('a reading goes stale once it passes the window', () => {
|
|
22
|
+
const justInside = new Date(NOW - DISK_USAGE_STALE_MS + 1_000).toISOString();
|
|
23
|
+
const justOutside = new Date(NOW - DISK_USAGE_STALE_MS - 1_000).toISOString();
|
|
24
|
+
expect(isDiskUsageStale(reading(justInside), NOW)).toBe(false);
|
|
25
|
+
expect(isDiskUsageStale(reading(justOutside), NOW)).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('having no reading at all is stale', () => {
|
|
29
|
+
expect(isDiskUsageStale(null, NOW)).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('a reading that did not say when it was taken is stale', () => {
|
|
33
|
+
// Rather than being treated as current, which would pin a number that
|
|
34
|
+
// nothing would ever replace.
|
|
35
|
+
expect(isDiskUsageStale(reading(null), NOW)).toBe(true);
|
|
36
|
+
expect(isDiskUsageStale(reading('not a date'), NOW)).toBe(true);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('a reading stamped in the future is not treated as stale', () => {
|
|
40
|
+
// A host whose clock runs ahead should not make the panel re-measure on
|
|
41
|
+
// every open — the invalidation after a prune is what keeps it honest.
|
|
42
|
+
expect(isDiskUsageStale(reading(new Date(NOW + 60_000).toISOString()), NOW)).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('command budgets', () => {
|
|
47
|
+
// The bug this guards against: the transport gave up at 30s while the backend
|
|
48
|
+
// was still allowed 300s for a prune, so a sweep that was still deleting came
|
|
49
|
+
// back as six failures and the user was invited to run it again.
|
|
50
|
+
test('every command budget leaves room for the transport to outlast it', () => {
|
|
51
|
+
for (const [name, budget] of Object.entries(CONTAINER_TIMEOUTS)) {
|
|
52
|
+
expect(budget + TRANSPORT_GRACE_MS, `${name} must outlast its command`).toBeGreaterThan(
|
|
53
|
+
budget
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('a full scan is allowed more than any single listing it runs', () => {
|
|
59
|
+
expect(CONTAINER_TIMEOUTS.scan).toBeGreaterThan(CONTAINER_TIMEOUTS.list);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('a prune is allowed the longest, because it walks what it deletes', () => {
|
|
63
|
+
const others = Object.entries(CONTAINER_TIMEOUTS)
|
|
64
|
+
.filter(([name]) => name !== 'prune')
|
|
65
|
+
.map(([, budget]) => budget);
|
|
66
|
+
expect(CONTAINER_TIMEOUTS.prune).toBeGreaterThan(Math.max(...others));
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Containers — what the host is holding, measured off the request path.
|
|
3
|
+
*
|
|
4
|
+
* `system df` is the one container command whose cost is set by how much disk
|
|
5
|
+
* the host holds rather than by how many containers it runs. The daemon walks
|
|
6
|
+
* every image layer and stats every file in every volume to answer it. On a
|
|
7
|
+
* developer machine with a few hundred volumes on a virtualised filesystem that
|
|
8
|
+
* is over a minute, and the volumes are almost all of it — measured on one such
|
|
9
|
+
* host: images 2.6s, containers 0.4s, build cache 0.3s, volumes 47s.
|
|
10
|
+
*
|
|
11
|
+
* That is too long to hold a request open for, and there is no shorter way to
|
|
12
|
+
* ask: the runtimes' own CLIs expose no way to measure part of it, and reaching
|
|
13
|
+
* for the Engine API's type filter would mean talking to a unix socket directly
|
|
14
|
+
* rather than through the one CommandRunner that makes a local host and an SSH
|
|
15
|
+
* host the same code.
|
|
16
|
+
*
|
|
17
|
+
* So the measurement is a background job. Asking for it returns whatever was
|
|
18
|
+
* last measured — immediately, however old — and starts a fresh reading only if
|
|
19
|
+
* the old one has gone stale. The fresh reading is pushed to whoever asked when
|
|
20
|
+
* it lands. Nothing ever waits on a command whose duration is set by someone
|
|
21
|
+
* else's disk.
|
|
22
|
+
*
|
|
23
|
+
* A host's reading outlives its connection. Clearing it on teardown would mean
|
|
24
|
+
* the monitor importing this module while this one imports the monitor, to save
|
|
25
|
+
* one small object per host that has ever been opened — and a reading that
|
|
26
|
+
* survives a reconnect is re-measured by the staleness check anyway.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import type { ContainerDiskUsage } from '$shared/types/containers';
|
|
30
|
+
import { DISK_USAGE_STALE_MS, DISK_USAGE_TIMEOUT_MS } from '$shared/types/containers';
|
|
31
|
+
import { containerMonitor } from './monitor';
|
|
32
|
+
import { containerArgv, detectRuntime, firstProblem, tryRun } from './runtime';
|
|
33
|
+
import { parseDiskUsage } from './parse';
|
|
34
|
+
import { ws } from '../utils/ws';
|
|
35
|
+
import { debug } from '$shared/utils/logger';
|
|
36
|
+
|
|
37
|
+
interface HostUsage {
|
|
38
|
+
/** The last completed reading, kept until something invalidates it. */
|
|
39
|
+
last: ContainerDiskUsage | null;
|
|
40
|
+
/** Users to push the in-flight reading to when it lands. */
|
|
41
|
+
waiting: Set<string>;
|
|
42
|
+
measuring: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const hosts = new Map<string, HostUsage>();
|
|
46
|
+
|
|
47
|
+
function stateFor(hostId: string): HostUsage {
|
|
48
|
+
const existing = hosts.get(hostId);
|
|
49
|
+
if (existing) return existing;
|
|
50
|
+
const created: HostUsage = { last: null, waiting: new Set(), measuring: false };
|
|
51
|
+
hosts.set(hostId, created);
|
|
52
|
+
return created;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Whether a reading is old enough to be worth paying for another one.
|
|
57
|
+
*
|
|
58
|
+
* A reading with no usable stamp counts as stale: it came from somewhere that
|
|
59
|
+
* did not say when it was taken, and guessing that it is current would pin a
|
|
60
|
+
* wrong number on screen until something invalidated it.
|
|
61
|
+
*/
|
|
62
|
+
export function isDiskUsageStale(usage: ContainerDiskUsage | null, now: number): boolean {
|
|
63
|
+
if (!usage?.measuredAt) return true;
|
|
64
|
+
const measuredAt = Date.parse(usage.measuredAt);
|
|
65
|
+
if (Number.isNaN(measuredAt)) return true;
|
|
66
|
+
return now - measuredAt >= DISK_USAGE_STALE_MS;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Run the measurement itself. Slow by nature, so nothing calls this directly. */
|
|
70
|
+
async function measure(hostId: string): Promise<ContainerDiskUsage> {
|
|
71
|
+
const measuredAt = new Date().toISOString();
|
|
72
|
+
|
|
73
|
+
return containerMonitor.withHost(hostId, async (runner, platform) => {
|
|
74
|
+
const info = await detectRuntime(hostId, runner, platform);
|
|
75
|
+
if (info.problem !== 'none' || !info.runtime) {
|
|
76
|
+
return {
|
|
77
|
+
rows: [],
|
|
78
|
+
error: 'This host has no container runtime available right now.',
|
|
79
|
+
measuredAt
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const result = await tryRun(
|
|
84
|
+
runner,
|
|
85
|
+
containerArgv(info.runtime, platform, [
|
|
86
|
+
'system',
|
|
87
|
+
'df',
|
|
88
|
+
'--format',
|
|
89
|
+
info.runtime === 'docker' ? '{{json .}}' : 'json'
|
|
90
|
+
]),
|
|
91
|
+
DISK_USAGE_TIMEOUT_MS
|
|
92
|
+
);
|
|
93
|
+
if (result.code !== 0) {
|
|
94
|
+
return {
|
|
95
|
+
rows: [],
|
|
96
|
+
error: firstProblem(result.stderr, result.stdout, result.code),
|
|
97
|
+
measuredAt
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return { ...parseDiskUsage(result.stdout, info.runtime), measuredAt };
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Start a reading if one is warranted, and register the caller for the result.
|
|
106
|
+
*
|
|
107
|
+
* A second caller arriving while a reading is in flight joins that one rather
|
|
108
|
+
* than starting a second: two dialogs open on the same host must not make the
|
|
109
|
+
* daemon walk the same disk twice.
|
|
110
|
+
*/
|
|
111
|
+
function startMeasuring(hostId: string, userId: string): void {
|
|
112
|
+
const state = stateFor(hostId);
|
|
113
|
+
state.waiting.add(userId);
|
|
114
|
+
if (state.measuring) return;
|
|
115
|
+
state.measuring = true;
|
|
116
|
+
|
|
117
|
+
void (async () => {
|
|
118
|
+
let usage: ContainerDiskUsage;
|
|
119
|
+
try {
|
|
120
|
+
usage = await measure(hostId);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
debug.warn('containers', `could not measure disk usage on ${hostId}:`, error);
|
|
123
|
+
usage = {
|
|
124
|
+
rows: [],
|
|
125
|
+
error: error instanceof Error ? error.message : String(error),
|
|
126
|
+
measuredAt: new Date().toISOString()
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// A reading that failed is still a reading: caching it stops a broken host
|
|
131
|
+
// being re-measured on every open. An invalidate clears it either way.
|
|
132
|
+
state.last = usage;
|
|
133
|
+
state.measuring = false;
|
|
134
|
+
|
|
135
|
+
const waiting = [...state.waiting];
|
|
136
|
+
state.waiting.clear();
|
|
137
|
+
for (const id of waiting) {
|
|
138
|
+
ws.emit.user(id, 'containers:disk-usage-measured', { hostId, usage });
|
|
139
|
+
}
|
|
140
|
+
})();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* What the dialog gets when it opens: the last reading, and whether a fresh one
|
|
145
|
+
* is on its way.
|
|
146
|
+
*
|
|
147
|
+
* `force` is the Re-check button — the one case where someone has decided the
|
|
148
|
+
* cached answer is not good enough and is willing to wait for a new one.
|
|
149
|
+
*/
|
|
150
|
+
export function requestDiskUsage(
|
|
151
|
+
hostId: string,
|
|
152
|
+
userId: string,
|
|
153
|
+
force = false
|
|
154
|
+
): { usage: ContainerDiskUsage | null; measuring: boolean } {
|
|
155
|
+
const state = stateFor(hostId);
|
|
156
|
+
if (force || isDiskUsageStale(state.last, Date.now())) startMeasuring(hostId, userId);
|
|
157
|
+
return { usage: state.last, measuring: state.measuring };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Drop a host's reading, because something changed what it would say.
|
|
162
|
+
*
|
|
163
|
+
* Called after a prune or a removal: that is exactly when these numbers move,
|
|
164
|
+
* and showing the figures the sweep was meant to fix would read as the sweep
|
|
165
|
+
* having done nothing.
|
|
166
|
+
*/
|
|
167
|
+
export function invalidateDiskUsage(hostId: string): void {
|
|
168
|
+
const state = hosts.get(hostId);
|
|
169
|
+
if (state) state.last = null;
|
|
170
|
+
}
|