hammerkit 2.1.0-next.16 → 2.1.0-next.17
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.
|
@@ -103,6 +103,10 @@ class Reconnecting {
|
|
|
103
103
|
if ('response' in attempt) {
|
|
104
104
|
await attempt.response.body?.cancel().catch(() => undefined);
|
|
105
105
|
}
|
|
106
|
+
if (timing.windowMs <= 0) {
|
|
107
|
+
// no window: the call is not waited for, so nothing is said about it either
|
|
108
|
+
throw fail(reason);
|
|
109
|
+
}
|
|
106
110
|
const window = (0, units_1.formatDuration)(timing.windowMs);
|
|
107
111
|
this.deadline ?? (this.deadline = started + timing.windowMs);
|
|
108
112
|
if (!this.away) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.quotePosix = exports.fetchFailure = exports.toSandboxPath = exports.checkSandboxPath = exports.exitCodeOf = exports.readRunLog = exports.downloadRunArtifact = exports.getRunArtifacts = exports.getRun = exports.followRun = exports.getMoveTarget = exports.lookupChecks = exports.submitRun = exports.FINAL_RUN_STATES = exports.readTextUpTo = exports.oneAtATime = exports.SandboxClient = exports.listPublishedTasks = exports.runCheck = exports.CHECK_DESCRIPTION = exports.getServerConnection = exports.SandboxClientError = exports.MAX_ENDPOINT_BYTES = void 0;
|
|
3
|
+
exports.request = exports.quotePosix = exports.fetchFailure = exports.toSandboxPath = exports.checkSandboxPath = exports.exitCodeOf = exports.readRunLog = exports.downloadRunArtifact = exports.getRunArtifacts = exports.getRun = exports.followRun = exports.getMoveTarget = exports.lookupChecks = exports.submitRun = exports.FINAL_RUN_STATES = exports.readTextUpTo = exports.oneAtATime = exports.SandboxClient = exports.listPublishedTasks = exports.runCheck = exports.CHECK_DESCRIPTION = exports.getServerConnection = exports.SandboxClientError = exports.MAX_ENDPOINT_BYTES = void 0;
|
|
4
4
|
const path_1 = require("path");
|
|
5
5
|
const token_source_1 = require("../remote/token-source");
|
|
6
6
|
const reconnect_1 = require("./reconnect");
|
|
@@ -659,7 +659,12 @@ exports.quotePosix = quotePosix;
|
|
|
659
659
|
function json(response) {
|
|
660
660
|
return response.json();
|
|
661
661
|
}
|
|
662
|
-
|
|
662
|
+
/**
|
|
663
|
+
* The answer to a call, whatever its status. A call the server is away for is sent again as
|
|
664
|
+
* `repeat` allows, within the window of the connection; a token the server does not accept is
|
|
665
|
+
* renewed once and the call sent again.
|
|
666
|
+
*/
|
|
667
|
+
async function request(connection, method, path, options = {}) {
|
|
663
668
|
const url = new URL(`${connection.url.replace(/\/$/, '')}${path}`);
|
|
664
669
|
for (const [key, value] of Object.entries(options.query ?? {})) {
|
|
665
670
|
if (value !== undefined) {
|
|
@@ -675,11 +680,11 @@ async function call(connection, method, path, options = {}) {
|
|
|
675
680
|
timing: options.reconnectMs !== undefined ? { ...timing, windowMs: options.reconnectMs } : timing,
|
|
676
681
|
say: connection.say,
|
|
677
682
|
signal: stop,
|
|
678
|
-
fail: (message) => new SandboxClientError(message),
|
|
683
|
+
fail: options.fail ?? ((message) => new SandboxClientError(message)),
|
|
679
684
|
});
|
|
680
685
|
const attempt = async () => {
|
|
681
|
-
const token = await connection.token();
|
|
682
|
-
if (!(0, token_source_1.isToken)(token)) {
|
|
686
|
+
const token = 'token' in connection ? await connection.token() : undefined;
|
|
687
|
+
if (token !== undefined && !(0, token_source_1.isToken)(token)) {
|
|
683
688
|
// a header refuses such a value and would quote it whole in its error
|
|
684
689
|
throw new SandboxClientError('the token is not a single line of visible characters');
|
|
685
690
|
}
|
|
@@ -688,7 +693,7 @@ async function call(connection, method, path, options = {}) {
|
|
|
688
693
|
return fetch(url, {
|
|
689
694
|
method,
|
|
690
695
|
headers: {
|
|
691
|
-
authorization: `Bearer ${token}
|
|
696
|
+
...(token !== undefined ? { authorization: `Bearer ${token}` } : {}),
|
|
692
697
|
...(options.body !== undefined ? { 'content-type': 'application/json' } : {}),
|
|
693
698
|
...(options.raw !== undefined ? { 'content-type': 'application/octet-stream' } : {}),
|
|
694
699
|
},
|
|
@@ -700,19 +705,27 @@ async function call(connection, method, path, options = {}) {
|
|
|
700
705
|
error: e,
|
|
701
706
|
message: stop?.aborted
|
|
702
707
|
? `stopped while asking ${shown}`
|
|
703
|
-
: `unable to reach ${shown}: ${redact(fetchFailure(e), token)}`,
|
|
708
|
+
: `unable to reach ${shown}: ${token === undefined ? fetchFailure(e) : redact(fetchFailure(e), token)}`,
|
|
704
709
|
}));
|
|
705
710
|
};
|
|
706
711
|
const repeat = options.repeat ?? (method === 'GET' || method === 'PUT' ? 'always' : 'unsent');
|
|
707
712
|
let response = await reconnecting.send(attempt, repeat);
|
|
708
713
|
// a new token is asked for within the window the call has left, not after it ran out
|
|
709
|
-
if (response.status === 401 && connection.renew && !reconnecting.exhausted) {
|
|
714
|
+
if (response.status === 401 && 'token' in connection && connection.renew && !reconnecting.exhausted) {
|
|
710
715
|
await connection.renew();
|
|
711
716
|
response = await reconnecting.send(attempt, repeat);
|
|
712
717
|
}
|
|
718
|
+
return response;
|
|
719
|
+
}
|
|
720
|
+
exports.request = request;
|
|
721
|
+
// The body of a call that answers 2xx (or a 404 it was asked to take as an answer); any other
|
|
722
|
+
// status is an error, which says what the server answered.
|
|
723
|
+
async function call(connection, method, path, options = {}) {
|
|
724
|
+
const response = await request(connection, method, path, options);
|
|
713
725
|
if (response.ok || (options.missing && response.status === 404)) {
|
|
714
726
|
return response;
|
|
715
727
|
}
|
|
728
|
+
const shown = (0, reconnect_1.showUrl)(connection.url);
|
|
716
729
|
let said = '';
|
|
717
730
|
try {
|
|
718
731
|
const body = await json(response);
|
|
@@ -87,7 +87,10 @@ function abortable(sleep, signal) {
|
|
|
87
87
|
async function deviceLogin(server, options) {
|
|
88
88
|
const sleep = abortable(options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms))), options.signal);
|
|
89
89
|
const now = options.now ?? Date.now;
|
|
90
|
-
|
|
90
|
+
if (options.signal?.aborted) {
|
|
91
|
+
throw new server_client_1.ServerError('login was cancelled');
|
|
92
|
+
}
|
|
93
|
+
const discovery = await (0, server_client_1.getDiscovery)(server, { say: options.say, signal: options.signal });
|
|
91
94
|
const offered = discovery.issuers.find((i) => i.issuer === server.issuer);
|
|
92
95
|
if (!offered) {
|
|
93
96
|
const accepted = discovery.issuers.map((i) => (0, server_client_1.printable)(i.issuer)).join(', ');
|
|
@@ -10,7 +10,7 @@ const log_1 = require("../log");
|
|
|
10
10
|
const host_config_1 = require("./host-config");
|
|
11
11
|
const device_login_1 = require("./device-login");
|
|
12
12
|
const credentials_1 = require("./credentials");
|
|
13
|
-
const
|
|
13
|
+
const sandbox_client_1 = require("../harness/sandbox-client");
|
|
14
14
|
const server_client_1 = require("./server-client");
|
|
15
15
|
const remote_work_commands_1 = require("./remote-work-commands");
|
|
16
16
|
const mcp_command_1 = require("../harness/mcp-command");
|
|
@@ -114,6 +114,7 @@ passthrough = []) {
|
|
|
114
114
|
try {
|
|
115
115
|
login = await (0, device_login_1.deviceLogin)(server, {
|
|
116
116
|
prompt: (message) => environment.stdout.write(`${message}\n`),
|
|
117
|
+
say: (line) => environment.stderr.write(`${line}\n`),
|
|
117
118
|
signal: environment.abortCtrl.signal,
|
|
118
119
|
});
|
|
119
120
|
}
|
|
@@ -128,15 +129,23 @@ passthrough = []) {
|
|
|
128
129
|
await (0, host_config_1.pinLogin)(environment, name, pinned);
|
|
129
130
|
await (0, credentials_1.saveToken)(environment, name, { url: server.url, issuer: server.issuer, ...result });
|
|
130
131
|
try {
|
|
131
|
-
const me = await (0, server_client_1.getMe)(
|
|
132
|
+
const me = await (0, server_client_1.getMe)({
|
|
133
|
+
url: server.url,
|
|
134
|
+
token: async () => result.accessToken,
|
|
135
|
+
say: (line) => environment.stderr.write(`${line}\n`),
|
|
136
|
+
signal: environment.abortCtrl.signal,
|
|
137
|
+
});
|
|
132
138
|
environment.stdout.write(`signed in to ${name} as ${me.sub}\n`);
|
|
133
139
|
environment.stdout.write(me.accounts.length > 0
|
|
134
140
|
? `service accounts: ${me.accounts.map((a) => `${(0, server_client_1.printable)(a.name)} (${a.level})`).join(', ')}\n`
|
|
135
141
|
: 'you have no service accounts on this server\n');
|
|
136
142
|
}
|
|
137
143
|
catch (e) {
|
|
138
|
-
// a token the server does not accept is of no use
|
|
139
|
-
|
|
144
|
+
// a token the server does not accept is of no use; one that could not be checked, because the
|
|
145
|
+
// server was away or the wait was stopped, is kept, and the next command checks it again
|
|
146
|
+
if (e instanceof server_client_1.ServerError && e.status === 401) {
|
|
147
|
+
await (0, credentials_1.removeToken)(environment, name);
|
|
148
|
+
}
|
|
140
149
|
throw e;
|
|
141
150
|
}
|
|
142
151
|
}));
|
|
@@ -154,9 +163,9 @@ passthrough = []) {
|
|
|
154
163
|
.description('show who you are on a server and which service accounts you may use')
|
|
155
164
|
.action(guarded('Remote accounts was not successful', async (name) => {
|
|
156
165
|
const chosen = await getServer(name);
|
|
157
|
-
const
|
|
158
|
-
const me = await (0, server_client_1.getMe)(
|
|
159
|
-
const { body } = await (0, server_client_1.callServer)(
|
|
166
|
+
const connection = (0, sandbox_client_1.getServerConnection)(environment, chosen.name, chosen.server);
|
|
167
|
+
const me = await (0, server_client_1.getMe)(connection);
|
|
168
|
+
const { body } = await (0, server_client_1.callServer)(connection, 'GET', '/v1/accounts');
|
|
160
169
|
environment.stdout.write(`${chosen.name}: ${(0, server_client_1.printable)(me.sub)} (${(0, server_client_1.printable)(me.iss)})\n`);
|
|
161
170
|
if (body.accounts.length === 0) {
|
|
162
171
|
environment.stdout.write('no service accounts\n');
|
|
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.registerWorkCommands = exports.describeArtifacts = exports.selectArtifacts = exports.keptProjectOutputs = exports.isProjectPath = exports.describePlan = exports.describeApproval = exports.keepRuns = exports.describeListedRun = exports.describeRunEnd = void 0;
|
|
7
7
|
const commander_1 = require("commander");
|
|
8
8
|
const colors_1 = __importDefault(require("colors"));
|
|
9
|
-
const token_source_1 = require("./token-source");
|
|
10
9
|
const collect_bundle_1 = require("./collect-bundle");
|
|
11
10
|
const bundle_1 = require("./bundle");
|
|
12
11
|
const annotations_1 = require("./annotations");
|
|
@@ -307,18 +306,19 @@ function registerWorkCommands(remote, context) {
|
|
|
307
306
|
const commit = options.commit ?? (options.ref ? undefined : checkout.commit);
|
|
308
307
|
return { type: 'ref', repo, ref, ...(commit ? { commit } : {}) };
|
|
309
308
|
};
|
|
309
|
+
// the server to call: its token is read for each call, a CI job or an agent brings its own, a
|
|
310
|
+
// person signs in
|
|
310
311
|
const connect = async (name, how) => {
|
|
311
312
|
const chosen = await getServer(name, how);
|
|
312
|
-
|
|
313
|
-
const token = await (0, token_source_1.getTokenSource)(environment, chosen.name, chosen.server).token();
|
|
314
|
-
return { ...chosen, token };
|
|
313
|
+
return { ...chosen, connection: (0, sandbox_client_1.getServerConnection)(environment, chosen.name, chosen.server) };
|
|
315
314
|
};
|
|
316
315
|
// publishes the bundle with where it comes from and says what it is now: published,
|
|
317
316
|
// already published, or only proposed
|
|
318
|
-
const publishBundle = async (serverName,
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
annotations,
|
|
317
|
+
const publishBundle = async (serverName, connection, bundle, annotations) => {
|
|
318
|
+
// what is published already changes nothing and is said so: sent again, it is the same
|
|
319
|
+
const answer = await (0, server_client_1.callServer)(connection, 'POST', '/v1/definitions', {
|
|
320
|
+
body: { ...bundle, annotations },
|
|
321
|
+
repeat: 'always',
|
|
322
322
|
});
|
|
323
323
|
const version = answer.body;
|
|
324
324
|
const files = Object.keys(bundle.files).length;
|
|
@@ -343,9 +343,13 @@ function registerWorkCommands(remote, context) {
|
|
|
343
343
|
return { version, proposed };
|
|
344
344
|
};
|
|
345
345
|
// what publishing the bundle would do, undefined from a server that can't say (before 2.0)
|
|
346
|
-
const planBundle = async (
|
|
346
|
+
const planBundle = async (connection, bundle) => {
|
|
347
347
|
try {
|
|
348
|
-
|
|
348
|
+
// a plan stores nothing, so it is sent again as often as the server does not answer
|
|
349
|
+
return (await (0, server_client_1.callServer)(connection, 'POST', '/v1/definitions/plan', {
|
|
350
|
+
body: bundle,
|
|
351
|
+
repeat: 'always',
|
|
352
|
+
})).body;
|
|
349
353
|
}
|
|
350
354
|
catch (e) {
|
|
351
355
|
if (e instanceof server_client_1.ServerError && e.status === 404) {
|
|
@@ -379,7 +383,7 @@ function registerWorkCommands(remote, context) {
|
|
|
379
383
|
// terminal says so. Without a terminal and without --publish the run fails and says how
|
|
380
384
|
// to publish it, so CI never waits on a question.
|
|
381
385
|
const submitPublishing = async (target, request, local, publish) => {
|
|
382
|
-
const { serverName,
|
|
386
|
+
const { serverName, connection } = target;
|
|
383
387
|
try {
|
|
384
388
|
return await (0, sandbox_client_1.submitRun)(connection, request);
|
|
385
389
|
}
|
|
@@ -391,14 +395,13 @@ function registerWorkCommands(remote, context) {
|
|
|
391
395
|
}
|
|
392
396
|
const definition = request.definition;
|
|
393
397
|
const notPublished = `the build file here is not published on ${serverName} (${definition})`;
|
|
394
|
-
const token = await connection.token();
|
|
395
398
|
if (!publish) {
|
|
396
399
|
if (!environment.prompt) {
|
|
397
400
|
throw new Error(`${notPublished}, publish it first with: hammerkit remote publish ${serverName}, or pass --publish`);
|
|
398
401
|
}
|
|
399
402
|
// a server before 2.0 can't say what publishing would do: it is asked all the same,
|
|
400
403
|
// and refuses then what the caller may not do
|
|
401
|
-
const planned = await planBundle(
|
|
404
|
+
const planned = await planBundle(connection, local);
|
|
402
405
|
if (planned && planned.outcome === null) {
|
|
403
406
|
throw new Error(`${notPublished}, and you may only run published build files (builds: run): ` +
|
|
404
407
|
`somebody who may publish publishes it with: hammerkit remote publish ${serverName}`);
|
|
@@ -424,7 +427,7 @@ function registerWorkCommands(remote, context) {
|
|
|
424
427
|
}
|
|
425
428
|
}
|
|
426
429
|
const annotations = await (0, annotations_1.collectAnnotations)(buildFile, Object.keys(local.files));
|
|
427
|
-
const { version, proposed } = await publishBundle(serverName,
|
|
430
|
+
const { version, proposed } = await publishBundle(serverName, connection, local, annotations).catch((e) => {
|
|
428
431
|
throw e instanceof server_client_1.ServerError && e.status === 403 ? new Error(`${notPublished}, ${e.message}`) : e;
|
|
429
432
|
});
|
|
430
433
|
if (version.hash !== definition) {
|
|
@@ -444,8 +447,11 @@ function registerWorkCommands(remote, context) {
|
|
|
444
447
|
.action(guarded('Remote publish was not successful', async (name, options) => {
|
|
445
448
|
const bundle = await (0, collect_bundle_1.collectBundle)(buildFile, environment);
|
|
446
449
|
if (options.dryRun) {
|
|
447
|
-
const { name: serverName,
|
|
448
|
-
const answer = await (0, server_client_1.callServer)(
|
|
450
|
+
const { name: serverName, connection } = await connect(name);
|
|
451
|
+
const answer = await (0, server_client_1.callServer)(connection, 'POST', '/v1/definitions/plan', {
|
|
452
|
+
body: bundle,
|
|
453
|
+
repeat: 'always',
|
|
454
|
+
}).catch((e) => {
|
|
449
455
|
throw e instanceof server_client_1.ServerError && e.status === 404
|
|
450
456
|
? new Error(`${serverName} can't check a build file without publishing it, it needs hammerkit 2.0`)
|
|
451
457
|
: e;
|
|
@@ -459,8 +465,8 @@ function registerWorkCommands(remote, context) {
|
|
|
459
465
|
return;
|
|
460
466
|
}
|
|
461
467
|
const annotations = (0, annotations_1.applyAnnotations)(await (0, annotations_1.collectAnnotations)(buildFile, Object.keys(bundle.files)), options.annotation ?? []);
|
|
462
|
-
const { name: serverName,
|
|
463
|
-
await publishBundle(serverName,
|
|
468
|
+
const { name: serverName, connection } = await connect(name);
|
|
469
|
+
await publishBundle(serverName, connection, bundle, annotations);
|
|
464
470
|
}));
|
|
465
471
|
remote
|
|
466
472
|
.command('run <tasks...>')
|
|
@@ -492,7 +498,7 @@ function registerWorkCommands(remote, context) {
|
|
|
492
498
|
if (passthrough.length > 0) {
|
|
493
499
|
runOptions.args = passthrough;
|
|
494
500
|
}
|
|
495
|
-
const submitted = await submitPublishing({ serverName,
|
|
501
|
+
const submitted = await submitPublishing({ serverName, connection }, { definition, tasks, source, options: runOptions }, local, !!options.publish);
|
|
496
502
|
const at = submitted.source.type === 'ref' ? ` at ${(0, server_client_1.printable)(submitted.source.commit)}` : '';
|
|
497
503
|
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`);
|
|
498
504
|
if (options.detach) {
|
|
@@ -553,7 +559,7 @@ function registerWorkCommands(remote, context) {
|
|
|
553
559
|
const source = await refSource(options);
|
|
554
560
|
const { name: serverName, server } = await getServer(options.server, 'with --server <name>');
|
|
555
561
|
const connection = (0, sandbox_client_1.getServerConnection)(environment, serverName, server);
|
|
556
|
-
const submitted = await submitPublishing({ serverName,
|
|
562
|
+
const submitted = await submitPublishing({ serverName, connection }, {
|
|
557
563
|
definition,
|
|
558
564
|
services,
|
|
559
565
|
endpoints,
|
|
@@ -612,8 +618,8 @@ function registerWorkCommands(remote, context) {
|
|
|
612
618
|
.description('show how a server stands for you: the places that run, the queue and why each run in it waits, your limits and the runs that count against them, your service runs')
|
|
613
619
|
.addOption(new commander_1.Option('--json', 'print the overview as the server answers it, for scripts and agents'))
|
|
614
620
|
.action(guarded('Remote status was not successful', async (name, options) => {
|
|
615
|
-
const { name: serverName,
|
|
616
|
-
const overview = (await (0, server_client_1.callServer)(
|
|
621
|
+
const { name: serverName, connection } = await connect(name);
|
|
622
|
+
const overview = (await (0, server_client_1.callServer)(connection, 'GET', '/v1/overview')).body;
|
|
617
623
|
if (options.json) {
|
|
618
624
|
environment.stdout.write(`${JSON.stringify(overview, null, 2)}\n`);
|
|
619
625
|
return;
|
|
@@ -622,7 +628,8 @@ function registerWorkCommands(remote, context) {
|
|
|
622
628
|
environment.stdout.write(`${line}\n`);
|
|
623
629
|
}
|
|
624
630
|
// a server from before /v1/capacity, or one that can not read it, prints without
|
|
625
|
-
|
|
631
|
+
// best effort, printed after the overview: it is not waited for, the overview is already out
|
|
632
|
+
const capacity = await (0, server_client_1.callServer)(connection, 'GET', '/v1/capacity', { reconnectMs: 0 }).catch(() => undefined);
|
|
626
633
|
if (capacity) {
|
|
627
634
|
for (const line of (0, remote_status_1.describeCapacity)(capacity.body, true)) {
|
|
628
635
|
environment.stdout.write(`${line}\n`);
|
|
@@ -634,8 +641,8 @@ function registerWorkCommands(remote, context) {
|
|
|
634
641
|
.description('show what the machines of a server have and what its runs take: CPU and memory requested and in use, per namespace on Kubernetes')
|
|
635
642
|
.addOption(new commander_1.Option('--json', 'print it as the server answers it'))
|
|
636
643
|
.action(guarded('Remote capacity was not successful', async (name, options) => {
|
|
637
|
-
const {
|
|
638
|
-
const capacity = (await (0, server_client_1.callServer)(
|
|
644
|
+
const { connection } = await connect(name);
|
|
645
|
+
const capacity = (await (0, server_client_1.callServer)(connection, 'GET', '/v1/capacity')).body;
|
|
639
646
|
if (options.json) {
|
|
640
647
|
environment.stdout.write(`${JSON.stringify(capacity, null, 2)}\n`);
|
|
641
648
|
return;
|
|
@@ -652,7 +659,7 @@ function registerWorkCommands(remote, context) {
|
|
|
652
659
|
.addOption(new commander_1.Option('--until <when>', 'only the runs created until then: back from now, or a time'))
|
|
653
660
|
.addOption(new commander_1.Option('--json', 'print it as the server answers it'))
|
|
654
661
|
.action(guarded('Remote tasks was not successful', async (name, options) => {
|
|
655
|
-
const { name: serverName,
|
|
662
|
+
const { name: serverName, connection } = await connect(name);
|
|
656
663
|
const query = new URLSearchParams({ scope: options.mine ? 'mine' : 'visible' });
|
|
657
664
|
if (options.since) {
|
|
658
665
|
query.set('since', options.since);
|
|
@@ -660,7 +667,7 @@ function registerWorkCommands(remote, context) {
|
|
|
660
667
|
if (options.until) {
|
|
661
668
|
query.set('until', options.until);
|
|
662
669
|
}
|
|
663
|
-
const { tasks } = (await (0, server_client_1.callServer)(
|
|
670
|
+
const { tasks } = (await (0, server_client_1.callServer)(connection, 'GET', `/v1/tasks?${query}`)).body;
|
|
664
671
|
if (options.json) {
|
|
665
672
|
environment.stdout.write(`${JSON.stringify(tasks, null, 2)}\n`);
|
|
666
673
|
return;
|
|
@@ -678,7 +685,10 @@ function registerWorkCommands(remote, context) {
|
|
|
678
685
|
.description('print the address of the dashboard of a server, the pages of remote status, services, runs and logs')
|
|
679
686
|
.action(guarded('Remote dashboard was not successful', async (name) => {
|
|
680
687
|
const { name: serverName, server } = await getServer(name);
|
|
681
|
-
const discovery = await (0, server_client_1.getDiscovery)(server
|
|
688
|
+
const discovery = await (0, server_client_1.getDiscovery)(server, {
|
|
689
|
+
say: (line) => environment.stderr.write(`${line}\n`),
|
|
690
|
+
signal: environment.abortCtrl.signal,
|
|
691
|
+
});
|
|
682
692
|
if (!discovery.dashboard) {
|
|
683
693
|
throw new Error(`${serverName} has no dashboard: it needs endpoints with a login in its config, see https://hammerkit.dev/docs/remote-compute/status#dashboard`);
|
|
684
694
|
}
|
|
@@ -689,10 +699,11 @@ function registerWorkCommands(remote, context) {
|
|
|
689
699
|
.description('list the service runs that are starting, up or asleep: yours, and those whose endpoints you may open, with their addresses and when they end or sleep')
|
|
690
700
|
.addOption(new commander_1.Option('--ended', 'also the ones that ended, as long as the server keeps them'))
|
|
691
701
|
.action(guarded('Remote services was not successful', async (name, options) => {
|
|
692
|
-
const { name: serverName,
|
|
702
|
+
const { name: serverName, connection } = await connect(name);
|
|
693
703
|
const open = ['awaiting-approval', 'queued', 'running', 'up', 'asleep'];
|
|
694
704
|
const states = options.ended ? '' : `&state=${open.join(',')}`;
|
|
695
|
-
const runs = keepRuns((await (0, server_client_1.callServer)(
|
|
705
|
+
const runs = keepRuns((await (0, server_client_1.callServer)(connection, 'GET', `/v1/runs?scope=visible&kind=services${states}`))
|
|
706
|
+
.body.runs, options.ended ? undefined : open, 'services');
|
|
696
707
|
if (runs.length === 0) {
|
|
697
708
|
environment.stdout.write(`${serverName}: no service runs${options.ended ? '' : ' are starting, up or asleep'}\n`);
|
|
698
709
|
return;
|
|
@@ -726,9 +737,9 @@ function registerWorkCommands(remote, context) {
|
|
|
726
737
|
if (options.services || options.tasks) {
|
|
727
738
|
query.set('kind', options.services ? 'services' : 'tasks');
|
|
728
739
|
}
|
|
729
|
-
const { name: serverName,
|
|
740
|
+
const { name: serverName, connection } = await connect(name);
|
|
730
741
|
const path = `/v1/runs${query.toString() ? `?${query}` : ''}`;
|
|
731
|
-
const runs = keepRuns((await (0, server_client_1.callServer)(
|
|
742
|
+
const runs = keepRuns((await (0, server_client_1.callServer)(connection, 'GET', path)).body.runs, options.state ? String(options.state).split(',') : undefined, options.services ? 'services' : options.tasks ? 'tasks' : undefined);
|
|
732
743
|
if (runs.length === 0) {
|
|
733
744
|
environment.stdout.write(`${serverName}: ${query.toString() ? 'no runs it keeps are like that' : 'you have no runs it still keeps'}\n`);
|
|
734
745
|
return;
|
|
@@ -832,8 +843,10 @@ function registerWorkCommands(remote, context) {
|
|
|
832
843
|
if (!RUN_ID.test(id)) {
|
|
833
844
|
throw new Error(`${(0, server_client_1.printable)(id)} is not a run id (run_...)`);
|
|
834
845
|
}
|
|
835
|
-
const { name: serverName,
|
|
836
|
-
|
|
846
|
+
const { name: serverName, connection } = await connect(name);
|
|
847
|
+
// the end becomes this long from now, and never sooner than it was: a repeat sent late ends the
|
|
848
|
+
// run at most a retry window later than the first one did
|
|
849
|
+
const run = (await (0, server_client_1.callServer)(connection, 'POST', `/v1/runs/${id}/extend`, { body: { ttl }, repeat: 'always' })).body;
|
|
837
850
|
environment.stdout.write(`${(0, server_client_1.printable)(run.id)} on ${serverName} stays up until ${(0, server_client_1.printable)(run.expiresAt ?? '-')}\n`);
|
|
838
851
|
}));
|
|
839
852
|
remote
|
|
@@ -843,8 +856,9 @@ function registerWorkCommands(remote, context) {
|
|
|
843
856
|
if (!RUN_ID.test(id)) {
|
|
844
857
|
throw new Error(`${(0, server_client_1.printable)(id)} is not a run id (run_...)`);
|
|
845
858
|
}
|
|
846
|
-
const { name: serverName,
|
|
847
|
-
|
|
859
|
+
const { name: serverName, connection } = await connect(name);
|
|
860
|
+
// a run that is over stays so: sent again, the stop answers the same
|
|
861
|
+
const run = (await (0, server_client_1.callServer)(connection, 'POST', `/v1/runs/${id}/cancel`, { repeat: 'always' })).body;
|
|
848
862
|
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`);
|
|
849
863
|
}));
|
|
850
864
|
remote
|
|
@@ -854,8 +868,9 @@ function registerWorkCommands(remote, context) {
|
|
|
854
868
|
if (!RUN_ID.test(id)) {
|
|
855
869
|
throw new Error(`${(0, server_client_1.printable)(id)} is not a run id (run_...)`);
|
|
856
870
|
}
|
|
857
|
-
const { name: serverName,
|
|
858
|
-
|
|
871
|
+
const { name: serverName, connection } = await connect(name);
|
|
872
|
+
// each retry starts a run of its own: sent again only when the server did not take it
|
|
873
|
+
const run = (await (0, server_client_1.callServer)(connection, 'POST', `/v1/runs/${id}/retry`, { repeat: 'unsent' })).body;
|
|
859
874
|
environment.stdout.write(`${(0, server_client_1.printable)(run.id)} on ${serverName} retries ${(0, server_client_1.printable)(id)}: ${(0, server_client_1.printable)(run.state)}, ` +
|
|
860
875
|
`follow it with hammerkit remote logs ${(0, server_client_1.printable)(run.id)} --follow\n`);
|
|
861
876
|
}));
|
|
@@ -930,9 +945,9 @@ function registerWorkCommands(remote, context) {
|
|
|
930
945
|
}
|
|
931
946
|
query.append(filter.slice(0, index), filter.slice(index + 1));
|
|
932
947
|
}
|
|
933
|
-
const { name: serverName,
|
|
948
|
+
const { name: serverName, connection } = await connect(name);
|
|
934
949
|
const path = `/v1/definitions${query.toString() !== '' ? `?${query}` : ''}`;
|
|
935
|
-
const { body } = await (0, server_client_1.callServer)(
|
|
950
|
+
const { body } = await (0, server_client_1.callServer)(connection, 'GET', path);
|
|
936
951
|
if (body.definitions.length === 0) {
|
|
937
952
|
environment.stdout.write(`${serverName}: no published versions${query.toString() !== '' ? ' match' : ''}\n`);
|
|
938
953
|
return;
|
|
@@ -957,8 +972,8 @@ function registerWorkCommands(remote, context) {
|
|
|
957
972
|
.command('approvals [server]')
|
|
958
973
|
.description('list the runs on a server that wait for your approval')
|
|
959
974
|
.action(guarded('Remote approvals was not successful', async (name) => {
|
|
960
|
-
const { name: serverName,
|
|
961
|
-
const { body } = await (0, server_client_1.callServer)(
|
|
975
|
+
const { name: serverName, connection } = await connect(name);
|
|
976
|
+
const { body } = await (0, server_client_1.callServer)(connection, 'GET', '/v1/approvals');
|
|
962
977
|
if (body.approvals.length === 0) {
|
|
963
978
|
environment.stdout.write(`${serverName}: no runs wait for your approval\n`);
|
|
964
979
|
return;
|
|
@@ -971,11 +986,11 @@ function registerWorkCommands(remote, context) {
|
|
|
971
986
|
}
|
|
972
987
|
}));
|
|
973
988
|
// A version that is published: who published it, what its tasks do and every file.
|
|
974
|
-
const showPublished = async (hash, serverName,
|
|
989
|
+
const showPublished = async (hash, serverName, connection) => {
|
|
975
990
|
const path = `/v1/definitions/${hash}`;
|
|
976
991
|
let version;
|
|
977
992
|
try {
|
|
978
|
-
version = (await (0, server_client_1.callServer)(
|
|
993
|
+
version = (await (0, server_client_1.callServer)(connection, 'GET', path)).body;
|
|
979
994
|
}
|
|
980
995
|
catch (e) {
|
|
981
996
|
if (e instanceof server_client_1.ServerError && e.status === 404) {
|
|
@@ -983,7 +998,7 @@ function registerWorkCommands(remote, context) {
|
|
|
983
998
|
}
|
|
984
999
|
throw e;
|
|
985
1000
|
}
|
|
986
|
-
const files = (await (0, server_client_1.callServer)(
|
|
1001
|
+
const files = (await (0, server_client_1.callServer)(connection, 'GET', `${path}/files`)).body;
|
|
987
1002
|
environment.stdout.write(`${colors_1.default.bold((0, server_client_1.printable)(version.hash))} on ${serverName}, published by ${(0, server_client_1.printable)(version.publishedBy.sub)} (${(0, server_client_1.printable)(version.publishedBy.iss)}) at ${(0, server_client_1.printable)(version.publishedAt)}${version.approvedBy ? `, approved by ${(0, server_client_1.printable)(version.approvedBy.sub)}` : ''}\n`);
|
|
988
1003
|
for (const line of [
|
|
989
1004
|
...yankLines(version),
|
|
@@ -1002,22 +1017,22 @@ function registerWorkCommands(remote, context) {
|
|
|
1002
1017
|
if (!RUN_ID.test(target) && !bundle_1.HASH_PATTERN.test(target)) {
|
|
1003
1018
|
throw new Error(`${(0, server_client_1.printable)(target)} is not a run id or a version, list them with: hammerkit remote approvals, remote proposals or remote versions`);
|
|
1004
1019
|
}
|
|
1005
|
-
const { name: serverName,
|
|
1020
|
+
const { name: serverName, connection } = await connect(name);
|
|
1006
1021
|
if (bundle_1.HASH_PATTERN.test(target)) {
|
|
1007
1022
|
const path = `/v1/proposals/${target}`;
|
|
1008
1023
|
let proposal;
|
|
1009
1024
|
try {
|
|
1010
|
-
proposal = (await (0, server_client_1.callServer)(
|
|
1025
|
+
proposal = (await (0, server_client_1.callServer)(connection, 'GET', path)).body;
|
|
1011
1026
|
}
|
|
1012
1027
|
catch (e) {
|
|
1013
1028
|
if (e instanceof server_client_1.ServerError && e.status === 404) {
|
|
1014
|
-
await showPublished(target, serverName,
|
|
1029
|
+
await showPublished(target, serverName, connection);
|
|
1015
1030
|
return;
|
|
1016
1031
|
}
|
|
1017
1032
|
throw e;
|
|
1018
1033
|
}
|
|
1019
1034
|
if (proposal.state === 'approved') {
|
|
1020
|
-
await showPublished(target, serverName,
|
|
1035
|
+
await showPublished(target, serverName, connection);
|
|
1021
1036
|
return;
|
|
1022
1037
|
}
|
|
1023
1038
|
if (proposal.state !== 'waiting') {
|
|
@@ -1029,7 +1044,7 @@ function registerWorkCommands(remote, context) {
|
|
|
1029
1044
|
environment.stdout.write(' its files are not kept once it is decided\n');
|
|
1030
1045
|
return;
|
|
1031
1046
|
}
|
|
1032
|
-
const files = (await (0, server_client_1.callServer)(
|
|
1047
|
+
const files = (await (0, server_client_1.callServer)(connection, 'GET', `${path}/files`)).body;
|
|
1033
1048
|
environment.stdout.write(`${colors_1.default.bold((0, server_client_1.printable)(proposal.hash))} on ${serverName}, proposed by ${(0, server_client_1.printable)(proposal.proposedBy.sub)} (${(0, server_client_1.printable)(proposal.proposedBy.iss)}), ${(0, server_client_1.printable)(proposal.state)}\n`);
|
|
1034
1049
|
for (const line of [
|
|
1035
1050
|
...annotationLines(proposal.annotations),
|
|
@@ -1041,7 +1056,7 @@ function registerWorkCommands(remote, context) {
|
|
|
1041
1056
|
}
|
|
1042
1057
|
return;
|
|
1043
1058
|
}
|
|
1044
|
-
const view = (await (0, server_client_1.callServer)(
|
|
1059
|
+
const view = (await (0, server_client_1.callServer)(connection, 'GET', `/v1/approvals/${target}`)).body;
|
|
1045
1060
|
for (const line of describeApproval(view)) {
|
|
1046
1061
|
environment.stdout.write(`${line}\n`);
|
|
1047
1062
|
}
|
|
@@ -1051,8 +1066,8 @@ function registerWorkCommands(remote, context) {
|
|
|
1051
1066
|
.command('proposals [server]')
|
|
1052
1067
|
.description('list the proposed versions of build files you made or may approve')
|
|
1053
1068
|
.action(guarded('Remote proposals was not successful', async (name) => {
|
|
1054
|
-
const { name: serverName,
|
|
1055
|
-
const { body } = await (0, server_client_1.callServer)(
|
|
1069
|
+
const { name: serverName, connection } = await connect(name);
|
|
1070
|
+
const { body } = await (0, server_client_1.callServer)(connection, 'GET', '/v1/proposals');
|
|
1056
1071
|
if (body.proposals.length === 0) {
|
|
1057
1072
|
environment.stdout.write(`${serverName}: no proposed versions\n`);
|
|
1058
1073
|
return;
|
|
@@ -1094,8 +1109,13 @@ function registerWorkCommands(remote, context) {
|
|
|
1094
1109
|
if (!bundle_1.HASH_PATTERN.test(target)) {
|
|
1095
1110
|
throw new Error(`${(0, server_client_1.printable)(target)} is not a version, name it by its hash: sha256:...`);
|
|
1096
1111
|
}
|
|
1097
|
-
const { name: serverName,
|
|
1098
|
-
|
|
1112
|
+
const { name: serverName, connection } = await connect(name);
|
|
1113
|
+
// a yank sent again after a lost answer ends the same, unless somebody let the version go on
|
|
1114
|
+
// in between. An unyank is not repeated then: it would let a version go on that somebody yanked
|
|
1115
|
+
await (0, server_client_1.callServer)(connection, 'POST', `/v1/definitions/${target}/${action}`, {
|
|
1116
|
+
body: options.reason ? { reason: options.reason } : undefined,
|
|
1117
|
+
repeat: action === 'yank' ? 'always' : 'unsent',
|
|
1118
|
+
});
|
|
1099
1119
|
environment.stdout.write(`${action === 'yank' ? 'yanked' : 'let go on'} ${(0, server_client_1.printable)(target)} on ${serverName}\n`);
|
|
1100
1120
|
}));
|
|
1101
1121
|
}
|
|
@@ -1111,9 +1131,14 @@ function registerWorkCommands(remote, context) {
|
|
|
1111
1131
|
if (!proposal && !RUN_ID.test(target)) {
|
|
1112
1132
|
throw new Error(`${(0, server_client_1.printable)(target)} is not a run id or a proposed version, list them with: hammerkit remote approvals or remote proposals`);
|
|
1113
1133
|
}
|
|
1114
|
-
const { name: serverName,
|
|
1134
|
+
const { name: serverName, connection } = await connect(name);
|
|
1115
1135
|
const path = proposal ? `/v1/proposals/${target}/${action}` : `/v1/approvals/${target}/${action}`;
|
|
1116
|
-
|
|
1136
|
+
// a decision taken twice is refused the second time (409): sent again only when the
|
|
1137
|
+
// server did not take it
|
|
1138
|
+
const { body } = await (0, server_client_1.callServer)(connection, 'POST', path, {
|
|
1139
|
+
body: options.reason ? { reason: options.reason } : undefined,
|
|
1140
|
+
repeat: 'unsent',
|
|
1141
|
+
});
|
|
1117
1142
|
const rejected = body.state === 'rejected';
|
|
1118
1143
|
environment.stdout.write(proposal
|
|
1119
1144
|
? rejected
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
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
|
+
const reconnect_1 = require("../harness/reconnect");
|
|
6
|
+
const sandbox_client_1 = require("../harness/sandbox-client");
|
|
5
7
|
const TIMEOUT_MS = 10000;
|
|
6
8
|
const WRITE_TIMEOUT_MS = 30000;
|
|
7
9
|
class ServerError extends Error {
|
|
@@ -12,6 +14,8 @@ class ServerError extends Error {
|
|
|
12
14
|
}
|
|
13
15
|
}
|
|
14
16
|
exports.ServerError = ServerError;
|
|
17
|
+
// a reason from the network, such as a TLS or DNS error, can hold control characters
|
|
18
|
+
const failWith = (message) => new ServerError(printable(message));
|
|
15
19
|
// Text from the server or the identity provider is shown to the user: no
|
|
16
20
|
// control characters, so it can't rewrite the terminal, and format characters
|
|
17
21
|
// (bidi overrides, zero-width) written out, so text reads in the order it runs.
|
|
@@ -39,58 +43,55 @@ function printableWhole(value) {
|
|
|
39
43
|
.replace(/\p{Cf}/gu, (char) => `\\u{${char.codePointAt(0)?.toString(16)}}`);
|
|
40
44
|
}
|
|
41
45
|
exports.printableWhole = printableWhole;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
catch (e) {
|
|
51
|
-
throw new ServerError(`unable to reach ${url}: ${e instanceof Error ? e.message : String(e)}`);
|
|
52
|
-
}
|
|
46
|
+
// A document of the server, read as the connection reads it: a 401 carries its status, as
|
|
47
|
+
// the caller tells a token the server refused from a server that is not there.
|
|
48
|
+
async function getJson(connection, path) {
|
|
49
|
+
const url = `${(0, reconnect_1.showUrl)(connection.url)}${path}`;
|
|
50
|
+
const response = await (0, sandbox_client_1.request)(connection, 'GET', path, { timeoutMs: TIMEOUT_MS, fail: failWith });
|
|
53
51
|
if (response.status === 401) {
|
|
54
|
-
throw new ServerError(`${url} did not accept the token
|
|
52
|
+
throw new ServerError(`${url} did not accept the token`, 401);
|
|
55
53
|
}
|
|
56
54
|
if (!response.ok) {
|
|
57
|
-
throw new ServerError(`${url} answered ${response.status}
|
|
55
|
+
throw new ServerError(`${url} answered ${response.status}`, response.status);
|
|
58
56
|
}
|
|
59
57
|
return response.json();
|
|
60
58
|
}
|
|
61
59
|
// What the server says it is. The issuer registered with `remote add` has to be
|
|
62
60
|
// one it accepts, otherwise a login would send the user to an identity provider
|
|
63
|
-
// the server does not trust.
|
|
64
|
-
async function getDiscovery(server) {
|
|
65
|
-
const
|
|
61
|
+
// the server does not trust. No token is sent: the document is for anybody.
|
|
62
|
+
async function getDiscovery(server, options = {}) {
|
|
63
|
+
const connection = { url: server.url, say: options.say, signal: options.signal };
|
|
64
|
+
const document = (await getJson(connection, discovery_1.DISCOVERY_PATH));
|
|
66
65
|
if (document?.name !== 'hammerkit' || !Array.isArray(document.issuers)) {
|
|
67
66
|
throw new ServerError(`${server.url} is not a hammerkit server`);
|
|
68
67
|
}
|
|
69
68
|
return document;
|
|
70
69
|
}
|
|
71
70
|
exports.getDiscovery = getDiscovery;
|
|
72
|
-
async function getMe(
|
|
73
|
-
return (await getJson(
|
|
71
|
+
async function getMe(connection) {
|
|
72
|
+
return (await getJson(connection, '/v1/me'));
|
|
74
73
|
}
|
|
75
74
|
exports.getMe = getMe;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
75
|
+
/**
|
|
76
|
+
* A call of the server: its answer, or an error that says what the server answered. The call
|
|
77
|
+
* is made the way the sandbox calls are (`request`): a server that is away is waited for within
|
|
78
|
+
* the window of the connection (`reconnectMs` shortens it, 0 is no wait), and the call is sent
|
|
79
|
+
* again only as `repeat` allows. A read, or a call that ends the same however often the server
|
|
80
|
+
* takes it, is `always`; by default a POST is `unsent`, sent again only when the server certainly
|
|
81
|
+
* did not take it. A call that times out counts as lost, so an `always` call that is slow is
|
|
82
|
+
* sent again too, for up to the window.
|
|
83
|
+
*/
|
|
84
|
+
async function callServer(connection, method, path, options = {}) {
|
|
85
|
+
const response = await (0, sandbox_client_1.request)(connection, method, path, {
|
|
86
|
+
body: options.body,
|
|
87
|
+
repeat: options.repeat,
|
|
88
|
+
reconnectMs: options.reconnectMs,
|
|
89
|
+
timeoutMs: method === 'GET' ? TIMEOUT_MS : WRITE_TIMEOUT_MS,
|
|
90
|
+
fail: failWith,
|
|
91
|
+
});
|
|
92
|
+
const shown = (0, reconnect_1.showUrl)(connection.url);
|
|
93
|
+
if (response.status === 401) {
|
|
94
|
+
throw new ServerError(`${shown} did not accept the token`, 401);
|
|
94
95
|
}
|
|
95
96
|
let answer;
|
|
96
97
|
try {
|
|
@@ -99,14 +100,11 @@ async function callServer(server, token, method, path, body) {
|
|
|
99
100
|
catch {
|
|
100
101
|
answer = undefined;
|
|
101
102
|
}
|
|
102
|
-
if (response.status === 401) {
|
|
103
|
-
throw new ServerError(`${server.url} did not accept the token`);
|
|
104
|
-
}
|
|
105
103
|
if (!response.ok) {
|
|
106
104
|
const lines = answer && typeof answer.error === 'string' ? printableLines(answer.error) : [];
|
|
107
105
|
// a refused publish says every problem, each on a line of its own
|
|
108
106
|
const said = lines.length > 1 ? `:\n${lines.map((line) => ` ${line}`).join('\n')}` : lines.length === 1 ? `: ${lines[0]}` : '';
|
|
109
|
-
throw new ServerError(`${
|
|
107
|
+
throw new ServerError(`${shown} answered ${response.status}${said}`, response.status);
|
|
110
108
|
}
|
|
111
109
|
return { status: response.status, body: answer };
|
|
112
110
|
}
|
package/package.json
CHANGED