hammerkit 2.1.0-next.9 → 2.2.0-next.1
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/dist/dashboard/assets/{graph-canvas-BFnEN5i0.js → graph-canvas-D6GMgkoe.js} +1 -1
- package/dist/dashboard/assets/index-BNq9j5Jo.js +23 -0
- package/dist/dashboard/assets/{index-Dt3ITvRO.css → index-DP3_hKOe.css} +1 -1
- package/dist/dashboard/index.html +2 -2
- package/dist/executer/archive-task-entry.js +1 -1
- package/dist/executer/deadline.js +63 -10
- package/dist/executer/event-cache.js +5 -1
- package/dist/executer/execute-work-task.js +15 -3
- package/dist/executer/runner-commands.js +36 -0
- package/dist/file/get-file-context.js +4 -7
- package/dist/harness/connect.js +12 -8
- package/dist/harness/reconnect.js +141 -0
- package/dist/harness/sandbox-client.js +242 -33
- package/dist/harness/service-tools.js +36 -3
- package/dist/kubernetes/cluster-state.js +48 -14
- package/dist/kubernetes/ensure-persistent-data.js +15 -4
- package/dist/kubernetes/follow-pod-logs.js +8 -5
- package/dist/kubernetes/kept-claims.js +26 -5
- package/dist/kubernetes/resources.js +5 -1
- package/dist/kubernetes/sandbox-runner.js +526 -156
- package/dist/kubernetes/store-kubernetes-data.js +18 -3
- package/dist/kubernetes/sync-changes.js +463 -0
- package/dist/kubernetes/volumes.js +23 -1
- package/dist/optimizer/get-work-cache-stats.js +7 -0
- package/dist/planner/work-item-status.js +12 -2
- package/dist/planner/work-runtime-aggregate.js +1 -0
- package/dist/planner/work-runtime-docker.js +9 -5
- package/dist/planner/work-runtime-kubernetes.js +277 -55
- package/dist/planner/work-runtime-local.js +8 -4
- package/dist/remote/annotations.js +29 -3
- package/dist/remote/bundle.js +6 -8
- package/dist/remote/collect-bundle.js +3 -2
- package/dist/remote/device-login.js +4 -1
- package/dist/remote/remote-commands.js +16 -7
- package/dist/remote/remote-work-commands.js +265 -72
- package/dist/remote/server-client.js +38 -40
- package/dist/server/artifacts-api.js +56 -0
- package/dist/server/definitions-api.js +62 -8
- package/dist/server/endpoints.js +52 -32
- package/dist/server/executor.js +117 -30
- package/dist/server/http.js +4 -1
- package/dist/server/keep-artifacts.js +230 -0
- package/dist/server/kubernetes-backend.js +186 -59
- package/dist/server/kubernetes-sandbox-storage.js +83 -22
- package/dist/server/overview.js +67 -9
- package/dist/server/paging.js +167 -0
- package/dist/server/plan.js +8 -1
- package/dist/server/public-paths.js +52 -0
- package/dist/server/run-access.js +24 -0
- package/dist/server/run-artifacts.js +171 -0
- package/dist/server/run-logs.js +25 -1
- package/dist/server/run-request.js +63 -1
- package/dist/server/run-retry.js +6 -0
- package/dist/server/run-worker.js +267 -33
- package/dist/server/runs.js +527 -99
- package/dist/server/sandbox-files.js +9 -8
- package/dist/server/sandbox-follow.js +54 -0
- package/dist/server/sandbox-storage.js +38 -1
- package/dist/server/sandbox-sync.js +392 -0
- package/dist/server/sandboxes-api.js +54 -8
- package/dist/server/sandboxes.js +19 -0
- package/dist/server/server-config.js +14 -0
- package/dist/server/server.js +189 -46
- package/dist/server/service-runs.js +41 -8
- package/dist/server/task-order.js +56 -0
- package/dist/utils/project-path.js +20 -0
- package/dist/utils/tar-files.js +52 -0
- package/dist/utils/tar.js +37 -1
- package/package.json +1 -1
- package/dist/dashboard/assets/index-CE5PSB3y.js +0 -23
|
@@ -4,12 +4,14 @@ exports.HostSandboxFiles = exports.isRunState = exports.RUN_STATE_DIRECTORY = ex
|
|
|
4
4
|
const child_process_1 = require("child_process");
|
|
5
5
|
const crypto_1 = require("crypto");
|
|
6
6
|
const fs_1 = require("fs");
|
|
7
|
+
const project_path_1 = require("../utils/project-path");
|
|
7
8
|
const promises_1 = require("fs/promises");
|
|
8
9
|
const os_1 = require("os");
|
|
9
10
|
const path_1 = require("path");
|
|
10
11
|
const minimatch_1 = require("minimatch");
|
|
11
12
|
const source_manifest_1 = require("../remote/source-manifest");
|
|
12
13
|
const sandbox_patch_1 = require("./sandbox-patch");
|
|
14
|
+
const runner_commands_1 = require("../executer/runner-commands");
|
|
13
15
|
// The largest file the API reads into memory, edits or writes: the limit of an upload.
|
|
14
16
|
exports.MAX_FILE_BYTES = source_manifest_1.MAX_SOURCE_FILE_BYTES;
|
|
15
17
|
// what one typed read answers at most
|
|
@@ -54,13 +56,7 @@ exports.SandboxFileError = SandboxFileError;
|
|
|
54
56
|
* deny list of uploads names (`.git/`, `.env`, keys).
|
|
55
57
|
*/
|
|
56
58
|
function checkPath(path) {
|
|
57
|
-
if (path
|
|
58
|
-
path.length > 4096 ||
|
|
59
|
-
path.startsWith('/') ||
|
|
60
|
-
path.includes('\\') ||
|
|
61
|
-
path.includes('\0') ||
|
|
62
|
-
/^[A-Za-z]:/.test(path) ||
|
|
63
|
-
path.split('/').some((part) => part === '' || part === '.' || part === '..')) {
|
|
59
|
+
if (!(0, project_path_1.isRelativePath)(path, 4096)) {
|
|
64
60
|
throw new SandboxFileError(400, `${JSON.stringify(path.slice(0, 200))} is not a path in the sandbox: relative, / between names, no . or ..`);
|
|
65
61
|
}
|
|
66
62
|
const denied = deniedPath(path);
|
|
@@ -71,8 +67,13 @@ function checkPath(path) {
|
|
|
71
67
|
}
|
|
72
68
|
exports.checkPath = checkPath;
|
|
73
69
|
// The deny list of uploads, with the last name taken as a directory too, so `.git` itself
|
|
74
|
-
// is as denied as what is in it.
|
|
70
|
+
// is as denied as what is in it. The output of the commands of runs in the sandbox's runner
|
|
71
|
+
// is denied too: it is the runs' to read (their logs), not a file of the sandbox, and a file
|
|
72
|
+
// written there could pass for how a command ended.
|
|
75
73
|
function deniedPath(path) {
|
|
74
|
+
if (path === runner_commands_1.RUNNER_COMMANDS_DIRECTORY || path.startsWith(`${runner_commands_1.RUNNER_COMMANDS_DIRECTORY}/`)) {
|
|
75
|
+
return 'the commands of runs keep their output there';
|
|
76
|
+
}
|
|
76
77
|
return (0, source_manifest_1.deniedSourcePath)(path) ?? (0, source_manifest_1.deniedSourcePath)(`${path}/-`);
|
|
77
78
|
}
|
|
78
79
|
exports.deniedPath = deniedPath;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SandboxFollow = void 0;
|
|
4
|
+
const log_1 = require("../log");
|
|
5
|
+
// How long a sandbox must stay unchanged before its service runs follow it, and how long
|
|
6
|
+
// they may wait while its files keep changing.
|
|
7
|
+
const DELAY_MS = 1000;
|
|
8
|
+
const MAX_WAIT_MS = 5000;
|
|
9
|
+
/**
|
|
10
|
+
* Batches the changes of each sandbox: a burst of edits (or of runs that wrote) makes one
|
|
11
|
+
* call of `follow` once the sandbox was quiet for `delay`, and at the latest `maxWait` after its
|
|
12
|
+
* first change, so a sandbox that never stops changing is still followed. Each sandbox has
|
|
13
|
+
* its own batch.
|
|
14
|
+
*/
|
|
15
|
+
class SandboxFollow {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.options = options;
|
|
18
|
+
// per sandbox, the timer of its batch and when the batch began
|
|
19
|
+
this.batches = new Map();
|
|
20
|
+
this.closed = false;
|
|
21
|
+
this.delay = options.delay ?? DELAY_MS;
|
|
22
|
+
this.maxWait = options.maxWait ?? MAX_WAIT_MS;
|
|
23
|
+
}
|
|
24
|
+
changed(sandbox) {
|
|
25
|
+
if (this.closed) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const now = Date.now();
|
|
29
|
+
const batch = this.batches.get(sandbox);
|
|
30
|
+
const since = batch?.since ?? now;
|
|
31
|
+
clearTimeout(batch?.timer);
|
|
32
|
+
const wait = Math.min(this.delay, since + this.maxWait - now);
|
|
33
|
+
const timer = setTimeout(() => this.run(sandbox), wait);
|
|
34
|
+
this.batches.set(sandbox, { timer, since });
|
|
35
|
+
}
|
|
36
|
+
close() {
|
|
37
|
+
this.closed = true;
|
|
38
|
+
for (const { timer } of this.batches.values()) {
|
|
39
|
+
clearTimeout(timer);
|
|
40
|
+
}
|
|
41
|
+
this.batches.clear();
|
|
42
|
+
}
|
|
43
|
+
async run(sandbox) {
|
|
44
|
+
this.batches.delete(sandbox);
|
|
45
|
+
try {
|
|
46
|
+
await this.options.follow(sandbox);
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
this.options.log?.(`follow ${sandbox}: ${(0, log_1.getErrorMessage)(e)}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.SandboxFollow = SandboxFollow;
|
|
54
|
+
//# sourceMappingURL=sandbox-follow.js.map
|
|
@@ -24,6 +24,11 @@ class SandboxPushFailed extends Error {
|
|
|
24
24
|
exports.SandboxPushFailed = SandboxPushFailed;
|
|
25
25
|
// the commit a sandbox pushed last, in its own git data
|
|
26
26
|
const PUSHED_REF = 'refs/hammerkit/pushed';
|
|
27
|
+
// the directories a path is in, from the top: `a/b/c` is in `a` and `a/b`
|
|
28
|
+
function ancestors(path) {
|
|
29
|
+
const parts = path.split('/');
|
|
30
|
+
return parts.slice(0, -1).map((_, i) => parts.slice(0, i + 1).join('/'));
|
|
31
|
+
}
|
|
27
32
|
/**
|
|
28
33
|
* Sandboxes as directories below `directory`, one per sandbox: `files` is the checkout the
|
|
29
34
|
* tasks work on, `git` its git data. The git data is kept outside of the files on purpose:
|
|
@@ -182,9 +187,41 @@ class HostSandboxStorage {
|
|
|
182
187
|
this.walked.delete(id);
|
|
183
188
|
// the branch by its full name: a tag of the same name is never taken
|
|
184
189
|
await this.git(id, ['fetch', '--quiet', '--depth', '1', '--', fetch.url, `refs/heads/${fetch.ref}`], fetch.env, fetch.signal, 'fetch');
|
|
185
|
-
|
|
190
|
+
try {
|
|
191
|
+
// a fetch does not let git write over an ignored file of the sandbox: git takes it for
|
|
192
|
+
// expendable, and the state of the sandbox does not count it as a change. Only a create
|
|
193
|
+
// passes force, into an empty work tree, which has no ignored file to write over.
|
|
194
|
+
await this.git(id, ['checkout', '--quiet', ...(force ? ['--force'] : ['--no-overwrite-ignore']), 'FETCH_HEAD'], fetch.env, fetch.signal, 'check out');
|
|
195
|
+
}
|
|
196
|
+
catch (e) {
|
|
197
|
+
const ignored = force ? undefined : await this.ignoredInTheWay(id, fetch);
|
|
198
|
+
if (ignored === undefined) {
|
|
199
|
+
throw e;
|
|
200
|
+
}
|
|
201
|
+
throw new sandbox_files_1.SandboxFileError(409, `${JSON.stringify(ignored)} is ignored in the sandbox and ${fetch.ref} has something in its place: ` +
|
|
202
|
+
'a fetch would overwrite it, move or delete it first');
|
|
203
|
+
}
|
|
186
204
|
return (await this.git(id, ['rev-parse', 'HEAD'], fetch.env, fetch.signal, 'read')).trim();
|
|
187
205
|
}
|
|
206
|
+
// The first ignored file of the sandbox that the ref has in its place: at its path, above it
|
|
207
|
+
// (the ref has a file where the sandbox has a directory of ignored files) or below it (the ref
|
|
208
|
+
// has files in a directory where the sandbox has an ignored file). A checkout over one of those
|
|
209
|
+
// would write over it. Read only once a checkout was refused.
|
|
210
|
+
async ignoredInTheWay(id, fetch) {
|
|
211
|
+
// a listing that fails names no file, so git's own error stands
|
|
212
|
+
const list = (args) => this.git(id, args, fetch.env, fetch.signal, 'read', {
|
|
213
|
+
// a sandbox with node_modules lists many files
|
|
214
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
215
|
+
}).then((out) => out.split('\0'), () => []);
|
|
216
|
+
const ignored = await list(['ls-files', '-z', '--others', '--ignored', '--exclude-standard']);
|
|
217
|
+
if (ignored.every((path) => path === '')) {
|
|
218
|
+
return undefined;
|
|
219
|
+
}
|
|
220
|
+
const tracked = await list(['ls-tree', '-r', '-z', '--name-only', 'FETCH_HEAD']);
|
|
221
|
+
const files = new Set(tracked);
|
|
222
|
+
const directories = new Set(tracked.flatMap(ancestors));
|
|
223
|
+
return ignored.find((path) => path !== '' && (files.has(path) || directories.has(path) || ancestors(path).some((dir) => files.has(dir))));
|
|
224
|
+
}
|
|
188
225
|
// One hold of the sandbox from the commit to the push: a prune in between would remove the
|
|
189
226
|
// commit. A ref keeps the commit until the sandbox is moved to it, also when the push
|
|
190
227
|
// failed late and the remote has it after all.
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SandboxSync = exports.unsafePath = exports.MAX_BATCH_FILES = exports.MAX_BATCH_BYTES = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
const log_1 = require("../log");
|
|
6
|
+
const tar_files_1 = require("../utils/tar-files");
|
|
7
|
+
const sandbox_files_1 = require("./sandbox-files");
|
|
8
|
+
// the most bytes of files one part of a batch sends; a larger batch goes in several parts
|
|
9
|
+
exports.MAX_BATCH_BYTES = 16 * 1024 * 1024;
|
|
10
|
+
// the most files one part of a batch sends
|
|
11
|
+
exports.MAX_BATCH_FILES = 10000;
|
|
12
|
+
// how many files of a sandbox are read at once, while a batch is worked out
|
|
13
|
+
const READ_CONCURRENCY = 8;
|
|
14
|
+
// a file whose mtime is this recent may be written again in the same timestamp: it is read again
|
|
15
|
+
// on every sync, as git does with its index (the racy rule)
|
|
16
|
+
const RACY_MS = 2000;
|
|
17
|
+
// how long a run whose services failed to apply a batch waits before the full set is sent again:
|
|
18
|
+
// the first wait, doubled on each failure up to RETRY_MAX_MS
|
|
19
|
+
const RETRY_MIN_MS = 1000;
|
|
20
|
+
const RETRY_MAX_MS = 10000;
|
|
21
|
+
// what a path is in the services when it is gone
|
|
22
|
+
const ABSENT = 'absent';
|
|
23
|
+
/**
|
|
24
|
+
* Why a path of the sandbox can not be a path of a service's folder, or none. A name with a
|
|
25
|
+
* control character (a newline in particular), an absolute path, an empty segment, a trailing `/`
|
|
26
|
+
* or a `.` or `..` segment is refused: each of those can name the folder itself or one above it.
|
|
27
|
+
*/
|
|
28
|
+
function unsafePath(path) {
|
|
29
|
+
if (path === '') {
|
|
30
|
+
return 'it is empty';
|
|
31
|
+
}
|
|
32
|
+
if ([...path].some((char) => char.charCodeAt(0) < 0x20 || char.charCodeAt(0) === 0x7f)) {
|
|
33
|
+
return 'it has a control character';
|
|
34
|
+
}
|
|
35
|
+
if (path.startsWith('/') || /^[A-Za-z]:/.test(path)) {
|
|
36
|
+
return 'it is not relative to the sandbox';
|
|
37
|
+
}
|
|
38
|
+
if (path.endsWith('/') || path.split('/').includes('')) {
|
|
39
|
+
return 'it has an empty segment or ends in /';
|
|
40
|
+
}
|
|
41
|
+
if (path.split('/').some((segment) => segment === '..' || segment === '.')) {
|
|
42
|
+
return 'it has a . or .. segment';
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
exports.unsafePath = unsafePath;
|
|
47
|
+
// whether a file with this mtime may still change without its mtime changing: an mtime that is
|
|
48
|
+
// recent, or one that is not a date, is read on every sync
|
|
49
|
+
function racy(modifiedAt) {
|
|
50
|
+
const time = Date.parse(modifiedAt);
|
|
51
|
+
return Number.isNaN(time) || Date.now() - time < RACY_MS;
|
|
52
|
+
}
|
|
53
|
+
// the mtime of a file as it is in the sandbox, for its entry of an archive
|
|
54
|
+
function mtimeOf(modifiedAt) {
|
|
55
|
+
const time = Date.parse(modifiedAt);
|
|
56
|
+
return Number.isNaN(time) ? new Date() : new Date(time);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The files of a batch in parts, each under the caps (MAX_BATCH_FILES, MAX_BATCH_BYTES), in the
|
|
60
|
+
* order they came. A file over the byte cap goes alone in its part: the parts are sent one after
|
|
61
|
+
* another, so each stays under the cap in memory.
|
|
62
|
+
*/
|
|
63
|
+
function partition(writes) {
|
|
64
|
+
const parts = [];
|
|
65
|
+
let part = [];
|
|
66
|
+
let bytes = 0;
|
|
67
|
+
for (const write of writes) {
|
|
68
|
+
if (part.length > 0 && (part.length === exports.MAX_BATCH_FILES || bytes + write.size > exports.MAX_BATCH_BYTES)) {
|
|
69
|
+
parts.push(part);
|
|
70
|
+
part = [];
|
|
71
|
+
bytes = 0;
|
|
72
|
+
}
|
|
73
|
+
part.push(write);
|
|
74
|
+
bytes += write.size;
|
|
75
|
+
}
|
|
76
|
+
if (part.length > 0) {
|
|
77
|
+
parts.push(part);
|
|
78
|
+
}
|
|
79
|
+
return parts;
|
|
80
|
+
}
|
|
81
|
+
// the results of `fn` for `items`, with at most `limit` calls in flight
|
|
82
|
+
async function mapLimit(items, limit, fn) {
|
|
83
|
+
const results = new Array(items.length);
|
|
84
|
+
let next = 0;
|
|
85
|
+
const worker = async () => {
|
|
86
|
+
while (next < items.length) {
|
|
87
|
+
const index = next++;
|
|
88
|
+
results[index] = await fn(items[index]);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
92
|
+
return results;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Keeps the services of the service runs on a sandbox in step with its files. Each change of the
|
|
96
|
+
* sandbox (batched by SandboxFollow) goes to each run that is up, as the files that changed since
|
|
97
|
+
* the services last got their files. Nothing else of the services is touched, and nothing is
|
|
98
|
+
* restarted here.
|
|
99
|
+
*
|
|
100
|
+
* The record of what the services have is kept in memory, per run: for each path, the stat and
|
|
101
|
+
* the hash of the file the services got, or ABSENT once its deletion went out. A file whose stat
|
|
102
|
+
* did not change, and whose mtime is not recent, is not read at all. A run without a record (the
|
|
103
|
+
* server started since it did) gets every file that differs from the commit once: "the full set"
|
|
104
|
+
* means exactly that. A run whose services failed to apply a batch loses its record, and the full
|
|
105
|
+
* set is sent again after a backoff.
|
|
106
|
+
*/
|
|
107
|
+
class SandboxSync {
|
|
108
|
+
constructor(options) {
|
|
109
|
+
this.options = options;
|
|
110
|
+
// per run, what the services have of each path they got (see the class comment)
|
|
111
|
+
this.records = new Map();
|
|
112
|
+
// the sandbox of each run that was started here, for the retry of its failed batch
|
|
113
|
+
this.sandboxes = new Map();
|
|
114
|
+
// per run, the timer of its retry and how long the wait was
|
|
115
|
+
this.retries = new Map();
|
|
116
|
+
// the batches of each sandbox, one after another
|
|
117
|
+
this.queues = new Map();
|
|
118
|
+
// the batch of each sandbox that waits to start: it reads the files when it starts, so a
|
|
119
|
+
// change that comes meanwhile is in it too
|
|
120
|
+
this.waiting = new Map();
|
|
121
|
+
// per run, the lines already logged: a path that is refused says so once
|
|
122
|
+
this.logged = new Map();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* A service run starts from a copy of the files now: what differs from the commit in the copy
|
|
126
|
+
* is what the services have from the start, and what a later change has to include.
|
|
127
|
+
*/
|
|
128
|
+
async started(run) {
|
|
129
|
+
if (run.source.type !== 'sandbox') {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.sandboxes.set(run.id, run.source.id);
|
|
133
|
+
const files = this.options.files(run.owner, run.source.id);
|
|
134
|
+
const record = new Map();
|
|
135
|
+
const paths = (await this.changedPaths(files)).filter((path) => !unsafePath(path));
|
|
136
|
+
const looked = await mapLimit(paths, READ_CONCURRENCY, (path) => this.look(files, path, undefined, () => undefined));
|
|
137
|
+
for (const item of looked) {
|
|
138
|
+
if (item.kind === 'same') {
|
|
139
|
+
record.set(item.path, item.mark);
|
|
140
|
+
}
|
|
141
|
+
else if (item.kind === 'changed') {
|
|
142
|
+
record.set(item.path, item.mark);
|
|
143
|
+
}
|
|
144
|
+
else if (item.kind === 'gone') {
|
|
145
|
+
record.set(item.path, ABSENT);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
this.records.set(run.id, record);
|
|
149
|
+
}
|
|
150
|
+
// A run that ended: nothing is synced for it any more, and its retry is stopped
|
|
151
|
+
async forget(run) {
|
|
152
|
+
this.records.delete(run);
|
|
153
|
+
this.sandboxes.delete(run);
|
|
154
|
+
this.logged.delete(run);
|
|
155
|
+
this.clearRetry(run);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The services of a run did not apply the last batch: its record goes, and the full set is sent
|
|
159
|
+
* again after a backoff (1 s, doubled on each failure up to 10 s).
|
|
160
|
+
*/
|
|
161
|
+
failed(run) {
|
|
162
|
+
this.records.delete(run);
|
|
163
|
+
const sandbox = this.sandboxes.get(run);
|
|
164
|
+
if (sandbox === undefined) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const previous = this.retries.get(run);
|
|
168
|
+
const delay = previous ? Math.min(previous.delay * 2, RETRY_MAX_MS) : RETRY_MIN_MS;
|
|
169
|
+
if (previous) {
|
|
170
|
+
clearTimeout(previous.timer);
|
|
171
|
+
}
|
|
172
|
+
const timer = setTimeout(() => void this.follow(sandbox), delay);
|
|
173
|
+
timer.unref?.();
|
|
174
|
+
this.retries.set(run, { timer, delay });
|
|
175
|
+
}
|
|
176
|
+
// Stops the retries: nothing is sent after the server closes
|
|
177
|
+
close() {
|
|
178
|
+
for (const { timer } of this.retries.values()) {
|
|
179
|
+
clearTimeout(timer);
|
|
180
|
+
}
|
|
181
|
+
this.retries.clear();
|
|
182
|
+
}
|
|
183
|
+
clearRetry(run) {
|
|
184
|
+
const retry = this.retries.get(run);
|
|
185
|
+
if (retry) {
|
|
186
|
+
clearTimeout(retry.timer);
|
|
187
|
+
this.retries.delete(run);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// The batch of changes of a sandbox, after the ones before it. One that waits already has the
|
|
191
|
+
// newest files when it starts, so a second one is not queued behind it.
|
|
192
|
+
follow(sandbox) {
|
|
193
|
+
const waiting = this.waiting.get(sandbox);
|
|
194
|
+
if (waiting) {
|
|
195
|
+
return waiting;
|
|
196
|
+
}
|
|
197
|
+
const previous = this.queues.get(sandbox) ?? Promise.resolve();
|
|
198
|
+
const next = previous
|
|
199
|
+
.then(() => {
|
|
200
|
+
this.waiting.delete(sandbox);
|
|
201
|
+
return this.sync(sandbox);
|
|
202
|
+
})
|
|
203
|
+
.catch((e) => this.log(`sync sandbox ${sandbox}: ${(0, log_1.getErrorMessage)(e)}`));
|
|
204
|
+
this.waiting.set(sandbox, next);
|
|
205
|
+
this.queues.set(sandbox, next);
|
|
206
|
+
void next.then(() => {
|
|
207
|
+
if (this.queues.get(sandbox) === next) {
|
|
208
|
+
this.queues.delete(sandbox);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
return next;
|
|
212
|
+
}
|
|
213
|
+
async sync(sandbox) {
|
|
214
|
+
const runs = this.options.upOn(sandbox);
|
|
215
|
+
if (runs.length === 0) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
// the files of a sandbox are one owner's: a run of another owner would read them as theirs
|
|
219
|
+
const [{ owner }] = runs;
|
|
220
|
+
if (runs.some((run) => run.owner.iss !== owner.iss || run.owner.sub !== owner.sub)) {
|
|
221
|
+
throw new Error(`the service runs on ${sandbox} have more than one owner, their changes are not synced`);
|
|
222
|
+
}
|
|
223
|
+
const files = this.options.files(owner, sandbox);
|
|
224
|
+
const changed = await this.changedPaths(files);
|
|
225
|
+
for (const run of runs) {
|
|
226
|
+
await this.sendTo(run, sandbox, files, changed);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Sends the batch of one run: the paths that changed since the record of the run (or, without
|
|
231
|
+
* one, the paths that differ from the commit). A batch over the caps goes in parts, one after the
|
|
232
|
+
* other. The record moves on for each part the services took; a part they refuse is in the next
|
|
233
|
+
* batch.
|
|
234
|
+
*/
|
|
235
|
+
async sendTo(run, sandbox, files, changed) {
|
|
236
|
+
const record = this.records.get(run.id) ?? new Map();
|
|
237
|
+
this.records.set(run.id, record);
|
|
238
|
+
const say = (line) => this.logOnce(run.id, line);
|
|
239
|
+
const candidates = [...new Set([...changed, ...record.keys()])].filter((path) => {
|
|
240
|
+
const problem = unsafePath(path);
|
|
241
|
+
if (problem) {
|
|
242
|
+
say(`skipped ${JSON.stringify(path)} of the sandbox: ${problem}`);
|
|
243
|
+
record.delete(path);
|
|
244
|
+
}
|
|
245
|
+
return !problem;
|
|
246
|
+
});
|
|
247
|
+
const writes = [];
|
|
248
|
+
const gone = [];
|
|
249
|
+
const looked = await mapLimit(candidates, READ_CONCURRENCY, (path) => this.look(files, path, record.get(path), say));
|
|
250
|
+
for (const item of looked) {
|
|
251
|
+
if (item.kind === 'refused') {
|
|
252
|
+
// not in the record: the next batch that has it sends it again
|
|
253
|
+
record.delete(item.path);
|
|
254
|
+
}
|
|
255
|
+
else if (item.kind === 'same') {
|
|
256
|
+
record.set(item.path, item.mark);
|
|
257
|
+
}
|
|
258
|
+
else if (item.kind === 'gone') {
|
|
259
|
+
gone.push(item.path);
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
writes.push({ ...item.mark, path: item.path, content: item.content });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const parts = partition(writes);
|
|
266
|
+
if (parts.length > 1) {
|
|
267
|
+
say(`the changes of ${sandbox} are more than a batch takes, ${run.id} gets them in ${parts.length} parts`);
|
|
268
|
+
}
|
|
269
|
+
// the gone paths go with the first part; with no file written, they go alone
|
|
270
|
+
const batches = parts.length > 0 ? parts : [[]];
|
|
271
|
+
for (const [index, part] of batches.entries()) {
|
|
272
|
+
const removed = index === 0 ? gone : [];
|
|
273
|
+
if (part.length + removed.length === 0) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const changes = {
|
|
277
|
+
archive: (0, tar_files_1.tarFiles)(part.map((write) => ({
|
|
278
|
+
path: write.path,
|
|
279
|
+
mode: write.exec ? 0o755 : 0o644,
|
|
280
|
+
content: write.content,
|
|
281
|
+
mtime: mtimeOf(write.modifiedAt),
|
|
282
|
+
}))),
|
|
283
|
+
written: part.map((write) => write.path),
|
|
284
|
+
deleted: removed,
|
|
285
|
+
restart: false,
|
|
286
|
+
};
|
|
287
|
+
if (!this.options.send(run.id, changes)) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
for (const write of part) {
|
|
291
|
+
record.set(write.path, { size: write.size, modifiedAt: write.modifiedAt, exec: write.exec, hash: write.hash });
|
|
292
|
+
}
|
|
293
|
+
// a deletion stays in the record as ABSENT: git lists a deleted file until it is committed,
|
|
294
|
+
// and a deletion that is not recorded would be sent again on every sync
|
|
295
|
+
for (const path of removed) {
|
|
296
|
+
record.set(path, ABSENT);
|
|
297
|
+
}
|
|
298
|
+
// a batch went: the next failure starts the backoff again
|
|
299
|
+
this.clearRetry(run.id);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* What a path of the sandbox is, against what the services have of it (`known`). A file whose
|
|
304
|
+
* size, mtime and exec bit are the ones the services have, and whose mtime is not racy, is the
|
|
305
|
+
* same without a read. Otherwise its content is read once and hashed, and the read is kept for
|
|
306
|
+
* the entry of the batch.
|
|
307
|
+
*/
|
|
308
|
+
async look(files, path, known, say) {
|
|
309
|
+
let stat;
|
|
310
|
+
try {
|
|
311
|
+
stat = await files.stat(path);
|
|
312
|
+
}
|
|
313
|
+
catch (e) {
|
|
314
|
+
say(`skipped ${JSON.stringify(path)} of the sandbox: ${(0, log_1.getErrorMessage)(e)}`);
|
|
315
|
+
return { path, kind: 'refused' };
|
|
316
|
+
}
|
|
317
|
+
if (stat?.type !== 'file') {
|
|
318
|
+
return known === ABSENT ? { path, kind: 'same', mark: ABSENT } : { path, kind: 'gone' };
|
|
319
|
+
}
|
|
320
|
+
if (stat.size > sandbox_files_1.MAX_FILE_BYTES) {
|
|
321
|
+
say(`refused ${JSON.stringify(path)} of the sandbox: it is over ${sandbox_files_1.MAX_FILE_BYTES} bytes`);
|
|
322
|
+
return { path, kind: 'refused' };
|
|
323
|
+
}
|
|
324
|
+
if (known !== undefined &&
|
|
325
|
+
known !== ABSENT &&
|
|
326
|
+
known.size === stat.size &&
|
|
327
|
+
known.modifiedAt === stat.modifiedAt &&
|
|
328
|
+
known.exec === stat.exec &&
|
|
329
|
+
!racy(stat.modifiedAt)) {
|
|
330
|
+
return { path, kind: 'same', mark: known };
|
|
331
|
+
}
|
|
332
|
+
let read;
|
|
333
|
+
try {
|
|
334
|
+
read = await this.readOf(files, path);
|
|
335
|
+
}
|
|
336
|
+
catch (e) {
|
|
337
|
+
say(`skipped ${JSON.stringify(path)} of the sandbox: ${(0, log_1.getErrorMessage)(e)}`);
|
|
338
|
+
return { path, kind: 'refused' };
|
|
339
|
+
}
|
|
340
|
+
if (read === undefined || read.content.length > sandbox_files_1.MAX_FILE_BYTES) {
|
|
341
|
+
say(`refused ${JSON.stringify(path)} of the sandbox: it is over ${sandbox_files_1.MAX_FILE_BYTES} bytes`);
|
|
342
|
+
return { path, kind: 'refused' };
|
|
343
|
+
}
|
|
344
|
+
const mark = {
|
|
345
|
+
size: stat.size,
|
|
346
|
+
modifiedAt: stat.modifiedAt,
|
|
347
|
+
exec: stat.exec,
|
|
348
|
+
hash: read.sha256 ?? (0, crypto_1.createHash)('sha256').update(read.content).digest('hex'),
|
|
349
|
+
};
|
|
350
|
+
if (known !== undefined && known !== ABSENT && known.hash === mark.hash && known.exec === mark.exec) {
|
|
351
|
+
return { path, kind: 'same', mark };
|
|
352
|
+
}
|
|
353
|
+
return { path, kind: 'changed', mark, content: read.content };
|
|
354
|
+
}
|
|
355
|
+
// logs a line of a run once: a path that stays refused is not said again on every sync
|
|
356
|
+
logOnce(run, line) {
|
|
357
|
+
const lines = this.logged.get(run) ?? new Set();
|
|
358
|
+
this.logged.set(run, lines);
|
|
359
|
+
if (!lines.has(line)) {
|
|
360
|
+
lines.add(line);
|
|
361
|
+
this.log(line);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
log(line) {
|
|
365
|
+
this.options.log?.(line);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* The content of a file of the sandbox, read whole, with its hash when the files give one.
|
|
369
|
+
* Undefined for a file that is over the cap at open: its stream is destroyed, and nothing is
|
|
370
|
+
* read into memory.
|
|
371
|
+
*/
|
|
372
|
+
async readOf(files, path) {
|
|
373
|
+
const raw = await files.raw(path);
|
|
374
|
+
if (raw.content) {
|
|
375
|
+
return { content: raw.content, sha256: raw.sha256 };
|
|
376
|
+
}
|
|
377
|
+
raw.stream?.destroy();
|
|
378
|
+
return undefined;
|
|
379
|
+
}
|
|
380
|
+
async changedPaths(files) {
|
|
381
|
+
const paths = new Set();
|
|
382
|
+
for (const change of await files.changes()) {
|
|
383
|
+
paths.add(change.path);
|
|
384
|
+
if (change.from) {
|
|
385
|
+
paths.add(change.from);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return [...paths];
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
exports.SandboxSync = SandboxSync;
|
|
392
|
+
//# sourceMappingURL=sandbox-sync.js.map
|
|
@@ -8,6 +8,7 @@ const repo_1 = require("./repo");
|
|
|
8
8
|
const runs_1 = require("./runs");
|
|
9
9
|
const sandbox_files_api_1 = require("./sandbox-files-api");
|
|
10
10
|
const sandboxes_1 = require("./sandboxes");
|
|
11
|
+
const paging_1 = require("./paging");
|
|
11
12
|
const refSchema = zod_1.z.string().min(1).max(200);
|
|
12
13
|
const createSchema = zod_1.z
|
|
13
14
|
.object({
|
|
@@ -46,7 +47,10 @@ const pushSchema = zod_1.z
|
|
|
46
47
|
*
|
|
47
48
|
* POST /v1/sandboxes { repo, ref } fetch a branch into a new sandbox
|
|
48
49
|
* GET /v1/sandboxes the caller's sandboxes, their limits and usage;
|
|
49
|
-
* `?scope=all` everybody's, with `observe`
|
|
50
|
+
* `?scope=all` everybody's, with `observe`; `owner`,
|
|
51
|
+
* `repo` and `ref` narrow them, `limit` and `cursor`
|
|
52
|
+
* page them the last used first, `values=<field>`
|
|
53
|
+
* answers the values of a filter (paging.ts)
|
|
50
54
|
* GET /v1/sandboxes/<id> one sandbox
|
|
51
55
|
* DELETE /v1/sandboxes/<id> cancel its runs and remove it
|
|
52
56
|
* POST /v1/sandboxes/<id>/fetch { ref } move a sandbox without changes to a branch's commit
|
|
@@ -81,13 +85,15 @@ async function handleSandboxes(config, sandboxes, repos, log, principal, req, re
|
|
|
81
85
|
(0, http_1.send)(res, 403, { error: 'scope all needs an entitlement with observe' });
|
|
82
86
|
return true;
|
|
83
87
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
88
|
+
try {
|
|
89
|
+
listSandboxes(config, sandboxes, principal, scope, url.searchParams, res, show);
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
if (!(e instanceof paging_1.ListParamError)) {
|
|
93
|
+
throw e;
|
|
94
|
+
}
|
|
95
|
+
(0, http_1.send)(res, 400, { error: e.message });
|
|
96
|
+
}
|
|
91
97
|
}
|
|
92
98
|
else if (method === 'POST') {
|
|
93
99
|
const body = parse(createSchema, await (0, http_1.readJson)(req));
|
|
@@ -264,4 +270,44 @@ function view(config, sandboxes, principal, sandbox) {
|
|
|
264
270
|
run: run ?? null,
|
|
265
271
|
};
|
|
266
272
|
}
|
|
273
|
+
// what the sandboxes page narrows by, and GET /v1/sandboxes?values= answers the values of
|
|
274
|
+
const SANDBOX_FIELDS = ['owner', 'repo', 'ref'];
|
|
275
|
+
function parseSandboxFilter(params) {
|
|
276
|
+
return Object.fromEntries(SANDBOX_FIELDS.map((field) => [field, (0, paging_1.readList)(params, field)]).filter(([, values]) => values));
|
|
277
|
+
}
|
|
278
|
+
function filterSandboxes(sandboxes, filter) {
|
|
279
|
+
return sandboxes.filter((sandbox) => (!filter.owner || filter.owner.includes(sandbox.owner.sub)) &&
|
|
280
|
+
(!filter.repo || filter.repo.includes(sandbox.repo)) &&
|
|
281
|
+
(!filter.ref || filter.ref.includes(sandbox.ref)));
|
|
282
|
+
}
|
|
283
|
+
// the last used first, as the sandboxes page shows them
|
|
284
|
+
const sandboxKey = (sandbox) => [sandbox.lastUsedAt, sandbox.id];
|
|
285
|
+
function pageOfSandboxes(sandboxes, page, show) {
|
|
286
|
+
const answered = (0, paging_1.pageByKey)((0, paging_1.newestFirst)(sandboxes, sandboxKey), page, sandboxKey);
|
|
287
|
+
return { ...answered, items: answered.items.map(show) };
|
|
288
|
+
}
|
|
289
|
+
// GET /v1/sandboxes: the sandboxes of the scope that the filters let through, a page of them,
|
|
290
|
+
// or the values of a filter.
|
|
291
|
+
function listSandboxes(config, sandboxes, principal, scope, params, res, show) {
|
|
292
|
+
const filter = parseSandboxFilter(params);
|
|
293
|
+
const values = (0, paging_1.parseValues)(params, SANDBOX_FIELDS);
|
|
294
|
+
const page = values ? undefined : (0, paging_1.parsePage)(params);
|
|
295
|
+
const listed = scope === 'all' ? sandboxes.all() : sandboxes.list(principal);
|
|
296
|
+
const nameOf = (owner) => owner.name ?? (0, policy_1.subjectName)(config.entitlements, owner);
|
|
297
|
+
if (values) {
|
|
298
|
+
// a value of the field is counted over the sandboxes the other filters let through
|
|
299
|
+
const { field } = values;
|
|
300
|
+
const counted = (0, paging_1.countValues)(filterSandboxes(listed, { ...filter, [field]: undefined }), (sandbox) => [field === 'owner' ? { value: sandbox.owner.sub, name: nameOf(sandbox.owner) } : sandbox[field]], values);
|
|
301
|
+
(0, http_1.send)(res, 200, (0, paging_1.answerPage)('values', counted));
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
const found = filterSandboxes(listed, filter);
|
|
305
|
+
const limits = sandboxes.limits;
|
|
306
|
+
// the limits and the usage are the caller's, whichever sandboxes they list
|
|
307
|
+
(0, http_1.send)(res, 200, {
|
|
308
|
+
...(page ? (0, paging_1.answerPage)('sandboxes', pageOfSandboxes(found, page, show)) : { sandboxes: found.map(show) }),
|
|
309
|
+
limits: { count: limits.count, bytes: limits.bytes },
|
|
310
|
+
used: sandboxes.usage(principal),
|
|
311
|
+
});
|
|
312
|
+
}
|
|
267
313
|
//# sourceMappingURL=sandboxes-api.js.map
|