@tsplc/agent 0.5.9 → 0.5.12
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 +2660 -525
- package/dist/cli.js.map +32 -18
- package/dist/client.js +262 -37
- package/dist/client.js.map +4 -4
- package/dist/index.js +2325 -415
- package/dist/index.js.map +27 -14
- package/package.json +6 -5
- package/src/access.ts +276 -0
- package/src/agent.ts +130 -29
- package/src/audit.ts +64 -0
- package/src/cli.ts +81 -5
- package/src/client.ts +204 -36
- package/src/controller.ts +60 -0
- package/src/dashboard/index.ts +118 -0
- package/src/dashboard/ui/access.ts +192 -0
- package/src/dashboard/ui/actions.ts +192 -0
- package/src/dashboard/ui/api.ts +158 -0
- package/src/dashboard/ui/app.ts +41 -0
- package/src/dashboard/ui/approval.ts +132 -0
- package/src/dashboard/ui/audit.ts +115 -0
- package/src/dashboard/ui/contract.ts +19 -0
- package/src/dashboard/ui/dashboard.css +1332 -0
- package/src/dashboard/ui/deployment.ts +432 -0
- package/src/dashboard/ui/deployments.ts +317 -0
- package/src/dashboard/ui/details.ts +240 -0
- package/src/dashboard/ui/dom.ts +91 -0
- package/src/dashboard/ui/environment.ts +326 -0
- package/src/dashboard/ui/format.ts +118 -0
- package/src/dashboard/ui/logs.ts +127 -0
- package/src/dashboard/ui/main.ts +86 -0
- package/src/dashboard/ui/overlay.ts +146 -0
- package/src/dashboard/ui/shell.ts +192 -0
- package/src/dashboard/ui/source.ts +144 -0
- package/src/deploy.ts +174 -144
- package/src/deployments/health.ts +80 -0
- package/src/deployments/retention.ts +45 -0
- package/src/deployments/routes.ts +273 -0
- package/src/deployments/runner.ts +877 -0
- package/src/deployments/store.ts +233 -0
- package/src/environment.ts +284 -0
- package/src/index.ts +4 -0
- package/src/login.ts +17 -0
- package/src/operations.ts +6 -1
- package/src/protocol.ts +259 -2
- package/src/releases.ts +73 -1
- package/src/routes.ts +52 -0
- package/src/secrets.ts +74 -4
- package/src/server-process.ts +33 -16
- package/src/sessions.ts +58 -0
- package/src/state.ts +2 -0
- package/src/tailscale.ts +112 -0
package/src/deploy.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
1
|
import {
|
|
4
2
|
controllerBaseline,
|
|
5
3
|
findControllerLogin,
|
|
4
|
+
type ResetKind,
|
|
5
|
+
restartWarmController,
|
|
6
|
+
reviewWarmController,
|
|
6
7
|
settleWarmController,
|
|
7
8
|
startWarmController,
|
|
8
9
|
updateWarmController,
|
|
9
10
|
workerScriptsDigest,
|
|
10
11
|
} from "@tsplc/build";
|
|
11
|
-
import { loadManifest } from "@tsplc/debug";
|
|
12
|
-
import { debugManifestFile } from "@tsplc/debug/manifest";
|
|
13
12
|
import {
|
|
14
13
|
type CodesysSettings,
|
|
15
14
|
controllerConfig,
|
|
@@ -20,35 +19,79 @@ import {
|
|
|
20
19
|
requireBuilder,
|
|
21
20
|
} from "./codesys";
|
|
22
21
|
import type { Debugging } from "./debugging";
|
|
22
|
+
import {
|
|
23
|
+
createDeploymentRunner,
|
|
24
|
+
type DeploymentController,
|
|
25
|
+
} from "./deployments/runner";
|
|
26
|
+
import { openDeploymentStore } from "./deployments/store";
|
|
23
27
|
import { AgentError, errorMessage } from "./errors";
|
|
24
|
-
import { appendJsonl } from "./logs";
|
|
25
28
|
import type { Operations } from "./operations";
|
|
26
29
|
import {
|
|
27
30
|
type ClientIdentity,
|
|
28
31
|
type ClientRelease,
|
|
29
|
-
type
|
|
30
|
-
type Release,
|
|
32
|
+
type Identity,
|
|
31
33
|
tsplcVersion,
|
|
32
34
|
versionMismatch,
|
|
33
35
|
} from "./protocol";
|
|
34
|
-
import { type DeployRequest, loadRelease
|
|
35
|
-
import { controllerCredentials, readSecrets
|
|
36
|
+
import { type DeployRequest, loadRelease } from "./releases";
|
|
37
|
+
import { controllerCredentials, readSecrets } from "./secrets";
|
|
36
38
|
import type { ServerSupervisor } from "./server-process";
|
|
37
39
|
import type { AgentState } from "./state";
|
|
38
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Refuses a client of another tsPLC version, which compiles for scripts this agent doesn't run,
|
|
43
|
+
* and returns what to warn the deployment about. A client from before version checks reports none
|
|
44
|
+
* and is only warned, so it can still deploy.
|
|
45
|
+
*/
|
|
46
|
+
export const clientWarnings = async (clientTsplc: ClientRelease) => {
|
|
47
|
+
if (clientTsplc.version === undefined) {
|
|
48
|
+
return [
|
|
49
|
+
`the deploying tsPLC predates version checks, while this plant agent runs tsPLC ${tsplcVersion}; update the client to ${tsplcVersion}`,
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
if (clientTsplc.version !== tsplcVersion) {
|
|
53
|
+
throw new AgentError(
|
|
54
|
+
versionMismatch({
|
|
55
|
+
agent: tsplcVersion,
|
|
56
|
+
client: clientTsplc.version,
|
|
57
|
+
agentUpgrades: true,
|
|
58
|
+
}),
|
|
59
|
+
409,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
if (
|
|
63
|
+
clientTsplc.scripts !== undefined &&
|
|
64
|
+
clientTsplc.scripts !== (await workerScriptsDigest())
|
|
65
|
+
) {
|
|
66
|
+
return [
|
|
67
|
+
`the plant agent's CODESYS scripts differ from the deploying tsPLC's, though both are ${tsplcVersion}, so one is a local build. The deployment runs the agent's scripts: update the plant agent to run the client's`,
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
return [];
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** Who asked for a deployment: the identity behind the request, and the deploying machine's user and host. */
|
|
74
|
+
export const requester = (identity: Identity, client: ClientIdentity) => ({
|
|
75
|
+
identity: { login: identity.login, name: identity.name, via: identity.via },
|
|
76
|
+
user: client.user ?? null,
|
|
77
|
+
host: client.host ?? null,
|
|
78
|
+
});
|
|
79
|
+
|
|
39
80
|
/**
|
|
40
81
|
* Deployments of uploaded releases: a download or online change through the CODESYS that stays
|
|
41
82
|
* running between them, then the release's server once the controller has its program.
|
|
42
83
|
*/
|
|
43
|
-
export const createDeployments = (context: {
|
|
84
|
+
export const createDeployments = async (context: {
|
|
44
85
|
state: AgentState;
|
|
45
86
|
operations: Operations;
|
|
46
87
|
codesys: CodesysSettings;
|
|
47
88
|
server: ServerSupervisor;
|
|
48
89
|
debugging: Debugging;
|
|
90
|
+
serverHealthUrl: string | undefined;
|
|
91
|
+
now: () => Date;
|
|
49
92
|
log: (message: string) => void;
|
|
50
93
|
}) => {
|
|
51
|
-
const { state
|
|
94
|
+
const { state } = context;
|
|
52
95
|
|
|
53
96
|
/** CODESYS starts for the deployed release in the background, so the first deployment finds it running. */
|
|
54
97
|
const warming = (async () => {
|
|
@@ -73,7 +116,91 @@ export const createDeployments = (context: {
|
|
|
73
116
|
context.log(`Starting CODESYS failed: ${errorMessage(error)}`);
|
|
74
117
|
});
|
|
75
118
|
|
|
119
|
+
const controller: DeploymentController = {
|
|
120
|
+
ready: async () => {
|
|
121
|
+
const builder = await requireBuilder();
|
|
122
|
+
if (await findControllerLogin(loginContainer)) {
|
|
123
|
+
throw new AgentError(
|
|
124
|
+
"A login is open; log out before deploying: tsplc logout",
|
|
125
|
+
409,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return builder.kind === "unverified"
|
|
129
|
+
? [
|
|
130
|
+
`${builder.reason}. Run tsplc-agent setup on the plant PC once to rebuild and label it`,
|
|
131
|
+
]
|
|
132
|
+
: [];
|
|
133
|
+
},
|
|
134
|
+
settle: (config) => settleWarmController(config, deployContainer),
|
|
135
|
+
baseline: () => controllerBaseline(state.paths.controller),
|
|
136
|
+
review: async (config, options) => {
|
|
137
|
+
await ensureGateway();
|
|
138
|
+
await warming;
|
|
139
|
+
return reviewWarmController(config, deployContainer, options);
|
|
140
|
+
},
|
|
141
|
+
// The previous server keeps running until the controller has the new program, so a failed
|
|
142
|
+
// transfer leaves both as they were.
|
|
143
|
+
apply: async (config, operation, options) => {
|
|
144
|
+
await ensureGateway();
|
|
145
|
+
await warming;
|
|
146
|
+
return updateWarmController(config, deployContainer, operation, options);
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const runner = createDeploymentRunner({
|
|
151
|
+
store: await openDeploymentStore({
|
|
152
|
+
directory: state.paths.deploymentRecords,
|
|
153
|
+
now: context.now,
|
|
154
|
+
log: context.log,
|
|
155
|
+
}),
|
|
156
|
+
state,
|
|
157
|
+
operations: context.operations,
|
|
158
|
+
codesys: context.codesys,
|
|
159
|
+
server: context.server,
|
|
160
|
+
controller,
|
|
161
|
+
serverHealthUrl: context.serverHealthUrl,
|
|
162
|
+
onActivated: () => context.debugging.restart(),
|
|
163
|
+
now: context.now,
|
|
164
|
+
log: context.log,
|
|
165
|
+
});
|
|
166
|
+
await runner.start();
|
|
167
|
+
|
|
168
|
+
const deployedConfig = async () => {
|
|
169
|
+
const deployed = await state.release();
|
|
170
|
+
const target = await state.target();
|
|
171
|
+
if (!deployed || !target) {
|
|
172
|
+
throw new AgentError("Nothing is deployed to the controller yet", 409);
|
|
173
|
+
}
|
|
174
|
+
return controllerConfig(
|
|
175
|
+
await loadRelease(state.paths.releases, deployed.id),
|
|
176
|
+
target,
|
|
177
|
+
state.paths.controller,
|
|
178
|
+
context.codesys,
|
|
179
|
+
);
|
|
180
|
+
};
|
|
181
|
+
|
|
76
182
|
return {
|
|
183
|
+
runner,
|
|
184
|
+
/**
|
|
185
|
+
* Resets the deployed application and starts it again through the CODESYS that stays running
|
|
186
|
+
* for deployments, as the agent's one operation, so it takes seconds and needs no login.
|
|
187
|
+
*/
|
|
188
|
+
restart: (reset: ResetKind) =>
|
|
189
|
+
context.operations.exclusive("a controller restart", async () => {
|
|
190
|
+
if (await findControllerLogin(loginContainer)) {
|
|
191
|
+
throw new AgentError(
|
|
192
|
+
"A login opened meanwhile; restart again to use it",
|
|
193
|
+
409,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
const config = await deployedConfig();
|
|
197
|
+
await requireBuilder();
|
|
198
|
+
await ensureGateway();
|
|
199
|
+
await warming;
|
|
200
|
+
return restartWarmController(config, deployContainer, reset, {
|
|
201
|
+
credentials: await controllerCredentials(null),
|
|
202
|
+
});
|
|
203
|
+
}),
|
|
77
204
|
/**
|
|
78
205
|
* Stops the warm container once it has committed the last deployment, so an upgraded agent
|
|
79
206
|
* starts one running its own code and none keeps the installed package's files in use.
|
|
@@ -95,154 +222,57 @@ export const createDeployments = (context: {
|
|
|
95
222
|
}
|
|
96
223
|
await removeContainer(deployContainer);
|
|
97
224
|
},
|
|
225
|
+
/**
|
|
226
|
+
* `POST /deploy`: runs a deployment at once and streams its progress, as clients from before
|
|
227
|
+
* `/api/deployments` expect. One that needs a download fails, for the client to ask again.
|
|
228
|
+
*/
|
|
98
229
|
deploy: async (
|
|
99
230
|
/** Reads the upload once the client's version is checked, so a mismatch is reported before a large body is read. */
|
|
100
231
|
readRequest: () => Promise<DeployRequest>,
|
|
232
|
+
identity: Identity,
|
|
101
233
|
client: ClientIdentity,
|
|
102
234
|
clientTsplc: ClientRelease,
|
|
103
235
|
) => {
|
|
104
|
-
|
|
105
|
-
// version checks reports none and is only warned, so it can still deploy.
|
|
106
|
-
if (
|
|
107
|
-
clientTsplc.version !== undefined &&
|
|
108
|
-
clientTsplc.version !== tsplcVersion
|
|
109
|
-
) {
|
|
110
|
-
throw new AgentError(
|
|
111
|
-
versionMismatch({
|
|
112
|
-
agent: tsplcVersion,
|
|
113
|
-
client: clientTsplc.version,
|
|
114
|
-
agentUpgrades: true,
|
|
115
|
-
}),
|
|
116
|
-
409,
|
|
117
|
-
);
|
|
118
|
-
}
|
|
236
|
+
const warnings = await clientWarnings(clientTsplc);
|
|
119
237
|
context.operations.requireIdle();
|
|
120
|
-
|
|
238
|
+
runner.requireIdle();
|
|
239
|
+
await requireBuilder();
|
|
121
240
|
const { settings, upload } = await readRequest();
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
409,
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
const credentials = await controllerCredentials(settings.credentials);
|
|
130
|
-
const secrets = await readSecrets(upload.manifest.secrets);
|
|
131
|
-
const scripts = await workerScriptsDigest();
|
|
241
|
+
await state.requireTarget();
|
|
242
|
+
await controllerCredentials(settings.credentials);
|
|
243
|
+
// A missing secret fails before anything stops.
|
|
244
|
+
await readSecrets(upload.manifest.secrets);
|
|
132
245
|
return context.operations.stream(
|
|
133
246
|
"a deployment",
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
kind: "progress",
|
|
138
|
-
message: `Warning: ${builder.reason}. Run tsplc-agent setup on the plant PC once to rebuild and label it`,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
if (clientTsplc.version === undefined) {
|
|
142
|
-
emit({
|
|
143
|
-
kind: "progress",
|
|
144
|
-
message: `Warning: the deploying tsPLC predates version checks, while this plant agent runs tsPLC ${tsplcVersion}; update the client to ${tsplcVersion}`,
|
|
145
|
-
});
|
|
146
|
-
} else if (
|
|
147
|
-
clientTsplc.scripts !== undefined &&
|
|
148
|
-
clientTsplc.scripts !== scripts
|
|
149
|
-
) {
|
|
150
|
-
emit({
|
|
151
|
-
kind: "progress",
|
|
152
|
-
message: `Warning: the plant agent's CODESYS scripts differ from the deploying tsPLC's, though both are ${tsplcVersion}, so one is a local build. The deployment runs the agent's scripts: update the plant agent to run the client's`,
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
const release = await saveRelease(state.paths.releases, upload);
|
|
156
|
-
const config = await withSecrets(
|
|
157
|
-
controllerConfig(
|
|
158
|
-
release,
|
|
159
|
-
target,
|
|
160
|
-
state.paths.controller,
|
|
161
|
-
context.codesys,
|
|
162
|
-
),
|
|
163
|
-
secrets,
|
|
164
|
-
state.paths.sources,
|
|
165
|
-
);
|
|
166
|
-
// The last deployment's project may still be saving, and its commit decides the baseline.
|
|
167
|
-
await settleWarmController(config, deployContainer);
|
|
168
|
-
const baseline = settings.download
|
|
169
|
-
? "valid"
|
|
170
|
-
: await controllerBaseline(state.paths.controller);
|
|
171
|
-
if (baseline !== "valid") {
|
|
172
|
-
throw new AgentError(
|
|
173
|
-
baseline === "missing"
|
|
174
|
-
? "The controller has no application downloaded by this agent; only a full download can deploy"
|
|
175
|
-
: "This agent's record of the controller's download can't be read; only a full download can deploy",
|
|
176
|
-
409,
|
|
177
|
-
{ downloadRequired: true },
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
const previous = await state.release();
|
|
181
|
-
const kind = settings.download ? "download" : "online-change";
|
|
182
|
-
emit({ kind: "progress", message: `Starting the ${kind}` });
|
|
183
|
-
await ensureGateway();
|
|
184
|
-
await warming;
|
|
185
|
-
// The previous server keeps running until the controller has the new program, so a
|
|
186
|
-
// failed transfer leaves both as they were.
|
|
187
|
-
const receipt = await updateWarmController(
|
|
188
|
-
config,
|
|
189
|
-
deployContainer,
|
|
190
|
-
kind,
|
|
247
|
+
(emit) =>
|
|
248
|
+
runner.runNow(
|
|
249
|
+
upload,
|
|
191
250
|
{
|
|
251
|
+
git: settings.deployment?.git ?? null,
|
|
252
|
+
requestedBy: requester(identity, client),
|
|
253
|
+
mode: "apply",
|
|
254
|
+
download: settings.download,
|
|
255
|
+
credentials: settings.credentials,
|
|
192
256
|
initializeUser: settings.initializeUser,
|
|
193
|
-
|
|
194
|
-
onProgress: (message) => emit({ kind: "progress", message }),
|
|
257
|
+
warnings,
|
|
195
258
|
},
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
)
|
|
211
|
-
: undefined;
|
|
212
|
-
const deployed: Release = {
|
|
213
|
-
id: release.id,
|
|
214
|
-
deployedAt: new Date().toISOString(),
|
|
215
|
-
operation: kind,
|
|
216
|
-
controllerIds: receipt.controllerIds,
|
|
217
|
-
// A release without a server, or whose server failed, leaves the previous one running.
|
|
218
|
-
server:
|
|
219
|
-
serverFailure === undefined
|
|
220
|
-
? (serverPath ?? previous?.server)
|
|
221
|
-
: previous?.server,
|
|
222
|
-
};
|
|
223
|
-
await state.setRelease(deployed);
|
|
224
|
-
const debugManifest = join(release.directory, debugManifestFile);
|
|
225
|
-
const deployment: Deployment = {
|
|
226
|
-
at: deployed.deployedAt,
|
|
227
|
-
release: release.id,
|
|
228
|
-
...(existsSync(debugManifest) && {
|
|
229
|
-
build: (await loadManifest(debugManifest)).build.id,
|
|
230
|
-
}),
|
|
231
|
-
operation: kind,
|
|
232
|
-
credentials: settings.credentials ? "provided" : "stored",
|
|
233
|
-
...client,
|
|
234
|
-
};
|
|
235
|
-
await appendJsonl(state.paths.deployments, deployment);
|
|
236
|
-
context.debugging.restart();
|
|
237
|
-
if (serverFailure !== undefined) {
|
|
238
|
-
throw new Error(
|
|
239
|
-
`The controller runs release ${release.id}. ${serverFailure}`,
|
|
240
|
-
);
|
|
241
|
-
}
|
|
242
|
-
return deployed;
|
|
243
|
-
},
|
|
259
|
+
(line) => {
|
|
260
|
+
// The failure itself ends the stream as its error.
|
|
261
|
+
if (line.level === "error") {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
emit({
|
|
265
|
+
kind: "progress",
|
|
266
|
+
message:
|
|
267
|
+
line.level === "warning"
|
|
268
|
+
? `Warning: ${line.message}`
|
|
269
|
+
: line.message,
|
|
270
|
+
});
|
|
271
|
+
},
|
|
272
|
+
),
|
|
244
273
|
{ reportsRollback: clientTsplc.version !== undefined },
|
|
245
274
|
);
|
|
246
275
|
},
|
|
247
276
|
};
|
|
248
277
|
};
|
|
278
|
+
export type Deployments = Awaited<ReturnType<typeof createDeployments>>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { DeploymentRecord } from "../protocol";
|
|
3
|
+
import type { ServerSupervisor } from "../server-process";
|
|
4
|
+
|
|
5
|
+
type Health = NonNullable<DeploymentRecord["health"]>;
|
|
6
|
+
|
|
7
|
+
/** What tsPLC's server answers on its health route; anything else there leaves the PLC unknown. */
|
|
8
|
+
const serverHealthSchema = z.looseObject({ plcConnected: z.boolean() });
|
|
9
|
+
|
|
10
|
+
/** How long the server's health route may take to answer. */
|
|
11
|
+
const healthTimeoutMs = 5000;
|
|
12
|
+
|
|
13
|
+
const plcHealth = (body: unknown): Health["plc"] => {
|
|
14
|
+
const parsed = serverHealthSchema.safeParse(body);
|
|
15
|
+
if (!parsed.success) {
|
|
16
|
+
return "unknown";
|
|
17
|
+
}
|
|
18
|
+
return parsed.data.plcConnected ? "ok" : "failed";
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const serverHealth = async (
|
|
22
|
+
healthUrl: string | undefined,
|
|
23
|
+
server: ServerSupervisor,
|
|
24
|
+
): Promise<Pick<Health, "server" | "plc">> => {
|
|
25
|
+
if (!healthUrl) {
|
|
26
|
+
return {
|
|
27
|
+
server: server.status()?.running ? "ok" : "failed",
|
|
28
|
+
plc: "unknown",
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const response = await fetch(healthUrl, {
|
|
32
|
+
signal: AbortSignal.timeout(healthTimeoutMs),
|
|
33
|
+
}).catch(() => null);
|
|
34
|
+
if (!response?.ok) {
|
|
35
|
+
return { server: "failed", plc: "unknown" };
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
server: "ok",
|
|
39
|
+
plc: plcHealth(await response.json().catch(() => null)),
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const noServer: Pick<Health, "server" | "plc"> = {
|
|
44
|
+
server: "none",
|
|
45
|
+
plc: "unknown",
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The check after a deployment applied: the controller runs its application, the release's server
|
|
50
|
+
* answers, and the server reaches the PLC.
|
|
51
|
+
*/
|
|
52
|
+
export const checkHealth = async (options: {
|
|
53
|
+
/** The application's state in the controller's receipt. */
|
|
54
|
+
controllerState: string;
|
|
55
|
+
/** The release has a server, which the agent started. */
|
|
56
|
+
hasServer: boolean;
|
|
57
|
+
healthUrl: string | undefined;
|
|
58
|
+
server: ServerSupervisor;
|
|
59
|
+
}): Promise<Health> => {
|
|
60
|
+
const controller = options.controllerState === "run" ? "ok" : "failed";
|
|
61
|
+
const { server, plc } = options.hasServer
|
|
62
|
+
? await serverHealth(options.healthUrl, options.server)
|
|
63
|
+
: noServer;
|
|
64
|
+
const problems = [
|
|
65
|
+
controller === "failed" &&
|
|
66
|
+
`The controller's application is in state ${options.controllerState}`,
|
|
67
|
+
server === "failed" && "The server isn't running",
|
|
68
|
+
plc === "failed" && "The server isn't connected to the PLC",
|
|
69
|
+
].filter((problem) => typeof problem === "string");
|
|
70
|
+
return {
|
|
71
|
+
controller,
|
|
72
|
+
server,
|
|
73
|
+
plc,
|
|
74
|
+
detail: problems.length ? problems.join(". ") : null,
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** Whether a deployment with this health counts as ready; a server still connecting to the PLC doesn't fail it. */
|
|
79
|
+
export const healthy = (health: Health) =>
|
|
80
|
+
health.controller === "ok" && health.server !== "failed";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Release } from "../protocol";
|
|
2
|
+
import { pruneReleases } from "../releases";
|
|
3
|
+
import { activeDeployments, type DeploymentStore, isFinished } from "./store";
|
|
4
|
+
|
|
5
|
+
/** Every deployment created within this long is kept. */
|
|
6
|
+
export const retentionMs = 14 * 24 * 60 * 60 * 1000;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Deletes the deployments created before the retention window, except the running one, the last
|
|
10
|
+
* healthy one and any not yet finished, then every release that no kept deployment and not the
|
|
11
|
+
* deployed release names. The deployed release's server may be an earlier release's.
|
|
12
|
+
*/
|
|
13
|
+
export const pruneDeployments = async (options: {
|
|
14
|
+
store: DeploymentStore;
|
|
15
|
+
releases: string;
|
|
16
|
+
deployed: Release | null;
|
|
17
|
+
now: () => Date;
|
|
18
|
+
}) => {
|
|
19
|
+
const { store } = options;
|
|
20
|
+
const records = store.list();
|
|
21
|
+
const { current, lastGood } = activeDeployments(records);
|
|
22
|
+
const cutoff = options.now().getTime() - retentionMs;
|
|
23
|
+
const expired = records.filter(
|
|
24
|
+
(record) =>
|
|
25
|
+
Date.parse(record.createdAt) < cutoff &&
|
|
26
|
+
record.id !== current &&
|
|
27
|
+
record.id !== lastGood &&
|
|
28
|
+
isFinished(record),
|
|
29
|
+
);
|
|
30
|
+
for (const record of expired) {
|
|
31
|
+
await store.remove(record.id);
|
|
32
|
+
}
|
|
33
|
+
const kept = new Set(store.list().map((record) => record.release));
|
|
34
|
+
if (options.deployed) {
|
|
35
|
+
kept.add(options.deployed.id);
|
|
36
|
+
const server = options.deployed.server?.split("/")[0];
|
|
37
|
+
if (server) {
|
|
38
|
+
kept.add(server);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
deployments: expired.map((record) => record.id),
|
|
43
|
+
releases: await pruneReleases(options.releases, kept),
|
|
44
|
+
};
|
|
45
|
+
};
|