hammerkit 2.1.0 → 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/build.schema.json +126 -28
- package/dist/cli.js +5 -0
- package/dist/dashboard/assets/graph-canvas-D6GMgkoe.js +8 -0
- package/dist/dashboard/assets/index-BNq9j5Jo.js +23 -0
- package/dist/dashboard/assets/index-DP3_hKOe.css +2 -0
- package/dist/dashboard/index.html +2 -2
- package/dist/docker/item-network.js +6 -5
- package/dist/docker/remove-orphaned-containers.js +5 -4
- package/dist/environment/replace-env-variables.js +8 -5
- package/dist/environment/run-source-envs.js +2 -1
- package/dist/executer/archive-task-entry.js +1 -1
- package/dist/executer/check-readiness.js +8 -4
- package/dist/executer/docker-healthcheck.js +73 -0
- package/dist/executer/docker-service.js +21 -15
- package/dist/executer/event-cache.js +5 -1
- 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 +241 -33
- package/dist/harness/service-tools.js +11 -2
- package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +62 -14
- package/dist/kubernetes/ensure-persistent-data.js +1 -1
- package/dist/kubernetes/follow-pod-logs.js +8 -5
- package/dist/kubernetes/kept-claims.js +38 -14
- 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/network/healthcheck-request.js +38 -0
- package/dist/optimizer/get-work-cache-stats.js +7 -0
- package/dist/optimizer/work-task-cache-description.js +5 -3
- package/dist/planner/image-build.js +5 -0
- package/dist/planner/service-definition.js +2 -1
- package/dist/planner/utils/append-work-service.js +12 -4
- package/dist/planner/utils/dependency-outputs.js +7 -6
- package/dist/planner/validate.js +41 -0
- package/dist/planner/work-healthcheck.js +23 -2
- 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 +33 -6
- package/dist/planner/work-runtime-kubernetes.js +313 -70
- package/dist/planner/work-runtime-local.js +8 -4
- package/dist/planner/work-secret.js +33 -11
- package/dist/planner/work-service-id.js +30 -6
- package/dist/planner/work-service.js +2 -2
- package/dist/program.js +19 -1
- package/dist/remote/annotations.js +8 -2
- package/dist/remote/bundle.js +6 -8
- package/dist/remote/device-login.js +4 -1
- package/dist/remote/remote-commands.js +16 -7
- package/dist/remote/remote-status.js +1 -1
- package/dist/remote/remote-work-commands.js +220 -68
- package/dist/remote/server-client.js +38 -40
- package/dist/schema/build-file-base-task-schema.js +16 -1
- package/dist/schema/build-file-image-build-schema.js +5 -8
- package/dist/schema/build-file-service-container-healthcheck.js +25 -7
- package/dist/schema/reference-parser.js +108 -43
- package/dist/schema/work-scope-parser.js +8 -0
- package/dist/secrets/prefetch-secrets.js +20 -1
- package/dist/server/artifacts-api.js +56 -0
- package/dist/server/borrowed-quota.js +216 -0
- package/dist/server/capacity.js +18 -6
- package/dist/server/definitions-api.js +62 -8
- 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 +292 -79
- package/dist/server/kubernetes-sandbox-storage.js +86 -24
- package/dist/server/overview.js +73 -9
- package/dist/server/paging.js +167 -0
- package/dist/server/plan.js +16 -1
- 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 +11 -0
- package/dist/server/run-source-environment.js +1 -1
- package/dist/server/run-worker.js +267 -33
- package/dist/server/runs.js +531 -105
- package/dist/server/sandbox-files-api.js +27 -12
- package/dist/server/sandbox-files.js +12 -10
- package/dist/server/sandbox-follow.js +54 -0
- package/dist/server/sandbox-pod.js +1 -1
- package/dist/server/sandbox-storage.js +38 -1
- package/dist/server/sandbox-sync.js +392 -0
- package/dist/server/sandboxes-api.js +116 -19
- package/dist/server/sandboxes.js +38 -3
- package/dist/server/server-config.js +73 -2
- package/dist/server/server.js +169 -38
- 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/graph-canvas-DtJSpova.js +0 -8
- package/dist/dashboard/assets/index-BndChMep.css +0 -2
- package/dist/dashboard/assets/index-DRSwIRXd.js +0 -17
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Reconnecting = exports.showUrl = exports.RECONNECT_TIMING = void 0;
|
|
4
|
+
const http_1 = require("../server/http");
|
|
5
|
+
const units_1 = require("../utils/units");
|
|
6
|
+
// a restart of the server takes a few minutes while its new pod starts
|
|
7
|
+
exports.RECONNECT_TIMING = { windowMs: 10 * 60000, firstDelayMs: 1000, maxDelayMs: 15000 };
|
|
8
|
+
// what a proxy or the server answers while the server is away or shuts down
|
|
9
|
+
const AWAY_STATUSES = [502, 503, 504];
|
|
10
|
+
// the call never got to the server. A connect timeout is one: the connection was never
|
|
11
|
+
// made, so nothing of the request was sent.
|
|
12
|
+
const NOT_SENT = ['ECONNREFUSED', 'EAI_AGAIN', 'ENETUNREACH', 'EHOSTUNREACH', 'UND_ERR_CONNECT_TIMEOUT'];
|
|
13
|
+
// the connection broke or timed out, maybe after the server took the call
|
|
14
|
+
const LOST = [
|
|
15
|
+
'ECONNRESET',
|
|
16
|
+
'ECONNABORTED',
|
|
17
|
+
'EPIPE',
|
|
18
|
+
'ETIMEDOUT',
|
|
19
|
+
'UND_ERR_SOCKET',
|
|
20
|
+
'UND_ERR_HEADERS_TIMEOUT',
|
|
21
|
+
'UND_ERR_BODY_TIMEOUT',
|
|
22
|
+
];
|
|
23
|
+
// The codes of the error a fetch failed with: node's fetch keeps it as the cause, with one
|
|
24
|
+
// error for each address it tried when a name has several.
|
|
25
|
+
function failureCodes(e) {
|
|
26
|
+
if (e instanceof Error && e.name === 'TimeoutError') {
|
|
27
|
+
return ['ETIMEDOUT'];
|
|
28
|
+
}
|
|
29
|
+
const cause = e?.cause;
|
|
30
|
+
const codes = cause?.errors?.length ? cause.errors.map((error) => error?.code) : [cause?.code];
|
|
31
|
+
return codes.map((code) => (typeof code === 'string' ? code : ''));
|
|
32
|
+
}
|
|
33
|
+
function isAway(response) {
|
|
34
|
+
return AWAY_STATUSES.includes(response.status);
|
|
35
|
+
}
|
|
36
|
+
function mayRepeat(attempt, repeat) {
|
|
37
|
+
if ('response' in attempt) {
|
|
38
|
+
return repeat === 'always'
|
|
39
|
+
? isAway(attempt.response)
|
|
40
|
+
: attempt.response.status === 503 && attempt.response.headers.get(http_1.RETRY_HEADER) === 'safe';
|
|
41
|
+
}
|
|
42
|
+
const codes = failureCodes(attempt.error);
|
|
43
|
+
return repeat === 'always'
|
|
44
|
+
? codes.some((code) => NOT_SENT.includes(code) || LOST.includes(code))
|
|
45
|
+
: codes.every((code) => NOT_SENT.includes(code));
|
|
46
|
+
}
|
|
47
|
+
// The address of a server as it is said, without a user or password it may hold.
|
|
48
|
+
function showUrl(url) {
|
|
49
|
+
try {
|
|
50
|
+
const parsed = new URL(url);
|
|
51
|
+
if (!parsed.username && !parsed.password) {
|
|
52
|
+
return url;
|
|
53
|
+
}
|
|
54
|
+
parsed.username = '';
|
|
55
|
+
parsed.password = '';
|
|
56
|
+
return url.endsWith('/') ? parsed.toString() : parsed.toString().replace(/\/$/, '');
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return url;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.showUrl = showUrl;
|
|
63
|
+
/**
|
|
64
|
+
* The window of one call: every send of it, a send again after a new token included, waits
|
|
65
|
+
* for the server at most the window, counted from the start of the first try that failed.
|
|
66
|
+
*/
|
|
67
|
+
class Reconnecting {
|
|
68
|
+
constructor(options) {
|
|
69
|
+
this.options = options;
|
|
70
|
+
this.away = false;
|
|
71
|
+
this.delay = options.timing.firstDelayMs;
|
|
72
|
+
}
|
|
73
|
+
// the window ran out
|
|
74
|
+
get exhausted() {
|
|
75
|
+
return this.deadline !== undefined && this.deadline <= Date.now();
|
|
76
|
+
}
|
|
77
|
+
// how long the next try may take: no longer than what is left of the window, once it started
|
|
78
|
+
timeoutMs(own) {
|
|
79
|
+
return this.deadline === undefined ? own : Math.max(1, Math.min(own, this.deadline - Date.now()));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Sends a call until the server answers it, as long as it may be sent again and the server
|
|
83
|
+
* comes back within the window: an answer other than one of a server that is away is the
|
|
84
|
+
* caller's, an error that is no sign of one fails at once.
|
|
85
|
+
*/
|
|
86
|
+
async send(send, repeat) {
|
|
87
|
+
const { url, timing, say, signal, fail } = this.options;
|
|
88
|
+
const shown = showUrl(url);
|
|
89
|
+
for (;;) {
|
|
90
|
+
const started = Date.now();
|
|
91
|
+
const attempt = await send();
|
|
92
|
+
if (!mayRepeat(attempt, repeat)) {
|
|
93
|
+
if ('response' in attempt) {
|
|
94
|
+
if (this.away && !isAway(attempt.response)) {
|
|
95
|
+
this.away = false;
|
|
96
|
+
say?.(`${shown} is back`);
|
|
97
|
+
}
|
|
98
|
+
return attempt.response;
|
|
99
|
+
}
|
|
100
|
+
throw fail(attempt.message);
|
|
101
|
+
}
|
|
102
|
+
const reason = 'response' in attempt ? `${shown} answered ${attempt.response.status}` : attempt.message;
|
|
103
|
+
if ('response' in attempt) {
|
|
104
|
+
await attempt.response.body?.cancel().catch(() => undefined);
|
|
105
|
+
}
|
|
106
|
+
if (timing.windowMs <= 0) {
|
|
107
|
+
// no window: the call is not waited for, so nothing is said about it either
|
|
108
|
+
throw fail(reason);
|
|
109
|
+
}
|
|
110
|
+
const window = (0, units_1.formatDuration)(timing.windowMs);
|
|
111
|
+
this.deadline ?? (this.deadline = started + timing.windowMs);
|
|
112
|
+
if (!this.away) {
|
|
113
|
+
this.away = true;
|
|
114
|
+
say?.(`${reason}, reconnecting for up to ${window}`);
|
|
115
|
+
}
|
|
116
|
+
const left = this.deadline - Date.now();
|
|
117
|
+
if (left <= 0) {
|
|
118
|
+
throw fail(`${shown} did not come back within ${window}: ${reason}`);
|
|
119
|
+
}
|
|
120
|
+
if (signal?.aborted || !(await pause(Math.min(this.delay, left), signal))) {
|
|
121
|
+
throw fail(reason);
|
|
122
|
+
}
|
|
123
|
+
this.delay = Math.min(this.delay * 2, timing.maxDelayMs);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.Reconnecting = Reconnecting;
|
|
128
|
+
// false when the signal ended the pause
|
|
129
|
+
function pause(ms, signal) {
|
|
130
|
+
return new Promise((resolve) => {
|
|
131
|
+
const done = (waited) => {
|
|
132
|
+
clearTimeout(timer);
|
|
133
|
+
signal?.removeEventListener('abort', aborted);
|
|
134
|
+
resolve(waited);
|
|
135
|
+
};
|
|
136
|
+
const aborted = () => done(false);
|
|
137
|
+
const timer = setTimeout(() => done(true), ms);
|
|
138
|
+
signal?.addEventListener('abort', aborted, { once: true });
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=reconnect.js.map
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.quotePosix = exports.fetchFailure = exports.toSandboxPath = exports.checkSandboxPath = exports.exitCodeOf = exports.readRunLog = exports.getRun = exports.followRun = exports.getMoveTarget = exports.submitRun = exports.FINAL_RUN_STATES = exports.readTextUpTo = exports.oneAtATime = exports.SandboxClient = exports.listPublishedTasks = exports.runCheck = exports.CHECK_DESCRIPTION = exports.SandboxClientError = exports.MAX_ENDPOINT_BYTES = void 0;
|
|
3
|
+
exports.request = exports.quotePosix = exports.fetchFailure = exports.toSandboxPath = exports.checkSandboxPath = exports.exitCodeOf = exports.readRunLog = exports.downloadRunArtifact = exports.getRunArtifacts = exports.getRun = exports.followRun = exports.getMoveTarget = exports.lookupChecks = exports.submitRun = exports.FINAL_RUN_STATES = exports.readTextUpTo = exports.oneAtATime = exports.SandboxClient = exports.listPublishedTasks = exports.runCheck = exports.CHECK_DESCRIPTION = exports.getServerConnection = exports.SandboxClientError = exports.MAX_ENDPOINT_BYTES = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
4
5
|
const path_1 = require("path");
|
|
5
6
|
const token_source_1 = require("../remote/token-source");
|
|
7
|
+
const reconnect_1 = require("./reconnect");
|
|
8
|
+
const fs_1 = require("fs");
|
|
9
|
+
const stream_1 = require("stream");
|
|
10
|
+
const promises_1 = require("stream/promises");
|
|
6
11
|
// a write carries up to 32 MiB
|
|
7
12
|
const TIMEOUT_MS = 60000;
|
|
13
|
+
// an output of a run carries up to runs.artifacts.maxBytes, 256 MiB by default: 30 minutes is
|
|
14
|
+
// what that takes at about 150 KiB/s, the slowest link the default still downloads over. A
|
|
15
|
+
// server with a larger maxBytes needs a longer timeout, as the download is not split up.
|
|
16
|
+
const DOWNLOAD_TIMEOUT_MS = 30 * 60000;
|
|
8
17
|
// a run is asked for this long at a time, the log is read in between
|
|
9
18
|
const POLL_WAIT = '1s';
|
|
10
19
|
// a cancelled run waits this long for the server to take the cancel, before it gives up
|
|
@@ -19,6 +28,17 @@ class SandboxClientError extends Error {
|
|
|
19
28
|
}
|
|
20
29
|
}
|
|
21
30
|
exports.SandboxClientError = SandboxClientError;
|
|
31
|
+
// A connection to a registered server, which says when it reconnects, on stderr unless told
|
|
32
|
+
// where else, and stops when the command is stopped.
|
|
33
|
+
function getServerConnection(environment, name, server, say = (line) => environment.stderr.write(`${line}\n`)) {
|
|
34
|
+
return {
|
|
35
|
+
url: server.url,
|
|
36
|
+
...(0, token_source_1.getTokenSource)(environment, name, server),
|
|
37
|
+
say,
|
|
38
|
+
signal: environment.abortCtrl.signal,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.getServerConnection = getServerConnection;
|
|
22
42
|
// What the agent is told of a check, on each harness.
|
|
23
43
|
exports.CHECK_DESCRIPTION = 'Run a task as a check before you push, such as test or e2e: it runs on a copy of the files a push ' +
|
|
24
44
|
'would commit (what .gitignore leaves out is not there, so dependencies are installed by the tasks ' +
|
|
@@ -75,6 +95,10 @@ class SandboxClient {
|
|
|
75
95
|
this.connection = connection;
|
|
76
96
|
this.sandbox = sandbox;
|
|
77
97
|
this.build = build;
|
|
98
|
+
// What the agent last read of each file, or wrote, by its sha256. A write sends it as If-Match,
|
|
99
|
+
// so a write over a file that changed since is refused. A file this client never read is written
|
|
100
|
+
// as it is, and so is a new file: there is no hash to check.
|
|
101
|
+
this.seen = new Map();
|
|
78
102
|
}
|
|
79
103
|
get id() {
|
|
80
104
|
return this.sandbox.id;
|
|
@@ -92,9 +116,14 @@ class SandboxClient {
|
|
|
92
116
|
static async list(connection) {
|
|
93
117
|
return (await json(await call(connection, 'GET', '/v1/sandboxes'))).sandboxes;
|
|
94
118
|
}
|
|
95
|
-
// checks out the ref again,
|
|
119
|
+
// checks out the ref again, and the server refuses that over changes of the sandbox. Not sent
|
|
120
|
+
// again after a lost answer: the server may have moved the sandbox already, and a second
|
|
121
|
+
// fetch would move it again, past what the agent did since.
|
|
96
122
|
static async fetch(connection, id, ref) {
|
|
97
|
-
return json(await call(connection, 'POST', `/v1/sandboxes/${encodeURIComponent(id)}/fetch`, {
|
|
123
|
+
return json(await call(connection, 'POST', `/v1/sandboxes/${encodeURIComponent(id)}/fetch`, {
|
|
124
|
+
body: { ref },
|
|
125
|
+
repeat: 'unsent',
|
|
126
|
+
}));
|
|
98
127
|
}
|
|
99
128
|
async stat(path) {
|
|
100
129
|
if (checkSandboxPath(path) === '') {
|
|
@@ -109,25 +138,54 @@ class SandboxClient {
|
|
|
109
138
|
if (response.headers.get('x-hammerkit-type') === 'directory') {
|
|
110
139
|
throw new SandboxClientError(`${path} is a directory`, 400);
|
|
111
140
|
}
|
|
112
|
-
|
|
141
|
+
const content = Buffer.from(await response.arrayBuffer());
|
|
142
|
+
this.remember(checkSandboxPath(path), sha256Of(content));
|
|
143
|
+
return content;
|
|
113
144
|
}
|
|
145
|
+
// not sent again after a lost answer: a write sent again would write over what the agent did since.
|
|
146
|
+
// Refused with 412 when the file changed since the agent read it (see `seen`).
|
|
114
147
|
async writeRaw(path, content) {
|
|
115
|
-
|
|
148
|
+
const key = checkSandboxPath(path);
|
|
149
|
+
const written = await json(await call(this.connection, 'PUT', this.file(path), {
|
|
150
|
+
raw: content,
|
|
151
|
+
repeat: 'unsent',
|
|
152
|
+
ifMatch: this.seen.get(key),
|
|
153
|
+
}));
|
|
154
|
+
this.remember(key, sha256Of(content));
|
|
155
|
+
return written;
|
|
116
156
|
}
|
|
117
157
|
// numbered lines, like `cat -n`
|
|
118
158
|
async read(path, offset, limit) {
|
|
119
|
-
|
|
159
|
+
const read = await json(await call(this.connection, 'GET', this.file(path), { query: { offset, limit } }));
|
|
160
|
+
// the hash is of the whole file, however many lines were asked for
|
|
161
|
+
this.remember(checkSandboxPath(path), read.sha256);
|
|
162
|
+
return read;
|
|
120
163
|
}
|
|
121
164
|
async tree(query) {
|
|
122
165
|
return json(await call(this.connection, 'GET', `${this.base()}/tree`, { query: { ...query } }));
|
|
123
166
|
}
|
|
124
167
|
async search(query) {
|
|
125
|
-
return json(await call(this.connection, 'POST', `${this.base()}/search`, { body: query }));
|
|
168
|
+
return json(await call(this.connection, 'POST', `${this.base()}/search`, { body: query, repeat: 'always' }));
|
|
126
169
|
}
|
|
127
|
-
// all or nothing
|
|
170
|
+
// all or nothing; a replace or a patch sent again would not find what it changed, so only a
|
|
171
|
+
// dry run is sent again on a broken connection
|
|
128
172
|
async edit(ops, dryRun = false) {
|
|
129
|
-
const answer = await call(this.connection, 'POST', `${this.base()}/edits`, {
|
|
130
|
-
|
|
173
|
+
const answer = await call(this.connection, 'POST', `${this.base()}/edits`, {
|
|
174
|
+
body: { dryRun, ops: this.withHashes(ops) },
|
|
175
|
+
repeat: dryRun ? 'always' : 'unsent',
|
|
176
|
+
});
|
|
177
|
+
const files = (await json(answer)).files;
|
|
178
|
+
// a dry run changes nothing, so what the agent read stays as it was
|
|
179
|
+
if (!dryRun) {
|
|
180
|
+
for (const file of files) {
|
|
181
|
+
const key = fileKey(file.path);
|
|
182
|
+
// a file the edit left without a hash is one the agent has to read again
|
|
183
|
+
if (key !== undefined) {
|
|
184
|
+
this.remember(key, file.change === 'deleted' ? undefined : file.sha256);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return files;
|
|
131
189
|
}
|
|
132
190
|
async changes() {
|
|
133
191
|
return json(await call(this.connection, 'GET', `${this.base()}/changes`));
|
|
@@ -205,15 +263,16 @@ class SandboxClient {
|
|
|
205
263
|
...(request.ttl ? { ttl: request.ttl } : {}),
|
|
206
264
|
...(request.sleepTtl ? { sleepTtl: request.sleepTtl } : {}),
|
|
207
265
|
...(request.name ? { name: request.name } : {}),
|
|
266
|
+
...(request.restart?.length ? { restart: request.restart } : {}),
|
|
208
267
|
source: request.branch
|
|
209
268
|
? { type: 'ref', repo: this.sandbox.repo, ref: request.branch }
|
|
210
269
|
: { type: 'sandbox', id: this.id },
|
|
211
270
|
});
|
|
212
271
|
return followRun(this.connection, submitted, { signal: options.signal, untilUp: true });
|
|
213
272
|
}
|
|
214
|
-
// Stops a run: a service run takes its services and endpoints down.
|
|
273
|
+
// Stops a run: a service run takes its services and endpoints down. The same however often it comes.
|
|
215
274
|
async stop(id) {
|
|
216
|
-
return json(await call(this.connection, 'POST', `/v1/runs/${encodeURIComponent(id)}/cancel
|
|
275
|
+
return json(await call(this.connection, 'POST', `/v1/runs/${encodeURIComponent(id)}/cancel`, { repeat: 'always' }));
|
|
217
276
|
}
|
|
218
277
|
/**
|
|
219
278
|
* A page of an endpoint of a service run, as the caller sees it: only at an address the
|
|
@@ -277,6 +336,41 @@ class SandboxClient {
|
|
|
277
336
|
base() {
|
|
278
337
|
return `/v1/sandboxes/${encodeURIComponent(this.id)}`;
|
|
279
338
|
}
|
|
339
|
+
// The ops of an edit as they go to the server, with the paths the server checks. The first op on
|
|
340
|
+
// a file carries the hash the agent read of it. The server checks a file as the edit has changed
|
|
341
|
+
// it so far, so an op after one that changed its file, a move's destination included, or after a
|
|
342
|
+
// patch (which names files of its own, with no hash), carries none. An invalid path goes as it is.
|
|
343
|
+
withHashes(ops) {
|
|
344
|
+
const changed = new Set();
|
|
345
|
+
let patched = false;
|
|
346
|
+
return ops.map((op) => {
|
|
347
|
+
if (op.op === 'patch') {
|
|
348
|
+
patched = true;
|
|
349
|
+
return op;
|
|
350
|
+
}
|
|
351
|
+
const key = fileKey(op.path);
|
|
352
|
+
if (key === undefined) {
|
|
353
|
+
return op;
|
|
354
|
+
}
|
|
355
|
+
const hash = !patched && !changed.has(key) ? this.seen.get(key) : undefined;
|
|
356
|
+
changed.add(key);
|
|
357
|
+
const to = op.op === 'move' ? fileKey(op.to) : undefined;
|
|
358
|
+
if (to !== undefined) {
|
|
359
|
+
changed.add(to);
|
|
360
|
+
}
|
|
361
|
+
const sent = op.op === 'move' ? { ...op, path: key, to: to ?? op.to } : { ...op, path: key };
|
|
362
|
+
return hash === undefined || op.ifHash !== undefined ? sent : { ...sent, ifHash: hash };
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
// a hash the server gave that is not a sha256 is not one the agent read, so the file is read again
|
|
366
|
+
remember(key, sha256) {
|
|
367
|
+
if (sha256 !== undefined && SHA256.test(sha256)) {
|
|
368
|
+
this.seen.set(key, sha256);
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
this.seen.delete(key);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
280
374
|
file(path) {
|
|
281
375
|
const clean = checkSandboxPath(path);
|
|
282
376
|
if (!clean) {
|
|
@@ -286,6 +380,20 @@ class SandboxClient {
|
|
|
286
380
|
}
|
|
287
381
|
}
|
|
288
382
|
exports.SandboxClient = SandboxClient;
|
|
383
|
+
// The key of a file in the record of what the agent read: its clean path, or undefined for a path
|
|
384
|
+
// outside the sandbox, which the server refuses anyway
|
|
385
|
+
function fileKey(path) {
|
|
386
|
+
try {
|
|
387
|
+
return checkSandboxPath(path);
|
|
388
|
+
}
|
|
389
|
+
catch {
|
|
390
|
+
return undefined;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
const SHA256 = /^[0-9a-f]{64}$/;
|
|
394
|
+
function sha256Of(content) {
|
|
395
|
+
return (0, crypto_1.createHash)('sha256').update(content).digest('hex');
|
|
396
|
+
}
|
|
289
397
|
// The first lines of every shell command: it stops when the working directory is empty,
|
|
290
398
|
// links included, without any tool but the shell itself.
|
|
291
399
|
const EMPTY_MARKER = 'hammerkit: the shell sees no files';
|
|
@@ -347,6 +455,12 @@ async function submitRun(connection, request) {
|
|
|
347
455
|
return json(await call(connection, 'POST', '/v1/runs', { body: request }));
|
|
348
456
|
}
|
|
349
457
|
exports.submitRun = submitRun;
|
|
458
|
+
// The newest passed run of each check key, as `POST /v1/checks/lookup` answers it: a read,
|
|
459
|
+
// asked again while the server is away.
|
|
460
|
+
async function lookupChecks(connection, repo, keys) {
|
|
461
|
+
return json(await call(connection, 'POST', '/v1/checks/lookup', { body: { repo, keys }, repeat: 'always' }));
|
|
462
|
+
}
|
|
463
|
+
exports.lookupChecks = lookupChecks;
|
|
350
464
|
/**
|
|
351
465
|
* Follows a run to its end: its log as it grows, and its state. A signal cancels the run
|
|
352
466
|
* on the server and fails the follow. A service run that moved to another commit is followed
|
|
@@ -373,29 +487,70 @@ function isThere(run, submitted) {
|
|
|
373
487
|
}
|
|
374
488
|
async function followRun(connection, submitted, options = {}) {
|
|
375
489
|
const path = `/v1/runs/${encodeURIComponent(submitted.id)}`;
|
|
490
|
+
// the stop of the follow, or of the command when it names none
|
|
491
|
+
const signal = options.signal ?? connection.signal;
|
|
376
492
|
let cancelling;
|
|
377
493
|
const cancel = () => {
|
|
378
494
|
if (!submitted.moved) {
|
|
379
|
-
|
|
495
|
+
// the same however often it comes, and waited for only as long as the follow waits for it
|
|
496
|
+
cancelling ?? (cancelling = call(connection, 'POST', `${path}/cancel`, {
|
|
497
|
+
repeat: 'always',
|
|
498
|
+
reconnectMs: CANCEL_WAIT_MS,
|
|
499
|
+
signal: null,
|
|
500
|
+
}).catch(() => undefined));
|
|
380
501
|
}
|
|
381
502
|
};
|
|
382
503
|
if (!options.detach) {
|
|
383
|
-
|
|
504
|
+
signal?.addEventListener('abort', cancel, { once: true });
|
|
384
505
|
}
|
|
385
506
|
const lines = [];
|
|
386
507
|
let from = submitted.moved?.logFrom ?? 1;
|
|
387
508
|
let truncated = false;
|
|
509
|
+
// where the log went on after a server that was killed lost its end (LogPage.lost), as the
|
|
510
|
+
// follow last heard it, and the last line it read itself: it reads again from `lost` only
|
|
511
|
+
// when it read lines there, which are gone, not when it started past it (a moved run)
|
|
512
|
+
let lost;
|
|
513
|
+
let read = 0;
|
|
388
514
|
const add = (line) => {
|
|
389
515
|
lines.push(line);
|
|
390
516
|
options.onLine?.(line);
|
|
391
517
|
};
|
|
518
|
+
// a server that is away is waited for until the signal stops the follow, which goes on then
|
|
519
|
+
// as a stopped one
|
|
520
|
+
const stoppable = (read) => read.catch((e) => {
|
|
521
|
+
if (signal?.aborted) {
|
|
522
|
+
return undefined;
|
|
523
|
+
}
|
|
524
|
+
throw e;
|
|
525
|
+
});
|
|
392
526
|
const readLog = async () => {
|
|
393
527
|
for (;;) {
|
|
394
|
-
const page = await
|
|
528
|
+
const page = await stoppable(call(connection, 'GET', `${path}/logs`, {
|
|
529
|
+
query: { task: options.task, from, limit: 1000 },
|
|
530
|
+
signal,
|
|
531
|
+
}).then(json));
|
|
532
|
+
if (!page) {
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
395
535
|
truncated = page.truncated;
|
|
536
|
+
if (page.lost !== undefined && page.lost !== lost) {
|
|
537
|
+
lost = page.lost;
|
|
538
|
+
if (read >= page.lost) {
|
|
539
|
+
// the lines from there on are not the ones read before: they are gone with the server
|
|
540
|
+
add({
|
|
541
|
+
line: 0,
|
|
542
|
+
task: 'hammerkit',
|
|
543
|
+
stream: 'stderr',
|
|
544
|
+
text: `… the server restarted and lost the last lines it had sent, the log goes on from line ${page.lost}`,
|
|
545
|
+
});
|
|
546
|
+
from = page.lost;
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
396
550
|
for (const line of page.lines) {
|
|
397
551
|
add(line);
|
|
398
552
|
from = line.line + 1;
|
|
553
|
+
read = line.line;
|
|
399
554
|
}
|
|
400
555
|
if (!page.next) {
|
|
401
556
|
return;
|
|
@@ -406,10 +561,10 @@ async function followRun(connection, submitted, options = {}) {
|
|
|
406
561
|
let run = submitted;
|
|
407
562
|
let waitingSaid = false;
|
|
408
563
|
for (;;) {
|
|
409
|
-
if (
|
|
564
|
+
if (signal?.aborted && options.detach) {
|
|
410
565
|
break;
|
|
411
566
|
}
|
|
412
|
-
if (
|
|
567
|
+
if (signal?.aborted) {
|
|
413
568
|
// the process may exit after this, so the cancel has to reach the server first
|
|
414
569
|
cancel();
|
|
415
570
|
let timer;
|
|
@@ -425,7 +580,8 @@ async function followRun(connection, submitted, options = {}) {
|
|
|
425
580
|
if (exports.FINAL_RUN_STATES.includes(run.state) || (options.untilUp && isThere(run, submitted))) {
|
|
426
581
|
break;
|
|
427
582
|
}
|
|
428
|
-
run =
|
|
583
|
+
run =
|
|
584
|
+
(await stoppable(call(connection, 'GET', path, { query: { wait: POLL_WAIT }, signal }).then(json))) ?? run;
|
|
429
585
|
}
|
|
430
586
|
if (truncated) {
|
|
431
587
|
truncatedEnd(run, options.task).forEach(add);
|
|
@@ -433,7 +589,7 @@ async function followRun(connection, submitted, options = {}) {
|
|
|
433
589
|
return { run, lines, truncated };
|
|
434
590
|
}
|
|
435
591
|
finally {
|
|
436
|
-
|
|
592
|
+
signal?.removeEventListener('abort', cancel);
|
|
437
593
|
}
|
|
438
594
|
}
|
|
439
595
|
exports.followRun = followRun;
|
|
@@ -460,6 +616,26 @@ async function getRun(connection, id) {
|
|
|
460
616
|
return response.status === 404 ? null : json(response);
|
|
461
617
|
}
|
|
462
618
|
exports.getRun = getRun;
|
|
619
|
+
// null when the caller may not see the run (or the server keeps none of it)
|
|
620
|
+
async function getRunArtifacts(connection, id) {
|
|
621
|
+
const response = await call(connection, 'GET', `/v1/runs/${encodeURIComponent(id)}/artifacts`, { missing: true });
|
|
622
|
+
return response.status === 404 ? null : json(response);
|
|
623
|
+
}
|
|
624
|
+
exports.getRunArtifacts = getRunArtifacts;
|
|
625
|
+
/**
|
|
626
|
+
* Writes the archive of one output of a run to `file`: a gzipped tar of its paths in the
|
|
627
|
+
* project. It may be large, so it is written as it comes and may take longer than a call.
|
|
628
|
+
*/
|
|
629
|
+
async function downloadRunArtifact(connection, id, output, file) {
|
|
630
|
+
const response = await call(connection, 'GET', `/v1/runs/${encodeURIComponent(id)}/artifacts/${output}`, {
|
|
631
|
+
timeoutMs: DOWNLOAD_TIMEOUT_MS,
|
|
632
|
+
});
|
|
633
|
+
if (!response.body) {
|
|
634
|
+
throw new SandboxClientError(`${connection.url} answered without the archive`);
|
|
635
|
+
}
|
|
636
|
+
await (0, promises_1.pipeline)(stream_1.Readable.fromWeb(response.body), (0, fs_1.createWriteStream)(file));
|
|
637
|
+
}
|
|
638
|
+
exports.downloadRunArtifact = downloadRunArtifact;
|
|
463
639
|
/**
|
|
464
640
|
* Reads the log of a run as it is now, every line or the last `tail` of them, of one task or
|
|
465
641
|
* of all: also of a run that ended, as long as the server keeps it.
|
|
@@ -566,42 +742,74 @@ exports.quotePosix = quotePosix;
|
|
|
566
742
|
function json(response) {
|
|
567
743
|
return response.json();
|
|
568
744
|
}
|
|
569
|
-
|
|
745
|
+
/**
|
|
746
|
+
* The answer to a call, whatever its status. A call the server is away for is sent again as
|
|
747
|
+
* `repeat` allows, within the window of the connection; a token the server does not accept is
|
|
748
|
+
* renewed once and the call sent again.
|
|
749
|
+
*/
|
|
750
|
+
async function request(connection, method, path, options = {}) {
|
|
570
751
|
const url = new URL(`${connection.url.replace(/\/$/, '')}${path}`);
|
|
571
752
|
for (const [key, value] of Object.entries(options.query ?? {})) {
|
|
572
753
|
if (value !== undefined) {
|
|
573
754
|
url.searchParams.set(key, String(value));
|
|
574
755
|
}
|
|
575
756
|
}
|
|
576
|
-
const
|
|
577
|
-
|
|
578
|
-
|
|
757
|
+
const shown = (0, reconnect_1.showUrl)(connection.url);
|
|
758
|
+
// the stop of the command, unless the call names its own or none
|
|
759
|
+
const stop = options.signal === undefined ? connection.signal : options.signal ?? undefined;
|
|
760
|
+
const timing = connection.reconnect ?? reconnect_1.RECONNECT_TIMING;
|
|
761
|
+
const reconnecting = new reconnect_1.Reconnecting({
|
|
762
|
+
url: connection.url,
|
|
763
|
+
timing: options.reconnectMs !== undefined ? { ...timing, windowMs: options.reconnectMs } : timing,
|
|
764
|
+
say: connection.say,
|
|
765
|
+
signal: stop,
|
|
766
|
+
fail: options.fail ?? ((message) => new SandboxClientError(message)),
|
|
767
|
+
});
|
|
768
|
+
const attempt = async () => {
|
|
769
|
+
const token = 'token' in connection ? await connection.token() : undefined;
|
|
770
|
+
if (token !== undefined && !(0, token_source_1.isToken)(token)) {
|
|
579
771
|
// a header refuses such a value and would quote it whole in its error
|
|
580
772
|
throw new SandboxClientError('the token is not a single line of visible characters');
|
|
581
773
|
}
|
|
774
|
+
// a try after one that failed takes no longer than what is left of the window
|
|
775
|
+
const timeout = AbortSignal.timeout(reconnecting.timeoutMs(options.timeoutMs ?? TIMEOUT_MS));
|
|
582
776
|
return fetch(url, {
|
|
583
777
|
method,
|
|
584
778
|
headers: {
|
|
585
|
-
authorization: `Bearer ${token}
|
|
779
|
+
...(token !== undefined ? { authorization: `Bearer ${token}` } : {}),
|
|
586
780
|
...(options.body !== undefined ? { 'content-type': 'application/json' } : {}),
|
|
587
781
|
...(options.raw !== undefined ? { 'content-type': 'application/octet-stream' } : {}),
|
|
782
|
+
...(options.ifMatch !== undefined ? { 'if-match': `"${options.ifMatch}"` } : {}),
|
|
588
783
|
},
|
|
589
784
|
body: options.body !== undefined ? JSON.stringify(options.body) : options.raw,
|
|
590
|
-
signal: AbortSignal.timeout
|
|
785
|
+
signal: stop ? AbortSignal.any([timeout, stop]) : timeout,
|
|
591
786
|
// an answer from anywhere else is not the server's, and a PUT would send its file on
|
|
592
787
|
redirect: 'error',
|
|
593
|
-
}).
|
|
594
|
-
|
|
595
|
-
|
|
788
|
+
}).then((response) => ({ response }), (e) => ({
|
|
789
|
+
error: e,
|
|
790
|
+
message: stop?.aborted
|
|
791
|
+
? `stopped while asking ${shown}`
|
|
792
|
+
: `unable to reach ${shown}: ${token === undefined ? fetchFailure(e) : redact(fetchFailure(e), token)}`,
|
|
793
|
+
}));
|
|
596
794
|
};
|
|
597
|
-
|
|
598
|
-
|
|
795
|
+
const repeat = options.repeat ?? (method === 'GET' || method === 'PUT' ? 'always' : 'unsent');
|
|
796
|
+
let response = await reconnecting.send(attempt, repeat);
|
|
797
|
+
// a new token is asked for within the window the call has left, not after it ran out
|
|
798
|
+
if (response.status === 401 && 'token' in connection && connection.renew && !reconnecting.exhausted) {
|
|
599
799
|
await connection.renew();
|
|
600
|
-
response = await send();
|
|
800
|
+
response = await reconnecting.send(attempt, repeat);
|
|
601
801
|
}
|
|
802
|
+
return response;
|
|
803
|
+
}
|
|
804
|
+
exports.request = request;
|
|
805
|
+
// The body of a call that answers 2xx (or a 404 it was asked to take as an answer); any other
|
|
806
|
+
// status is an error, which says what the server answered.
|
|
807
|
+
async function call(connection, method, path, options = {}) {
|
|
808
|
+
const response = await request(connection, method, path, options);
|
|
602
809
|
if (response.ok || (options.missing && response.status === 404)) {
|
|
603
810
|
return response;
|
|
604
811
|
}
|
|
812
|
+
const shown = (0, reconnect_1.showUrl)(connection.url);
|
|
605
813
|
let said = '';
|
|
606
814
|
try {
|
|
607
815
|
const body = await json(response);
|
|
@@ -611,8 +819,8 @@ async function call(connection, method, path, options = {}) {
|
|
|
611
819
|
// no JSON
|
|
612
820
|
}
|
|
613
821
|
if (response.status === 401) {
|
|
614
|
-
throw new SandboxClientError(`${
|
|
822
|
+
throw new SandboxClientError(`${shown} did not accept the token`, 401);
|
|
615
823
|
}
|
|
616
|
-
throw new SandboxClientError(said || `${
|
|
824
|
+
throw new SandboxClientError(said || `${shown} answered ${response.status}`, response.status);
|
|
617
825
|
}
|
|
618
826
|
//# sourceMappingURL=sandbox-client.js.map
|
|
@@ -88,8 +88,11 @@ exports.SERVICE_TOOLS = [
|
|
|
88
88
|
description: 'Start services of the published build file on the hammerkit server, with the files of the sandbox (or of ' +
|
|
89
89
|
'a branch), and keep them up for a person to look at: each endpoint (service:port) gets an address of its ' +
|
|
90
90
|
'own behind the sign-in of the server. Answers once they are up, with the addresses and until when they ' +
|
|
91
|
-
'stay. A second start with the same name replaces the first, so its addresses stay the same.
|
|
92
|
-
'Kubernetes
|
|
91
|
+
'stay. A second start with the same name replaces the first, so its addresses stay the same. Services from ' +
|
|
92
|
+
'the sandbox on a Kubernetes server follow its edits a few seconds after each one: the changed files go into ' +
|
|
93
|
+
'the services that watch their files (continuous), and the others keep the files they started with, unless ' +
|
|
94
|
+
'restart names them, which starts them again with the new files. On a host the services run on the sandbox ' +
|
|
95
|
+
'files themselves, so there is nothing to follow and restart is refused.',
|
|
93
96
|
parameters: {
|
|
94
97
|
services: { type: 'string[]', description: 'The services to start, as the build file names them' },
|
|
95
98
|
endpoints: { type: 'string[]', description: 'The ports to publish, as service:port, such as web:8080' },
|
|
@@ -116,6 +119,11 @@ exports.SERVICE_TOOLS = [
|
|
|
116
119
|
type: 'string',
|
|
117
120
|
description: 'Start from this pushed branch of the repository instead of the files of the sandbox',
|
|
118
121
|
},
|
|
122
|
+
restart: {
|
|
123
|
+
type: 'string[]',
|
|
124
|
+
description: 'The services that start again with the files of the sandbox after an edit of it; the others keep ' +
|
|
125
|
+
'the files they started with. Only with the sandbox, not with branch',
|
|
126
|
+
},
|
|
119
127
|
},
|
|
120
128
|
required: ['services'],
|
|
121
129
|
async call(client, args, signal) {
|
|
@@ -127,6 +135,7 @@ exports.SERVICE_TOOLS = [
|
|
|
127
135
|
sleepTtl: args.sleepTtl || undefined,
|
|
128
136
|
name: args.name || undefined,
|
|
129
137
|
branch: args.branch || undefined,
|
|
138
|
+
restart: args.restart?.length ? args.restart : undefined,
|
|
130
139
|
}, { signal });
|
|
131
140
|
return describeServiceRun(followed.run);
|
|
132
141
|
},
|