hammerkit 2.1.0-next.17 → 2.1.0-next.19
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/harness/connect.js +2 -1
- package/dist/harness/sandbox-client.js +7 -3
- package/dist/harness/service-tools.js +11 -2
- package/dist/kubernetes/sync-changes.js +180 -0
- package/dist/planner/work-runtime-kubernetes.js +20 -0
- package/dist/server/executor.js +41 -7
- package/dist/server/kubernetes-backend.js +7 -4
- package/dist/server/run-request.js +11 -0
- package/dist/server/run-worker.js +145 -6
- package/dist/server/runs.js +20 -0
- package/dist/server/sandbox-follow.js +54 -0
- package/dist/server/sandbox-storage.js +38 -1
- package/dist/server/sandbox-sync.js +357 -0
- package/dist/server/sandboxes.js +19 -0
- package/dist/server/server.js +39 -7
- package/package.json +1 -1
package/dist/harness/connect.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getHarnessEnvironment = exports.connectSandbox = exports.connectServer = exports.hasShell = exports.harnessOptionsFromEnv = exports.DEFAULT_SHELL_TASK = void 0;
|
|
3
|
+
exports.getHarnessEnvironment = exports.reuseSandbox = exports.connectSandbox = exports.connectServer = exports.hasShell = exports.harnessOptionsFromEnv = exports.DEFAULT_SHELL_TASK = void 0;
|
|
4
4
|
const get_file_context_1 = require("../file/get-file-context");
|
|
5
5
|
const log_1 = require("../log");
|
|
6
6
|
const work_item_status_1 = require("../planner/work-item-status");
|
|
@@ -90,6 +90,7 @@ async function reuseSandbox(connection, repo, ref) {
|
|
|
90
90
|
}
|
|
91
91
|
return undefined;
|
|
92
92
|
}
|
|
93
|
+
exports.reuseSandbox = reuseSandbox;
|
|
93
94
|
// What hammerkit needs of its environment to read the host config and the sign-in, for a
|
|
94
95
|
// harness that loads it as a plugin rather than through the command line.
|
|
95
96
|
function getHarnessEnvironment(cwd = process.cwd()) {
|
|
@@ -111,11 +111,13 @@ class SandboxClient {
|
|
|
111
111
|
static async list(connection) {
|
|
112
112
|
return (await json(await call(connection, 'GET', '/v1/sandboxes'))).sandboxes;
|
|
113
113
|
}
|
|
114
|
-
// checks out the ref again,
|
|
114
|
+
// checks out the ref again, and the server refuses that over changes of the sandbox. Not sent
|
|
115
|
+
// again after a lost answer: the server may have moved the sandbox already, and a second
|
|
116
|
+
// fetch would move it again, past what the agent did since.
|
|
115
117
|
static async fetch(connection, id, ref) {
|
|
116
118
|
return json(await call(connection, 'POST', `/v1/sandboxes/${encodeURIComponent(id)}/fetch`, {
|
|
117
119
|
body: { ref },
|
|
118
|
-
repeat: '
|
|
120
|
+
repeat: 'unsent',
|
|
119
121
|
}));
|
|
120
122
|
}
|
|
121
123
|
async stat(path) {
|
|
@@ -133,8 +135,9 @@ class SandboxClient {
|
|
|
133
135
|
}
|
|
134
136
|
return Buffer.from(await response.arrayBuffer());
|
|
135
137
|
}
|
|
138
|
+
// not sent again after a lost answer: a write sent again would write over what the agent did since
|
|
136
139
|
async writeRaw(path, content) {
|
|
137
|
-
return json(await call(this.connection, 'PUT', this.file(path), { raw: content }));
|
|
140
|
+
return json(await call(this.connection, 'PUT', this.file(path), { raw: content, repeat: 'unsent' }));
|
|
138
141
|
}
|
|
139
142
|
// numbered lines, like `cat -n`
|
|
140
143
|
async read(path, offset, limit) {
|
|
@@ -230,6 +233,7 @@ class SandboxClient {
|
|
|
230
233
|
...(request.ttl ? { ttl: request.ttl } : {}),
|
|
231
234
|
...(request.sleepTtl ? { sleepTtl: request.sleepTtl } : {}),
|
|
232
235
|
...(request.name ? { name: request.name } : {}),
|
|
236
|
+
...(request.restart?.length ? { restart: request.restart } : {}),
|
|
233
237
|
source: request.branch
|
|
234
238
|
? { type: 'ref', repo: this.sandbox.repo, ref: request.branch }
|
|
235
239
|
: { type: 'sandbox', id: this.id },
|
|
@@ -71,8 +71,11 @@ exports.SERVICE_TOOLS = [
|
|
|
71
71
|
description: 'Start services of the published build file on the hammerkit server, with the files of the sandbox (or of ' +
|
|
72
72
|
'a branch), and keep them up for a person to look at: each endpoint (service:port) gets an address of its ' +
|
|
73
73
|
'own behind the sign-in of the server. Answers once they are up, with the addresses and until when they ' +
|
|
74
|
-
'stay. A second start with the same name replaces the first, so its addresses stay the same.
|
|
75
|
-
'Kubernetes
|
|
74
|
+
'stay. A second start with the same name replaces the first, so its addresses stay the same. Services from ' +
|
|
75
|
+
'the sandbox on a Kubernetes server follow its edits a few seconds after each one: the changed files go into ' +
|
|
76
|
+
'the services that watch their files (continuous), and the others keep the files they started with, unless ' +
|
|
77
|
+
'restart names them, which starts them again with the new files. On a host the services run on the sandbox ' +
|
|
78
|
+
'files themselves, so there is nothing to follow and restart is refused.',
|
|
76
79
|
parameters: {
|
|
77
80
|
services: { type: 'string[]', description: 'The services to start, as the build file names them' },
|
|
78
81
|
endpoints: { type: 'string[]', description: 'The ports to publish, as service:port, such as web:8080' },
|
|
@@ -93,6 +96,11 @@ exports.SERVICE_TOOLS = [
|
|
|
93
96
|
type: 'string',
|
|
94
97
|
description: 'Start from this pushed branch of the repository instead of the files of the sandbox',
|
|
95
98
|
},
|
|
99
|
+
restart: {
|
|
100
|
+
type: 'string[]',
|
|
101
|
+
description: 'The services that start again with the files of the sandbox after an edit of it; the others keep ' +
|
|
102
|
+
'the files they started with. Only with the sandbox, not with branch',
|
|
103
|
+
},
|
|
96
104
|
},
|
|
97
105
|
required: ['services'],
|
|
98
106
|
async call(client, args, signal) {
|
|
@@ -103,6 +111,7 @@ exports.SERVICE_TOOLS = [
|
|
|
103
111
|
sleepTtl: args.sleepTtl || undefined,
|
|
104
112
|
name: args.name || undefined,
|
|
105
113
|
branch: args.branch || undefined,
|
|
114
|
+
restart: args.restart?.length ? args.restart : undefined,
|
|
106
115
|
}, { signal });
|
|
107
116
|
return describeServiceRun(followed.run);
|
|
108
117
|
},
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.restartPods = exports.syncChangedFiles = exports.SYNC_CHANGES = exports.getChangeTarget = exports.CHANGES_STAGING_DIR = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const stream_1 = require("stream");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const volumes_1 = require("./volumes");
|
|
8
|
+
const ensure_persistent_data_1 = require("./ensure-persistent-data");
|
|
9
|
+
const transfer_tools_1 = require("./transfer-tools");
|
|
10
|
+
const tar_1 = require("../utils/tar");
|
|
11
|
+
const resources_1 = require("./resources");
|
|
12
|
+
// The folder of the claim the changed files of a sandbox are uploaded to, before they are
|
|
13
|
+
// applied to the folder of the service (SYNC_CHANGES). It is not a folder of a state, so the
|
|
14
|
+
// removal of other states leaves it be.
|
|
15
|
+
exports.CHANGES_STAGING_DIR = '.hammerkit-sync';
|
|
16
|
+
/**
|
|
17
|
+
* Where the changed files of a service of a service run go, and whether its Deployment starts
|
|
18
|
+
* again with them. A service that watches its files (`live`) gets them into the folder its
|
|
19
|
+
* pod mounts, and keeps running. One that doesn't keeps the files it started with, unless it
|
|
20
|
+
* restarts: then they go into the folder of its state, the one its pod mounts, and its pods
|
|
21
|
+
* start again. Undefined for a service that takes nothing.
|
|
22
|
+
*/
|
|
23
|
+
function getChangeTarget(live, restart, stateDir) {
|
|
24
|
+
if (live) {
|
|
25
|
+
return { dir: volumes_1.LIVE_FILES_DIR, rollout: restart };
|
|
26
|
+
}
|
|
27
|
+
return restart ? { dir: stateDir, rollout: true } : undefined;
|
|
28
|
+
}
|
|
29
|
+
exports.getChangeTarget = getChangeTarget;
|
|
30
|
+
/**
|
|
31
|
+
* Applies the staged batch at `$1` to the folder `$2`. Its `deleted` file lists the paths that
|
|
32
|
+
* are gone, NUL-separated, and its `files` folder the files that changed. The paths after `$2`
|
|
33
|
+
* are the ones pods mount from the folder: a deleted one of them stays.
|
|
34
|
+
* - Deletions come first. Then each file is written in place, only where its content or mode
|
|
35
|
+
* differs, so a file a pod mounts keeps its inode.
|
|
36
|
+
* - Nothing is written or removed through a link, or where a file is in the way of a folder:
|
|
37
|
+
* each folder on the way is checked from `$2` down, one component at a time. Such a path, and
|
|
38
|
+
* one with a `.` or `..` segment or a leading `/`, is skipped and reported as `skipped <path>: <why>`.
|
|
39
|
+
* The staged folder goes at the end.
|
|
40
|
+
*/
|
|
41
|
+
exports.SYNC_CHANGES = `src="$1"
|
|
42
|
+
dst="$2"
|
|
43
|
+
shift 2
|
|
44
|
+
mkdir -p "$src" "$dst" || exit 1
|
|
45
|
+
if [ -L "$dst" ]; then echo "skipped $dst: it is a link" >&2; exit 1; fi
|
|
46
|
+
# checks the folders of the relative path $1 under $dst: none may be a link or a file, and with
|
|
47
|
+
# $2 set the missing ones are made. False when one is a link or a file on the way.
|
|
48
|
+
walk() {
|
|
49
|
+
cur="$dst"
|
|
50
|
+
rest="$1"
|
|
51
|
+
while [ "$rest" != "\${rest#*/}" ]; do
|
|
52
|
+
comp="\${rest%%/*}"
|
|
53
|
+
rest="\${rest#*/}"
|
|
54
|
+
cur="$cur/$comp"
|
|
55
|
+
if [ -L "$cur" ]; then return 1; fi
|
|
56
|
+
if [ -e "$cur" ]; then
|
|
57
|
+
if [ ! -d "$cur" ]; then return 1; fi
|
|
58
|
+
elif [ -n "$2" ]; then
|
|
59
|
+
mkdir "$cur" || return 1
|
|
60
|
+
else
|
|
61
|
+
return 0
|
|
62
|
+
fi
|
|
63
|
+
done
|
|
64
|
+
return 0
|
|
65
|
+
}
|
|
66
|
+
if [ -f "$src/deleted" ]; then
|
|
67
|
+
tr '\\0' '\\n' < "$src/deleted" | while IFS= read -r p || [ -n "$p" ]; do
|
|
68
|
+
if [ -z "$p" ]; then continue; fi
|
|
69
|
+
case "$p" in
|
|
70
|
+
/*|.|./*|*/.|*/./*|..|../*|*/..|*/../*) echo "skipped $p: not a path of the sandbox"; continue ;;
|
|
71
|
+
esac
|
|
72
|
+
keep=
|
|
73
|
+
for m in "$@"; do
|
|
74
|
+
if [ "$p" = "$m" ]; then keep=1; fi
|
|
75
|
+
done
|
|
76
|
+
if [ -n "$keep" ]; then continue; fi
|
|
77
|
+
if ! walk "$p"; then echo "skipped $p: a link or a file is in its way"; continue; fi
|
|
78
|
+
rm -rf "$dst/$p" || exit 1
|
|
79
|
+
done || exit 1
|
|
80
|
+
fi
|
|
81
|
+
if [ -d "$src/files" ]; then
|
|
82
|
+
find "$src/files" -type f | while IFS= read -r s; do
|
|
83
|
+
f="\${s#"$src/files/"}"
|
|
84
|
+
case "$f" in
|
|
85
|
+
/*|.|./*|*/.|*/./*|..|../*|*/..|*/../*) echo "skipped $f: not a path of the sandbox"; continue ;;
|
|
86
|
+
esac
|
|
87
|
+
if ! walk "$f" yes; then echo "skipped $f: a link or a file is in its way"; continue; fi
|
|
88
|
+
t="$dst/$f"
|
|
89
|
+
if [ -L "$t" ] || [ -d "$t" ]; then rm -rf "$t" || exit 1; fi
|
|
90
|
+
if [ -e "$t" ]; then
|
|
91
|
+
if ! cmp -s "$s" "$t"; then cat "$s" > "$t" || exit 1; fi
|
|
92
|
+
else
|
|
93
|
+
cp -p "$s" "$t" || exit 1
|
|
94
|
+
fi
|
|
95
|
+
m=$(stat -c %a "$s") || exit 1
|
|
96
|
+
if [ "$(stat -c %a "$t")" != "$m" ]; then chmod "$m" "$t" || exit 1; fi
|
|
97
|
+
done || exit 1
|
|
98
|
+
fi
|
|
99
|
+
rm -rf "$src" || exit 1
|
|
100
|
+
exit 0`;
|
|
101
|
+
// The paths of the folder `dir` that the pods of the service mount, relative to it
|
|
102
|
+
function mountedUnder(persistence, dir) {
|
|
103
|
+
return persistence.mounts.flatMap(({ mount }) => mount.subPath?.startsWith(`${dir}/`) ? [mount.subPath.slice(dir.length + 1)] : []);
|
|
104
|
+
}
|
|
105
|
+
// Uploads the entries of the batch folder `batch` into `target` of the upload pod `pod`
|
|
106
|
+
async function uploadStaged(instance, env, pod, batch, target) {
|
|
107
|
+
const entries = (0, fs_1.readdirSync)(batch);
|
|
108
|
+
const data = stream_1.Readable.from((0, tar_1.packArchive)({ cwd: batch }, entries));
|
|
109
|
+
const res = await instance.exec.exec(env.namespace, pod, 'volume', (0, transfer_tools_1.inTransferPod)(['tar', 'xf', '-', '-C', target]), process.stdout, process.stderr, data, false);
|
|
110
|
+
await new Promise((resolve, reject) => {
|
|
111
|
+
res.on('error', reject);
|
|
112
|
+
res.on('close', () => resolve());
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
// Runs `command` in the upload pod `pod`: what failed (null when it went well), and what it printed
|
|
116
|
+
async function execCapturing(instance, env, pod, command) {
|
|
117
|
+
let failure = null;
|
|
118
|
+
let output = '';
|
|
119
|
+
const out = new stream_1.PassThrough();
|
|
120
|
+
out.on('data', (chunk) => (output += chunk.toString('utf8')));
|
|
121
|
+
const res = await instance.exec.exec(env.namespace, pod, 'volume', (0, transfer_tools_1.inTransferPod)(command), out, process.stderr, null, false, (status) => {
|
|
122
|
+
if (status.status !== 'Success') {
|
|
123
|
+
failure = status.message ?? status.reason ?? 'unknown';
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
await new Promise((resolve, reject) => {
|
|
127
|
+
res.on('error', reject);
|
|
128
|
+
res.on('close', () => resolve());
|
|
129
|
+
});
|
|
130
|
+
return { failure, output };
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Syncs the changed files of a sandbox into the folder `dir` of the claim of `service`: only
|
|
134
|
+
* what changed is uploaded and applied, nothing else of the service is touched. The batch
|
|
135
|
+
* folder of the changes (`dirname` of `changes.dir`) gets its `deleted` list here, NUL-separated.
|
|
136
|
+
* Returns the paths the pod skipped (see SYNC_CHANGES).
|
|
137
|
+
*/
|
|
138
|
+
async function syncChangedFiles(instance, env, service, persistence, dir, changes) {
|
|
139
|
+
const root = `/dev/hammerkit/${service.id()}`;
|
|
140
|
+
const staging = `${root}/${exports.CHANGES_STAGING_DIR}`;
|
|
141
|
+
const mounted = mountedUnder(persistence, dir);
|
|
142
|
+
const batch = (0, path_1.dirname)(changes.dir);
|
|
143
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(batch, 'deleted'), changes.deleted.join('\0'));
|
|
144
|
+
const skipped = [];
|
|
145
|
+
await (0, ensure_persistent_data_1.getPodForPersistence)(instance, env, service, persistence, 'write', undefined, async (pod) => {
|
|
146
|
+
const cleared = await execCapturing(instance, env, pod, ['sh', '-c', 'rm -rf "$1" && mkdir -p "$1"', 'sh', staging]);
|
|
147
|
+
if (cleared.failure) {
|
|
148
|
+
throw new Error(`unable to stage the changes of ${service.name} in ${pod}: ${cleared.failure}`);
|
|
149
|
+
}
|
|
150
|
+
await uploadStaged(instance, env, pod, batch, staging);
|
|
151
|
+
const { failure, output } = await execCapturing(instance, env, pod, [
|
|
152
|
+
'sh',
|
|
153
|
+
'-c',
|
|
154
|
+
exports.SYNC_CHANGES,
|
|
155
|
+
'sh',
|
|
156
|
+
staging,
|
|
157
|
+
`${root}/${dir}`,
|
|
158
|
+
...mounted,
|
|
159
|
+
]);
|
|
160
|
+
skipped.push(...output.split('\n').filter((line) => line.startsWith('skipped ')));
|
|
161
|
+
if (failure) {
|
|
162
|
+
throw new Error(`unable to sync the changed files of ${service.name} into ${dir}: ${failure}`);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
return skipped;
|
|
166
|
+
}
|
|
167
|
+
exports.syncChangedFiles = syncChangedFiles;
|
|
168
|
+
/**
|
|
169
|
+
* Starts the pods of a service again: its Deployment makes new ones from its template, which
|
|
170
|
+
* mount the folder the files were synced into. A service that keeps its files is down for the
|
|
171
|
+
* time the new pods take to start.
|
|
172
|
+
*/
|
|
173
|
+
async function restartPods(instance, env, service) {
|
|
174
|
+
await instance.coreApi.deleteCollectionNamespacedPod({
|
|
175
|
+
namespace: env.namespace,
|
|
176
|
+
labelSelector: (0, resources_1.getItemSelector)(service, env),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
exports.restartPods = restartPods;
|
|
180
|
+
//# sourceMappingURL=sync-changes.js.map
|
|
@@ -20,6 +20,7 @@ const ensure_persistent_data_1 = require("../kubernetes/ensure-persistent-data")
|
|
|
20
20
|
const network_gate_1 = require("../kubernetes/network-gate");
|
|
21
21
|
const await_running_state_1 = require("../kubernetes/await-running-state");
|
|
22
22
|
const volumes_1 = require("../kubernetes/volumes");
|
|
23
|
+
const sync_changes_1 = require("../kubernetes/sync-changes");
|
|
23
24
|
const service_rollouts_1 = require("../kubernetes/service-rollouts");
|
|
24
25
|
const resources_1 = require("../kubernetes/resources");
|
|
25
26
|
const ensure_http_route_1 = require("../kubernetes/ensure-http-route");
|
|
@@ -784,6 +785,25 @@ function kubernetesServiceRuntime(service, kubernetes) {
|
|
|
784
785
|
remote: null,
|
|
785
786
|
});
|
|
786
787
|
},
|
|
788
|
+
// The files of the sandbox changed while the service of a service run is up (FileChanges):
|
|
789
|
+
// synced into the folder its pod mounts, which the service sees, and its pods start again
|
|
790
|
+
// when it restarts. A service that keeps its files takes nothing.
|
|
791
|
+
async syncFiles(changes) {
|
|
792
|
+
const stateDir = kubernetes.server?.serviceRun ? (0, volumes_1.getItemFilesDir)(service, undefined) : undefined;
|
|
793
|
+
const target = stateDir
|
|
794
|
+
? (0, sync_changes_1.getChangeTarget)((0, volumes_1.isLiveService)(service, kubernetes), changes.restart, stateDir)
|
|
795
|
+
: undefined;
|
|
796
|
+
if (!target) {
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
const persistence = await (0, volumes_1.getKubernetesPersistence)(service, kubernetes);
|
|
800
|
+
for (const line of await (0, sync_changes_1.syncChangedFiles)(instance, kubernetes, service, persistence, target.dir, changes)) {
|
|
801
|
+
service.status.write('info', line);
|
|
802
|
+
}
|
|
803
|
+
if (target.rollout) {
|
|
804
|
+
await (0, sync_changes_1.restartPods)(instance, kubernetes, service);
|
|
805
|
+
}
|
|
806
|
+
},
|
|
787
807
|
async restore(environment, path) {
|
|
788
808
|
await (0, restore_kubernetes_data_1.restoreKubernetesData)(service, kubernetes, instance, environment, path);
|
|
789
809
|
},
|
package/dist/server/executor.js
CHANGED
|
@@ -131,6 +131,22 @@ class HostExecutor {
|
|
|
131
131
|
this.options = options;
|
|
132
132
|
// the directories of the executions that run now
|
|
133
133
|
this.directories = new Set();
|
|
134
|
+
// the processes of the runs that run now, by run id, while they take input
|
|
135
|
+
this.children = new Map();
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Hands the changed files of a sandbox to the process of a service run that is up here, which
|
|
139
|
+
* syncs them into its services (see run-worker). False when no process of the run runs here.
|
|
140
|
+
*/
|
|
141
|
+
syncChildFiles(runId, changes) {
|
|
142
|
+
const child = this.children.get(runId);
|
|
143
|
+
const staging = this.options.sandboxes?.staging;
|
|
144
|
+
if (!child?.stdin || !staging) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
// the worker removes the batch folder only inside the staging folder of the run it is in
|
|
148
|
+
child.stdin.write(`sync ${JSON.stringify({ staging: (0, path_1.join)(staging, runId), changes })}\n`);
|
|
149
|
+
return true;
|
|
134
150
|
}
|
|
135
151
|
// the docker daemon the runs' services are on, as their processes reach it
|
|
136
152
|
docker() {
|
|
@@ -256,12 +272,21 @@ class HostExecutor {
|
|
|
256
272
|
// does a run that was approved: what was approved is those files, and the sandbox has
|
|
257
273
|
// more (its ignored files, node_modules, outputs), which its owner and their runs write
|
|
258
274
|
// while the run waits and runs.
|
|
259
|
-
|
|
275
|
+
// A service run from a sandbox on a cluster gets a copy of the files too: its pods mount no
|
|
276
|
+
// claim of the sandbox, and the files of later edits reach it by a move (RunRegistry.follow)
|
|
277
|
+
const approved = (run.approvals?.length ?? 0) > 0;
|
|
278
|
+
const onCluster = !!run.up && !!this.options.placement;
|
|
279
|
+
const copied = source.type === 'sandbox' && (source.check || approved || onCluster);
|
|
260
280
|
// (`source.type` again: TypeScript narrows `source` by it, not by `copied`)
|
|
261
281
|
if (source.type === 'sandbox' && copied) {
|
|
262
|
-
const { registry, network } = sandboxes;
|
|
263
|
-
const
|
|
264
|
-
if (
|
|
282
|
+
const { registry, network, started } = sandboxes;
|
|
283
|
+
const kind = source.check ? 'check' : approved ? 'approved run' : 'service run';
|
|
284
|
+
if (onCluster) {
|
|
285
|
+
// before the copy: a change from here on is one the services have not got
|
|
286
|
+
await started?.(run);
|
|
287
|
+
}
|
|
288
|
+
const what = `the ${kind}`;
|
|
289
|
+
if (!source.check && approved) {
|
|
265
290
|
// what was approved is what runs: the files may have changed since, by a run
|
|
266
291
|
// before this one on the sandbox (the copy checks it again, under the sandbox's lock)
|
|
267
292
|
const state = await registry.state(source.id, abortCtrl.signal);
|
|
@@ -283,7 +308,7 @@ class HostExecutor {
|
|
|
283
308
|
type: 'log',
|
|
284
309
|
task: 'hammerkit',
|
|
285
310
|
stream: 'stdout',
|
|
286
|
-
text: `${
|
|
311
|
+
text: `${kind} of sandbox ${source.id} of ${source.repo}: files ${source.tree}, fetched at ${source.commit}`,
|
|
287
312
|
});
|
|
288
313
|
// the tasks see the published build files in place of the sandbox's own
|
|
289
314
|
await this.writeBundle(work, bundle.files);
|
|
@@ -373,12 +398,13 @@ class HostExecutor {
|
|
|
373
398
|
up: {
|
|
374
399
|
services: run.up.services,
|
|
375
400
|
endpoints: run.up.endpoints.map(({ service, port }) => ({ service, port })),
|
|
401
|
+
...(run.up.restart ? { restart: run.up.restart } : {}),
|
|
376
402
|
},
|
|
377
403
|
}
|
|
378
404
|
: {}),
|
|
379
405
|
...(placed ? { kubernetes: placed.kubernetes } : {}),
|
|
380
406
|
...(!placed && this.options.capabilityRuntime ? { capabilityRuntime: this.options.capabilityRuntime } : {}),
|
|
381
|
-
}, published, { ...(0, run_source_environment_1.withRunSource)(this.processEnvironment(home, cache), run.source), ...placed?.env }, context, abortCtrl.signal, !!placed);
|
|
407
|
+
}, published, { ...(0, run_source_environment_1.withRunSource)(this.processEnvironment(home, cache), run.source), ...placed?.env }, context, abortCtrl.signal, !!placed, run.id);
|
|
382
408
|
}
|
|
383
409
|
finally {
|
|
384
410
|
if (placement && placed) {
|
|
@@ -396,6 +422,9 @@ class HostExecutor {
|
|
|
396
422
|
finally {
|
|
397
423
|
context.signal.removeEventListener('abort', abort);
|
|
398
424
|
attachment?.release?.();
|
|
425
|
+
if (run.up && this.options.placement) {
|
|
426
|
+
await sandboxes?.ended?.(run);
|
|
427
|
+
}
|
|
399
428
|
await this.removeRunDirectory(run, root, context);
|
|
400
429
|
this.directories.delete(root);
|
|
401
430
|
if (source.type === 'sandbox') {
|
|
@@ -459,7 +488,7 @@ class HostExecutor {
|
|
|
459
488
|
// A run that the server leaves to the next one (RunContext.detached) and whose tasks are on
|
|
460
489
|
// a cluster: the process is killed at once, so its Jobs and services stay. One whose tasks
|
|
461
490
|
// are on this machine stops them as a cancel does, in less time.
|
|
462
|
-
runInProcess(command, input, cwd, env, context, signal, onCluster) {
|
|
491
|
+
runInProcess(command, input, cwd, env, context, signal, onCluster, runId) {
|
|
463
492
|
return new Promise((resolvePromise, reject) => {
|
|
464
493
|
const child = (0, child_process_1.spawn)(command[0], command.slice(1), {
|
|
465
494
|
cwd,
|
|
@@ -470,6 +499,7 @@ class HostExecutor {
|
|
|
470
499
|
child.stdin.on('error', () => undefined);
|
|
471
500
|
// the run first, so a cancel comes after it; kept open, it is how a cancel reaches the process
|
|
472
501
|
child.stdin.write(`${JSON.stringify(input)}\n`);
|
|
502
|
+
this.children.set(runId, child);
|
|
473
503
|
let result;
|
|
474
504
|
let stderr = '';
|
|
475
505
|
let killTimer;
|
|
@@ -506,6 +536,7 @@ class HostExecutor {
|
|
|
506
536
|
else if (message.kind === 'result') {
|
|
507
537
|
result = message;
|
|
508
538
|
// nothing left to tell the process
|
|
539
|
+
this.children.delete(runId);
|
|
509
540
|
child.stdin.end();
|
|
510
541
|
}
|
|
511
542
|
}
|
|
@@ -524,6 +555,9 @@ class HostExecutor {
|
|
|
524
555
|
child.on('close', (code, killed) => {
|
|
525
556
|
signal.removeEventListener('abort', stop);
|
|
526
557
|
clearTimeout(killTimer);
|
|
558
|
+
if (this.children.get(runId) === child) {
|
|
559
|
+
this.children.delete(runId);
|
|
560
|
+
}
|
|
527
561
|
if (result?.ok) {
|
|
528
562
|
resolvePromise();
|
|
529
563
|
}
|
|
@@ -404,10 +404,8 @@ class KubernetesExecutor {
|
|
|
404
404
|
}
|
|
405
405
|
async executeOn(run, context) {
|
|
406
406
|
if (run.up) {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
}
|
|
410
|
-
// in a namespace of its own, apart from the caller's runs and their claims
|
|
407
|
+
// in a namespace of its own, apart from the caller's runs and their claims, from a branch
|
|
408
|
+
// or from the files of a sandbox (copied, see HostExecutor.execute)
|
|
411
409
|
return this.host.execute(run, context);
|
|
412
410
|
}
|
|
413
411
|
// it executes: its objects are kept from a tidy as those of any run that executes
|
|
@@ -438,6 +436,11 @@ class KubernetesExecutor {
|
|
|
438
436
|
}
|
|
439
437
|
}
|
|
440
438
|
}
|
|
439
|
+
// The changed files of a sandbox for the services of a service run here: they are synced in
|
|
440
|
+
// place, nothing of the run moves (see RunExecutor.syncFiles)
|
|
441
|
+
syncFiles(run, changes) {
|
|
442
|
+
return !!run.up && this.host.syncChildFiles(run.id, changes);
|
|
443
|
+
}
|
|
441
444
|
// A service run moves to another commit in its namespace: unchanged services stay, the
|
|
442
445
|
// others roll to their new pods (see kubernetesServiceRuntime)
|
|
443
446
|
movesBeside(run) {
|
|
@@ -124,6 +124,9 @@ exports.runRequestSchema = zod_1.z
|
|
|
124
124
|
.array(zod_1.z.object({ service: name, port: zod_1.z.number().int().min(1).max(65535) }).strict())
|
|
125
125
|
.max(exports.MAX_ENDPOINTS)
|
|
126
126
|
.default([]),
|
|
127
|
+
// the services that start again with the files of their sandbox after an edit of it; the
|
|
128
|
+
// others keep the files they started with (see ServiceRunSpec.restart)
|
|
129
|
+
restart: zod_1.z.array(name).max(20).optional(),
|
|
127
130
|
})
|
|
128
131
|
.strict()
|
|
129
132
|
.refine((request) => request.services.length > 0 || request.tasks.length > 0 || (request.options.filter ?? []).length > 0, {
|
|
@@ -145,6 +148,14 @@ exports.runRequestSchema = zod_1.z
|
|
|
145
148
|
request.sleepTtl === undefined &&
|
|
146
149
|
request.name === undefined &&
|
|
147
150
|
request.endpoints.length === 0), { message: 'ttl, sleepTtl, name and endpoints are for a run of services', path: ['services'] })
|
|
151
|
+
.refine((request) => (request.restart ?? []).every((service) => request.services.includes(service)), {
|
|
152
|
+
message: 'restart names services the run starts',
|
|
153
|
+
path: ['restart'],
|
|
154
|
+
})
|
|
155
|
+
.refine((request) => !request.restart?.length || request.source.type === 'sandbox', {
|
|
156
|
+
message: 'restart is for a service run from a sandbox, which changes with its files',
|
|
157
|
+
path: ['restart'],
|
|
158
|
+
})
|
|
148
159
|
.refine((request) => new Set(request.endpoints.map((endpoint) => `${endpoint.service}:${endpoint.port}`)).size ===
|
|
149
160
|
request.endpoints.length, { message: 'an endpoint is named twice', path: ['endpoints'] })
|
|
150
161
|
.refine((request) => (request.options.args ?? []).length === 0 || request.tasks.length === 1, {
|