hammerkit 2.1.0 → 2.2.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/build.schema.json +126 -28
  2. package/dist/cli.js +5 -0
  3. package/dist/dashboard/assets/graph-canvas-D6GMgkoe.js +8 -0
  4. package/dist/dashboard/assets/index-BNq9j5Jo.js +23 -0
  5. package/dist/dashboard/assets/index-DP3_hKOe.css +2 -0
  6. package/dist/dashboard/index.html +2 -2
  7. package/dist/docker/item-network.js +6 -5
  8. package/dist/docker/remove-orphaned-containers.js +5 -4
  9. package/dist/environment/replace-env-variables.js +8 -5
  10. package/dist/environment/run-source-envs.js +2 -1
  11. package/dist/executer/archive-task-entry.js +1 -1
  12. package/dist/executer/check-readiness.js +8 -4
  13. package/dist/executer/docker-healthcheck.js +73 -0
  14. package/dist/executer/docker-service.js +21 -15
  15. package/dist/executer/event-cache.js +5 -1
  16. package/dist/executer/runner-commands.js +36 -0
  17. package/dist/file/get-file-context.js +4 -7
  18. package/dist/harness/connect.js +12 -8
  19. package/dist/harness/reconnect.js +141 -0
  20. package/dist/harness/sandbox-client.js +241 -33
  21. package/dist/harness/service-tools.js +11 -2
  22. package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +62 -14
  23. package/dist/kubernetes/ensure-persistent-data.js +1 -1
  24. package/dist/kubernetes/follow-pod-logs.js +8 -5
  25. package/dist/kubernetes/kept-claims.js +38 -14
  26. package/dist/kubernetes/sandbox-runner.js +526 -156
  27. package/dist/kubernetes/store-kubernetes-data.js +18 -3
  28. package/dist/kubernetes/sync-changes.js +463 -0
  29. package/dist/kubernetes/volumes.js +23 -1
  30. package/dist/network/healthcheck-request.js +38 -0
  31. package/dist/optimizer/get-work-cache-stats.js +7 -0
  32. package/dist/optimizer/work-task-cache-description.js +5 -3
  33. package/dist/planner/image-build.js +5 -0
  34. package/dist/planner/service-definition.js +2 -1
  35. package/dist/planner/utils/append-work-service.js +12 -4
  36. package/dist/planner/utils/dependency-outputs.js +7 -6
  37. package/dist/planner/validate.js +41 -0
  38. package/dist/planner/work-healthcheck.js +23 -2
  39. package/dist/planner/work-item-status.js +12 -2
  40. package/dist/planner/work-runtime-aggregate.js +1 -0
  41. package/dist/planner/work-runtime-docker.js +33 -6
  42. package/dist/planner/work-runtime-kubernetes.js +313 -70
  43. package/dist/planner/work-runtime-local.js +8 -4
  44. package/dist/planner/work-secret.js +33 -11
  45. package/dist/planner/work-service-id.js +30 -6
  46. package/dist/planner/work-service.js +2 -2
  47. package/dist/program.js +19 -1
  48. package/dist/remote/annotations.js +8 -2
  49. package/dist/remote/bundle.js +6 -8
  50. package/dist/remote/device-login.js +4 -1
  51. package/dist/remote/remote-commands.js +16 -7
  52. package/dist/remote/remote-status.js +1 -1
  53. package/dist/remote/remote-work-commands.js +220 -68
  54. package/dist/remote/server-client.js +38 -40
  55. package/dist/schema/build-file-base-task-schema.js +16 -1
  56. package/dist/schema/build-file-image-build-schema.js +5 -8
  57. package/dist/schema/build-file-service-container-healthcheck.js +25 -7
  58. package/dist/schema/reference-parser.js +108 -43
  59. package/dist/schema/work-scope-parser.js +8 -0
  60. package/dist/secrets/prefetch-secrets.js +20 -1
  61. package/dist/server/artifacts-api.js +56 -0
  62. package/dist/server/borrowed-quota.js +216 -0
  63. package/dist/server/capacity.js +18 -6
  64. package/dist/server/definitions-api.js +62 -8
  65. package/dist/server/executor.js +117 -30
  66. package/dist/server/http.js +4 -1
  67. package/dist/server/keep-artifacts.js +230 -0
  68. package/dist/server/kubernetes-backend.js +292 -79
  69. package/dist/server/kubernetes-sandbox-storage.js +86 -24
  70. package/dist/server/overview.js +73 -9
  71. package/dist/server/paging.js +167 -0
  72. package/dist/server/plan.js +16 -1
  73. package/dist/server/run-access.js +24 -0
  74. package/dist/server/run-artifacts.js +171 -0
  75. package/dist/server/run-logs.js +25 -1
  76. package/dist/server/run-request.js +11 -0
  77. package/dist/server/run-source-environment.js +1 -1
  78. package/dist/server/run-worker.js +267 -33
  79. package/dist/server/runs.js +531 -105
  80. package/dist/server/sandbox-files-api.js +27 -12
  81. package/dist/server/sandbox-files.js +12 -10
  82. package/dist/server/sandbox-follow.js +54 -0
  83. package/dist/server/sandbox-pod.js +1 -1
  84. package/dist/server/sandbox-storage.js +38 -1
  85. package/dist/server/sandbox-sync.js +392 -0
  86. package/dist/server/sandboxes-api.js +116 -19
  87. package/dist/server/sandboxes.js +38 -3
  88. package/dist/server/server-config.js +73 -2
  89. package/dist/server/server.js +169 -38
  90. package/dist/server/task-order.js +56 -0
  91. package/dist/utils/project-path.js +20 -0
  92. package/dist/utils/tar-files.js +52 -0
  93. package/dist/utils/tar.js +37 -1
  94. package/package.json +1 -1
  95. package/dist/dashboard/assets/graph-canvas-DtJSpova.js +0 -8
  96. package/dist/dashboard/assets/index-BndChMep.css +0 -2
  97. package/dist/dashboard/assets/index-DRSwIRXd.js +0 -17
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.keepArtifacts = exports.MAX_OUTPUT_ENTRIES = void 0;
4
+ const fs_1 = require("fs");
5
+ const promises_1 = require("fs/promises");
6
+ const path_1 = require("path");
7
+ const stream_1 = require("stream");
8
+ const promises_2 = require("stream/promises");
9
+ const log_1 = require("../log");
10
+ const plan_work_tasks_1 = require("../planner/utils/plan-work-tasks");
11
+ const work_task_1 = require("../planner/work-task");
12
+ const tar_1 = require("../utils/tar");
13
+ const units_1 = require("../utils/units");
14
+ const run_artifacts_1 = require("./run-artifacts");
15
+ // The most entries an output may have when it is unpacked: what the server packs again.
16
+ exports.MAX_OUTPUT_ENTRIES = 10000;
17
+ // Why the outputs of a task that came from the cache are not kept: a cache is shared by the
18
+ // runs of the server, and its key holds no value of a secret, so what an entry holds may be
19
+ // what a run that read a secret wrote, and this run may read none.
20
+ const CACHE_REASON = 'it came from the cache: the cache is shared by the runs of this server and its key holds no secret value, ' +
21
+ 'so what it holds may be what a run that read a secret wrote';
22
+ /**
23
+ * Keeps the outputs of the tasks of a run that the build file says to copy out (`export`),
24
+ * as a run on a dev machine copies them out of their containers: those of a task that
25
+ * completed, and those with `export: always` of one that failed too. Each goes through the
26
+ * runtime its task ran on, as `hammerkit store` takes it, and is kept as a gzipped tar of
27
+ * its paths in the project, so a download is laid out the same whatever ran it.
28
+ */
29
+ async function keepArtifacts(workTree, environment, options) {
30
+ const manifest = { outputs: [], left: [] };
31
+ const staging = (0, path_1.join)(options.directory, '.staging');
32
+ await (0, promises_1.mkdir)(staging, { recursive: true });
33
+ // a failed task aborts the run's controller, the archives still have to be made
34
+ const keeping = { ...environment, abortCtrl: new AbortController() };
35
+ let bytes = 0;
36
+ try {
37
+ const tasks = [...(0, plan_work_tasks_1.iterateWorkTasks)(workTree)].sort((a, b) => a.name.localeCompare(b.name));
38
+ for (const task of tasks) {
39
+ for (const generate of getKeptOutputs(task)) {
40
+ const path = toPosix((0, path_1.relative)(options.root, generate.path));
41
+ const file = `${manifest.outputs.length + manifest.left.length}.tgz`;
42
+ const target = (0, path_1.join)(options.directory, file);
43
+ if (options.cancelled()) {
44
+ manifest.left.push({ task: task.name, path, reason: 'the run was cancelled' });
45
+ continue;
46
+ }
47
+ if (isFromCache(task)) {
48
+ manifest.left.push({ task: task.name, path, reason: CACHE_REASON });
49
+ continue;
50
+ }
51
+ try {
52
+ const kept = await keepOutput(task, generate, keeping, {
53
+ staging,
54
+ target,
55
+ root: options.root,
56
+ maxBytes: options.maxBytes,
57
+ remaining: options.maxBytes - bytes,
58
+ });
59
+ if (kept === null) {
60
+ manifest.left.push({ task: task.name, path, reason: 'the task left nothing there' });
61
+ }
62
+ else {
63
+ bytes += kept;
64
+ manifest.outputs.push({ task: task.name, path, file, bytes: kept });
65
+ }
66
+ }
67
+ catch (e) {
68
+ await (0, promises_1.rm)(target, { force: true });
69
+ // what this module says of the limit is ours to say, what else went wrong is redacted
70
+ const reason = e instanceof Refused ? e.message : options.redact((0, log_1.getErrorMessage)(e)).slice(0, 500);
71
+ manifest.left.push({ task: task.name, path, reason });
72
+ }
73
+ }
74
+ }
75
+ }
76
+ finally {
77
+ await (0, promises_1.rm)(staging, { recursive: true, force: true });
78
+ }
79
+ await (0, promises_1.writeFile)((0, path_1.join)(options.directory, run_artifacts_1.ARTIFACTS_MANIFEST), JSON.stringify(manifest));
80
+ return manifest;
81
+ }
82
+ exports.keepArtifacts = keepArtifacts;
83
+ // The outputs of a task a run may keep: what its build file exports, of a task that completed,
84
+ // and what it exports always of one that failed. What it inherits is its dependency's to keep.
85
+ // A task that was skipped, as nothing needed it, has none. Those of a task that came from the
86
+ // cache are candidates too, and are refused in keepArtifacts (isFromCache).
87
+ function getKeptOutputs(task) {
88
+ const state = task.state.current;
89
+ if ((0, work_task_1.isAggregateTask)(task.data)) {
90
+ return [];
91
+ }
92
+ const completed = state.type === 'completed' && !state.skipped;
93
+ const failed = state.type === 'crash' || state.type === 'error';
94
+ return task.data.generates.filter((generate) => !generate.inherited && (generate.exportAlways || (generate.export && completed)) && (completed || failed));
95
+ }
96
+ function isFromCache(task) {
97
+ const state = task.state.current;
98
+ return state.type === 'completed' && !!state.cached;
99
+ }
100
+ // A refusal of one output, with what the run may say of it.
101
+ class Refused extends Error {
102
+ }
103
+ function overBudget(maxBytes, detail) {
104
+ return `more than the ${(0, units_1.formatSize)(maxBytes)} a run keeps (runs.artifacts.maxBytes)${detail ? `, ${detail}` : ''}`;
105
+ }
106
+ /**
107
+ * What the archives of one output may still take: the rest of the run's budget. Past it a
108
+ * write is refused and the stream writing the archive stops there, so a task can not fill the
109
+ * disk of the server with an output that is refused in the end.
110
+ */
111
+ class Budget {
112
+ constructor(limit, reason) {
113
+ this.limit = limit;
114
+ this.reason = reason;
115
+ this.used = 0;
116
+ }
117
+ take(bytes) {
118
+ this.used += bytes;
119
+ if (this.used > this.limit) {
120
+ throw new Refused(this.reason);
121
+ }
122
+ }
123
+ // a stream that writes an archive, metered: what passes it counts
124
+ meter() {
125
+ return new stream_1.Transform({
126
+ transform: (chunk, _encoding, callback) => {
127
+ try {
128
+ this.take(chunk.length);
129
+ callback(null, chunk);
130
+ }
131
+ catch (e) {
132
+ callback(e);
133
+ }
134
+ },
135
+ });
136
+ }
137
+ // a file an archive is written to, metered at its writes: past the limit it is destroyed
138
+ count(file) {
139
+ const write = file.write.bind(file);
140
+ file.on('error', (error) => {
141
+ this.refusal ?? (this.refusal = error);
142
+ });
143
+ file.write = ((chunk, ...rest) => {
144
+ if (this.refusal) {
145
+ return false;
146
+ }
147
+ try {
148
+ this.take(Buffer.byteLength(chunk));
149
+ }
150
+ catch (e) {
151
+ file.destroy(e);
152
+ return false;
153
+ }
154
+ return write(chunk, ...rest);
155
+ });
156
+ return file;
157
+ }
158
+ // throws what refused a write, when a stream did not say it by itself
159
+ check() {
160
+ if (this.refusal) {
161
+ throw this.refusal;
162
+ }
163
+ }
164
+ }
165
+ // The environment with its writes of archives counted by the budget: the runtimes write their
166
+ // archives through `file`, so every byte an output is archived with is counted here.
167
+ function budgeted(environment, budget) {
168
+ const { file } = environment;
169
+ return {
170
+ ...environment,
171
+ file: {
172
+ ...file,
173
+ writeStream: (path, stream) => {
174
+ const metered = budget.meter();
175
+ // the source is stopped where the meter stops it; the error reaches the write through `metered`
176
+ (0, promises_2.pipeline)(stream, metered).catch(() => undefined);
177
+ return file.writeStream(path, metered);
178
+ },
179
+ createWriteStream: (path) => budget.count(file.createWriteStream(path)),
180
+ },
181
+ };
182
+ }
183
+ // The archive of one output as the runtime of its task makes it, made again with the paths
184
+ // in the project: the size of the new archive, or null when there is nothing.
185
+ async function keepOutput(task, generate, environment, options) {
186
+ const raw = (0, path_1.join)(options.staging, 'raw');
187
+ const tree = (0, path_1.join)(options.staging, 'tree');
188
+ await (0, promises_1.rm)(raw, { recursive: true, force: true });
189
+ await (0, promises_1.rm)(tree, { recursive: true, force: true });
190
+ await (0, promises_1.mkdir)(raw, { recursive: true });
191
+ const budget = new Budget(options.remaining, overBudget(options.maxBytes));
192
+ const [archive] = await task.runtime.archive(budgeted(environment, budget), raw, { only: [generate.path] });
193
+ budget.check();
194
+ if (!archive || !(0, fs_1.existsSync)(archive.filename)) {
195
+ return null;
196
+ }
197
+ await checkArchive(archive, options);
198
+ const into = (0, path_1.join)(tree, (0, path_1.relative)(options.root, archive.root));
199
+ if ((0, path_1.relative)(tree, into).startsWith('..') || (0, path_1.isAbsolute)((0, path_1.relative)(tree, into))) {
200
+ throw new Error(`${archive.root} is not in the project`);
201
+ }
202
+ await (0, promises_1.mkdir)(into, { recursive: true });
203
+ // only entries of the output, and no link out of it: whatever the task wrote there
204
+ await (0, tar_1.extractOutput)(archive.filename, into, toPosix((0, path_1.relative)(archive.root, generate.path)) || '.', true);
205
+ const path = (0, path_1.relative)(options.root, generate.path);
206
+ if (!(0, fs_1.existsSync)((0, path_1.join)(tree, path))) {
207
+ return null;
208
+ }
209
+ await (0, tar_1.packArchive)({ cwd: tree, file: options.target, gzip: true, portable: true }, [toPosix(path)]);
210
+ const packed = (await (0, promises_1.stat)(options.target)).size;
211
+ if (packed > options.remaining) {
212
+ throw new Refused(overBudget(options.maxBytes, `${(0, units_1.formatSize)(packed)} packed`));
213
+ }
214
+ return packed;
215
+ }
216
+ // What an archive holds once it is unpacked is measured before it is, so a few bytes packed
217
+ // from a huge file, or from many files, are refused before anything is written.
218
+ async function checkArchive(archive, options) {
219
+ const measured = await (0, tar_1.measureArchive)(archive.filename, { entries: exports.MAX_OUTPUT_ENTRIES, bytes: options.remaining });
220
+ if (measured.entries > exports.MAX_OUTPUT_ENTRIES) {
221
+ throw new Refused(`more than ${exports.MAX_OUTPUT_ENTRIES} files in it`);
222
+ }
223
+ if (measured.bytes > options.remaining) {
224
+ throw new Refused(overBudget(options.maxBytes, `${(0, units_1.formatSize)(measured.bytes)} unpacked`));
225
+ }
226
+ }
227
+ function toPosix(path) {
228
+ return path.split(path_1.sep).join(path_1.posix.sep);
229
+ }
230
+ //# sourceMappingURL=keep-artifacts.js.map