@tsplc/compiler 0.5.9 → 0.5.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +353 -58
- package/dist/cli.js.map +9 -7
- package/dist/codesys.js +2 -2
- package/dist/codesys.js.map +1 -1
- package/package.json +9 -9
- package/src/project/cli.ts +14 -3
- package/src/project/deployments.ts +443 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +68 -34
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import type {
|
|
3
|
+
DeploymentMode,
|
|
4
|
+
DeploymentRecord,
|
|
5
|
+
DeployOptions,
|
|
6
|
+
GitSource,
|
|
7
|
+
} from "@tsplc/agent";
|
|
8
|
+
import {
|
|
9
|
+
type AgentClient,
|
|
10
|
+
AgentOperationError,
|
|
11
|
+
AgentUnreachable,
|
|
12
|
+
} from "@tsplc/agent/client";
|
|
13
|
+
import { describeFinding } from "@tsplc/build";
|
|
14
|
+
import { progress, timedProgress } from "./terminal";
|
|
15
|
+
|
|
16
|
+
/** `tsplc deploy --wait` and the like exit with this while the deployment waits on a person. */
|
|
17
|
+
export const awaitingApprovalExit = 2;
|
|
18
|
+
|
|
19
|
+
/** How a command that follows a deployment may ask before it approves one. */
|
|
20
|
+
export interface Approval {
|
|
21
|
+
/** Approve without asking: `--yes`. */
|
|
22
|
+
yes: boolean;
|
|
23
|
+
/** Someone at a terminal can answer. */
|
|
24
|
+
interactive: boolean;
|
|
25
|
+
/** Asks a yes-or-no question. */
|
|
26
|
+
confirm: (question: string) => Promise<boolean>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** What a command that queues a deployment does once it is queued. */
|
|
30
|
+
export interface Follow {
|
|
31
|
+
/** Follow it until it finishes: `--wait`. */
|
|
32
|
+
wait: boolean;
|
|
33
|
+
approval: Approval;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** How often, a second apart, following a deployment reconnects before it gives up. */
|
|
37
|
+
const reconnectAttempts = 5;
|
|
38
|
+
const reconnectDelay = 1000;
|
|
39
|
+
|
|
40
|
+
const stateMessage = (record: DeploymentRecord) => {
|
|
41
|
+
switch (record.state) {
|
|
42
|
+
case "queued":
|
|
43
|
+
return "Queued.";
|
|
44
|
+
case "building":
|
|
45
|
+
return "Building the release…";
|
|
46
|
+
case "applying":
|
|
47
|
+
return record.operation === "download"
|
|
48
|
+
? "Downloading the program; the application stops while it loads…"
|
|
49
|
+
: "Applying the online change…";
|
|
50
|
+
case "starting":
|
|
51
|
+
return "Starting the server…";
|
|
52
|
+
case "verifying":
|
|
53
|
+
return "Checking the deployment's health…";
|
|
54
|
+
default:
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const printFindings = (record: DeploymentRecord) => {
|
|
60
|
+
if (!record.findings.length) {
|
|
61
|
+
progress("No findings.");
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
for (const finding of record.findings) {
|
|
65
|
+
progress(
|
|
66
|
+
`${finding.refuses ? "refuses" : "note"}: ${describeFinding(finding)}`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** Whether to approve: `--yes` does, a person may, and null when nobody can be asked. */
|
|
72
|
+
const decide = async (approval: Approval, question: string) => {
|
|
73
|
+
if (approval.yes) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
if (!approval.interactive) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
return approval.confirm(question);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Shows what a deployment waits on and approves it when the deployer agrees. False when it is
|
|
84
|
+
* left waiting.
|
|
85
|
+
*/
|
|
86
|
+
const approve = async (
|
|
87
|
+
agent: AgentClient,
|
|
88
|
+
record: DeploymentRecord,
|
|
89
|
+
reason: "review" | "downloadRequired",
|
|
90
|
+
{ url, approval }: { url: string; approval: Approval },
|
|
91
|
+
) => {
|
|
92
|
+
const download = reason === "downloadRequired";
|
|
93
|
+
if (download) {
|
|
94
|
+
console.log(
|
|
95
|
+
"Only a full download can deploy this release, and a full download stops the running application while it loads.",
|
|
96
|
+
);
|
|
97
|
+
} else {
|
|
98
|
+
console.log("The review found what the online change would do:");
|
|
99
|
+
}
|
|
100
|
+
printFindings(record);
|
|
101
|
+
const approved = await decide(
|
|
102
|
+
approval,
|
|
103
|
+
download ? "Download now?" : "Apply it?",
|
|
104
|
+
);
|
|
105
|
+
if (!approved) {
|
|
106
|
+
console.log(
|
|
107
|
+
approved === null
|
|
108
|
+
? `It waits for approval on the dashboard: ${url}`
|
|
109
|
+
: `It waits for approval or cancellation on the dashboard: ${url}`,
|
|
110
|
+
);
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
await agent.approve(record.id, { download });
|
|
114
|
+
console.log(download ? "Approved the full download." : "Approved.");
|
|
115
|
+
return true;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Follows a deployment until it finishes, printing its log and the states it goes through, and
|
|
120
|
+
* asking for approval when it waits on one. Reconnects from the last log line it printed. The
|
|
121
|
+
* exit code: 0 when it is ready, `awaitingApprovalExit` when it is left waiting; a failed or
|
|
122
|
+
* canceled deployment throws.
|
|
123
|
+
*/
|
|
124
|
+
export const followDeployment = async (
|
|
125
|
+
agent: AgentClient,
|
|
126
|
+
deployment: { id: string; url: string },
|
|
127
|
+
approval: Approval,
|
|
128
|
+
): Promise<number> => {
|
|
129
|
+
const seen: {
|
|
130
|
+
after: number;
|
|
131
|
+
state: DeploymentRecord["state"] | null;
|
|
132
|
+
approval: string | null;
|
|
133
|
+
failures: number;
|
|
134
|
+
} = { after: 0, state: null, approval: null, failures: 0 };
|
|
135
|
+
|
|
136
|
+
/** The exit code once the deployment is done with this command, else undefined. */
|
|
137
|
+
const onRecord = async (record: DeploymentRecord) => {
|
|
138
|
+
if (record.state !== seen.state) {
|
|
139
|
+
seen.state = record.state;
|
|
140
|
+
const message = stateMessage(record);
|
|
141
|
+
if (message) {
|
|
142
|
+
console.log(message);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (record.state !== "awaitingApproval" || !record.approval) {
|
|
146
|
+
seen.approval = null;
|
|
147
|
+
} else {
|
|
148
|
+
const asked = `${record.approval.reason}: ${record.approval.message}`;
|
|
149
|
+
if (asked !== seen.approval) {
|
|
150
|
+
seen.approval = asked;
|
|
151
|
+
console.log(record.approval.message);
|
|
152
|
+
const approved = await approve(agent, record, record.approval.reason, {
|
|
153
|
+
url: deployment.url,
|
|
154
|
+
approval,
|
|
155
|
+
});
|
|
156
|
+
if (!approved) {
|
|
157
|
+
return awaitingApprovalExit;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (record.state === "ready") {
|
|
162
|
+
const { health } = record;
|
|
163
|
+
console.log(
|
|
164
|
+
health
|
|
165
|
+
? `Ready: controller ${health.controller}, server ${health.server}, PLC ${health.plc}.${health.detail ? ` ${health.detail}` : ""}`
|
|
166
|
+
: "Ready.",
|
|
167
|
+
);
|
|
168
|
+
return 0;
|
|
169
|
+
}
|
|
170
|
+
if (record.state === "error") {
|
|
171
|
+
throw new Error(
|
|
172
|
+
`The deployment failed: ${record.error?.message ?? "no reason given"}. ${deployment.url}`,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
if (record.state === "canceled") {
|
|
176
|
+
throw new Error(`The deployment was canceled. ${deployment.url}`);
|
|
177
|
+
}
|
|
178
|
+
return undefined;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
while (true) {
|
|
182
|
+
try {
|
|
183
|
+
for await (const event of agent.deploymentEvents(
|
|
184
|
+
deployment.id,
|
|
185
|
+
seen.after,
|
|
186
|
+
)) {
|
|
187
|
+
seen.failures = 0;
|
|
188
|
+
if (event.kind === "record") {
|
|
189
|
+
const exit = await onRecord(event.record);
|
|
190
|
+
if (exit !== undefined) {
|
|
191
|
+
return exit;
|
|
192
|
+
}
|
|
193
|
+
} else if (event.line.seq > seen.after) {
|
|
194
|
+
seen.after = event.line.seq;
|
|
195
|
+
progress(
|
|
196
|
+
event.line.level === "info"
|
|
197
|
+
? event.line.message
|
|
198
|
+
: `${event.line.level}: ${event.line.message}`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
} catch (error) {
|
|
203
|
+
if (!(error instanceof AgentUnreachable)) {
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// The stream broke, or ended before the deployment finished.
|
|
208
|
+
seen.failures += 1;
|
|
209
|
+
if (seen.failures > reconnectAttempts) {
|
|
210
|
+
throw new Error(
|
|
211
|
+
`Lost the connection to the plant agent; the deployment goes on: ${deployment.url}`,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
await Bun.sleep(reconnectDelay);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/** Prints a queued deployment's page, then follows it when asked to. */
|
|
219
|
+
const queued = async (
|
|
220
|
+
agent: AgentClient,
|
|
221
|
+
created: { id: string; url: string },
|
|
222
|
+
follow: Follow,
|
|
223
|
+
) => {
|
|
224
|
+
console.log(`Deployment ${created.id} queued: ${created.url}`);
|
|
225
|
+
if (!follow.wait) {
|
|
226
|
+
return 0;
|
|
227
|
+
}
|
|
228
|
+
return followDeployment(agent, created, follow.approval);
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Uploads a built release as a queued deployment and prints its page. An agent without the
|
|
233
|
+
* deployments API deploys it at once instead, as `tsplc deploy` did before.
|
|
234
|
+
*/
|
|
235
|
+
export const submitDeployment = async (
|
|
236
|
+
agent: AgentClient,
|
|
237
|
+
release: Parameters<AgentClient["deploy"]>[0],
|
|
238
|
+
settings: Omit<DeployOptions, "deployment">,
|
|
239
|
+
request: { mode: DeploymentMode; git: GitSource | null },
|
|
240
|
+
follow: Follow,
|
|
241
|
+
) => {
|
|
242
|
+
const uploading = timedProgress();
|
|
243
|
+
const created = await agent.queueDeployment(
|
|
244
|
+
release,
|
|
245
|
+
{ ...settings, deployment: request },
|
|
246
|
+
uploading,
|
|
247
|
+
);
|
|
248
|
+
if (created) {
|
|
249
|
+
uploading("Uploaded");
|
|
250
|
+
if (request.mode === "review" && !follow.wait) {
|
|
251
|
+
console.log("Once reviewed, it waits for approval on its page.");
|
|
252
|
+
}
|
|
253
|
+
return queued(agent, created, follow);
|
|
254
|
+
}
|
|
255
|
+
if (request.mode === "review") {
|
|
256
|
+
throw new Error(
|
|
257
|
+
"This plant agent can't review a deployment before applying it; deploy without --review.",
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
console.log("This plant agent can't queue deployments; deploying at once.");
|
|
261
|
+
const deployed = await deployAtOnce(
|
|
262
|
+
agent,
|
|
263
|
+
release,
|
|
264
|
+
settings,
|
|
265
|
+
follow.approval,
|
|
266
|
+
);
|
|
267
|
+
console.log(`Deployed release ${deployed.id} (${deployed.operation}).`);
|
|
268
|
+
return 0;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/** Deploys through `POST /deploy`, offering a full download when only one can deploy. */
|
|
272
|
+
const deployAtOnce = async (
|
|
273
|
+
agent: AgentClient,
|
|
274
|
+
release: Parameters<AgentClient["deploy"]>[0],
|
|
275
|
+
settings: Omit<DeployOptions, "deployment">,
|
|
276
|
+
approval: Approval,
|
|
277
|
+
) => {
|
|
278
|
+
const deploying = timedProgress();
|
|
279
|
+
try {
|
|
280
|
+
return await agent.deploy(release, settings, deploying);
|
|
281
|
+
} catch (error) {
|
|
282
|
+
if (
|
|
283
|
+
!(error instanceof AgentOperationError && error.downloadRequired) ||
|
|
284
|
+
!(
|
|
285
|
+
approval.yes ||
|
|
286
|
+
(await approval.confirm(
|
|
287
|
+
`${error.message}. A full download stops the running application while it loads. Download now?`,
|
|
288
|
+
))
|
|
289
|
+
)
|
|
290
|
+
) {
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
return agent.deploy(
|
|
294
|
+
release,
|
|
295
|
+
{ ...settings, download: true },
|
|
296
|
+
timedProgress(),
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
/** The deployments `GET /api/deployments` answers, newest first, with the running and last healthy ones. */
|
|
302
|
+
interface DeploymentList {
|
|
303
|
+
deployments: DeploymentRecord[];
|
|
304
|
+
current: string | null;
|
|
305
|
+
lastGood: string | null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Where `tsplc rollback` goes without an id: the newest deployment before the running one that
|
|
310
|
+
* ran healthy with another release. Without a running one, the last healthy one.
|
|
311
|
+
*/
|
|
312
|
+
export const rollbackTarget = ({
|
|
313
|
+
deployments,
|
|
314
|
+
current,
|
|
315
|
+
lastGood,
|
|
316
|
+
}: DeploymentList) => {
|
|
317
|
+
const index = deployments.findIndex(({ id }) => id === current);
|
|
318
|
+
const running = deployments[index];
|
|
319
|
+
if (!running) {
|
|
320
|
+
return deployments.find(({ id }) => id === lastGood);
|
|
321
|
+
}
|
|
322
|
+
return deployments
|
|
323
|
+
.slice(index + 1)
|
|
324
|
+
.find(
|
|
325
|
+
(deployment) =>
|
|
326
|
+
deployment.state === "ready" &&
|
|
327
|
+
deployment.activeFrom !== null &&
|
|
328
|
+
deployment.release !== running.release,
|
|
329
|
+
);
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const age = (from: string, now: number) => {
|
|
333
|
+
const seconds = Math.max(0, Math.round((now - Date.parse(from)) / 1000));
|
|
334
|
+
if (seconds < 60) {
|
|
335
|
+
return `${seconds}s`;
|
|
336
|
+
}
|
|
337
|
+
if (seconds < 3600) {
|
|
338
|
+
return `${Math.floor(seconds / 60)}m`;
|
|
339
|
+
}
|
|
340
|
+
if (seconds < 48 * 3600) {
|
|
341
|
+
return `${Math.floor(seconds / 3600)}h`;
|
|
342
|
+
}
|
|
343
|
+
return `${Math.floor(seconds / 86400)}d`;
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const commitLabel = (git: GitSource | null) =>
|
|
347
|
+
git ? `${git.commit.slice(0, 7)}${git.dirty ? "*" : ""}` : "-";
|
|
348
|
+
|
|
349
|
+
const requester = ({ requestedBy }: DeploymentRecord) =>
|
|
350
|
+
requestedBy.user && requestedBy.host
|
|
351
|
+
? `${requestedBy.user}@${requestedBy.host}`
|
|
352
|
+
: (requestedBy.user ?? requestedBy.identity.name);
|
|
353
|
+
|
|
354
|
+
/** `tsplc deployments`: the agent's deployments, newest first, or as JSON. */
|
|
355
|
+
export const printDeployments = async (
|
|
356
|
+
agent: AgentClient,
|
|
357
|
+
flags: { format?: "json" | "text" },
|
|
358
|
+
) => {
|
|
359
|
+
const list = await agent.deployments();
|
|
360
|
+
if (flags.format === "json") {
|
|
361
|
+
console.log(JSON.stringify({ ok: true, ...list }));
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (!list.deployments.length) {
|
|
365
|
+
console.log("No deployments yet.");
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const now = Date.now();
|
|
369
|
+
const rows = [
|
|
370
|
+
["ID", "STATE", "AGE", "RELEASE", "COMMIT", "BY", ""],
|
|
371
|
+
...list.deployments.map((deployment) => [
|
|
372
|
+
deployment.id,
|
|
373
|
+
deployment.state,
|
|
374
|
+
age(deployment.createdAt, now),
|
|
375
|
+
deployment.release,
|
|
376
|
+
commitLabel(deployment.git),
|
|
377
|
+
requester(deployment),
|
|
378
|
+
[
|
|
379
|
+
deployment.id === list.current ? "current" : "",
|
|
380
|
+
deployment.id === list.lastGood ? "last good" : "",
|
|
381
|
+
]
|
|
382
|
+
.filter(Boolean)
|
|
383
|
+
.join(", "),
|
|
384
|
+
]),
|
|
385
|
+
];
|
|
386
|
+
const widths = rows[0]?.map((_, column) =>
|
|
387
|
+
Math.max(...rows.map((row) => row[column]?.length ?? 0)),
|
|
388
|
+
);
|
|
389
|
+
for (const row of rows) {
|
|
390
|
+
console.log(
|
|
391
|
+
row
|
|
392
|
+
.map((cell, column) => cell.padEnd(widths?.[column] ?? 0))
|
|
393
|
+
.join(" ")
|
|
394
|
+
.trimEnd(),
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
/** `tsplc rollback [id]`: deploys that deployment's release again, by default the last good one before the running one. */
|
|
400
|
+
export const rollBack = async (
|
|
401
|
+
agent: AgentClient,
|
|
402
|
+
id: string | undefined,
|
|
403
|
+
follow: Follow,
|
|
404
|
+
) => {
|
|
405
|
+
const target = id ?? rollbackTarget(await agent.deployments())?.id;
|
|
406
|
+
if (!target) {
|
|
407
|
+
throw new Error(
|
|
408
|
+
"No earlier healthy deployment to roll back to; name one: tsplc rollback <id>",
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
console.log(`Rolling back to ${target}…`);
|
|
412
|
+
return queued(agent, await agent.rollback(target), follow);
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
/** `tsplc redeploy <id>`: deploys that deployment's release again. */
|
|
416
|
+
export const redeploy = async (
|
|
417
|
+
agent: AgentClient,
|
|
418
|
+
id: string,
|
|
419
|
+
mode: DeploymentMode,
|
|
420
|
+
follow: Follow,
|
|
421
|
+
) => queued(agent, await agent.redeploy(id, mode), follow);
|
|
422
|
+
|
|
423
|
+
/** The command that opens a URL in the desktop's browser, by platform. */
|
|
424
|
+
const openers: Partial<Record<NodeJS.Platform, string[]>> = {
|
|
425
|
+
darwin: ["open"],
|
|
426
|
+
win32: ["cmd", "/c", "start", ""],
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
/** `tsplc dashboard`: prints the agent's dashboard, and opens it when someone is at the terminal. */
|
|
430
|
+
export const openDashboard = (agent: AgentClient, interactive: boolean) => {
|
|
431
|
+
const url = new URL("/", agent.url).href;
|
|
432
|
+
console.log(url);
|
|
433
|
+
if (!interactive) {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
const [command = "xdg-open", ...args] = openers[process.platform] ?? [
|
|
437
|
+
"xdg-open",
|
|
438
|
+
];
|
|
439
|
+
// Without a desktop to open it on, the printed URL is the answer.
|
|
440
|
+
spawn(command, [...args, url], { stdio: "ignore", detached: true })
|
|
441
|
+
.on("error", () => {})
|
|
442
|
+
.unref();
|
|
443
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import type { GitSource } from "@tsplc/agent";
|
|
3
|
+
|
|
4
|
+
/** What `git` prints for `args` in `directory`, or null when it fails or isn't installed. */
|
|
5
|
+
const git = (directory: string, args: readonly string[]) =>
|
|
6
|
+
new Promise<string | null>((resolve) => {
|
|
7
|
+
execFile("git", args, { cwd: directory }, (error, stdout) => {
|
|
8
|
+
resolve(error ? null : stdout.trim());
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The commit checked out in `directory`, for a deployment to record. Null outside a git checkout,
|
|
14
|
+
* before its first commit or without git: a deployment never fails for want of it.
|
|
15
|
+
*/
|
|
16
|
+
export const gitSource = async (
|
|
17
|
+
directory: string,
|
|
18
|
+
): Promise<GitSource | null> => {
|
|
19
|
+
const commit = await git(directory, ["rev-parse", "--verify", "HEAD"]);
|
|
20
|
+
if (!commit) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const [branch, status, message] = await Promise.all([
|
|
24
|
+
git(directory, ["symbolic-ref", "--quiet", "--short", "HEAD"]),
|
|
25
|
+
git(directory, ["status", "--porcelain"]),
|
|
26
|
+
git(directory, ["log", "-1", "--format=%s"]),
|
|
27
|
+
]);
|
|
28
|
+
return {
|
|
29
|
+
commit,
|
|
30
|
+
branch: branch || null,
|
|
31
|
+
// A status that can't be read can't vouch for a clean tree.
|
|
32
|
+
dirty: status !== "",
|
|
33
|
+
message: message || null,
|
|
34
|
+
};
|
|
35
|
+
};
|
package/src/project/plant.ts
CHANGED
|
@@ -5,21 +5,31 @@ import {
|
|
|
5
5
|
type Target,
|
|
6
6
|
type Variable,
|
|
7
7
|
} from "@tsplc/agent";
|
|
8
|
-
import {
|
|
9
|
-
type AgentClient,
|
|
10
|
-
AgentOperationError,
|
|
11
|
-
createAgentClient,
|
|
12
|
-
} from "@tsplc/agent/client";
|
|
8
|
+
import { type AgentClient, createAgentClient } from "@tsplc/agent/client";
|
|
13
9
|
import type { LoginCommand, LoginResult } from "@tsplc/build";
|
|
14
10
|
import type { CompileEnvironment } from "@tsplc/compiler";
|
|
15
11
|
import type { LoadedConfig } from "@tsplc/config";
|
|
16
12
|
import { LoginUnavailable, noLogin } from "@tsplc/debug";
|
|
17
13
|
import { z } from "zod";
|
|
14
|
+
import {
|
|
15
|
+
type Approval,
|
|
16
|
+
type Follow,
|
|
17
|
+
openDashboard,
|
|
18
|
+
printDeployments,
|
|
19
|
+
redeploy,
|
|
20
|
+
rollBack,
|
|
21
|
+
submitDeployment,
|
|
22
|
+
} from "./deployments";
|
|
23
|
+
import { gitSource } from "./git";
|
|
18
24
|
import { prepareRelease } from "./release";
|
|
19
25
|
import { choose, confirm, progress, timedProgress } from "./terminal";
|
|
20
26
|
|
|
21
27
|
const plantCommands = [
|
|
22
28
|
"deploy",
|
|
29
|
+
"deployments",
|
|
30
|
+
"rollback",
|
|
31
|
+
"redeploy",
|
|
32
|
+
"dashboard",
|
|
23
33
|
"target",
|
|
24
34
|
"status",
|
|
25
35
|
"login",
|
|
@@ -41,6 +51,9 @@ export const plantCommand = (name: string) =>
|
|
|
41
51
|
|
|
42
52
|
export interface PlantFlags {
|
|
43
53
|
download?: boolean;
|
|
54
|
+
format?: "json" | "text";
|
|
55
|
+
review?: boolean;
|
|
56
|
+
wait?: boolean;
|
|
44
57
|
"initialize-user"?: boolean;
|
|
45
58
|
restore?: boolean;
|
|
46
59
|
select?: boolean;
|
|
@@ -159,6 +172,7 @@ const deploy = async (
|
|
|
159
172
|
loaded: LoadedConfig,
|
|
160
173
|
flags: PlantFlags,
|
|
161
174
|
environment: CompileEnvironment,
|
|
175
|
+
follow: Follow,
|
|
162
176
|
) => {
|
|
163
177
|
const status = await agent.status();
|
|
164
178
|
if (status.login) {
|
|
@@ -202,29 +216,21 @@ const deploy = async (
|
|
|
202
216
|
};
|
|
203
217
|
console.log(
|
|
204
218
|
release.manifest.server
|
|
205
|
-
? "
|
|
206
|
-
: "
|
|
219
|
+
? "Uploading the server and the program…"
|
|
220
|
+
: "Uploading the program…",
|
|
207
221
|
);
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return await agent.deploy(release, settings, deploying);
|
|
212
|
-
} catch (error) {
|
|
213
|
-
if (
|
|
214
|
-
!(error instanceof AgentOperationError && error.downloadRequired) ||
|
|
215
|
-
!(await confirm(
|
|
216
|
-
`${error.message}. A full download stops the running application while it loads. Download now?`,
|
|
217
|
-
flags,
|
|
218
|
-
))
|
|
219
|
-
) {
|
|
220
|
-
throw error;
|
|
221
|
-
}
|
|
222
|
-
return agent.deploy(
|
|
222
|
+
return {
|
|
223
|
+
exitCode: await submitDeployment(
|
|
224
|
+
agent,
|
|
223
225
|
release,
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
226
|
+
settings,
|
|
227
|
+
{
|
|
228
|
+
mode: flags.review ? "review" : "apply",
|
|
229
|
+
git: await gitSource(loaded.root),
|
|
230
|
+
},
|
|
231
|
+
follow,
|
|
232
|
+
),
|
|
233
|
+
};
|
|
228
234
|
};
|
|
229
235
|
|
|
230
236
|
/** Opens a login when none is working; it stays open across commands until `tsplc logout`. */
|
|
@@ -346,7 +352,7 @@ const printLogin = (result: LoginResult) => {
|
|
|
346
352
|
|
|
347
353
|
/**
|
|
348
354
|
* Runs a command against the plant agent. Returns a deployment's compiler diagnostics when the
|
|
349
|
-
* program doesn't compile.
|
|
355
|
+
* program doesn't compile, and the exit code of a command that queues a deployment.
|
|
350
356
|
*/
|
|
351
357
|
export const runPlantCommand = async (
|
|
352
358
|
agent: AgentClient,
|
|
@@ -358,8 +364,16 @@ export const runPlantCommand = async (
|
|
|
358
364
|
load: () => Promise<LoadedConfig>;
|
|
359
365
|
/** What the release's compilation reads. */
|
|
360
366
|
environment: CompileEnvironment;
|
|
367
|
+
/** Someone at a terminal can answer prompts. */
|
|
368
|
+
interactive: boolean;
|
|
361
369
|
},
|
|
362
370
|
) => {
|
|
371
|
+
const approval: Approval = {
|
|
372
|
+
yes: flags.yes ?? false,
|
|
373
|
+
interactive: project.interactive,
|
|
374
|
+
confirm: (question) => confirm(question, {}),
|
|
375
|
+
};
|
|
376
|
+
const follow = { wait: flags.wait ?? false, approval };
|
|
363
377
|
const expect = (count: number, usage: string) => {
|
|
364
378
|
if (args.length !== count) {
|
|
365
379
|
throw new Error(`Usage: tsplc ${command} ${usage}`);
|
|
@@ -370,24 +384,44 @@ export const runPlantCommand = async (
|
|
|
370
384
|
printLogin(await agent.command(loginCommand));
|
|
371
385
|
};
|
|
372
386
|
switch (command) {
|
|
373
|
-
case "deploy":
|
|
387
|
+
case "deploy":
|
|
374
388
|
expect(
|
|
375
389
|
0,
|
|
376
|
-
"[--target name] [--download] [--select] [--initialize-user] [--simulate]",
|
|
390
|
+
"[--target name] [--download] [--select] [--initialize-user] [--simulate] [--review] [--wait] [-y]",
|
|
377
391
|
);
|
|
378
|
-
|
|
392
|
+
return deploy(
|
|
379
393
|
agent,
|
|
380
394
|
project.file,
|
|
381
395
|
await project.load(),
|
|
382
396
|
flags,
|
|
383
397
|
project.environment,
|
|
398
|
+
follow,
|
|
384
399
|
);
|
|
385
|
-
|
|
386
|
-
|
|
400
|
+
case "deployments":
|
|
401
|
+
expect(0, "[--format json]");
|
|
402
|
+
return printDeployments(agent, flags);
|
|
403
|
+
case "rollback":
|
|
404
|
+
if (args.length > 1) {
|
|
405
|
+
throw new Error("Usage: tsplc rollback [id] [--wait] [-y]");
|
|
387
406
|
}
|
|
388
|
-
|
|
389
|
-
|
|
407
|
+
return { exitCode: await rollBack(agent, args[0], follow) };
|
|
408
|
+
case "redeploy": {
|
|
409
|
+
const [id, ...extra] = args;
|
|
410
|
+
if (!id || extra.length) {
|
|
411
|
+
throw new Error("Usage: tsplc redeploy <id> [--review] [--wait] [-y]");
|
|
412
|
+
}
|
|
413
|
+
return {
|
|
414
|
+
exitCode: await redeploy(
|
|
415
|
+
agent,
|
|
416
|
+
id,
|
|
417
|
+
flags.review ? "review" : "apply",
|
|
418
|
+
follow,
|
|
419
|
+
),
|
|
420
|
+
};
|
|
390
421
|
}
|
|
422
|
+
case "dashboard":
|
|
423
|
+
expect(0, "");
|
|
424
|
+
return openDashboard(agent, project.interactive);
|
|
391
425
|
case "target": {
|
|
392
426
|
expect(0, "");
|
|
393
427
|
const target = await selectTarget(agent);
|