hammerkit 1.27.0-next.3 → 1.27.0-next.30

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 (119) hide show
  1. package/build.schema.json +188 -0
  2. package/dist/cache/backends/local-cache-backend.js +6 -0
  3. package/dist/cache/backends/registry-cache-backend.js +2 -2
  4. package/dist/cache/explain.js +17 -0
  5. package/dist/cli.js +2 -0
  6. package/dist/docker/item-network.js +13 -5
  7. package/dist/executer/deadline.js +17 -3
  8. package/dist/executer/deferred-restore.js +36 -0
  9. package/dist/executer/docker-service.js +30 -8
  10. package/dist/executer/docker-task.js +21 -1
  11. package/dist/executer/event-cache.js +20 -2
  12. package/dist/executer/execute-work-service.js +2 -0
  13. package/dist/executer/execute-work-task.js +10 -3
  14. package/dist/executer/execution-steps.js +25 -4
  15. package/dist/executer/reset-work-tree.js +4 -0
  16. package/dist/executer/scheduler/enqueue-next.js +57 -6
  17. package/dist/executer/scheduler/state-key.js +42 -5
  18. package/dist/executer/watch-loop.js +4 -1
  19. package/dist/harness/connect.js +20 -6
  20. package/dist/harness/mcp-command.js +22 -6
  21. package/dist/harness/mcp.js +63 -114
  22. package/dist/harness/opencode.js +158 -50
  23. package/dist/harness/pi.js +29 -0
  24. package/dist/harness/sandbox-client.js +168 -17
  25. package/dist/harness/service-tools.js +141 -0
  26. package/dist/harness/task-tools.js +221 -0
  27. package/dist/kubernetes/apply.js +27 -16
  28. package/dist/kubernetes/await-running-state.js +59 -13
  29. package/dist/kubernetes/claim-layout.js +71 -0
  30. package/dist/kubernetes/cluster-state.js +245 -0
  31. package/dist/kubernetes/ensure-http-route.js +1 -1
  32. package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +35 -12
  33. package/dist/kubernetes/ensure-kubernetes-persistent-volume-claim-exists.js +1 -1
  34. package/dist/kubernetes/ensure-kubernetes-service-exists.js +6 -9
  35. package/dist/kubernetes/ensure-persistent-data.js +30 -20
  36. package/dist/kubernetes/follow-pod-logs.js +182 -0
  37. package/dist/kubernetes/get-service-ip.js +2 -2
  38. package/dist/kubernetes/job-placement.js +36 -12
  39. package/dist/kubernetes/job-secrets.js +33 -19
  40. package/dist/kubernetes/kept-claims.js +22 -10
  41. package/dist/kubernetes/network-policy.js +85 -27
  42. package/dist/kubernetes/remove-persistent-data.js +3 -1
  43. package/dist/kubernetes/resources.js +25 -3
  44. package/dist/kubernetes/restore-kubernetes-data.js +9 -3
  45. package/dist/kubernetes/sandbox-runner.js +628 -0
  46. package/dist/kubernetes/secrets.js +7 -11
  47. package/dist/kubernetes/volumes.js +6 -3
  48. package/dist/main.js +2 -0
  49. package/dist/network/egress-proxy.js +45 -6
  50. package/dist/optimizer/calculate-checksum.js +3 -2
  51. package/dist/optimizer/check-key.js +94 -0
  52. package/dist/optimizer/check-keys.js +64 -0
  53. package/dist/optimizer/get-work-cache-stats.js +17 -3
  54. package/dist/optimizer/work-task-cache-description.js +51 -1
  55. package/dist/planner/utils/append-work-dependencies.js +10 -5
  56. package/dist/planner/utils/append-work-service.js +19 -1
  57. package/dist/planner/utils/append-work-task.js +18 -3
  58. package/dist/planner/utils/get-work-runtime.js +6 -1
  59. package/dist/planner/utils/normalize-path.js +19 -1
  60. package/dist/planner/utils/parse-work-mount.js +7 -1
  61. package/dist/planner/utils/parse-work-port.js +4 -1
  62. package/dist/planner/utils/task-arguments.js +3 -0
  63. package/dist/planner/validate.js +22 -0
  64. package/dist/planner/work-network.js +3 -1
  65. package/dist/planner/work-runtime-aggregate.js +32 -0
  66. package/dist/planner/work-runtime-docker.js +2 -1
  67. package/dist/planner/work-runtime-kubernetes.js +124 -48
  68. package/dist/planner/work-runtime-local.js +2 -1
  69. package/dist/planner/work-task.js +6 -1
  70. package/dist/program.js +31 -10
  71. package/dist/remote/device-login.js +7 -2
  72. package/dist/remote/host-config.js +69 -3
  73. package/dist/remote/remote-commands.js +30 -7
  74. package/dist/remote/remote-work-commands.js +426 -45
  75. package/dist/remote/server-client.js +15 -2
  76. package/dist/remote/token-source.js +64 -26
  77. package/dist/run-program.js +9 -0
  78. package/dist/schema/build-file-base-task-schema.js +8 -1
  79. package/dist/schema/build-file-container-service-schema.js +12 -0
  80. package/dist/schema/build-file-container-task-schema.js +16 -1
  81. package/dist/schema/build-file-include-schema.js +18 -4
  82. package/dist/schema/build-file-kubernetes-service-schema.js +2 -0
  83. package/dist/schema/build-file-task-schema.js +5 -1
  84. package/dist/schema/capabilities-schema.js +56 -0
  85. package/dist/schema/reference-parser.js +120 -11
  86. package/dist/schema/schema-parser.js +92 -41
  87. package/dist/schema/work-scope-parser.js +33 -10
  88. package/dist/secrets/command-secret-provider.js +3 -1
  89. package/dist/secrets/github-app-token-command.js +3 -1
  90. package/dist/secrets/secret-catalog.js +43 -19
  91. package/dist/server/accounts-api.js +3 -1
  92. package/dist/server/approvals-api.js +1 -1
  93. package/dist/server/checks-api.js +83 -0
  94. package/dist/server/checks.js +116 -0
  95. package/dist/server/definitions-api.js +66 -3
  96. package/dist/server/endpoints.js +739 -0
  97. package/dist/server/executor.js +57 -5
  98. package/dist/server/kubernetes-backend.js +367 -65
  99. package/dist/server/kubernetes-sandbox-storage.js +48 -3
  100. package/dist/server/oauth.js +12 -1
  101. package/dist/server/plan.js +188 -103
  102. package/dist/server/policy.js +32 -2
  103. package/dist/server/repo-access.js +5 -0
  104. package/dist/server/run-plan.js +50 -19
  105. package/dist/server/run-request.js +58 -3
  106. package/dist/server/run-worker.js +245 -12
  107. package/dist/server/runs.js +410 -27
  108. package/dist/server/sandbox-pod.js +28 -0
  109. package/dist/server/sandbox-storage.js +23 -0
  110. package/dist/server/sandboxes-api.js +10 -4
  111. package/dist/server/sandboxes.js +8 -0
  112. package/dist/server/server-command.js +39 -20
  113. package/dist/server/server-config.js +255 -14
  114. package/dist/server/server.js +269 -15
  115. package/dist/server/service-runs.js +82 -0
  116. package/dist/utils/abort-on-signals.js +46 -6
  117. package/dist/utils/tar.js +36 -0
  118. package/dist/utils/terminal-prompt.js +53 -0
  119. package/package.json +1 -1
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.registerWorkCommands = exports.describePlan = exports.describeApproval = void 0;
6
+ exports.registerWorkCommands = exports.describePlan = exports.describeApproval = exports.describeListedRun = exports.describeRunEnd = void 0;
7
7
  const commander_1 = require("commander");
8
8
  const colors_1 = __importDefault(require("colors"));
9
9
  const token_source_1 = require("./token-source");
@@ -13,8 +13,39 @@ const annotations_1 = require("./annotations");
13
13
  const server_client_1 = require("./server-client");
14
14
  const bundle_text_1 = require("./bundle-text");
15
15
  const sandbox_client_1 = require("../harness/sandbox-client");
16
+ const units_1 = require("../utils/units");
17
+ const service_tools_1 = require("../harness/service-tools");
16
18
  const path_1 = require("path");
19
+ const check_keys_1 = require("../optimizer/check-keys");
20
+ const terminal_prompt_1 = require("../utils/terminal-prompt");
21
+ function describeOutcome(planned) {
22
+ switch (planned.outcome) {
23
+ case 'published':
24
+ return 'publishing it would publish it';
25
+ case 'already published':
26
+ return planned.yanked ? 'it is published already, and yanked' : 'it is published already';
27
+ case 'proposed':
28
+ return planned.missing?.length
29
+ ? `publishing it would propose it, it names accounts you do not use: ${planned.missing
30
+ .map(server_client_1.printable)
31
+ .join(', ')}`
32
+ : 'publishing it would propose it, somebody who may publish approves it';
33
+ default:
34
+ return 'the server accepts it, publishing it needs builds: propose or publish';
35
+ }
36
+ }
17
37
  const RUN_ID = /^run_[A-Za-z0-9_-]{16}$/;
38
+ // how a check passed, one line
39
+ function describePassed(passed) {
40
+ const from = passed.source.type === 'ref'
41
+ ? `${passed.source.ref} at ${passed.source.commit.slice(0, 12)}`
42
+ : `a check of sandbox ${passed.source.sandbox}, fetched at ${passed.source.commit.slice(0, 12)}`;
43
+ return `passed ${passed.finishedAt}${passed.status === 'cached'
44
+ ? ' from the cache'
45
+ : passed.durationMs !== undefined
46
+ ? ` in ${(0, units_1.formatDuration)(passed.durationMs)}`
47
+ : ''}, ${passed.run} by ${passed.by.sub}, from ${from}`;
48
+ }
18
49
  // what the publisher said about where a version comes from, one line
19
50
  // What a publisher said about where a version comes from: the server takes it as said,
20
51
  // so anybody else is told it is not checked.
@@ -37,6 +68,31 @@ function printableLine(text) {
37
68
  return text.split('\t').map(server_client_1.printableWhole).join('\t');
38
69
  }
39
70
  const collectOption = (value, previous = []) => [...previous, value];
71
+ /**
72
+ * Why a run that did not succeed ended: its error, then each task that failed with its own
73
+ * error or exit code, where the error of the run does not say it already.
74
+ */
75
+ function describeRunEnd(run) {
76
+ const lines = [`${(0, server_client_1.printable)(run.id)} ${(0, server_client_1.printable)(run.state)}${run.error ? `: ${(0, server_client_1.printableWhole)(run.error)}` : ''}`];
77
+ for (const [task, result] of Object.entries(run.results ?? {})) {
78
+ if (result.status !== 'failed') {
79
+ continue;
80
+ }
81
+ const why = result.error ?? (result.exitCode !== undefined ? `exit code ${result.exitCode}` : undefined);
82
+ if (why && !run.error?.includes(`${task}: ${why}`)) {
83
+ lines.push(` ${(0, server_client_1.printable)(task)}: ${(0, server_client_1.printableWhole)(why)}`);
84
+ }
85
+ }
86
+ return lines;
87
+ }
88
+ exports.describeRunEnd = describeRunEnd;
89
+ // What `remote runs` shows of a run, on one line.
90
+ function describeListedRun(run) {
91
+ const what = run.up ? `up ${run.up.services.join(', ')}` : run.tasks.join(', ') || 'by labels';
92
+ const until = run.up && (run.state === 'up' || run.state === 'asleep') && run.expiresAt ? ` until ${run.expiresAt}` : '';
93
+ return `${(0, server_client_1.printable)(run.id)} ${(0, server_client_1.printable)(run.createdAt)} ${(0, server_client_1.printable)(run.state)}${(0, server_client_1.printable)(until)} ${(0, server_client_1.printable)(what)}${run.error ? `: ${(0, server_client_1.printable)(run.error)}` : ''}`;
94
+ }
95
+ exports.describeListedRun = describeListedRun;
40
96
  // What a person who approves is shown of a run: everything the decision rests on.
41
97
  function describeApproval(run) {
42
98
  const source = run.source.type === 'ref'
@@ -92,33 +148,51 @@ function describePlan(plan) {
92
148
  ...(entry.mounts.length > 0 ? [` mounts: ${entry.mounts.map(server_client_1.printable).join(', ')}`] : []),
93
149
  ...(entry.ports.length > 0 ? [` ports: ${entry.ports.map(server_client_1.printable).join(', ')}`] : []),
94
150
  ...(entry.generates.length > 0 ? [` generates: ${entry.generates.map(server_client_1.printable).join(', ')}`] : []),
151
+ ...(entry.capAdd?.length ? [` capAdd: ${entry.capAdd.map(server_client_1.printable).join(', ')}`] : []),
95
152
  ]);
96
153
  }
97
154
  exports.describePlan = describePlan;
98
155
  function registerWorkCommands(remote, context) {
99
156
  const { environment, buildFile, getServer, guarded, passthrough, fail } = context;
157
+ // the ref a run starts from: what the options name, else the checkout here
158
+ const refSource = async (options) => {
159
+ const checkout = options.repo && options.ref ? {} : await (0, annotations_1.readCheckout)((0, path_1.dirname)(buildFile));
160
+ const repo = options.repo ?? checkout.repo;
161
+ const ref = options.ref ?? checkout.branch;
162
+ // the commit checked out here is the code meant, when the ref is the branch it is on
163
+ const commit = options.commit ?? (options.ref ? undefined : checkout.commit);
164
+ if (!repo) {
165
+ throw new Error('name the repository with --repo, the checkout here has no origin it can be told by');
166
+ }
167
+ if (!ref) {
168
+ throw new Error('name the branch or tag with --ref, the checkout here is on no branch');
169
+ }
170
+ return { type: 'ref', repo, ref, ...(commit ? { commit } : {}) };
171
+ };
100
172
  const connect = async (name) => {
101
173
  const chosen = await getServer(name);
102
174
  // a CI job or an agent brings its own token, a person signs in
103
175
  const token = await (0, token_source_1.getTokenSource)(environment, chosen.name, chosen.server).token();
104
176
  return { ...chosen, token };
105
177
  };
106
- remote
107
- .command('publish [server]')
108
- .description('publish the build file and the files it includes to a server, runs refer to it by its hash')
109
- .addOption(new commander_1.Option('--annotation <key=value>', 'say more about where it comes from, or leave one out with key=; path, repo, commit, machine and user are filled in').argParser(collectOption))
110
- .action(guarded('Remote publish was not successful', async (name, options) => {
111
- const bundle = await (0, collect_bundle_1.collectBundle)(buildFile, environment);
112
- const annotations = (0, annotations_1.applyAnnotations)(await (0, annotations_1.collectAnnotations)(buildFile, Object.keys(bundle.files)), options.annotation ?? []);
113
- const { name: serverName, server, token } = await connect(name);
178
+ // publishes the bundle with where it comes from and says what it is now: published,
179
+ // already published, or only proposed
180
+ const publishBundle = async (serverName, server, token, bundle, annotations) => {
114
181
  const answer = await (0, server_client_1.callServer)(server, token, 'POST', '/v1/definitions', {
115
182
  ...bundle,
116
183
  annotations,
117
184
  });
118
185
  const version = answer.body;
119
186
  const files = Object.keys(bundle.files).length;
120
- const proposed = answer.status === 202;
121
- const verb = proposed ? 'proposed' : answer.status === 201 ? 'published' : 'already published';
187
+ // a proposal that already waits answers 200 like a published version, with its state
188
+ const proposed = answer.status === 202 || version.state !== undefined;
189
+ const verb = answer.status === 202
190
+ ? 'proposed'
191
+ : proposed
192
+ ? 'already proposed'
193
+ : answer.status === 201
194
+ ? 'published'
195
+ : 'already published';
122
196
  environment.stdout.write(`${verb} to ${serverName}: ${(0, server_client_1.printable)(version.hash)} (${files} ${files === 1 ? 'file' : 'files'})\n`);
123
197
  const names = [...(version.tasks ?? []), ...(version.services ?? [])];
124
198
  environment.stdout.write(`tasks and services: ${names.map(server_client_1.printable).join(', ') || 'none'}\n`);
@@ -128,6 +202,123 @@ function registerWorkCommands(remote, context) {
128
202
  if (proposed) {
129
203
  environment.stdout.write(`it can not run before somebody who may publish approves it, until ${(0, server_client_1.printable)(version.expiresAt)}\n`);
130
204
  }
205
+ return { version, proposed };
206
+ };
207
+ // what publishing the bundle would do, undefined from a server that can't say (before 1.27)
208
+ const planBundle = async (server, token, bundle) => {
209
+ try {
210
+ return (await (0, server_client_1.callServer)(server, token, 'POST', '/v1/definitions/plan', bundle)).body;
211
+ }
212
+ catch (e) {
213
+ if (e instanceof server_client_1.ServerError && e.status === 404) {
214
+ return undefined;
215
+ }
216
+ throw e;
217
+ }
218
+ };
219
+ // The build file a run or up names, and what `--publish` may publish: the hash of the
220
+ // build file here, unless `--build` or HAMMERKIT_BUILD names a version.
221
+ const runDefinition = async (options) => {
222
+ const named = options.build ?? environment.processEnvs.HAMMERKIT_BUILD;
223
+ if (named !== undefined) {
224
+ if (options.publish) {
225
+ throw new Error(`--publish publishes the build file here, it does not go with ${options.build !== undefined ? '--build' : 'HAMMERKIT_BUILD'}`);
226
+ }
227
+ if (!bundle_1.HASH_PATTERN.test(named)) {
228
+ throw new Error(`${(0, server_client_1.printable)(named)} is not the hash of a published version (sha256:...)`);
229
+ }
230
+ return { definition: named };
231
+ }
232
+ const local = await (0, collect_bundle_1.collectBundle)(buildFile, environment);
233
+ return { definition: (0, bundle_1.hashBundle)(local), local };
234
+ };
235
+ // Submits a run of a published version. When the server does not have the build file
236
+ // here, it is published first: with --publish at once, else when the person at the
237
+ // terminal says so. Without a terminal and without --publish the run fails and says how
238
+ // to publish it, so CI never waits on a question.
239
+ const submitPublishing = async (target, request, local, publish) => {
240
+ const { serverName, server, connection } = target;
241
+ try {
242
+ return await (0, sandbox_client_1.submitRun)(connection, request);
243
+ }
244
+ catch (e) {
245
+ const status = e.status;
246
+ if (!local || status !== 400 || !/no published build file/.test(String(e))) {
247
+ throw e;
248
+ }
249
+ }
250
+ const definition = request.definition;
251
+ const notPublished = `the build file here is not published on ${serverName} (${definition})`;
252
+ const token = await connection.token();
253
+ if (!publish) {
254
+ if (!environment.prompt) {
255
+ throw new Error(`${notPublished}, publish it first with: hammerkit remote publish ${serverName}, or pass --publish`);
256
+ }
257
+ // a server before 1.27 can't say what publishing would do: it is asked all the same,
258
+ // and refuses then what the caller may not do
259
+ const planned = await planBundle(server, token, local);
260
+ if (planned && planned.outcome === null) {
261
+ throw new Error(`${notPublished}, and you may only run published build files (builds: run): ` +
262
+ `somebody who may publish publishes it with: hammerkit remote publish ${serverName}`);
263
+ }
264
+ if (planned?.outcome !== 'already published') {
265
+ const files = Object.keys(local.files).length;
266
+ environment.stdout.write(`${notPublished}, ${files} ${files === 1 ? 'file' : 'files'}\n`);
267
+ if (planned) {
268
+ const names = [...(planned.tasks ?? []), ...(planned.services ?? [])];
269
+ environment.stdout.write(`tasks and services: ${names.map(server_client_1.printable).join(', ') || 'none'}\n`);
270
+ }
271
+ const proposing = planned?.outcome === 'proposed';
272
+ if (proposing) {
273
+ environment.stdout.write(`${describeOutcome(planned)}\n`);
274
+ }
275
+ const answer = await environment.prompt(proposing ? `propose it to ${serverName} now? [y/N] ` : `publish it to ${serverName} now and go on? [y/N] `);
276
+ if (environment.abortCtrl.signal.aborted) {
277
+ throw new Error(`stopped, nothing was published`);
278
+ }
279
+ if (!(0, terminal_prompt_1.isYes)(answer)) {
280
+ throw new Error(`${notPublished}, nothing was published`);
281
+ }
282
+ }
283
+ }
284
+ const annotations = await (0, annotations_1.collectAnnotations)(buildFile, Object.keys(local.files));
285
+ const { version, proposed } = await publishBundle(serverName, server, token, local, annotations).catch((e) => {
286
+ throw e instanceof server_client_1.ServerError && e.status === 403 ? new Error(`${notPublished}, ${e.message}`) : e;
287
+ });
288
+ if (version.hash !== definition) {
289
+ throw new Error(`${serverName} published ${(0, server_client_1.printable)(version.hash)}, not ${definition} as computed here, nothing was started`);
290
+ }
291
+ if (proposed) {
292
+ throw new Error(`${definition} is only proposed on ${serverName}, nothing was started: ` +
293
+ 'run this again once somebody who may publish approves it');
294
+ }
295
+ return (0, sandbox_client_1.submitRun)(connection, request);
296
+ };
297
+ remote
298
+ .command('publish [server]')
299
+ .description('publish the build file and the files it includes to a server, runs refer to it by its hash')
300
+ .addOption(new commander_1.Option('--annotation <key=value>', 'say more about where it comes from, or leave one out with key=; path, repo, commit, machine and user are filled in').argParser(collectOption))
301
+ .addOption(new commander_1.Option('--dry-run', "check it against the server's policy and say what publishing would do, without publishing; builds: run is enough"))
302
+ .action(guarded('Remote publish was not successful', async (name, options) => {
303
+ const bundle = await (0, collect_bundle_1.collectBundle)(buildFile, environment);
304
+ if (options.dryRun) {
305
+ const { name: serverName, server, token } = await connect(name);
306
+ const answer = await (0, server_client_1.callServer)(server, token, 'POST', '/v1/definitions/plan', bundle).catch((e) => {
307
+ throw e instanceof server_client_1.ServerError && e.status === 404
308
+ ? new Error(`${serverName} can't check a build file without publishing it, it needs hammerkit 1.27`)
309
+ : e;
310
+ });
311
+ const planned = answer.body;
312
+ const files = Object.keys(bundle.files).length;
313
+ environment.stdout.write(`checked with the policy of ${serverName}: ${(0, server_client_1.printable)(planned.hash)} (${files} ${files === 1 ? 'file' : 'files'}), nothing was published\n`);
314
+ const names = [...(planned.tasks ?? []), ...(planned.services ?? [])];
315
+ environment.stdout.write(`tasks and services: ${names.map(server_client_1.printable).join(', ') || 'none'}\n`);
316
+ environment.stdout.write(`${describeOutcome(planned)}\n`);
317
+ return;
318
+ }
319
+ const annotations = (0, annotations_1.applyAnnotations)(await (0, annotations_1.collectAnnotations)(buildFile, Object.keys(bundle.files)), options.annotation ?? []);
320
+ const { name: serverName, server, token } = await connect(name);
321
+ await publishBundle(serverName, server, token, bundle, annotations);
131
322
  }));
132
323
  remote
133
324
  .command('run <tasks...>')
@@ -139,28 +330,18 @@ function registerWorkCommands(remote, context) {
139
330
  .addOption(new commander_1.Option('--commit <sha>', 'the commit the ref has to be at, else the run is refused; the commit checked out here when --ref is left out'))
140
331
  .addOption(new commander_1.Option('--env <name>', 'the environment of the build file, as --env of run'))
141
332
  .addOption(new commander_1.Option('--cache <method>', 'as --cache of run').choices(['checksum', 'modify-date', 'none']))
142
- .addOption(new commander_1.Option('--timeout <duration>', 'stop the run after this long, such as 30m'))
333
+ .addOption(new commander_1.Option('--timeout <duration>', 'as --timeout of run: for tasks without their own, such as 30m'))
334
+ .addOption(new commander_1.Option('--max-timeout <duration>', 'as --max-timeout of run: for every task, also one with its own timeout'))
335
+ .addOption(new commander_1.Option('--detach', 'submit the run and exit, without following it: for a check that CI verifies later'))
336
+ .addOption(new commander_1.Option('--publish', 'publish the build file here first when the server does not have it, without asking; for CI and scripts'))
143
337
  .action(guarded('Remote run was not successful', async (tasks, options) => {
144
- const named = options.build ?? environment.processEnvs.HAMMERKIT_BUILD;
145
- const definition = named ?? (0, bundle_1.hashBundle)(await (0, collect_bundle_1.collectBundle)(buildFile, environment));
146
- if (!bundle_1.HASH_PATTERN.test(definition)) {
147
- throw new Error(`${(0, server_client_1.printable)(definition)} is not the hash of a published version (sha256:...)`);
148
- }
149
- const checkout = options.repo && options.ref ? {} : await (0, annotations_1.readCheckout)((0, path_1.dirname)(buildFile));
150
- const repo = options.repo ?? checkout.repo;
151
- const ref = options.ref ?? checkout.branch;
152
- // the commit checked out here is the code meant, when the ref is the branch it is on
153
- const commit = options.commit ?? (options.ref ? undefined : checkout.commit);
154
- if (!repo) {
155
- throw new Error('name the repository with --repo, the checkout here has no origin it can be told by');
156
- }
157
- if (!ref) {
158
- throw new Error('name the branch or tag with --ref, the checkout here is on no branch');
159
- }
338
+ const { definition, local } = await runDefinition(options);
339
+ const source = await refSource(options);
340
+ const ref = source.ref;
160
341
  const { name: serverName, server } = await getServer(options.server);
161
342
  const connection = { url: server.url, ...(0, token_source_1.getTokenSource)(environment, serverName, server) };
162
343
  const runOptions = {};
163
- for (const key of ['env', 'cache', 'timeout']) {
344
+ for (const key of ['env', 'cache', 'timeout', 'maxTimeout']) {
164
345
  if (options[key] !== undefined) {
165
346
  runOptions[key] = options[key];
166
347
  }
@@ -168,23 +349,13 @@ function registerWorkCommands(remote, context) {
168
349
  if (passthrough.length > 0) {
169
350
  runOptions.args = passthrough;
170
351
  }
171
- let submitted;
172
- try {
173
- submitted = await (0, sandbox_client_1.submitRun)(connection, {
174
- definition,
175
- tasks,
176
- source: { type: 'ref', repo, ref, ...(commit ? { commit } : {}) },
177
- options: runOptions,
178
- });
179
- }
180
- catch (e) {
181
- if (e.status === 400 && !named && /no published build file/.test(String(e))) {
182
- throw new Error(`the build file here is not published on ${serverName} (${definition}), publish it first`);
183
- }
184
- throw e;
185
- }
352
+ const submitted = await submitPublishing({ serverName, server, connection }, { definition, tasks, source, options: runOptions }, local, !!options.publish);
186
353
  const at = submitted.source.type === 'ref' ? ` at ${(0, server_client_1.printable)(submitted.source.commit)}` : '';
187
354
  environment.stdout.write(`${(0, server_client_1.printable)(submitted.id)} on ${serverName}: ${tasks.map(server_client_1.printable).join(', ')} from ${(0, server_client_1.printable)(ref)}${at}\n`);
355
+ if (options.detach) {
356
+ environment.stdout.write(`follow it with: hammerkit remote logs ${(0, server_client_1.printable)(submitted.id)} --follow\n`);
357
+ return;
358
+ }
188
359
  const prefixed = tasks.length > 1;
189
360
  const followed = await (0, sandbox_client_1.followRun)(connection, submitted, {
190
361
  signal: environment.abortCtrl.signal,
@@ -208,7 +379,217 @@ function registerWorkCommands(remote, context) {
208
379
  .concat(Object.values(run.results ?? {}))
209
380
  .find((result) => result?.status === 'failed');
210
381
  const exitCode = failed?.exitCode && failed.exitCode > 0 ? failed.exitCode : 1;
211
- fail(`${(0, server_client_1.printable)(run.id)} ${run.state}${run.error ? `: ${(0, server_client_1.printable)(run.error)}` : ''}`, exitCode);
382
+ const [said, ...why] = describeRunEnd(run);
383
+ for (const line of why) {
384
+ environment.stdout.write(`${line}\n`);
385
+ }
386
+ environment.stdout.write(`read its log again with: hammerkit remote logs ${(0, server_client_1.printable)(run.id)}\n`);
387
+ fail(said, exitCode);
388
+ }));
389
+ remote
390
+ .command('up <services...>')
391
+ .description('start services of a published build file on a server from a git ref and keep them up until their ttl ends, ' +
392
+ 'with their endpoints at addresses of their own behind the sign-in of the server')
393
+ .addOption(new commander_1.Option('--server <name>', 'the server to run on; the default of remote use when left out'))
394
+ .addOption(new commander_1.Option('--endpoint <service:port>', 'publish this port of a service, such as web:8080; may be given more than once').argParser(collectOption))
395
+ .addOption(new commander_1.Option('--ttl <duration>', 'how long they stay once ready, such as 2h, then they are removed; the server default when left out'))
396
+ .addOption(new commander_1.Option('--sleep-ttl <duration>', 'how long they stay up without a request, such as 30m, then they sleep until the next request wakes them; ' +
397
+ 'the server default when left out'))
398
+ .addOption(new commander_1.Option('--name <name>', 'a later run of yours with the same name replaces this one and keeps its addresses; the branch when left out'))
399
+ .addOption(new commander_1.Option('--build <hash>', 'the published version to run (sha256:...), or HAMMERKIT_BUILD; the hash of the build file here when left out'))
400
+ .addOption(new commander_1.Option('--repo <host/path>', 'the repository; origin of the checkout here when left out'))
401
+ .addOption(new commander_1.Option('--ref <ref>', 'the branch or tag to start; the branch checked out here when left out'))
402
+ .addOption(new commander_1.Option('--commit <sha>', 'the commit the ref has to be at; the commit checked out here when --ref is left out'))
403
+ .addOption(new commander_1.Option('--env <name>', 'the environment of the build file, as --env of run'))
404
+ .addOption(new commander_1.Option('--publish', 'publish the build file here first when the server does not have it, without asking; for CI and scripts'))
405
+ .action(guarded('Remote up was not successful', async (services, options) => {
406
+ const { definition, local } = await runDefinition(options);
407
+ const endpoints = (options.endpoint ?? []).map(service_tools_1.parseEndpoint);
408
+ const source = await refSource(options);
409
+ const { name: serverName, server } = await getServer(options.server);
410
+ const connection = { url: server.url, ...(0, token_source_1.getTokenSource)(environment, serverName, server) };
411
+ const submitted = await submitPublishing({ serverName, server, connection }, {
412
+ definition,
413
+ services,
414
+ endpoints,
415
+ ...(options.ttl ? { ttl: options.ttl } : {}),
416
+ ...(options.sleepTtl ? { sleepTtl: options.sleepTtl } : {}),
417
+ ...(options.name ? { name: options.name } : {}),
418
+ source,
419
+ options: options.env ? { env: options.env } : {},
420
+ }, local, !!options.publish);
421
+ environment.stdout.write(`${(0, server_client_1.printable)(submitted.id)} on ${serverName}: ${services.map(server_client_1.printable).join(', ')} from ${(0, server_client_1.printable)(source.ref)}${submitted.source.type === 'ref' ? ` at ${(0, server_client_1.printable)(submitted.source.commit)}` : ''}\n`);
422
+ const followed = await (0, sandbox_client_1.followRun)(connection, submitted, {
423
+ signal: environment.abortCtrl.signal,
424
+ untilUp: true,
425
+ onLine: (line) => environment.stdout.write(`[${(0, server_client_1.printable)(line.task)}] ${printableLine(line.text)}\n`),
426
+ onWaiting: (run) => {
427
+ for (const approval of run.approvals ?? []) {
428
+ environment.stdout.write(`waiting until somebody who uses ${(0, server_client_1.printable)(approval.account)} approves it, before ${(0, server_client_1.printable)(approval.expiresAt)}: hammerkit remote approve ${(0, server_client_1.printable)(run.id)}\n`);
429
+ }
430
+ },
431
+ });
432
+ const run = followed.run;
433
+ if (run.state !== 'up') {
434
+ const [said, ...why] = describeRunEnd(run);
435
+ for (const line of why) {
436
+ environment.stdout.write(`${line}\n`);
437
+ }
438
+ environment.stdout.write(`read its log again with: hammerkit remote logs ${(0, server_client_1.printable)(run.id)}\n`);
439
+ fail(said, 1);
440
+ }
441
+ environment.stdout.write(`${(0, server_client_1.printable)(run.id)} is up until ${(0, server_client_1.printable)(run.expiresAt ?? '-')}${run.up?.sleepTtl ? `, asleep after ${(0, units_1.formatDuration)(run.up.sleepTtl)} without a request` : ''}\n`);
442
+ for (const endpoint of run.up?.endpoints ?? []) {
443
+ environment.stdout.write(` ${(0, server_client_1.printable)(endpoint.service)}:${endpoint.port} ${(0, server_client_1.printable)(endpoint.url)}\n`);
444
+ }
445
+ environment.stdout.write(`extend it with: hammerkit remote extend ${(0, server_client_1.printable)(run.id)} 8h, stop it with: hammerkit remote stop ${(0, server_client_1.printable)(run.id)}\n`);
446
+ }));
447
+ remote
448
+ .command('runs [server]')
449
+ .description('list your runs on a server, newest first: their state, and why one failed')
450
+ .addOption(new commander_1.Option('--limit <count>', 'show at most this many').default('20'))
451
+ .action(guarded('Remote runs was not successful', async (name, options) => {
452
+ const limit = Number(options.limit);
453
+ if (!Number.isInteger(limit) || limit < 1) {
454
+ throw new Error('--limit is a number of runs, 1 or more');
455
+ }
456
+ const { name: serverName, server, token } = await connect(name);
457
+ const { runs } = (await (0, server_client_1.callServer)(server, token, 'GET', '/v1/runs')).body;
458
+ if (runs.length === 0) {
459
+ environment.stdout.write(`${serverName}: you have no runs it still keeps\n`);
460
+ return;
461
+ }
462
+ for (const run of runs.slice(0, limit)) {
463
+ environment.stdout.write(`${describeListedRun(run)}\n`);
464
+ }
465
+ if (runs.length > limit) {
466
+ environment.stdout.write(`… and ${runs.length - limit} older, see them with --limit ${runs.length}\n`);
467
+ }
468
+ }));
469
+ remote
470
+ .command('logs <run> [server]')
471
+ .description('print the log of a run, also of one that ended: what its tasks and services printed, and what the server and the cluster said')
472
+ .addOption(new commander_1.Option('--task <name>', 'only the lines of this task or service'))
473
+ .addOption(new commander_1.Option('--tail <lines>', 'only the last lines, at most 1000').argParser((value) => {
474
+ if (!/^\d+$/.test(value) || Number(value) < 1 || Number(value) > 1000) {
475
+ throw new commander_1.InvalidArgumentError('a number of lines from 1 to 1000');
476
+ }
477
+ return Number(value);
478
+ }))
479
+ .addOption(new commander_1.Option('-f, --follow', 'keep printing what comes until the run ends; ctrl-c stops following, not the run'))
480
+ .action(guarded('Remote logs was not successful', async (id, name, options) => {
481
+ if (!RUN_ID.test(id)) {
482
+ throw new Error(`${(0, server_client_1.printable)(id)} is not a run id (run_...), list them with: hammerkit remote runs`);
483
+ }
484
+ if (options.follow && options.tail !== undefined) {
485
+ throw new Error('--follow prints the whole log, leave --tail out');
486
+ }
487
+ const { name: serverName, server } = await getServer(name);
488
+ const connection = { url: server.url, ...(0, token_source_1.getTokenSource)(environment, serverName, server) };
489
+ const found = await (0, sandbox_client_1.getRun)(connection, id);
490
+ if (!found) {
491
+ throw new Error(`${serverName} keeps no run ${(0, server_client_1.printable)(id)} you may see, list them with: hammerkit remote runs`);
492
+ }
493
+ let run = found;
494
+ const task = options.task;
495
+ const print = (line) => environment.stdout.write(`${task ? '' : `[${(0, server_client_1.printable)(line.task)}] `}${printableLine(line.text)}\n`);
496
+ if (options.follow) {
497
+ run = (await (0, sandbox_client_1.followRun)(connection, run, {
498
+ task,
499
+ signal: environment.abortCtrl.signal,
500
+ detach: true,
501
+ onLine: print,
502
+ })).run;
503
+ }
504
+ else {
505
+ await (0, sandbox_client_1.readRunLog)(connection, run, { task, tail: options.tail }, print);
506
+ }
507
+ if (run.state === 'succeeded' || run.state === 'up' || run.state === 'asleep') {
508
+ environment.stdout.write(`${(0, server_client_1.printable)(run.id)} ${(0, server_client_1.printable)(run.state)}\n`);
509
+ }
510
+ else if (sandbox_client_1.FINAL_RUN_STATES.includes(run.state)) {
511
+ for (const line of describeRunEnd(run)) {
512
+ environment.stdout.write(`${line}\n`);
513
+ }
514
+ }
515
+ else {
516
+ environment.stdout.write(`${(0, server_client_1.printable)(run.id)} is ${(0, server_client_1.printable)(run.state)}, follow it with --follow\n`);
517
+ }
518
+ }));
519
+ remote
520
+ .command('extend <run> <ttl> [server]')
521
+ .description('keep a service run that is up for this long from now, such as 8h, at most the maxTtl of the server')
522
+ .action(guarded('Remote extend was not successful', async (id, ttl, name) => {
523
+ if (!RUN_ID.test(id)) {
524
+ throw new Error(`${(0, server_client_1.printable)(id)} is not a run id (run_...)`);
525
+ }
526
+ const { name: serverName, server, token } = await connect(name);
527
+ const run = (await (0, server_client_1.callServer)(server, token, 'POST', `/v1/runs/${id}/extend`, { ttl })).body;
528
+ environment.stdout.write(`${(0, server_client_1.printable)(run.id)} on ${serverName} stays up until ${(0, server_client_1.printable)(run.expiresAt ?? '-')}\n`);
529
+ }));
530
+ remote
531
+ .command('stop <run> [server]')
532
+ .description('stop a run: a service run takes its services and endpoints down')
533
+ .action(guarded('Remote stop was not successful', async (id, name) => {
534
+ if (!RUN_ID.test(id)) {
535
+ throw new Error(`${(0, server_client_1.printable)(id)} is not a run id (run_...)`);
536
+ }
537
+ const { name: serverName, server, token } = await connect(name);
538
+ const run = (await (0, server_client_1.callServer)(server, token, 'POST', `/v1/runs/${id}/cancel`)).body;
539
+ environment.stdout.write(`${(0, server_client_1.printable)(run.id)} on ${serverName}: ${run.state === 'up' || run.state === 'running' ? 'stopping' : (0, server_client_1.printable)(run.state)}\n`);
540
+ }));
541
+ remote
542
+ .command('verify <tasks...>')
543
+ .description('check that the server already passed tasks for the files checked out here, without running them; ' +
544
+ 'exits 1 at once for any it did not')
545
+ .addOption(new commander_1.Option('--server <name>', 'the server to ask; the default of remote use when left out'))
546
+ .addOption(new commander_1.Option('--repo <host/path>', 'the repository, such as github.com/corp/payments; origin of the checkout here when left out'))
547
+ .addOption(new commander_1.Option('--env <name>', 'the environment of the build file, as --env of run'))
548
+ .action(guarded('Remote verify was not successful', async (tasks, options) => {
549
+ const repo = options.repo ?? (await (0, annotations_1.readCheckout)((0, path_1.dirname)(buildFile))).repo;
550
+ if (!repo) {
551
+ throw new Error('name the repository with --repo, the checkout here has no origin it can be told by');
552
+ }
553
+ const keys = [];
554
+ const unkeyed = [];
555
+ for (const task of tasks) {
556
+ const found = (await (0, check_keys_1.getCheckKeys)(buildFile, environment, { taskName: task, environmentName: options.env ?? null }))[task];
557
+ if (!found) {
558
+ throw new Error(`the build file has no task ${(0, server_client_1.printable)(task)}`);
559
+ }
560
+ if (found.key === null) {
561
+ unkeyed.push({ task, reason: found.reason });
562
+ }
563
+ else {
564
+ keys.push({ task, key: found.key });
565
+ }
566
+ }
567
+ const { name: serverName, server, token } = await connect(options.server);
568
+ const answer = keys.length > 0
569
+ ? await (0, server_client_1.callServer)(server, token, 'POST', '/v1/checks/lookup', { repo, keys })
570
+ : { body: { checks: [] } };
571
+ const missing = [];
572
+ // by what was asked, never by what was answered: a task the answer leaves out, or
573
+ // answers for another key, is not verified
574
+ for (const { task, key } of keys) {
575
+ const check = answer.body.checks.find((answered) => answered.task === task && answered.key === key);
576
+ if (check?.passed) {
577
+ environment.stdout.write(`${(0, server_client_1.printable)(task)}: ${(0, server_client_1.printable)(describePassed(check.passed))}\n`);
578
+ }
579
+ else {
580
+ missing.push(task);
581
+ environment.stdout.write(`${(0, server_client_1.printable)(task)}: no run on ${serverName} passed it for these files${check?.reason ? `, ${(0, server_client_1.printable)(check.reason)}` : ''}\n`);
582
+ }
583
+ }
584
+ for (const { task, reason } of unkeyed) {
585
+ environment.stdout.write(`${(0, server_client_1.printable)(task)}: can not be verified, ${(0, server_client_1.printable)(reason)}\n`);
586
+ }
587
+ if (missing.length > 0 || unkeyed.length > 0) {
588
+ const run = missing.length > 0 ? `; run on the server: hammerkit remote run ${missing.map(server_client_1.printable).join(' ')}` : '';
589
+ // whole names: a task of an include is named like build:all
590
+ const names = [...missing, ...unkeyed.map(({ task }) => task)].map(server_client_1.printable);
591
+ fail(`not verified: ${names.join(', ')}${run}`, 1);
592
+ }
212
593
  }));
213
594
  remote
214
595
  .command('versions [server]')
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.callServer = exports.getMe = exports.getDiscovery = exports.printableWhole = exports.printable = exports.ServerError = void 0;
3
+ exports.callServer = exports.getMe = exports.getDiscovery = exports.printableWhole = exports.printableLines = exports.printable = exports.ServerError = void 0;
4
4
  const discovery_1 = require("../server/discovery");
5
5
  const TIMEOUT_MS = 10000;
6
6
  const WRITE_TIMEOUT_MS = 30000;
@@ -21,6 +21,17 @@ function printable(value) {
21
21
  return text.length > 300 ? `${text.slice(0, 300)}… (+${text.length - 300} chars)` : text;
22
22
  }
23
23
  exports.printable = printable;
24
+ // Text of several lines, such as every problem of a refused publish: each line made
25
+ // printable and cut like `printable`, one per line, and at most `MAX_LINES` of them.
26
+ const MAX_LINES = 100;
27
+ function printableLines(value) {
28
+ const lines = String(value)
29
+ .split(/\r?\n/)
30
+ .filter((line) => line.trim().length > 0);
31
+ const shown = lines.slice(0, MAX_LINES).map(printable);
32
+ return lines.length > MAX_LINES ? [...shown, `… (+${lines.length - MAX_LINES} lines)`] : shown;
33
+ }
34
+ exports.printableLines = printableLines;
24
35
  // The same, never cut: for what somebody decides on, such as a command.
25
36
  function printableWhole(value) {
26
37
  return String(value)
@@ -92,7 +103,9 @@ async function callServer(server, token, method, path, body) {
92
103
  throw new ServerError(`${server.url} did not accept the token`);
93
104
  }
94
105
  if (!response.ok) {
95
- const said = answer && typeof answer.error === 'string' ? `: ${printable(answer.error)}` : '';
106
+ const lines = answer && typeof answer.error === 'string' ? printableLines(answer.error) : [];
107
+ // a refused publish says every problem, each on a line of its own
108
+ const said = lines.length > 1 ? `:\n${lines.map((line) => ` ${line}`).join('\n')}` : lines.length === 1 ? `: ${lines[0]}` : '';
96
109
  throw new ServerError(`${server.url} answered ${response.status}${said}`, response.status);
97
110
  }
98
111
  return { status: response.status, body: answer };