@ship.zone/runner 0.1.0
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/.smartconfig.json +48 -0
- package/changelog.md +12 -0
- package/deno.json +41 -0
- package/deno.lock +1843 -0
- package/dist_npm/mod.d.ts +689 -0
- package/dist_npm/mod.js +8980 -0
- package/dist_rust/runnerworkspace_linux_amd64_musl +0 -0
- package/dist_rust/runnerworkspace_linux_arm64_musl +0 -0
- package/license.md +18 -0
- package/mod.ts +8 -0
- package/musl-copyright.md +81 -0
- package/package.json +58 -0
- package/readme.md +274 -0
- package/rust/.cargo/config.toml +5 -0
- package/rust/Cargo.lock +144 -0
- package/rust/Cargo.toml +17 -0
- package/rust/src/lib.rs +1784 -0
- package/rust/src/main.rs +6 -0
- package/third-party-licenses.md +95 -0
- package/ts/00_commitinfo_data.ts +8 -0
- package/ts/api-client.ts +1059 -0
- package/ts/artifact-manager.ts +218 -0
- package/ts/attempt-state.ts +190 -0
- package/ts/cache-manager.ts +431 -0
- package/ts/cli.ts +239 -0
- package/ts/config.ts +724 -0
- package/ts/errors.ts +68 -0
- package/ts/index.ts +13 -0
- package/ts/interfaces.ts +547 -0
- package/ts/log-sender.ts +134 -0
- package/ts/oci-executor.ts +2472 -0
- package/ts/plugins.ts +11 -0
- package/ts/protocol-validator.ts +535 -0
- package/ts/redactor.ts +41 -0
- package/ts/registration-state.ts +253 -0
- package/ts/runner.ts +1036 -0
- package/ts/source-workspace.ts +161 -0
- package/ts/staging-scavenger.ts +62 -0
- package/ts/transfer-archive.ts +1605 -0
- package/ts/validation.ts +77 -0
package/ts/config.ts
ADDED
|
@@ -0,0 +1,724 @@
|
|
|
1
|
+
import { dirname, isAbsolute, join } from '@std/path';
|
|
2
|
+
import { ConfigurationError } from './errors.ts';
|
|
3
|
+
import type { IOciExecutorConfig, IRunnerConfig, IRunnerCredential } from './interfaces.ts';
|
|
4
|
+
import { isRecord, validateHttpUrl } from './validation.ts';
|
|
5
|
+
|
|
6
|
+
const identifierPattern = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
7
|
+
const timestampPattern =
|
|
8
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-](\d{2}):(\d{2}))$/;
|
|
9
|
+
|
|
10
|
+
const defaultOciConfig: IOciExecutorConfig = {
|
|
11
|
+
runtime: 'docker',
|
|
12
|
+
runtimePath: '/usr/bin/docker',
|
|
13
|
+
requireRootless: true,
|
|
14
|
+
allowedNetworks: ['none'],
|
|
15
|
+
containerUser: '65532:65532',
|
|
16
|
+
defaultTimeoutMs: 30 * 60 * 1000,
|
|
17
|
+
maximumTimeoutMs: 2 * 60 * 60 * 1000,
|
|
18
|
+
defaultMemoryBytes: 2 * 1024 * 1024 * 1024,
|
|
19
|
+
maximumMemoryBytes: 8 * 1024 * 1024 * 1024,
|
|
20
|
+
defaultCpus: 2,
|
|
21
|
+
maximumCpus: 8,
|
|
22
|
+
defaultPids: 256,
|
|
23
|
+
maximumPids: 1024,
|
|
24
|
+
workspaceSizeBytes: 4 * 1024 * 1024 * 1024,
|
|
25
|
+
temporarySizeBytes: 256 * 1024 * 1024,
|
|
26
|
+
stopTimeoutSeconds: 10,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const defaultRunnerConfig = (): IRunnerConfig => ({
|
|
30
|
+
controlPlaneUrl: '',
|
|
31
|
+
allowInsecureLoopback: false,
|
|
32
|
+
name: `runner-${Deno.env.get('HOSTNAME') ?? 'local'}`,
|
|
33
|
+
labels: ['linux', Deno.build.arch, 'oci'],
|
|
34
|
+
stateDirectory: './.runner-state',
|
|
35
|
+
transferStagingReserveBytes: 1024 * 1024 * 1024,
|
|
36
|
+
transferStagingReserveInodes: 10_000,
|
|
37
|
+
pollIntervalMs: 5000,
|
|
38
|
+
runnerHeartbeatIntervalMs: 30_000,
|
|
39
|
+
jobHeartbeatIntervalMs: 10_000,
|
|
40
|
+
leaseSafetyMarginMs: 5000,
|
|
41
|
+
requestTimeoutMs: 15_000,
|
|
42
|
+
maximumResponseBytes: 1024 * 1024,
|
|
43
|
+
sourceTransferTimeoutMs: 10 * 60 * 1000,
|
|
44
|
+
maximumSourceArchiveBytes: 128 * 1024 * 1024,
|
|
45
|
+
maximumSourceExtractedBytes: 512 * 1024 * 1024,
|
|
46
|
+
maximumSourceEntries: 100_000,
|
|
47
|
+
maximumLogBytes: 64 * 1024 * 1024,
|
|
48
|
+
maximumLogChunkBytes: 64 * 1024,
|
|
49
|
+
maximumArtifactBytes: 256 * 1024 * 1024,
|
|
50
|
+
maximumArtifactExtractedBytes: 1024 * 1024 * 1024,
|
|
51
|
+
maximumArtifactEntries: 100_000,
|
|
52
|
+
maximumArtifacts: 16,
|
|
53
|
+
artifactUploadTimeoutMs: 10 * 60 * 1000,
|
|
54
|
+
maximumCacheArchiveBytes: 512 * 1024 * 1024,
|
|
55
|
+
maximumCacheExtractedBytes: 2 * 1024 * 1024 * 1024,
|
|
56
|
+
maximumCacheEntries: 100_000,
|
|
57
|
+
maximumCaches: 16,
|
|
58
|
+
cacheTransferTimeoutMs: 10 * 60 * 1000,
|
|
59
|
+
maximumWorkspaceEntries: 100_000,
|
|
60
|
+
maximumEnvironmentVariables: 128,
|
|
61
|
+
maximumEnvironmentValueBytes: 64 * 1024,
|
|
62
|
+
maximumSecretBytes: 1024 * 1024,
|
|
63
|
+
oci: { ...defaultOciConfig, allowedNetworks: [...defaultOciConfig.allowedNetworks] },
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const optionalString = (
|
|
67
|
+
value: unknown,
|
|
68
|
+
field: string,
|
|
69
|
+
maximumLength = 8192,
|
|
70
|
+
): string | undefined => {
|
|
71
|
+
if (value === undefined) return undefined;
|
|
72
|
+
if (typeof value !== 'string' || value.length === 0 || value.length > maximumLength) {
|
|
73
|
+
throw new ConfigurationError(`${field} must be a non-empty string`);
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const stringValue = (
|
|
79
|
+
value: unknown,
|
|
80
|
+
fallback: string,
|
|
81
|
+
field: string,
|
|
82
|
+
maximumLength = 8192,
|
|
83
|
+
): string => optionalString(value, field, maximumLength) ?? fallback;
|
|
84
|
+
|
|
85
|
+
const booleanValue = (value: unknown, fallback: boolean, field: string): boolean => {
|
|
86
|
+
if (value === undefined) return fallback;
|
|
87
|
+
if (typeof value !== 'boolean') throw new ConfigurationError(`${field} must be a boolean`);
|
|
88
|
+
return value;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const integerValue = (
|
|
92
|
+
value: unknown,
|
|
93
|
+
fallback: number,
|
|
94
|
+
field: string,
|
|
95
|
+
minimum: number,
|
|
96
|
+
maximum: number,
|
|
97
|
+
): number => {
|
|
98
|
+
const result = value === undefined ? fallback : value;
|
|
99
|
+
if (
|
|
100
|
+
typeof result !== 'number' || !Number.isSafeInteger(result) || result < minimum ||
|
|
101
|
+
result > maximum
|
|
102
|
+
) {
|
|
103
|
+
throw new ConfigurationError(`${field} must be an integer between ${minimum} and ${maximum}`);
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const numberValue = (
|
|
109
|
+
value: unknown,
|
|
110
|
+
fallback: number,
|
|
111
|
+
field: string,
|
|
112
|
+
minimumExclusive: number,
|
|
113
|
+
maximum: number,
|
|
114
|
+
): number => {
|
|
115
|
+
const result = value === undefined ? fallback : value;
|
|
116
|
+
if (
|
|
117
|
+
typeof result !== 'number' || !Number.isFinite(result) || result <= minimumExclusive ||
|
|
118
|
+
result > maximum
|
|
119
|
+
) {
|
|
120
|
+
throw new ConfigurationError(
|
|
121
|
+
`${field} must be greater than ${minimumExclusive} and at most ${maximum}`,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const stringArray = (
|
|
128
|
+
value: unknown,
|
|
129
|
+
fallback: string[],
|
|
130
|
+
field: string,
|
|
131
|
+
maximumLength: number,
|
|
132
|
+
): string[] => {
|
|
133
|
+
const result = value === undefined ? [...fallback] : value;
|
|
134
|
+
if (
|
|
135
|
+
!Array.isArray(result) || result.length > maximumLength ||
|
|
136
|
+
result.some((entry) => typeof entry !== 'string' || !identifierPattern.test(entry)) ||
|
|
137
|
+
new Set(result).size !== result.length
|
|
138
|
+
) {
|
|
139
|
+
throw new ConfigurationError(`${field} must contain unique protocol identifiers`);
|
|
140
|
+
}
|
|
141
|
+
return [...result] as string[];
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const validTimestamp = (value: string): boolean => {
|
|
145
|
+
const match = timestampPattern.exec(value);
|
|
146
|
+
if (!match || !Number.isFinite(Date.parse(value))) return false;
|
|
147
|
+
const year = Number(match[1]);
|
|
148
|
+
const month = Number(match[2]);
|
|
149
|
+
const day = Number(match[3]);
|
|
150
|
+
const hour = Number(match[4]);
|
|
151
|
+
const minute = Number(match[5]);
|
|
152
|
+
const second = Number(match[6]);
|
|
153
|
+
const offsetHour = match[7] === undefined ? 0 : Number(match[7]);
|
|
154
|
+
const offsetMinute = match[8] === undefined ? 0 : Number(match[8]);
|
|
155
|
+
return month >= 1 && month <= 12 && day >= 1 &&
|
|
156
|
+
day <= new Date(Date.UTC(year, month, 0)).getUTCDate() && hour <= 23 && minute <= 59 &&
|
|
157
|
+
second <= 59 && offsetHour <= 23 && offsetMinute <= 59;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const parseOciConfig = (input: unknown, fallback: IOciExecutorConfig): IOciExecutorConfig => {
|
|
161
|
+
const value = input === undefined ? {} : input;
|
|
162
|
+
if (!isRecord(value)) throw new ConfigurationError('oci must be an object');
|
|
163
|
+
if (Object.hasOwn(value, 'allowMutableImages')) {
|
|
164
|
+
throw new ConfigurationError('oci.allowMutableImages is not supported by runner protocol v2');
|
|
165
|
+
}
|
|
166
|
+
const runtime = value.runtime ?? fallback.runtime;
|
|
167
|
+
if (runtime !== 'docker' && runtime !== 'podman') {
|
|
168
|
+
throw new ConfigurationError('oci.runtime must be docker or podman');
|
|
169
|
+
}
|
|
170
|
+
const allowedNetworks = stringArray(
|
|
171
|
+
value.allowedNetworks,
|
|
172
|
+
fallback.allowedNetworks,
|
|
173
|
+
'oci.allowedNetworks',
|
|
174
|
+
2,
|
|
175
|
+
);
|
|
176
|
+
if (
|
|
177
|
+
allowedNetworks.length === 0 ||
|
|
178
|
+
allowedNetworks.some((network) => network !== 'none' && network !== 'bridge')
|
|
179
|
+
) {
|
|
180
|
+
throw new ConfigurationError('oci.allowedNetworks may only contain none or bridge');
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
runtime,
|
|
184
|
+
runtimePath: stringValue(value.runtimePath, fallback.runtimePath, 'oci.runtimePath'),
|
|
185
|
+
workspaceHelperPath: optionalString(value.workspaceHelperPath, 'oci.workspaceHelperPath'),
|
|
186
|
+
requireRootless: booleanValue(
|
|
187
|
+
value.requireRootless,
|
|
188
|
+
fallback.requireRootless,
|
|
189
|
+
'oci.requireRootless',
|
|
190
|
+
),
|
|
191
|
+
allowedNetworks: allowedNetworks as IOciExecutorConfig['allowedNetworks'],
|
|
192
|
+
containerUser: stringValue(value.containerUser, fallback.containerUser, 'oci.containerUser'),
|
|
193
|
+
defaultTimeoutMs: integerValue(
|
|
194
|
+
value.defaultTimeoutMs,
|
|
195
|
+
fallback.defaultTimeoutMs,
|
|
196
|
+
'oci.defaultTimeoutMs',
|
|
197
|
+
1000,
|
|
198
|
+
86_400_000,
|
|
199
|
+
),
|
|
200
|
+
maximumTimeoutMs: integerValue(
|
|
201
|
+
value.maximumTimeoutMs,
|
|
202
|
+
fallback.maximumTimeoutMs,
|
|
203
|
+
'oci.maximumTimeoutMs',
|
|
204
|
+
1000,
|
|
205
|
+
86_400_000,
|
|
206
|
+
),
|
|
207
|
+
defaultMemoryBytes: integerValue(
|
|
208
|
+
value.defaultMemoryBytes,
|
|
209
|
+
fallback.defaultMemoryBytes,
|
|
210
|
+
'oci.defaultMemoryBytes',
|
|
211
|
+
16_777_216,
|
|
212
|
+
1_099_511_627_776,
|
|
213
|
+
),
|
|
214
|
+
maximumMemoryBytes: integerValue(
|
|
215
|
+
value.maximumMemoryBytes,
|
|
216
|
+
fallback.maximumMemoryBytes,
|
|
217
|
+
'oci.maximumMemoryBytes',
|
|
218
|
+
16_777_216,
|
|
219
|
+
1_099_511_627_776,
|
|
220
|
+
),
|
|
221
|
+
defaultCpus: numberValue(value.defaultCpus, fallback.defaultCpus, 'oci.defaultCpus', 0, 1024),
|
|
222
|
+
maximumCpus: numberValue(value.maximumCpus, fallback.maximumCpus, 'oci.maximumCpus', 0, 1024),
|
|
223
|
+
defaultPids: integerValue(
|
|
224
|
+
value.defaultPids,
|
|
225
|
+
fallback.defaultPids,
|
|
226
|
+
'oci.defaultPids',
|
|
227
|
+
1,
|
|
228
|
+
1_048_576,
|
|
229
|
+
),
|
|
230
|
+
maximumPids: integerValue(
|
|
231
|
+
value.maximumPids,
|
|
232
|
+
fallback.maximumPids,
|
|
233
|
+
'oci.maximumPids',
|
|
234
|
+
1,
|
|
235
|
+
1_048_576,
|
|
236
|
+
),
|
|
237
|
+
workspaceSizeBytes: integerValue(
|
|
238
|
+
value.workspaceSizeBytes,
|
|
239
|
+
fallback.workspaceSizeBytes,
|
|
240
|
+
'oci.workspaceSizeBytes',
|
|
241
|
+
16_777_216,
|
|
242
|
+
1_099_511_627_776,
|
|
243
|
+
),
|
|
244
|
+
temporarySizeBytes: integerValue(
|
|
245
|
+
value.temporarySizeBytes,
|
|
246
|
+
fallback.temporarySizeBytes,
|
|
247
|
+
'oci.temporarySizeBytes',
|
|
248
|
+
1,
|
|
249
|
+
1_099_511_627_776,
|
|
250
|
+
),
|
|
251
|
+
stopTimeoutSeconds: integerValue(
|
|
252
|
+
value.stopTimeoutSeconds,
|
|
253
|
+
fallback.stopTimeoutSeconds,
|
|
254
|
+
'oci.stopTimeoutSeconds',
|
|
255
|
+
1,
|
|
256
|
+
30,
|
|
257
|
+
),
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
export const parseRunnerConfig = (input: unknown): IRunnerConfig => {
|
|
262
|
+
if (!isRecord(input)) throw new ConfigurationError('Runner config must be a JSON object');
|
|
263
|
+
const defaults = defaultRunnerConfig();
|
|
264
|
+
const config: IRunnerConfig = {
|
|
265
|
+
controlPlaneUrl: stringValue(
|
|
266
|
+
input.controlPlaneUrl,
|
|
267
|
+
defaults.controlPlaneUrl,
|
|
268
|
+
'controlPlaneUrl',
|
|
269
|
+
),
|
|
270
|
+
allowInsecureLoopback: booleanValue(
|
|
271
|
+
input.allowInsecureLoopback,
|
|
272
|
+
defaults.allowInsecureLoopback,
|
|
273
|
+
'allowInsecureLoopback',
|
|
274
|
+
),
|
|
275
|
+
name: stringValue(input.name, defaults.name, 'name', 128),
|
|
276
|
+
labels: stringArray(input.labels, defaults.labels, 'labels', 32),
|
|
277
|
+
bootstrapToken: optionalString(input.bootstrapToken, 'bootstrapToken'),
|
|
278
|
+
runnerId: optionalString(input.runnerId, 'runnerId', 64),
|
|
279
|
+
runnerCredentialId: optionalString(input.runnerCredentialId, 'runnerCredentialId', 64),
|
|
280
|
+
runnerToken: optionalString(input.runnerToken, 'runnerToken'),
|
|
281
|
+
runnerTokenExpiresAt: optionalString(input.runnerTokenExpiresAt, 'runnerTokenExpiresAt', 64),
|
|
282
|
+
stateDirectory: stringValue(input.stateDirectory, defaults.stateDirectory, 'stateDirectory'),
|
|
283
|
+
transferStagingReserveBytes: integerValue(
|
|
284
|
+
input.transferStagingReserveBytes,
|
|
285
|
+
defaults.transferStagingReserveBytes,
|
|
286
|
+
'transferStagingReserveBytes',
|
|
287
|
+
0,
|
|
288
|
+
Number.MAX_SAFE_INTEGER,
|
|
289
|
+
),
|
|
290
|
+
transferStagingReserveInodes: integerValue(
|
|
291
|
+
input.transferStagingReserveInodes,
|
|
292
|
+
defaults.transferStagingReserveInodes,
|
|
293
|
+
'transferStagingReserveInodes',
|
|
294
|
+
0,
|
|
295
|
+
Number.MAX_SAFE_INTEGER,
|
|
296
|
+
),
|
|
297
|
+
pollIntervalMs: integerValue(
|
|
298
|
+
input.pollIntervalMs,
|
|
299
|
+
defaults.pollIntervalMs,
|
|
300
|
+
'pollIntervalMs',
|
|
301
|
+
1,
|
|
302
|
+
30_000,
|
|
303
|
+
),
|
|
304
|
+
runnerHeartbeatIntervalMs: integerValue(
|
|
305
|
+
input.runnerHeartbeatIntervalMs,
|
|
306
|
+
defaults.runnerHeartbeatIntervalMs,
|
|
307
|
+
'runnerHeartbeatIntervalMs',
|
|
308
|
+
5000,
|
|
309
|
+
60_000,
|
|
310
|
+
),
|
|
311
|
+
jobHeartbeatIntervalMs: integerValue(
|
|
312
|
+
input.jobHeartbeatIntervalMs,
|
|
313
|
+
defaults.jobHeartbeatIntervalMs,
|
|
314
|
+
'jobHeartbeatIntervalMs',
|
|
315
|
+
5000,
|
|
316
|
+
60_000,
|
|
317
|
+
),
|
|
318
|
+
leaseSafetyMarginMs: integerValue(
|
|
319
|
+
input.leaseSafetyMarginMs,
|
|
320
|
+
defaults.leaseSafetyMarginMs,
|
|
321
|
+
'leaseSafetyMarginMs',
|
|
322
|
+
0,
|
|
323
|
+
599_999,
|
|
324
|
+
),
|
|
325
|
+
requestTimeoutMs: integerValue(
|
|
326
|
+
input.requestTimeoutMs,
|
|
327
|
+
defaults.requestTimeoutMs,
|
|
328
|
+
'requestTimeoutMs',
|
|
329
|
+
1,
|
|
330
|
+
600_000,
|
|
331
|
+
),
|
|
332
|
+
maximumResponseBytes: integerValue(
|
|
333
|
+
input.maximumResponseBytes,
|
|
334
|
+
defaults.maximumResponseBytes,
|
|
335
|
+
'maximumResponseBytes',
|
|
336
|
+
1,
|
|
337
|
+
2_097_152,
|
|
338
|
+
),
|
|
339
|
+
sourceTransferTimeoutMs: integerValue(
|
|
340
|
+
input.sourceTransferTimeoutMs,
|
|
341
|
+
defaults.sourceTransferTimeoutMs,
|
|
342
|
+
'sourceTransferTimeoutMs',
|
|
343
|
+
1,
|
|
344
|
+
86_400_000,
|
|
345
|
+
),
|
|
346
|
+
maximumSourceArchiveBytes: integerValue(
|
|
347
|
+
input.maximumSourceArchiveBytes,
|
|
348
|
+
defaults.maximumSourceArchiveBytes,
|
|
349
|
+
'maximumSourceArchiveBytes',
|
|
350
|
+
0,
|
|
351
|
+
8_589_934_592,
|
|
352
|
+
),
|
|
353
|
+
maximumSourceExtractedBytes: integerValue(
|
|
354
|
+
input.maximumSourceExtractedBytes,
|
|
355
|
+
defaults.maximumSourceExtractedBytes,
|
|
356
|
+
'maximumSourceExtractedBytes',
|
|
357
|
+
0,
|
|
358
|
+
68_719_476_736,
|
|
359
|
+
),
|
|
360
|
+
maximumSourceEntries: integerValue(
|
|
361
|
+
input.maximumSourceEntries,
|
|
362
|
+
defaults.maximumSourceEntries,
|
|
363
|
+
'maximumSourceEntries',
|
|
364
|
+
0,
|
|
365
|
+
1_000_000,
|
|
366
|
+
),
|
|
367
|
+
maximumLogBytes: integerValue(
|
|
368
|
+
input.maximumLogBytes,
|
|
369
|
+
defaults.maximumLogBytes,
|
|
370
|
+
'maximumLogBytes',
|
|
371
|
+
0,
|
|
372
|
+
1_073_741_824,
|
|
373
|
+
),
|
|
374
|
+
maximumLogChunkBytes: integerValue(
|
|
375
|
+
input.maximumLogChunkBytes,
|
|
376
|
+
defaults.maximumLogChunkBytes,
|
|
377
|
+
'maximumLogChunkBytes',
|
|
378
|
+
1,
|
|
379
|
+
65_536,
|
|
380
|
+
),
|
|
381
|
+
maximumArtifactBytes: integerValue(
|
|
382
|
+
input.maximumArtifactBytes,
|
|
383
|
+
defaults.maximumArtifactBytes,
|
|
384
|
+
'maximumArtifactBytes',
|
|
385
|
+
0,
|
|
386
|
+
17_179_869_184,
|
|
387
|
+
),
|
|
388
|
+
maximumArtifactExtractedBytes: integerValue(
|
|
389
|
+
input.maximumArtifactExtractedBytes,
|
|
390
|
+
defaults.maximumArtifactExtractedBytes,
|
|
391
|
+
'maximumArtifactExtractedBytes',
|
|
392
|
+
0,
|
|
393
|
+
137_438_953_472,
|
|
394
|
+
),
|
|
395
|
+
maximumArtifactEntries: integerValue(
|
|
396
|
+
input.maximumArtifactEntries,
|
|
397
|
+
defaults.maximumArtifactEntries,
|
|
398
|
+
'maximumArtifactEntries',
|
|
399
|
+
0,
|
|
400
|
+
1_000_000,
|
|
401
|
+
),
|
|
402
|
+
maximumArtifacts: integerValue(
|
|
403
|
+
input.maximumArtifacts,
|
|
404
|
+
defaults.maximumArtifacts,
|
|
405
|
+
'maximumArtifacts',
|
|
406
|
+
0,
|
|
407
|
+
64,
|
|
408
|
+
),
|
|
409
|
+
artifactUploadTimeoutMs: integerValue(
|
|
410
|
+
input.artifactUploadTimeoutMs,
|
|
411
|
+
defaults.artifactUploadTimeoutMs,
|
|
412
|
+
'artifactUploadTimeoutMs',
|
|
413
|
+
1,
|
|
414
|
+
86_400_000,
|
|
415
|
+
),
|
|
416
|
+
maximumCacheArchiveBytes: integerValue(
|
|
417
|
+
input.maximumCacheArchiveBytes,
|
|
418
|
+
defaults.maximumCacheArchiveBytes,
|
|
419
|
+
'maximumCacheArchiveBytes',
|
|
420
|
+
0,
|
|
421
|
+
17_179_869_184,
|
|
422
|
+
),
|
|
423
|
+
maximumCacheExtractedBytes: integerValue(
|
|
424
|
+
input.maximumCacheExtractedBytes,
|
|
425
|
+
defaults.maximumCacheExtractedBytes,
|
|
426
|
+
'maximumCacheExtractedBytes',
|
|
427
|
+
0,
|
|
428
|
+
137_438_953_472,
|
|
429
|
+
),
|
|
430
|
+
maximumCacheEntries: integerValue(
|
|
431
|
+
input.maximumCacheEntries,
|
|
432
|
+
defaults.maximumCacheEntries,
|
|
433
|
+
'maximumCacheEntries',
|
|
434
|
+
0,
|
|
435
|
+
1_000_000,
|
|
436
|
+
),
|
|
437
|
+
maximumCaches: integerValue(
|
|
438
|
+
input.maximumCaches,
|
|
439
|
+
defaults.maximumCaches,
|
|
440
|
+
'maximumCaches',
|
|
441
|
+
0,
|
|
442
|
+
32,
|
|
443
|
+
),
|
|
444
|
+
cacheTransferTimeoutMs: integerValue(
|
|
445
|
+
input.cacheTransferTimeoutMs,
|
|
446
|
+
defaults.cacheTransferTimeoutMs,
|
|
447
|
+
'cacheTransferTimeoutMs',
|
|
448
|
+
1,
|
|
449
|
+
86_400_000,
|
|
450
|
+
),
|
|
451
|
+
maximumWorkspaceEntries: integerValue(
|
|
452
|
+
input.maximumWorkspaceEntries,
|
|
453
|
+
defaults.maximumWorkspaceEntries,
|
|
454
|
+
'maximumWorkspaceEntries',
|
|
455
|
+
1,
|
|
456
|
+
2_000_000,
|
|
457
|
+
),
|
|
458
|
+
maximumEnvironmentVariables: integerValue(
|
|
459
|
+
input.maximumEnvironmentVariables,
|
|
460
|
+
defaults.maximumEnvironmentVariables,
|
|
461
|
+
'maximumEnvironmentVariables',
|
|
462
|
+
2,
|
|
463
|
+
128,
|
|
464
|
+
),
|
|
465
|
+
maximumEnvironmentValueBytes: integerValue(
|
|
466
|
+
input.maximumEnvironmentValueBytes,
|
|
467
|
+
defaults.maximumEnvironmentValueBytes,
|
|
468
|
+
'maximumEnvironmentValueBytes',
|
|
469
|
+
1,
|
|
470
|
+
65_536,
|
|
471
|
+
),
|
|
472
|
+
maximumSecretBytes: integerValue(
|
|
473
|
+
input.maximumSecretBytes,
|
|
474
|
+
defaults.maximumSecretBytes,
|
|
475
|
+
'maximumSecretBytes',
|
|
476
|
+
0,
|
|
477
|
+
8_388_608,
|
|
478
|
+
),
|
|
479
|
+
oci: parseOciConfig(input.oci, defaults.oci),
|
|
480
|
+
};
|
|
481
|
+
validateRunnerConfig(config);
|
|
482
|
+
return config;
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
export const validateRunnerConfig = (config: IRunnerConfig): void => {
|
|
486
|
+
validateHttpUrl(config.controlPlaneUrl, config.allowInsecureLoopback, 'controlPlaneUrl');
|
|
487
|
+
if (!config.name || config.name.length > 128) {
|
|
488
|
+
throw new ConfigurationError('name must contain between 1 and 128 characters');
|
|
489
|
+
}
|
|
490
|
+
stringArray(config.labels, [], 'labels', 32);
|
|
491
|
+
const credentials = [
|
|
492
|
+
config.runnerId,
|
|
493
|
+
config.runnerCredentialId,
|
|
494
|
+
config.runnerToken,
|
|
495
|
+
config.runnerTokenExpiresAt,
|
|
496
|
+
];
|
|
497
|
+
const configuredCredentials = credentials.filter((entry) => entry !== undefined).length;
|
|
498
|
+
if (configuredCredentials !== 0 && configuredCredentials !== credentials.length) {
|
|
499
|
+
throw new ConfigurationError(
|
|
500
|
+
'runnerId, runnerCredentialId, runnerToken, and runnerTokenExpiresAt must be configured together',
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
if (configuredCredentials !== 0) {
|
|
504
|
+
if (
|
|
505
|
+
!identifierPattern.test(config.runnerId!) ||
|
|
506
|
+
!identifierPattern.test(config.runnerCredentialId!)
|
|
507
|
+
) {
|
|
508
|
+
throw new ConfigurationError('Runner credential identifiers are invalid');
|
|
509
|
+
}
|
|
510
|
+
if (config.runnerToken!.length < 32 || config.runnerToken!.length > 8192) {
|
|
511
|
+
throw new ConfigurationError('runnerToken must contain between 32 and 8192 characters');
|
|
512
|
+
}
|
|
513
|
+
if (!validTimestamp(config.runnerTokenExpiresAt!)) {
|
|
514
|
+
throw new ConfigurationError('runnerTokenExpiresAt must be a valid protocol timestamp');
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const integerPolicies: Array<[number, string, number, number]> = [
|
|
518
|
+
[config.transferStagingReserveBytes, 'transferStagingReserveBytes', 0, Number.MAX_SAFE_INTEGER],
|
|
519
|
+
[
|
|
520
|
+
config.transferStagingReserveInodes,
|
|
521
|
+
'transferStagingReserveInodes',
|
|
522
|
+
0,
|
|
523
|
+
Number.MAX_SAFE_INTEGER,
|
|
524
|
+
],
|
|
525
|
+
[config.pollIntervalMs, 'pollIntervalMs', 1, 30_000],
|
|
526
|
+
[config.runnerHeartbeatIntervalMs, 'runnerHeartbeatIntervalMs', 5000, 60_000],
|
|
527
|
+
[config.jobHeartbeatIntervalMs, 'jobHeartbeatIntervalMs', 5000, 60_000],
|
|
528
|
+
[config.leaseSafetyMarginMs, 'leaseSafetyMarginMs', 0, 599_999],
|
|
529
|
+
[config.requestTimeoutMs, 'requestTimeoutMs', 1, 600_000],
|
|
530
|
+
[config.maximumResponseBytes, 'maximumResponseBytes', 1, 2_097_152],
|
|
531
|
+
[config.sourceTransferTimeoutMs, 'sourceTransferTimeoutMs', 1, 86_400_000],
|
|
532
|
+
[config.maximumSourceArchiveBytes, 'maximumSourceArchiveBytes', 0, 8_589_934_592],
|
|
533
|
+
[config.maximumSourceExtractedBytes, 'maximumSourceExtractedBytes', 0, 68_719_476_736],
|
|
534
|
+
[config.maximumSourceEntries, 'maximumSourceEntries', 0, 1_000_000],
|
|
535
|
+
[config.maximumLogBytes, 'maximumLogBytes', 0, 1_073_741_824],
|
|
536
|
+
[config.maximumLogChunkBytes, 'maximumLogChunkBytes', 1, 65_536],
|
|
537
|
+
[config.maximumArtifactBytes, 'maximumArtifactBytes', 0, 17_179_869_184],
|
|
538
|
+
[
|
|
539
|
+
config.maximumArtifactExtractedBytes,
|
|
540
|
+
'maximumArtifactExtractedBytes',
|
|
541
|
+
0,
|
|
542
|
+
137_438_953_472,
|
|
543
|
+
],
|
|
544
|
+
[config.maximumArtifactEntries, 'maximumArtifactEntries', 0, 1_000_000],
|
|
545
|
+
[config.maximumArtifacts, 'maximumArtifacts', 0, 64],
|
|
546
|
+
[config.artifactUploadTimeoutMs, 'artifactUploadTimeoutMs', 1, 86_400_000],
|
|
547
|
+
[config.maximumCacheArchiveBytes, 'maximumCacheArchiveBytes', 0, 17_179_869_184],
|
|
548
|
+
[config.maximumCacheExtractedBytes, 'maximumCacheExtractedBytes', 0, 137_438_953_472],
|
|
549
|
+
[config.maximumCacheEntries, 'maximumCacheEntries', 0, 1_000_000],
|
|
550
|
+
[config.maximumCaches, 'maximumCaches', 0, 32],
|
|
551
|
+
[config.cacheTransferTimeoutMs, 'cacheTransferTimeoutMs', 1, 86_400_000],
|
|
552
|
+
[config.maximumWorkspaceEntries, 'maximumWorkspaceEntries', 1, 2_000_000],
|
|
553
|
+
[config.maximumEnvironmentVariables, 'maximumEnvironmentVariables', 2, 128],
|
|
554
|
+
[config.maximumEnvironmentValueBytes, 'maximumEnvironmentValueBytes', 1, 65_536],
|
|
555
|
+
[config.maximumSecretBytes, 'maximumSecretBytes', 0, 8_388_608],
|
|
556
|
+
[config.oci.defaultTimeoutMs, 'oci.defaultTimeoutMs', 1000, 86_400_000],
|
|
557
|
+
[config.oci.maximumTimeoutMs, 'oci.maximumTimeoutMs', 1000, 86_400_000],
|
|
558
|
+
[config.oci.defaultMemoryBytes, 'oci.defaultMemoryBytes', 16_777_216, 1_099_511_627_776],
|
|
559
|
+
[config.oci.maximumMemoryBytes, 'oci.maximumMemoryBytes', 16_777_216, 1_099_511_627_776],
|
|
560
|
+
[config.oci.defaultPids, 'oci.defaultPids', 1, 1_048_576],
|
|
561
|
+
[config.oci.maximumPids, 'oci.maximumPids', 1, 1_048_576],
|
|
562
|
+
[config.oci.workspaceSizeBytes, 'oci.workspaceSizeBytes', 16_777_216, 1_099_511_627_776],
|
|
563
|
+
[config.oci.temporarySizeBytes, 'oci.temporarySizeBytes', 1, 1_099_511_627_776],
|
|
564
|
+
[config.oci.stopTimeoutSeconds, 'oci.stopTimeoutSeconds', 1, 30],
|
|
565
|
+
];
|
|
566
|
+
for (const [value, field, minimum, maximum] of integerPolicies) {
|
|
567
|
+
integerValue(value, value, field, minimum, maximum);
|
|
568
|
+
}
|
|
569
|
+
numberValue(config.oci.defaultCpus, config.oci.defaultCpus, 'oci.defaultCpus', 0, 1024);
|
|
570
|
+
numberValue(config.oci.maximumCpus, config.oci.maximumCpus, 'oci.maximumCpus', 0, 1024);
|
|
571
|
+
if (config.oci.runtime !== 'docker' && config.oci.runtime !== 'podman') {
|
|
572
|
+
throw new ConfigurationError('oci.runtime must be docker or podman');
|
|
573
|
+
}
|
|
574
|
+
if (
|
|
575
|
+
config.oci.allowedNetworks.length === 0 || config.oci.allowedNetworks.length > 2 ||
|
|
576
|
+
new Set(config.oci.allowedNetworks).size !== config.oci.allowedNetworks.length ||
|
|
577
|
+
config.oci.allowedNetworks.some((network) => network !== 'none' && network !== 'bridge')
|
|
578
|
+
) {
|
|
579
|
+
throw new ConfigurationError(
|
|
580
|
+
'oci.allowedNetworks may only contain unique none or bridge values',
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
if (!isAbsolute(config.oci.runtimePath)) {
|
|
584
|
+
throw new ConfigurationError('oci.runtimePath must be absolute');
|
|
585
|
+
}
|
|
586
|
+
if (config.oci.workspaceHelperPath && !isAbsolute(config.oci.workspaceHelperPath)) {
|
|
587
|
+
throw new ConfigurationError('oci.workspaceHelperPath must be absolute');
|
|
588
|
+
}
|
|
589
|
+
const user = /^([1-9][0-9]*)(?::([1-9][0-9]*))?$/.exec(config.oci.containerUser);
|
|
590
|
+
if (!user || [user[1], user[2] ?? user[1]].some((entry) => Number(entry) > 4_294_967_294)) {
|
|
591
|
+
throw new ConfigurationError('oci.containerUser must be a numeric non-root UID[:GID]');
|
|
592
|
+
}
|
|
593
|
+
if (config.oci.defaultTimeoutMs > config.oci.maximumTimeoutMs) {
|
|
594
|
+
throw new ConfigurationError('oci.defaultTimeoutMs exceeds oci.maximumTimeoutMs');
|
|
595
|
+
}
|
|
596
|
+
if (config.oci.defaultMemoryBytes > config.oci.maximumMemoryBytes) {
|
|
597
|
+
throw new ConfigurationError('oci.defaultMemoryBytes exceeds oci.maximumMemoryBytes');
|
|
598
|
+
}
|
|
599
|
+
if (config.oci.defaultCpus > config.oci.maximumCpus) {
|
|
600
|
+
throw new ConfigurationError('oci.defaultCpus exceeds oci.maximumCpus');
|
|
601
|
+
}
|
|
602
|
+
if (config.oci.defaultPids > config.oci.maximumPids) {
|
|
603
|
+
throw new ConfigurationError('oci.defaultPids exceeds oci.maximumPids');
|
|
604
|
+
}
|
|
605
|
+
if (
|
|
606
|
+
config.maximumSourceExtractedBytes > config.oci.workspaceSizeBytes ||
|
|
607
|
+
config.maximumArtifactExtractedBytes > config.oci.workspaceSizeBytes ||
|
|
608
|
+
config.maximumCacheExtractedBytes > config.oci.workspaceSizeBytes
|
|
609
|
+
) {
|
|
610
|
+
throw new ConfigurationError('Extracted transfer limits exceed the workspace byte limit');
|
|
611
|
+
}
|
|
612
|
+
if (
|
|
613
|
+
config.maximumSourceEntries > config.maximumWorkspaceEntries ||
|
|
614
|
+
config.maximumArtifactEntries > config.maximumWorkspaceEntries ||
|
|
615
|
+
config.maximumCacheEntries > config.maximumWorkspaceEntries
|
|
616
|
+
) {
|
|
617
|
+
throw new ConfigurationError('Transfer entry limits exceed the workspace entry limit');
|
|
618
|
+
}
|
|
619
|
+
if (config.maximumLogBytes < config.maximumLogChunkBytes) {
|
|
620
|
+
throw new ConfigurationError('maximumLogChunkBytes exceeds maximumLogBytes');
|
|
621
|
+
}
|
|
622
|
+
if (config.jobHeartbeatIntervalMs + config.leaseSafetyMarginMs >= 30_000) {
|
|
623
|
+
throw new ConfigurationError(
|
|
624
|
+
'Job heartbeat and lease safety margin do not fit a minimum lease',
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
const applyEnvironment = (value: Record<string, unknown>): Record<string, unknown> => {
|
|
630
|
+
const result = structuredClone(value);
|
|
631
|
+
const mappings: Array<[string, string]> = [
|
|
632
|
+
['SHIP_RUNNER_CONTROL_PLANE_URL', 'controlPlaneUrl'],
|
|
633
|
+
['SHIP_RUNNER_NAME', 'name'],
|
|
634
|
+
['SHIP_RUNNER_BOOTSTRAP_TOKEN', 'bootstrapToken'],
|
|
635
|
+
['SHIP_RUNNER_ID', 'runnerId'],
|
|
636
|
+
['SHIP_RUNNER_CREDENTIAL_ID', 'runnerCredentialId'],
|
|
637
|
+
['SHIP_RUNNER_TOKEN', 'runnerToken'],
|
|
638
|
+
['SHIP_RUNNER_TOKEN_EXPIRES_AT', 'runnerTokenExpiresAt'],
|
|
639
|
+
['SHIP_RUNNER_STATE_DIRECTORY', 'stateDirectory'],
|
|
640
|
+
];
|
|
641
|
+
for (const [environmentName, field] of mappings) {
|
|
642
|
+
const environmentValue = Deno.env.get(environmentName);
|
|
643
|
+
if (environmentValue) result[field] = environmentValue;
|
|
644
|
+
}
|
|
645
|
+
const labels = Deno.env.get('SHIP_RUNNER_LABELS');
|
|
646
|
+
if (labels) result.labels = labels.split(',').map((entry) => entry.trim()).filter(Boolean);
|
|
647
|
+
return result;
|
|
648
|
+
};
|
|
649
|
+
|
|
650
|
+
export const loadRunnerConfig = async (path: string): Promise<IRunnerConfig> => {
|
|
651
|
+
let input: Record<string, unknown> = {};
|
|
652
|
+
try {
|
|
653
|
+
const parsed: unknown = JSON.parse(await Deno.readTextFile(path));
|
|
654
|
+
if (!isRecord(parsed)) throw new ConfigurationError('Runner config must be a JSON object');
|
|
655
|
+
input = parsed;
|
|
656
|
+
} catch (error) {
|
|
657
|
+
if (!(error instanceof Deno.errors.NotFound)) throw error;
|
|
658
|
+
}
|
|
659
|
+
return parseRunnerConfig(applyEnvironment(input));
|
|
660
|
+
};
|
|
661
|
+
|
|
662
|
+
export const saveRunnerConfig = async (path: string, config: IRunnerConfig): Promise<void> => {
|
|
663
|
+
validateRunnerConfig(config);
|
|
664
|
+
const parent = dirname(path);
|
|
665
|
+
await Deno.mkdir(parent, { recursive: true, mode: 0o700 });
|
|
666
|
+
const temporaryPath = join(parent, `.${crypto.randomUUID()}.runner-config.tmp`);
|
|
667
|
+
const bytes = new TextEncoder().encode(`${JSON.stringify(config, null, 2)}\n`);
|
|
668
|
+
let file: Deno.FsFile | undefined;
|
|
669
|
+
try {
|
|
670
|
+
file = await Deno.open(temporaryPath, {
|
|
671
|
+
write: true,
|
|
672
|
+
createNew: true,
|
|
673
|
+
mode: 0o600,
|
|
674
|
+
});
|
|
675
|
+
let offset = 0;
|
|
676
|
+
while (offset < bytes.byteLength) {
|
|
677
|
+
const written = await file.write(bytes.subarray(offset));
|
|
678
|
+
if (written <= 0) throw new ConfigurationError('Runner config write made no progress');
|
|
679
|
+
offset += written;
|
|
680
|
+
}
|
|
681
|
+
await file.sync();
|
|
682
|
+
file.close();
|
|
683
|
+
file = undefined;
|
|
684
|
+
await Deno.chmod(temporaryPath, 0o600);
|
|
685
|
+
await Deno.rename(temporaryPath, path);
|
|
686
|
+
await Deno.chmod(path, 0o600);
|
|
687
|
+
const directory = await Deno.open(parent, { read: true });
|
|
688
|
+
try {
|
|
689
|
+
await directory.sync();
|
|
690
|
+
} finally {
|
|
691
|
+
directory.close();
|
|
692
|
+
}
|
|
693
|
+
} catch (error) {
|
|
694
|
+
file?.close();
|
|
695
|
+
try {
|
|
696
|
+
await Deno.remove(temporaryPath);
|
|
697
|
+
} catch (cleanupError) {
|
|
698
|
+
if (!(cleanupError instanceof Deno.errors.NotFound)) {
|
|
699
|
+
throw new AggregateError([error, cleanupError], 'Config write and cleanup failed');
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
throw error;
|
|
703
|
+
}
|
|
704
|
+
};
|
|
705
|
+
|
|
706
|
+
export const withRunnerCredential = (
|
|
707
|
+
config: IRunnerConfig,
|
|
708
|
+
credential: IRunnerCredential,
|
|
709
|
+
): IRunnerConfig => ({
|
|
710
|
+
...config,
|
|
711
|
+
bootstrapToken: undefined,
|
|
712
|
+
runnerId: credential.runnerId,
|
|
713
|
+
runnerCredentialId: credential.credentialId,
|
|
714
|
+
runnerToken: credential.token,
|
|
715
|
+
runnerTokenExpiresAt: credential.expiresAt,
|
|
716
|
+
labels: [...config.labels],
|
|
717
|
+
oci: { ...config.oci, allowedNetworks: [...config.oci.allowedNetworks] },
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
export const maskRunnerConfig = (config: IRunnerConfig): Record<string, unknown> => ({
|
|
721
|
+
...config,
|
|
722
|
+
bootstrapToken: config.bootstrapToken ? '***' : undefined,
|
|
723
|
+
runnerToken: config.runnerToken ? '***' : undefined,
|
|
724
|
+
});
|