hammerkit 2.0.0 → 2.1.0-next.10
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-BFnEN5i0.js +8 -0
- package/dist/dashboard/assets/{index-DRSwIRXd.js → index-CE5PSB3y.js} +15 -9
- package/dist/dashboard/assets/index-Dt3ITvRO.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/check-readiness.js +8 -4
- package/dist/executer/docker-healthcheck.js +73 -0
- package/dist/executer/docker-service.js +21 -15
- package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +62 -14
- package/dist/kubernetes/kept-claims.js +12 -9
- package/dist/network/healthcheck-request.js +38 -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-runtime-docker.js +24 -1
- package/dist/planner/work-runtime-kubernetes.js +37 -16
- 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/remote-status.js +1 -1
- package/dist/remote/remote-work-commands.js +5 -3
- 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/borrowed-quota.js +216 -0
- package/dist/server/capacity.js +18 -6
- package/dist/server/kubernetes-backend.js +114 -28
- package/dist/server/kubernetes-sandbox-storage.js +3 -2
- package/dist/server/overview.js +7 -1
- package/dist/server/plan.js +16 -1
- package/dist/server/run-source-environment.js +1 -1
- package/dist/server/runs.js +4 -6
- package/dist/server/sandbox-files-api.js +27 -12
- package/dist/server/sandbox-files.js +3 -2
- package/dist/server/sandbox-pod.js +1 -1
- package/dist/server/sandboxes-api.js +65 -14
- package/dist/server/sandboxes.js +19 -3
- package/dist/server/server-config.js +59 -2
- 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/server/capacity.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatMemory = exports.formatCpu = exports.HostCapacity = exports.KubernetesCapacity = exports.cachedCapacity = exports.parseQuantity = void 0;
|
|
3
|
+
exports.formatMemory = exports.formatCpu = exports.HostCapacity = exports.KubernetesCapacity = exports.getQuotaResources = exports.cachedCapacity = exports.parseQuantity = void 0;
|
|
4
4
|
const os_1 = require("os");
|
|
5
5
|
const log_1 = require("../log");
|
|
6
6
|
const apply_1 = require("../kubernetes/apply");
|
|
@@ -71,6 +71,16 @@ function briefly(e, what) {
|
|
|
71
71
|
}
|
|
72
72
|
return what === 'nodes' ? 'nodes not readable' : 'metrics not readable';
|
|
73
73
|
}
|
|
74
|
+
// The CPU and memory pods may request under a quota, if it limits them
|
|
75
|
+
function getQuotaResources(hard) {
|
|
76
|
+
const cpu = (0, units_1.parseQuantity)(hard['requests.cpu'] ?? hard.cpu);
|
|
77
|
+
const memory = (0, units_1.parseQuantity)(hard['requests.memory'] ?? hard.memory);
|
|
78
|
+
if (cpu === undefined && memory === undefined) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
return { ...(cpu !== undefined ? { cpu } : {}), ...(memory !== undefined ? { memory } : {}) };
|
|
82
|
+
}
|
|
83
|
+
exports.getQuotaResources = getQuotaResources;
|
|
74
84
|
const ENDED = ['Succeeded', 'Failed'];
|
|
75
85
|
// On Kubernetes: per namespace of the server, its pods' requests, its quota and its usage.
|
|
76
86
|
class KubernetesCapacity {
|
|
@@ -162,11 +172,13 @@ class KubernetesCapacity {
|
|
|
162
172
|
name: this.options.quotaName,
|
|
163
173
|
namespace,
|
|
164
174
|
});
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
175
|
+
const resources = getQuotaResources(quota.spec?.hard ?? {});
|
|
176
|
+
if (resources) {
|
|
177
|
+
group.quota = resources;
|
|
178
|
+
const share = this.options.share;
|
|
179
|
+
if (kind === 'caller' && share && (share.cpu !== resources.cpu || share.memory !== resources.memory)) {
|
|
180
|
+
group.share = share;
|
|
181
|
+
}
|
|
170
182
|
}
|
|
171
183
|
}
|
|
172
184
|
catch (e) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.awaitRunObjectsGone = exports.removeRunObjects = exports.CallerNamespaces = exports.getVolumeScope = exports.getPrivilegedNamespaceProblem = exports.getCapabilitiesProblem = exports.getPodSecurityLabels = exports.KubernetesExecutor = exports.getRunRoleBinding = exports.removeServiceRunNamespace = exports.getServerIngressPolicy = exports.getNamespacePolicies = exports.getClusterAccess = exports.isCallerNamespace = exports.SERVICE_RUN_LABEL = exports.getServiceRunNamespace = exports.getCallerNamespace = void 0;
|
|
3
|
+
exports.awaitRunObjectsGone = exports.removeRunObjects = exports.CallerNamespaces = exports.getNamespaceQuota = exports.getVolumeScope = exports.getKeptClaimTasks = exports.getPrivilegedNamespaceProblem = exports.getCapabilitiesProblem = exports.getPodSecurityLabels = exports.KubernetesExecutor = exports.getRunRoleBinding = exports.removeServiceRunNamespace = exports.getServerIngressPolicy = exports.getNamespacePolicies = exports.getClusterAccess = exports.isCallerNamespace = exports.SERVICE_RUN_LABEL = exports.getServiceRunNamespace = exports.getCallerNamespace = void 0;
|
|
4
4
|
const crypto_1 = require("crypto");
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const os_1 = require("os");
|
|
@@ -9,6 +9,7 @@ const net_1 = require("net");
|
|
|
9
9
|
const apply_1 = require("../kubernetes/apply");
|
|
10
10
|
const job_placement_1 = require("../kubernetes/job-placement");
|
|
11
11
|
const capacity_1 = require("./capacity");
|
|
12
|
+
const borrowed_quota_1 = require("./borrowed-quota");
|
|
12
13
|
const kubernetes_instance_1 = require("../kubernetes/kubernetes-instance");
|
|
13
14
|
const log_1 = require("../log");
|
|
14
15
|
const network_policy_1 = require("../kubernetes/network-policy");
|
|
@@ -259,9 +260,9 @@ exports.getRunRoleBinding = getRunRoleBinding;
|
|
|
259
260
|
* of a caller execute side by side in their namespace, up to `runs.limits.running`:
|
|
260
261
|
* the objects of a run are named and found by its run label (getItemKey), so two runs
|
|
261
262
|
* of the same task have their own. What two runs share are the claims the server keeps
|
|
262
|
-
* (`backend.keepVolumes`) of a scope (getVolumeScope):
|
|
263
|
-
* a time (
|
|
264
|
-
* kept claims, which go once they expired.
|
|
263
|
+
* (`backend.keepVolumes`) of a scope (getVolumeScope), one per task: two runs of one scope
|
|
264
|
+
* with a task in common go one at a time (excludes). When a run ends, everything made for it
|
|
265
|
+
* is removed, but for the kept claims, which go once they expired.
|
|
265
266
|
*/
|
|
266
267
|
class KubernetesExecutor {
|
|
267
268
|
constructor(config, processEnvs, options) {
|
|
@@ -269,7 +270,8 @@ class KubernetesExecutor {
|
|
|
269
270
|
// per caller namespace, what changes objects of the whole namespace: its setup, the
|
|
270
271
|
// removal of what a stopped server left, the sweeps of kept claims. The last one waiting.
|
|
271
272
|
this.queues = new Map();
|
|
272
|
-
// per caller namespace, the runs that execute in it: by run label,
|
|
273
|
+
// per caller namespace, the runs that execute in it and mount kept claims: by run label,
|
|
274
|
+
// their volume scope
|
|
273
275
|
this.active = new Map();
|
|
274
276
|
// per caller namespace, when its kept claims are next looked at
|
|
275
277
|
this.sweeps = new Map();
|
|
@@ -302,13 +304,27 @@ class KubernetesExecutor {
|
|
|
302
304
|
this.keep = config.backend.keepVolumes === false ? 0 : (0, units_1.parseDuration)(config.backend.keepVolumes);
|
|
303
305
|
this.keepServiceRuns = config.runs.up.keepVolumes;
|
|
304
306
|
this.log = options.log;
|
|
307
|
+
const { quota, maxQuota, pools } = config.backend;
|
|
308
|
+
if (quota && maxQuota) {
|
|
309
|
+
this.quotas = new borrowed_quota_1.BorrowedQuotas({
|
|
310
|
+
quota,
|
|
311
|
+
maxQuota,
|
|
312
|
+
room: () => (0, borrowed_quota_1.readPoolRoom)(this.instance, pools),
|
|
313
|
+
write: async (namespace, hard) => {
|
|
314
|
+
await (0, apply_1.replace)(this.instance, getNamespaceQuota(namespace, hard));
|
|
315
|
+
},
|
|
316
|
+
log: options.log,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
305
319
|
this.host = new executor_1.HostExecutor({ ...options, placement: this.placement() });
|
|
306
320
|
}
|
|
307
321
|
// what the namespaces of the server's callers and service runs request and use
|
|
308
322
|
capacity() {
|
|
309
323
|
const prefix = this.config.backend.namespacePrefix;
|
|
324
|
+
const { quota, maxQuota } = this.config.backend;
|
|
310
325
|
return new capacity_1.KubernetesCapacity(this.instance, {
|
|
311
326
|
quotaName: POLICY_NAME,
|
|
327
|
+
...(quota && maxQuota ? { share: (0, capacity_1.getQuotaResources)(quota) } : {}),
|
|
312
328
|
namespaces: async () => (await this.instance.coreApi.listNamespace({ labelSelector: `${MANAGED_LABEL}=true` })).items
|
|
313
329
|
.filter((namespace) => isCallerNamespace(prefix, namespace.metadata?.name ?? ''))
|
|
314
330
|
.map((namespace) => ({
|
|
@@ -343,7 +359,11 @@ class KubernetesExecutor {
|
|
|
343
359
|
runs = new Map();
|
|
344
360
|
this.active.set(namespace, runs);
|
|
345
361
|
}
|
|
346
|
-
|
|
362
|
+
if (this.holds(run)) {
|
|
363
|
+
runs.set(label, getVolumeScope(run));
|
|
364
|
+
}
|
|
365
|
+
// the caller is promised its share while the run executes
|
|
366
|
+
this.quotas?.enter(namespace, run.id);
|
|
347
367
|
try {
|
|
348
368
|
await this.host.execute(run, context);
|
|
349
369
|
}
|
|
@@ -352,6 +372,9 @@ class KubernetesExecutor {
|
|
|
352
372
|
if (runs.size === 0 && this.active.get(namespace) === runs) {
|
|
353
373
|
this.active.delete(namespace);
|
|
354
374
|
}
|
|
375
|
+
await this.quotas
|
|
376
|
+
?.leave(namespace, run.id)
|
|
377
|
+
.catch((e) => this.log?.(`the quotas could not be worked out again: ${(0, log_1.getErrorMessage)(e)}`));
|
|
355
378
|
}
|
|
356
379
|
}
|
|
357
380
|
// A service run moves to another commit in its namespace: unchanged services stay, the
|
|
@@ -417,17 +440,41 @@ class KubernetesExecutor {
|
|
|
417
440
|
this.serviceRunsSwept = false;
|
|
418
441
|
}
|
|
419
442
|
}
|
|
420
|
-
// The runs of a scope share
|
|
421
|
-
// what a task finds in
|
|
422
|
-
//
|
|
423
|
-
|
|
443
|
+
// The runs of a scope share the kept claim of each task, which one of them may change at a
|
|
444
|
+
// time: what a task finds in it is what the last run left there. Two runs of a scope go
|
|
445
|
+
// side by side when they have no task in common, and a run that mounts no kept claim holds
|
|
446
|
+
// nothing. Without kept claims, runs share no object and nothing is held.
|
|
447
|
+
excludes(run, other) {
|
|
448
|
+
const held = this.holds(run);
|
|
449
|
+
const theirs = this.holds(other);
|
|
450
|
+
if (!held || !theirs || held.scope !== theirs.scope) {
|
|
451
|
+
return false;
|
|
452
|
+
}
|
|
453
|
+
if (!held.tasks || !theirs.tasks) {
|
|
454
|
+
return true;
|
|
455
|
+
}
|
|
456
|
+
const tasks = new Set(theirs.tasks);
|
|
457
|
+
return held.tasks.some((task) => tasks.has(task));
|
|
458
|
+
}
|
|
459
|
+
// The kept claims a run mounts: its scope in the caller's namespace and the tasks of the
|
|
460
|
+
// claims (getKeptClaimTasks), all of the scope's for a run without a plan. Nothing for a
|
|
461
|
+
// run that mounts none.
|
|
462
|
+
holds(run) {
|
|
424
463
|
if (this.keep === 0 || run.up) {
|
|
425
464
|
return undefined;
|
|
426
465
|
}
|
|
427
|
-
|
|
466
|
+
const tasks = getKeptClaimTasks(run);
|
|
467
|
+
if (tasks?.length === 0) {
|
|
468
|
+
return undefined;
|
|
469
|
+
}
|
|
470
|
+
return {
|
|
471
|
+
scope: `${getCallerNamespace(this.config.backend.namespacePrefix, run.owner)}/${getVolumeScope(run)}`,
|
|
472
|
+
tasks,
|
|
473
|
+
};
|
|
428
474
|
}
|
|
429
|
-
// The scopes of the runs that execute in the namespace
|
|
430
|
-
// are theirs to mount and stay. A server that keeps none
|
|
475
|
+
// The scopes of the runs that execute in the namespace and mount kept claims, but
|
|
476
|
+
// `except`'s: their kept claims are theirs to mount and stay. A server that keeps none
|
|
477
|
+
// mounts none.
|
|
431
478
|
scopesInUse(namespace, except) {
|
|
432
479
|
if (this.keep === 0) {
|
|
433
480
|
return new Set();
|
|
@@ -474,6 +521,8 @@ class KubernetesExecutor {
|
|
|
474
521
|
quota: this.config.runs.up.quota,
|
|
475
522
|
});
|
|
476
523
|
});
|
|
524
|
+
// its pods take room the callers can't borrow
|
|
525
|
+
await this.quotas?.serviceRun(namespace, this.config.runs.up.quota ?? this.config.backend.quota);
|
|
477
526
|
context.emit({ type: 'log', task: 'hammerkit', stream: 'stdout', text: `runs in the namespace ${namespace}` });
|
|
478
527
|
return { kubernetes: this.environmentOf(run, namespace), env: this.access.env };
|
|
479
528
|
}
|
|
@@ -504,6 +553,9 @@ class KubernetesExecutor {
|
|
|
504
553
|
}
|
|
505
554
|
});
|
|
506
555
|
this.sweepLeftClaims();
|
|
556
|
+
// the quota of the caller, and of the callers it borrows from or lends to, before its
|
|
557
|
+
// tasks run
|
|
558
|
+
await this.quotas?.apply(namespace);
|
|
507
559
|
context.emit({ type: 'log', task: 'hammerkit', stream: 'stdout', text: `runs in the namespace ${namespace}` });
|
|
508
560
|
return {
|
|
509
561
|
kubernetes: this.environmentOf(run, namespace, work, capabilitiesProblem),
|
|
@@ -540,6 +592,9 @@ class KubernetesExecutor {
|
|
|
540
592
|
}
|
|
541
593
|
else {
|
|
542
594
|
this.serviceRuns.delete(namespace);
|
|
595
|
+
await this.quotas
|
|
596
|
+
?.serviceRunEnded(namespace)
|
|
597
|
+
.catch((e) => this.log?.(`the quotas could not be worked out again: ${(0, log_1.getErrorMessage)(e)}`));
|
|
543
598
|
}
|
|
544
599
|
}
|
|
545
600
|
return;
|
|
@@ -736,6 +791,22 @@ function getPrivilegedNamespaceProblem(namespace, labels, backend) {
|
|
|
736
791
|
`${namespace} ${ENFORCE_LABEL}=baseline ${AUDIT_LABEL}- --overwrite`);
|
|
737
792
|
}
|
|
738
793
|
exports.getPrivilegedNamespaceProblem = getPrivilegedNamespaceProblem;
|
|
794
|
+
// The tasks of a run whose kept claims it mounts, by name: each container task of its plan,
|
|
795
|
+
// which mounts its own claim, while the tasks that depend on it and the services that mount its
|
|
796
|
+
// outputs mount it too (they have it in their plan). Another task has none: an aggregate, or a
|
|
797
|
+
// local task on a server that allows them. A kept claim is named after its task
|
|
798
|
+
// (getClaimName), so two runs with no task in common mount no claim in common, whatever
|
|
799
|
+
// their tasks are. Null for a run without a plan (submitted by a version that recorded none).
|
|
800
|
+
function getKeptClaimTasks(run) {
|
|
801
|
+
if (!run.plan) {
|
|
802
|
+
return null;
|
|
803
|
+
}
|
|
804
|
+
return Object.entries(run.plan)
|
|
805
|
+
.filter(([, task]) => task.kind === 'task' && task.runtime === 'docker')
|
|
806
|
+
.map(([name]) => name)
|
|
807
|
+
.sort();
|
|
808
|
+
}
|
|
809
|
+
exports.getKeptClaimTasks = getKeptClaimTasks;
|
|
739
810
|
// What may mount a claim a run kept: the runs of the same branch or tag of a repository,
|
|
740
811
|
// which share its cache, so that the runs of two branches go side by side and the runs of one find what
|
|
741
812
|
// the last one left; on a sandbox the runs of that sandbox only, whose claim is on the same
|
|
@@ -755,6 +826,19 @@ function getVolumeScope(run) {
|
|
|
755
826
|
return (0, executor_1.cacheNamespace)(run);
|
|
756
827
|
}
|
|
757
828
|
exports.getVolumeScope = getVolumeScope;
|
|
829
|
+
function getPolicyMetadata(namespace) {
|
|
830
|
+
return {
|
|
831
|
+
name: POLICY_NAME,
|
|
832
|
+
namespace,
|
|
833
|
+
annotations: { 'hammerkit.dev/version': (0, version_1.getVersion)() },
|
|
834
|
+
labels: { [MANAGED_LABEL]: 'true' },
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
// The ResourceQuota of a namespace of the server
|
|
838
|
+
function getNamespaceQuota(namespace, hard) {
|
|
839
|
+
return { apiVersion: 'v1', kind: 'ResourceQuota', metadata: getPolicyMetadata(namespace), spec: { hard } };
|
|
840
|
+
}
|
|
841
|
+
exports.getNamespaceQuota = getNamespaceQuota;
|
|
758
842
|
/**
|
|
759
843
|
* The namespaces of callers on the cluster: named by the caller (getCallerNamespace),
|
|
760
844
|
* made on first use, with the operator's quota.
|
|
@@ -842,18 +926,9 @@ class CallerNamespaces {
|
|
|
842
926
|
]) {
|
|
843
927
|
await (0, apply_1.replace)(this.instance, policy);
|
|
844
928
|
}
|
|
845
|
-
const metadata =
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
annotations: { 'hammerkit.dev/version': (0, version_1.getVersion)() },
|
|
849
|
-
labels: { [MANAGED_LABEL]: 'true' },
|
|
850
|
-
};
|
|
851
|
-
const quota = {
|
|
852
|
-
apiVersion: 'v1',
|
|
853
|
-
kind: 'ResourceQuota',
|
|
854
|
-
metadata,
|
|
855
|
-
spec: { hard: serviceRun?.quota ?? backend.quota },
|
|
856
|
-
};
|
|
929
|
+
const metadata = getPolicyMetadata(namespace);
|
|
930
|
+
const hard = serviceRun?.quota ?? backend.quota;
|
|
931
|
+
const quota = getNamespaceQuota(namespace, hard ?? {});
|
|
857
932
|
const defaults = backend.defaultResources;
|
|
858
933
|
const limits = {
|
|
859
934
|
apiVersion: 'v1',
|
|
@@ -869,10 +944,21 @@ class CallerNamespaces {
|
|
|
869
944
|
],
|
|
870
945
|
},
|
|
871
946
|
};
|
|
872
|
-
|
|
873
|
-
|
|
947
|
+
// a caller who may borrow has the quota the server works out for them (BorrowedQuotas),
|
|
948
|
+
// written once the namespace is there: here it is made with the share, never replaced
|
|
949
|
+
const borrows = !!backend.maxQuota && !serviceRun;
|
|
950
|
+
if (borrows) {
|
|
951
|
+
await this.instance.objectApi.create(quota).catch((e) => {
|
|
952
|
+
if ((0, apply_1.statusCodeOf)(e) !== 409) {
|
|
953
|
+
throw e;
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
const objects = [
|
|
958
|
+
...(borrows ? [] : [[quota, !!hard]]),
|
|
874
959
|
[limits, !!defaults],
|
|
875
|
-
]
|
|
960
|
+
];
|
|
961
|
+
for (const [object, wanted] of objects) {
|
|
876
962
|
if (wanted) {
|
|
877
963
|
await (0, apply_1.replace)(this.instance, object);
|
|
878
964
|
}
|
|
@@ -1030,8 +1030,9 @@ class KubernetesSandboxFiles {
|
|
|
1030
1030
|
changes() {
|
|
1031
1031
|
return this.ask({ op: 'changes' });
|
|
1032
1032
|
}
|
|
1033
|
-
diff(format, author) {
|
|
1034
|
-
|
|
1033
|
+
diff(format, author, paths) {
|
|
1034
|
+
// a pod of an older image leaves `paths` out and answers the whole diff
|
|
1035
|
+
return this.ask({ op: 'diff', format, author, ...(paths ? { paths } : {}) });
|
|
1035
1036
|
}
|
|
1036
1037
|
async commit(author) {
|
|
1037
1038
|
return (await this.ask({ op: 'commit', author })) ?? undefined;
|
package/dist/server/overview.js
CHANGED
|
@@ -238,7 +238,7 @@ const STATES = [
|
|
|
238
238
|
'cancelled',
|
|
239
239
|
'rejected',
|
|
240
240
|
];
|
|
241
|
-
// `scope`, `state` (a comma separated list) and `
|
|
241
|
+
// `scope`, `state` (a comma separated list), `kind`, `id`, `sandbox`, `since` and `until` of GET /v1/runs.
|
|
242
242
|
function parseRunFilter(params, grant, now = Date.now()) {
|
|
243
243
|
const scope = params.get('scope') ?? 'mine';
|
|
244
244
|
if (scope !== 'mine' && scope !== 'visible' && scope !== 'all') {
|
|
@@ -264,6 +264,10 @@ function parseRunFilter(params, grant, now = Date.now()) {
|
|
|
264
264
|
if (id !== undefined && !/^run_[A-Za-z0-9_-]{16}$/.test(id)) {
|
|
265
265
|
throw new Error('id: the id of a run, run_...');
|
|
266
266
|
}
|
|
267
|
+
const sandbox = params.get('sandbox') ?? undefined;
|
|
268
|
+
if (sandbox !== undefined && !runs_1.SANDBOX_ID_PATTERN.test(sandbox)) {
|
|
269
|
+
throw new Error('sandbox: the id of a sandbox, sbx_...');
|
|
270
|
+
}
|
|
267
271
|
const since = parseMoment(params.get('since'), now, 'since');
|
|
268
272
|
const until = parseMoment(params.get('until'), now, 'until');
|
|
269
273
|
if (since !== undefined && until !== undefined && until < since) {
|
|
@@ -274,6 +278,7 @@ function parseRunFilter(params, grant, now = Date.now()) {
|
|
|
274
278
|
...(states ? { states } : {}),
|
|
275
279
|
...(kind ? { kind } : {}),
|
|
276
280
|
...(id ? { id } : {}),
|
|
281
|
+
...(sandbox ? { sandbox } : {}),
|
|
277
282
|
...(since !== undefined ? { since } : {}),
|
|
278
283
|
...(until !== undefined ? { until } : {}),
|
|
279
284
|
};
|
|
@@ -299,6 +304,7 @@ exports.FilterRefused = FilterRefused;
|
|
|
299
304
|
function filterRuns(runs, viewer, filter) {
|
|
300
305
|
return runs.filter((run) => (filter.scope === 'mine' ? owns(viewer, run) : maySee(viewer, run)) &&
|
|
301
306
|
(!filter.id || run.id === filter.id) &&
|
|
307
|
+
(!filter.sandbox || (run.source.type === 'sandbox' && run.source.id === filter.sandbox)) &&
|
|
302
308
|
(!filter.states || filter.states.includes(run.state)) &&
|
|
303
309
|
(!filter.kind || (filter.kind === 'services') === !!run.up) &&
|
|
304
310
|
(filter.since === undefined || Date.parse(run.createdAt) >= filter.since) &&
|
package/dist/server/plan.js
CHANGED
|
@@ -19,6 +19,7 @@ const parse_error_1 = require("../schema/parse-error");
|
|
|
19
19
|
const task_arguments_1 = require("../planner/utils/task-arguments");
|
|
20
20
|
const work_network_1 = require("../planner/work-network");
|
|
21
21
|
const run_source_envs_1 = require("../environment/run-source-envs");
|
|
22
|
+
const prefetch_secrets_1 = require("../secrets/prefetch-secrets");
|
|
22
23
|
class DefinitionError extends Error {
|
|
23
24
|
}
|
|
24
25
|
exports.DefinitionError = DefinitionError;
|
|
@@ -167,6 +168,9 @@ function describeSecret(secret) {
|
|
|
167
168
|
if (source.type === 'provider') {
|
|
168
169
|
return { from: `${source.provider}:${source.ref}`, account: source.account, target };
|
|
169
170
|
}
|
|
171
|
+
if (source.type === 'unavailable') {
|
|
172
|
+
return { from: source.from, account: null, target };
|
|
173
|
+
}
|
|
170
174
|
return { from: source.type === 'env' ? `env:${source.name}` : `file:${source.path}`, account: null, target };
|
|
171
175
|
}
|
|
172
176
|
// What a server refuses to publish. A secret that reads a file or a variable of the
|
|
@@ -335,7 +339,14 @@ function toPlan(root, item) {
|
|
|
335
339
|
const mounts = (0, work_task_1.isContainerWorkTask)(data) || (0, work_service_1.isContainerWorkService)(data)
|
|
336
340
|
? data.mounts.map((mount) => {
|
|
337
341
|
const inside = insideBundle(root, data.cwd, mount.localPath);
|
|
338
|
-
|
|
342
|
+
const readOnly = mount.readOnly ? ':ro' : '';
|
|
343
|
+
// a mount that names only its source (`- .`) lands at the same path in the container,
|
|
344
|
+
// which is in the directory the server read the bundle in, one of its own each time:
|
|
345
|
+
// such a mount is named as the build file names it
|
|
346
|
+
if (inside.length > 0 && mount.containerPath === (0, path_1.resolve)(data.cwd, mount.localPath)) {
|
|
347
|
+
return `${inside[0]}${readOnly}`;
|
|
348
|
+
}
|
|
349
|
+
return `${inside.length > 0 ? inside[0] : mount.localPath}:${mount.containerPath}${readOnly}`;
|
|
339
350
|
})
|
|
340
351
|
: [];
|
|
341
352
|
const ports = 'ports' in data ? data.ports.map((port) => `${port.hostPort ?? ''}:${port.containerPort}`) : [];
|
|
@@ -423,6 +434,10 @@ async function planBundle(bundle, environment, options = {}) {
|
|
|
423
434
|
}
|
|
424
435
|
const context = await (0, reference_parser_1.parseReferences)(ctx, scope, inspected);
|
|
425
436
|
const workTree = (0, work_scope_parser_1.getWorkContext)(context, options.scope ?? { environmentName: null }, inspected);
|
|
437
|
+
// the server's providers and accounts are the ones that count: a task it could never read
|
|
438
|
+
// a secret of is refused when it is published, and a run when it is planned (only the
|
|
439
|
+
// tasks and services of the run count then)
|
|
440
|
+
(0, prefetch_secrets_1.requireReadableSecrets)(workTree);
|
|
426
441
|
const items = itemsOf(workTree);
|
|
427
442
|
if (options.policy) {
|
|
428
443
|
const policyProblems = findPolicyProblems(items, options.policy, directory);
|
|
@@ -9,7 +9,7 @@ const COMMIT = /^[0-9a-f]{40}([0-9a-f]{24})?$/i;
|
|
|
9
9
|
// repository, and the branch or tag and the commit it was pinned to, or the repository and the
|
|
10
10
|
// sandbox. None of them is a credential, so a task that reads them still takes arguments and
|
|
11
11
|
// keeps its network (see PLAIN_SERVER_ENVS). A sandbox has no commit: its files may hold
|
|
12
|
-
// changes. A run from an upload has none of them.
|
|
12
|
+
// changes. A run from an upload has none of them. Each is a name of RUN_SOURCE_ENVS.
|
|
13
13
|
function runSourceEnvironment(source) {
|
|
14
14
|
if (source.type === 'ref') {
|
|
15
15
|
return {
|
package/dist/server/runs.js
CHANGED
|
@@ -817,15 +817,13 @@ class RunRegistry {
|
|
|
817
817
|
return [...this.entries.values()].find((entry) => entry.run.state === 'running' && onSandbox(entry.run, source.id))
|
|
818
818
|
?.run.id;
|
|
819
819
|
}
|
|
820
|
-
// The run that executes and holds what the run would (RunExecutor.
|
|
820
|
+
// The run that executes and holds what the run would (RunExecutor.excludes)
|
|
821
821
|
held(run) {
|
|
822
|
-
const
|
|
823
|
-
|
|
824
|
-
if (key === undefined) {
|
|
822
|
+
const executor = this.options.executor;
|
|
823
|
+
if (!executor.excludes) {
|
|
825
824
|
return undefined;
|
|
826
825
|
}
|
|
827
|
-
return [...this.entries.values()].find((entry) => entry.run.state === 'running' &&
|
|
828
|
-
?.run.id;
|
|
826
|
+
return [...this.entries.values()].find((entry) => entry.run.state === 'running' && entry.run.id !== run.id && executor.excludes?.(run, entry.run))?.run.id;
|
|
829
827
|
}
|
|
830
828
|
// What keeps a queued run from starting, checked as dispatch checks it: what holds it
|
|
831
829
|
// even when a place frees up is named first.
|
|
@@ -76,10 +76,14 @@ const searchSchema = zod_1.z.preprocess((body) => {
|
|
|
76
76
|
* PUT /v1/sandboxes/<id>/files/<path> write bytes, If-Match: "<sha256>"
|
|
77
77
|
* POST /v1/sandboxes/<id>/search ripgrep
|
|
78
78
|
* POST /v1/sandboxes/<id>/edits { dryRun, ops }, all or nothing
|
|
79
|
-
* GET /v1/sandboxes/<id>/changes?format=summary|diff|patch
|
|
79
|
+
* GET /v1/sandboxes/<id>/changes?format=summary|diff|patch (diff: &path=… for some files)
|
|
80
80
|
*
|
|
81
81
|
* Reading a file works at any time. Writes, and the reads that walk the files (tree, search,
|
|
82
82
|
* changes), are `409` while a run holds the sandbox.
|
|
83
|
+
*
|
|
84
|
+
* Who may read the sandbox, its owner and who observes, was decided by `handleSandboxes`:
|
|
85
|
+
* reads go as its owner. Writes go as the caller, so the registry answers anybody but the
|
|
86
|
+
* owner `404`.
|
|
83
87
|
*/
|
|
84
88
|
async function handleSandboxFiles(sandboxes, log, principal, sandbox, action, path, req, res, url) {
|
|
85
89
|
const who = `iss=${principal.iss} sub=${principal.sub}`;
|
|
@@ -99,7 +103,7 @@ async function handleSandboxFiles(sandboxes, log, principal, sandbox, action, pa
|
|
|
99
103
|
limit: intParam(url, 'limit'),
|
|
100
104
|
cursor: url.searchParams.get('cursor') ?? undefined,
|
|
101
105
|
};
|
|
102
|
-
const tree = await sandboxes.read(
|
|
106
|
+
const tree = await sandboxes.read(sandbox.owner, sandbox.id, (files) => files.tree(query));
|
|
103
107
|
(0, http_1.send)(res, 200, tree);
|
|
104
108
|
}
|
|
105
109
|
else if (action === 'search') {
|
|
@@ -107,7 +111,7 @@ async function handleSandboxFiles(sandboxes, log, principal, sandbox, action, pa
|
|
|
107
111
|
return (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'POST' });
|
|
108
112
|
}
|
|
109
113
|
const query = parse(searchSchema, await (0, http_1.readJson)(req));
|
|
110
|
-
(0, http_1.send)(res, 200, await sandboxes.read(
|
|
114
|
+
(0, http_1.send)(res, 200, await sandboxes.read(sandbox.owner, sandbox.id, (files) => files.search(query)));
|
|
111
115
|
}
|
|
112
116
|
else if (action === 'edits') {
|
|
113
117
|
if (method !== 'POST') {
|
|
@@ -124,7 +128,7 @@ async function handleSandboxFiles(sandboxes, log, principal, sandbox, action, pa
|
|
|
124
128
|
if (method !== 'GET') {
|
|
125
129
|
return (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'GET' });
|
|
126
130
|
}
|
|
127
|
-
await handleChanges(sandboxes,
|
|
131
|
+
await handleChanges(sandboxes, sandbox, res, url);
|
|
128
132
|
}
|
|
129
133
|
}
|
|
130
134
|
catch (e) {
|
|
@@ -140,9 +144,10 @@ async function handleSandboxFiles(sandboxes, log, principal, sandbox, action, pa
|
|
|
140
144
|
exports.handleSandboxFiles = handleSandboxFiles;
|
|
141
145
|
async function handleFile(sandboxes, log, principal, sandbox, path, req, res, url) {
|
|
142
146
|
const method = req.method;
|
|
143
|
-
|
|
147
|
+
// reading, as its owner
|
|
148
|
+
const files = () => sandboxes.files(sandbox.owner, sandbox.id);
|
|
144
149
|
if (method === 'HEAD') {
|
|
145
|
-
const stat = await files.stat(path);
|
|
150
|
+
const stat = await files().stat(path);
|
|
146
151
|
if (!stat) {
|
|
147
152
|
res.writeHead(404, { 'cache-control': 'no-store' });
|
|
148
153
|
res.end();
|
|
@@ -153,11 +158,11 @@ async function handleFile(sandboxes, log, principal, sandbox, path, req, res, ur
|
|
|
153
158
|
}
|
|
154
159
|
else if (method === 'GET') {
|
|
155
160
|
if (url.searchParams.has('stat')) {
|
|
156
|
-
const stat = await files.stat(path);
|
|
161
|
+
const stat = await files().stat(path);
|
|
157
162
|
return stat ? (0, http_1.send)(res, 200, stat) : (0, http_1.send)(res, 404, { error: `${path} does not exist` });
|
|
158
163
|
}
|
|
159
164
|
if (url.searchParams.has('raw')) {
|
|
160
|
-
const raw = await files.raw(path);
|
|
165
|
+
const raw = await files().raw(path);
|
|
161
166
|
const headers = { ...statHeaders(raw.stat), 'content-type': 'application/octet-stream' };
|
|
162
167
|
if (raw.content) {
|
|
163
168
|
res.writeHead(200, { ...headers, etag: `"${raw.sha256}"` });
|
|
@@ -179,7 +184,7 @@ async function handleFile(sandboxes, log, principal, sandbox, path, req, res, ur
|
|
|
179
184
|
});
|
|
180
185
|
return;
|
|
181
186
|
}
|
|
182
|
-
(0, http_1.send)(res, 200, await files.read(path, { offset: intParam(url, 'offset'), limit: intParam(url, 'limit') }));
|
|
187
|
+
(0, http_1.send)(res, 200, await files().read(path, { offset: intParam(url, 'offset'), limit: intParam(url, 'limit') }));
|
|
183
188
|
}
|
|
184
189
|
else if (method === 'PUT') {
|
|
185
190
|
const content = await readBytes(req, sandbox_files_1.MAX_FILE_BYTES);
|
|
@@ -198,10 +203,12 @@ async function handleFile(sandboxes, log, principal, sandbox, path, req, res, ur
|
|
|
198
203
|
(0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'GET, HEAD, PUT' });
|
|
199
204
|
}
|
|
200
205
|
}
|
|
201
|
-
|
|
206
|
+
// the most files one diff may name
|
|
207
|
+
const MAX_DIFF_PATHS = 100;
|
|
208
|
+
async function handleChanges(sandboxes, sandbox, res, url) {
|
|
202
209
|
const format = url.searchParams.get('format') ?? 'summary';
|
|
203
210
|
if (format === 'summary') {
|
|
204
|
-
const files = await sandboxes.read(
|
|
211
|
+
const files = await sandboxes.read(sandbox.owner, sandbox.id, (files) => files.changes());
|
|
205
212
|
return (0, http_1.send)(res, 200, { commit: sandbox.commit, files });
|
|
206
213
|
}
|
|
207
214
|
if (format !== 'diff' && format !== 'patch') {
|
|
@@ -212,7 +219,15 @@ async function handleChanges(sandboxes, principal, sandbox, res, url) {
|
|
|
212
219
|
email: textParam(url, 'email') ?? 'hammerkit@localhost',
|
|
213
220
|
message: textParam(url, 'message', 4000, true) ?? `Changes of sandbox ${sandbox.id}`,
|
|
214
221
|
};
|
|
215
|
-
|
|
222
|
+
// the diff of some files only, as a list of changes opens one; a rename names both its paths
|
|
223
|
+
const paths = url.searchParams.getAll('path');
|
|
224
|
+
if (paths.length > 0 && format !== 'diff') {
|
|
225
|
+
return (0, http_1.send)(res, 400, { error: 'path: only a diff takes paths, a patch is whole' });
|
|
226
|
+
}
|
|
227
|
+
if (paths.length > MAX_DIFF_PATHS || paths.some((path) => path === '' || path.length > 4096 || path.includes('\0'))) {
|
|
228
|
+
return (0, http_1.send)(res, 400, { error: `path: at most ${MAX_DIFF_PATHS}, each a path of the sandbox` });
|
|
229
|
+
}
|
|
230
|
+
const text = await sandboxes.read(sandbox.owner, sandbox.id, (files) => files.diff(format, author, paths.length > 0 ? paths : undefined));
|
|
216
231
|
res.writeHead(200, { 'content-type': 'text/x-diff; charset=utf-8', 'cache-control': 'no-store' });
|
|
217
232
|
res.end(text);
|
|
218
233
|
}
|
|
@@ -822,10 +822,11 @@ class HostSandboxFiles {
|
|
|
822
822
|
return files;
|
|
823
823
|
});
|
|
824
824
|
}
|
|
825
|
-
async diff(format, author) {
|
|
825
|
+
async diff(format, author, paths) {
|
|
826
826
|
return this.withFilesIndex(async (run) => {
|
|
827
827
|
if (format === 'diff') {
|
|
828
|
-
|
|
828
|
+
const only = paths && paths.length > 0 ? ['--', ...paths.map((path) => `:(literal)${path}`)] : [];
|
|
829
|
+
return run(['diff', '--cached', '-M', 'HEAD', ...only], 'read', { maxBuffer: this.maxChangesBytes });
|
|
829
830
|
}
|
|
830
831
|
// a commit of the changes on top of the commit the sandbox is at, which no branch
|
|
831
832
|
// points to, written out as `git format-patch` writes it
|
|
@@ -80,7 +80,7 @@ async function answer(storage, directory, request, out) {
|
|
|
80
80
|
case 'changes':
|
|
81
81
|
return send(await files().changes());
|
|
82
82
|
case 'diff':
|
|
83
|
-
return send(await files().diff(request.format, request.author));
|
|
83
|
+
return send(await files().diff(request.format, request.author, request.paths));
|
|
84
84
|
case 'commit':
|
|
85
85
|
return send((await storage.commit(key, request.author, AbortSignal.timeout(10 * 60000))) ?? null);
|
|
86
86
|
case 'move':
|