@tsplc/agent 0.2.20 → 0.3.0
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/README.md +2 -2
- package/dist/cli.js +367 -152
- package/dist/cli.js.map +16 -15
- package/dist/client.js +121 -20
- package/dist/client.js.map +6 -5
- package/dist/index.js +352 -144
- package/dist/index.js.map +14 -13
- package/package.json +16 -6
- package/src/agent.ts +94 -54
- package/src/cli.ts +12 -5
- package/src/client.ts +114 -23
- package/src/codesys.ts +23 -24
- package/src/debugging.ts +8 -2
- package/src/deploy.ts +141 -125
- package/src/index.ts +4 -4
- package/src/login.ts +8 -5
- package/src/operations.ts +11 -6
- package/src/protocol.ts +91 -4
- package/src/releases.ts +219 -11
- package/src/secrets.ts +3 -2
- package/src/server-process.ts +1 -1
- package/src/transfer.ts +94 -0
package/src/deploy.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
|
-
import { readdir, readFile, stat } from "node:fs/promises";
|
|
3
2
|
import { join } from "node:path";
|
|
4
|
-
import { setTimeout as delay } from "node:timers/promises";
|
|
5
3
|
import {
|
|
4
|
+
controllerBaseline,
|
|
6
5
|
findControllerLogin,
|
|
7
6
|
startWarmController,
|
|
8
7
|
updateWarmController,
|
|
8
|
+
workerScriptsDigest,
|
|
9
9
|
} from "@tsplc/build";
|
|
10
10
|
import { loadManifest } from "@tsplc/debug";
|
|
11
|
-
import {
|
|
11
|
+
import { debugManifestFile } from "@tsplc/debug/manifest";
|
|
12
12
|
import {
|
|
13
|
+
type CodesysSettings,
|
|
13
14
|
controllerConfig,
|
|
14
15
|
deployContainer,
|
|
15
16
|
ensureGateway,
|
|
@@ -20,61 +21,26 @@ import { AgentError, errorMessage } from "./errors";
|
|
|
20
21
|
import { appendJsonl } from "./logs";
|
|
21
22
|
import type { Operations } from "./operations";
|
|
22
23
|
import {
|
|
23
|
-
type AgentEvent,
|
|
24
24
|
type ClientIdentity,
|
|
25
|
+
type ClientRelease,
|
|
25
26
|
type Deployment,
|
|
26
|
-
deployOptionsSchema,
|
|
27
27
|
type Release,
|
|
28
|
+
tsplcVersion,
|
|
29
|
+
versionMismatch,
|
|
28
30
|
} from "./protocol";
|
|
29
|
-
import {
|
|
31
|
+
import { type DeployRequest, loadRelease, saveRelease } from "./releases";
|
|
30
32
|
import { controllerCredentials, readSecrets, withSecrets } from "./secrets";
|
|
31
33
|
import type { ServerSupervisor } from "./server-process";
|
|
32
34
|
import type { AgentState } from "./state";
|
|
33
35
|
|
|
34
|
-
/** Relays the newest controller job's progress log, which CODESYS appends to as it works. */
|
|
35
|
-
const followProgress = (
|
|
36
|
-
controller: string,
|
|
37
|
-
emit: (event: AgentEvent) => void,
|
|
38
|
-
) => {
|
|
39
|
-
const jobs = join(controller, ".codesys/controller/jobs");
|
|
40
|
-
const since = Date.now();
|
|
41
|
-
let following = true;
|
|
42
|
-
const done = (async () => {
|
|
43
|
-
let log: string | undefined;
|
|
44
|
-
let offset = 0;
|
|
45
|
-
while (following) {
|
|
46
|
-
await delay(2000);
|
|
47
|
-
if (!log && existsSync(jobs)) {
|
|
48
|
-
for (const job of await readdir(jobs)) {
|
|
49
|
-
const path = join(jobs, job, ".codesys/controller/progress.log");
|
|
50
|
-
if (existsSync(path) && (await stat(path)).mtimeMs >= since) {
|
|
51
|
-
log = path;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
if (!log) {
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
const text = await readFile(log, "utf8");
|
|
59
|
-
for (const line of text.slice(offset).split("\n").filter(Boolean)) {
|
|
60
|
-
emit({ kind: "progress", message: line.replace(/^\S+Z /, "") });
|
|
61
|
-
}
|
|
62
|
-
offset = text.length;
|
|
63
|
-
}
|
|
64
|
-
})();
|
|
65
|
-
return async () => {
|
|
66
|
-
following = false;
|
|
67
|
-
await done;
|
|
68
|
-
};
|
|
69
|
-
};
|
|
70
|
-
|
|
71
36
|
/**
|
|
72
|
-
* Deployments of uploaded releases:
|
|
73
|
-
*
|
|
37
|
+
* Deployments of uploaded releases: a download or online change through the CODESYS that stays
|
|
38
|
+
* running between them, then the release's server once the controller has its program.
|
|
74
39
|
*/
|
|
75
40
|
export const createDeployments = (context: {
|
|
76
41
|
state: AgentState;
|
|
77
42
|
operations: Operations;
|
|
43
|
+
codesys: CodesysSettings;
|
|
78
44
|
server: ServerSupervisor;
|
|
79
45
|
debugging: Debugging;
|
|
80
46
|
log: (message: string) => void;
|
|
@@ -91,7 +57,12 @@ export const createDeployments = (context: {
|
|
|
91
57
|
await ensureGateway();
|
|
92
58
|
const release = await loadRelease(state.paths.releases, deployed.id);
|
|
93
59
|
await startWarmController(
|
|
94
|
-
controllerConfig(
|
|
60
|
+
controllerConfig(
|
|
61
|
+
release,
|
|
62
|
+
target,
|
|
63
|
+
state.paths.controller,
|
|
64
|
+
context.codesys,
|
|
65
|
+
),
|
|
95
66
|
deployContainer,
|
|
96
67
|
);
|
|
97
68
|
})().catch((error: unknown) => {
|
|
@@ -99,11 +70,24 @@ export const createDeployments = (context: {
|
|
|
99
70
|
});
|
|
100
71
|
|
|
101
72
|
return {
|
|
102
|
-
deploy: async (
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
73
|
+
deploy: async (
|
|
74
|
+
/** Reads the upload once the client's version is checked, so a mismatch is reported before a large body is read. */
|
|
75
|
+
readRequest: () => Promise<DeployRequest>,
|
|
76
|
+
client: ClientIdentity,
|
|
77
|
+
clientTsplc: ClientRelease,
|
|
78
|
+
) => {
|
|
79
|
+
// A client of another version compiles for scripts this agent doesn't run. One from before
|
|
80
|
+
// version checks reports none and is only warned, so it can still deploy.
|
|
81
|
+
if (
|
|
82
|
+
clientTsplc.version !== undefined &&
|
|
83
|
+
clientTsplc.version !== tsplcVersion
|
|
84
|
+
) {
|
|
85
|
+
throw new AgentError(
|
|
86
|
+
versionMismatch({ agent: tsplcVersion, client: clientTsplc.version }),
|
|
87
|
+
409,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
const { settings, upload } = await readRequest();
|
|
107
91
|
const target = await state.requireTarget();
|
|
108
92
|
if (await findControllerLogin(loginContainer)) {
|
|
109
93
|
throw new AgentError(
|
|
@@ -113,81 +97,113 @@ export const createDeployments = (context: {
|
|
|
113
97
|
}
|
|
114
98
|
const credentials = await controllerCredentials(settings.credentials);
|
|
115
99
|
const secrets = await readSecrets(upload.manifest.secrets);
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
100
|
+
const scripts = await workerScriptsDigest();
|
|
101
|
+
return context.operations.stream(
|
|
102
|
+
"a deployment",
|
|
103
|
+
async (emit) => {
|
|
104
|
+
if (clientTsplc.version === undefined) {
|
|
105
|
+
emit({
|
|
106
|
+
kind: "progress",
|
|
107
|
+
message: `Warning: the deploying tsPLC predates version checks, while this plant agent runs tsPLC ${tsplcVersion}; update the client to ${tsplcVersion}`,
|
|
108
|
+
});
|
|
109
|
+
} else if (
|
|
110
|
+
clientTsplc.scripts !== undefined &&
|
|
111
|
+
clientTsplc.scripts !== scripts
|
|
112
|
+
) {
|
|
113
|
+
emit({
|
|
114
|
+
kind: "progress",
|
|
115
|
+
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`,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
const release = await saveRelease(state.paths.releases, upload);
|
|
119
|
+
const baseline = settings.download
|
|
120
|
+
? "valid"
|
|
121
|
+
: await controllerBaseline(state.paths.controller);
|
|
122
|
+
if (baseline !== "valid") {
|
|
123
|
+
throw new AgentError(
|
|
124
|
+
baseline === "missing"
|
|
125
|
+
? "The controller has no application downloaded by this agent; only a full download can deploy"
|
|
126
|
+
: "This agent's record of the controller's download can't be read; only a full download can deploy",
|
|
127
|
+
409,
|
|
128
|
+
{ downloadRequired: true },
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
const config = await withSecrets(
|
|
132
|
+
controllerConfig(
|
|
133
|
+
release,
|
|
134
|
+
target,
|
|
135
|
+
state.paths.controller,
|
|
136
|
+
context.codesys,
|
|
137
|
+
),
|
|
138
|
+
secrets,
|
|
139
|
+
state.paths.sources,
|
|
128
140
|
);
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
141
|
+
const previous = await state.release();
|
|
142
|
+
const kind = settings.download ? "download" : "online-change";
|
|
143
|
+
emit({ kind: "progress", message: `Starting the ${kind}` });
|
|
144
|
+
await ensureGateway();
|
|
145
|
+
await warming;
|
|
146
|
+
// The previous server keeps running until the controller has the new program, so a
|
|
147
|
+
// failed transfer leaves both as they were.
|
|
148
|
+
const receipt = await updateWarmController(
|
|
149
|
+
config,
|
|
150
|
+
deployContainer,
|
|
151
|
+
kind,
|
|
152
|
+
{
|
|
153
|
+
initializeUser: settings.initializeUser,
|
|
154
|
+
credentials,
|
|
155
|
+
onProgress: (message) => emit({ kind: "progress", message }),
|
|
156
|
+
},
|
|
157
|
+
);
|
|
158
|
+
// Server paths are relative to the releases directory.
|
|
159
|
+
const serverPath =
|
|
160
|
+
release.manifest.server &&
|
|
161
|
+
`${release.id}/${release.manifest.server}`;
|
|
162
|
+
if (serverPath) {
|
|
163
|
+
emit({ kind: "progress", message: "Starting the new server" });
|
|
164
|
+
}
|
|
165
|
+
// A server that fails to start is replaced by the previous one again, and the
|
|
166
|
+
// controller keeps the new program, which is recorded before reporting the failure.
|
|
167
|
+
const serverFailure = serverPath
|
|
168
|
+
? await server.replace(join(state.paths.releases, serverPath)).then(
|
|
169
|
+
() => undefined,
|
|
170
|
+
(error: unknown) => errorMessage(error),
|
|
171
|
+
)
|
|
172
|
+
: undefined;
|
|
173
|
+
const deployed: Release = {
|
|
174
|
+
id: release.id,
|
|
175
|
+
deployedAt: new Date().toISOString(),
|
|
176
|
+
operation: kind,
|
|
177
|
+
controllerIds: receipt.controllerIds,
|
|
178
|
+
// A release without a server, or whose server failed, leaves the previous one running.
|
|
179
|
+
server:
|
|
180
|
+
serverFailure === undefined
|
|
181
|
+
? (serverPath ?? previous?.server)
|
|
182
|
+
: previous?.server,
|
|
183
|
+
};
|
|
184
|
+
await state.setRelease(deployed);
|
|
185
|
+
const debugManifest = join(release.directory, debugManifestFile);
|
|
186
|
+
const deployment: Deployment = {
|
|
187
|
+
at: deployed.deployedAt,
|
|
188
|
+
release: release.id,
|
|
189
|
+
...(existsSync(debugManifest) && {
|
|
190
|
+
build: (await loadManifest(debugManifest)).build.id,
|
|
191
|
+
}),
|
|
192
|
+
operation: kind,
|
|
193
|
+
credentials: settings.credentials ? "provided" : "stored",
|
|
194
|
+
...client,
|
|
195
|
+
};
|
|
196
|
+
await appendJsonl(state.paths.deployments, deployment);
|
|
197
|
+
context.debugging.restart();
|
|
198
|
+
if (serverFailure !== undefined) {
|
|
199
|
+
throw new Error(
|
|
200
|
+
`The controller runs release ${release.id}. ${serverFailure}`,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
return deployed;
|
|
204
|
+
},
|
|
205
|
+
{ reportsRollback: clientTsplc.version !== undefined },
|
|
206
|
+
);
|
|
191
207
|
},
|
|
192
208
|
};
|
|
193
209
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
export { type AgentOptions, createAgent } from "./agent.ts";
|
|
2
|
-
export { ask, askCredentials, askSecret } from "./terminal.ts";
|
|
3
2
|
export {
|
|
4
3
|
type AgentEvent,
|
|
5
4
|
type AgentStatus,
|
|
6
5
|
type Audit,
|
|
7
6
|
auditSchema,
|
|
7
|
+
type Credentials,
|
|
8
8
|
clientHeaders,
|
|
9
9
|
controllerSchema,
|
|
10
|
-
type Credentials,
|
|
11
10
|
credentialsSchema,
|
|
12
11
|
type Deployment,
|
|
13
|
-
deploymentSchema,
|
|
14
12
|
type DeployOptions,
|
|
15
|
-
deployOptionsSchema,
|
|
16
13
|
type DiscoveredController,
|
|
14
|
+
deploymentSchema,
|
|
15
|
+
deployOptionsSchema,
|
|
17
16
|
eventSchema,
|
|
18
17
|
logoutSchema,
|
|
19
18
|
type NetworkInterface,
|
|
@@ -29,3 +28,4 @@ export {
|
|
|
29
28
|
type Variable,
|
|
30
29
|
variableSchema,
|
|
31
30
|
} from "./protocol.ts";
|
|
31
|
+
export { ask, askCredentials, askSecret } from "./terminal.ts";
|
package/src/login.ts
CHANGED
|
@@ -8,13 +8,13 @@ import {
|
|
|
8
8
|
} from "@tsplc/build";
|
|
9
9
|
import { LoginUnavailable, noLogin } from "@tsplc/debug";
|
|
10
10
|
import {
|
|
11
|
+
type CodesysSettings,
|
|
11
12
|
controllerConfig,
|
|
13
|
+
credentialEnvironment,
|
|
12
14
|
ensureGateway,
|
|
13
15
|
loginContainer,
|
|
14
|
-
withCredentials,
|
|
15
16
|
} from "./codesys";
|
|
16
17
|
import { AgentError, errorMessage } from "./errors";
|
|
17
|
-
import { controllerCredentials } from "./secrets";
|
|
18
18
|
import { appendJsonl } from "./logs";
|
|
19
19
|
import type { Operations } from "./operations";
|
|
20
20
|
import type {
|
|
@@ -23,6 +23,7 @@ import type {
|
|
|
23
23
|
ClientIdentity,
|
|
24
24
|
Credentials,
|
|
25
25
|
} from "./protocol";
|
|
26
|
+
import { controllerCredentials } from "./secrets";
|
|
26
27
|
import type { AgentState } from "./state";
|
|
27
28
|
|
|
28
29
|
/** A login's state comes back within a second; one that takes this long waits on a hung worker. */
|
|
@@ -60,6 +61,7 @@ const auditOf = (command: LoginCommand) => {
|
|
|
60
61
|
export const createLogin = (context: {
|
|
61
62
|
state: AgentState;
|
|
62
63
|
operations: Operations;
|
|
64
|
+
codesys: CodesysSettings;
|
|
63
65
|
/** Called with each audited command once it is in the audit log. */
|
|
64
66
|
onAudit: (entry: Audit) => Promise<void>;
|
|
65
67
|
log: (message: string) => void;
|
|
@@ -134,6 +136,7 @@ export const createLogin = (context: {
|
|
|
134
136
|
await state.requireRelease(),
|
|
135
137
|
await state.requireTarget(),
|
|
136
138
|
state.paths.controller,
|
|
139
|
+
context.codesys,
|
|
137
140
|
);
|
|
138
141
|
return operations.stream("a login", async (emit) => {
|
|
139
142
|
if (await findControllerLogin(loginContainer)) {
|
|
@@ -144,9 +147,9 @@ export const createLogin = (context: {
|
|
|
144
147
|
message: "Logging in; CODESYS takes minutes to start",
|
|
145
148
|
});
|
|
146
149
|
await ensureGateway();
|
|
147
|
-
const login = await
|
|
148
|
-
|
|
149
|
-
);
|
|
150
|
+
const login = await openControllerLogin(config, loginContainer, {
|
|
151
|
+
environment: credentialEnvironment(credentials),
|
|
152
|
+
});
|
|
150
153
|
return send(login, { command: "state" });
|
|
151
154
|
});
|
|
152
155
|
},
|
package/src/operations.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { requiresDownload, transferRollback } from "@tsplc/build";
|
|
2
2
|
import { AgentError, errorMessage } from "./errors";
|
|
3
3
|
import type { AgentEvent } from "./protocol";
|
|
4
4
|
|
|
@@ -31,10 +31,14 @@ export const createOperations = (log: (message: string) => void) => {
|
|
|
31
31
|
busy = undefined;
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Streams one exclusive operation's events, then its result, as newline-delimited JSON. A
|
|
36
|
+
* failure names its `rollback` only to a client that `reportsRollback`.
|
|
37
|
+
*/
|
|
35
38
|
stream: (
|
|
36
39
|
operation: Operation,
|
|
37
40
|
run: (emit: (event: AgentEvent) => void) => Promise<unknown>,
|
|
41
|
+
{ reportsRollback = false }: { reportsRollback?: boolean } = {},
|
|
38
42
|
) => {
|
|
39
43
|
requireIdle();
|
|
40
44
|
busy = operation;
|
|
@@ -59,13 +63,14 @@ export const createOperations = (log: (message: string) => void) => {
|
|
|
59
63
|
log(
|
|
60
64
|
`${operation} failed: ${error instanceof Error ? error.stack : String(error)}`,
|
|
61
65
|
);
|
|
66
|
+
const rollback = reportsRollback
|
|
67
|
+
? transferRollback(error)
|
|
68
|
+
: undefined;
|
|
62
69
|
emit({
|
|
63
70
|
kind: "error",
|
|
64
71
|
message: errorMessage(error),
|
|
65
|
-
downloadRequired:
|
|
66
|
-
|
|
67
|
-
(error instanceof Error &&
|
|
68
|
-
error.message.includes(explicitDownload)),
|
|
72
|
+
downloadRequired: requiresDownload(error),
|
|
73
|
+
...(rollback && { rollback }),
|
|
69
74
|
});
|
|
70
75
|
} finally {
|
|
71
76
|
clearInterval(heartbeat);
|
package/src/protocol.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
import { rollbackSchema } from "@tsplc/build";
|
|
1
2
|
import { tsplcConfigSchema } from "@tsplc/config";
|
|
2
|
-
import { tcpPort } from "@tsplc/config/build";
|
|
3
|
+
import { releaseConfigSchema, tcpPort } from "@tsplc/config/build";
|
|
3
4
|
import { z } from "zod";
|
|
5
|
+
import { version } from "../package.json";
|
|
6
|
+
|
|
7
|
+
/** The tsPLC version a client and the agent it deploys through must share. */
|
|
8
|
+
export const tsplcVersion = version;
|
|
4
9
|
|
|
5
10
|
/** The controller the agent deploys to, as its runtime's TCP block driver address. */
|
|
6
11
|
export const targetSchema = z.strictObject({
|
|
@@ -49,7 +54,16 @@ export type Variable = z.infer<typeof variableSchema>;
|
|
|
49
54
|
* relative to the release: `st/`, `template.project` and `libraries/`.
|
|
50
55
|
*/
|
|
51
56
|
export const releaseManifestSchema = z.strictObject({
|
|
52
|
-
|
|
57
|
+
// Checked against the owner's schema but kept as uploaded: the release id hashes the manifest.
|
|
58
|
+
config: z.record(z.string(), z.unknown()).superRefine((config, context) => {
|
|
59
|
+
const release = releaseConfigSchema.safeParse(config);
|
|
60
|
+
if (!release.success) {
|
|
61
|
+
context.addIssue({
|
|
62
|
+
code: "custom",
|
|
63
|
+
message: `Invalid release config: ${z.prettifyError(release.error)}`,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}),
|
|
53
67
|
variables: z.array(variableSchema),
|
|
54
68
|
/** The server executable's path in the release, when the project builds one. */
|
|
55
69
|
server: z.string().nonempty().optional(),
|
|
@@ -62,7 +76,7 @@ export const releaseManifestSchema = z.strictObject({
|
|
|
62
76
|
});
|
|
63
77
|
export type ReleaseManifest = z.infer<typeof releaseManifestSchema>;
|
|
64
78
|
|
|
65
|
-
/** How `tsplc deploy` compresses each
|
|
79
|
+
/** How `tsplc deploy` compresses each file of a multipart upload, the only form agents without `POST /releases/compare` accept. */
|
|
66
80
|
export const uploadEncoding = "gzip";
|
|
67
81
|
|
|
68
82
|
export const deployOptionsSchema = z
|
|
@@ -78,6 +92,52 @@ export const deployOptionsSchema = z
|
|
|
78
92
|
);
|
|
79
93
|
export type DeployOptions = z.infer<typeof deployOptionsSchema>;
|
|
80
94
|
|
|
95
|
+
/** How many buckets a release's files fall into for `POST /releases/compare`. */
|
|
96
|
+
export const releaseBuckets = 256;
|
|
97
|
+
|
|
98
|
+
/** A release id, or the start of a SHA-256 of content: 16 lowercase hex digits. */
|
|
99
|
+
const shortHex = z.string().regex(/^[a-f0-9]{16}$/);
|
|
100
|
+
|
|
101
|
+
/** `POST /releases/compare`: the digest of each bucket of the release about to be deployed. */
|
|
102
|
+
export const compareRequestSchema = z.strictObject({
|
|
103
|
+
buckets: z.array(shortHex).length(releaseBuckets),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Its answer: the stored release the upload can build on, and that release's files, by path and
|
|
108
|
+
* digest, in each bucket that differs. Without a base the whole release is uploaded.
|
|
109
|
+
*/
|
|
110
|
+
export const comparisonSchema = z.strictObject({
|
|
111
|
+
base: shortHex.nullable(),
|
|
112
|
+
changed: z.array(
|
|
113
|
+
z.strictObject({
|
|
114
|
+
bucket: z
|
|
115
|
+
.number()
|
|
116
|
+
.int()
|
|
117
|
+
.min(0)
|
|
118
|
+
.max(releaseBuckets - 1),
|
|
119
|
+
files: z.record(z.string(), shortHex),
|
|
120
|
+
}),
|
|
121
|
+
),
|
|
122
|
+
});
|
|
123
|
+
export type Comparison = z.infer<typeof comparisonSchema>;
|
|
124
|
+
|
|
125
|
+
/** A `POST /deploy` body in this form is one release upload rather than a multipart form. */
|
|
126
|
+
export const releaseUploadType = "application/vnd.tsplc.release+zstd";
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* A release upload's header. The release is `base`'s files without the `remove` paths, with the
|
|
130
|
+
* uploaded files added or replacing theirs; `buckets` are the digests it must then have.
|
|
131
|
+
*/
|
|
132
|
+
export const releaseUploadSchema = z.strictObject({
|
|
133
|
+
manifest: releaseManifestSchema,
|
|
134
|
+
options: deployOptionsSchema,
|
|
135
|
+
base: shortHex.nullable(),
|
|
136
|
+
buckets: compareRequestSchema.shape.buckets,
|
|
137
|
+
remove: z.array(z.string()),
|
|
138
|
+
});
|
|
139
|
+
export type ReleaseUpload = z.infer<typeof releaseUploadSchema>;
|
|
140
|
+
|
|
81
141
|
/** Long operations stream one of these per line. */
|
|
82
142
|
export const eventSchema = z.discriminatedUnion("kind", [
|
|
83
143
|
z.strictObject({ kind: z.literal("progress"), message: z.string() }),
|
|
@@ -87,6 +147,11 @@ export const eventSchema = z.discriminatedUnion("kind", [
|
|
|
87
147
|
message: z.string(),
|
|
88
148
|
/** The controller has no application from this agent yet, so only a full download can deploy. */
|
|
89
149
|
downloadRequired: z.boolean(),
|
|
150
|
+
/**
|
|
151
|
+
* What became of the controller after a download failed mid-transfer. Sent only to clients that
|
|
152
|
+
* send their version, since earlier clients reject an event with fields they don't know.
|
|
153
|
+
*/
|
|
154
|
+
rollback: rollbackSchema.optional(),
|
|
90
155
|
}),
|
|
91
156
|
]);
|
|
92
157
|
export type AgentEvent = z.infer<typeof eventSchema>;
|
|
@@ -127,12 +192,25 @@ export type AgentStatus = z.infer<typeof statusSchema>;
|
|
|
127
192
|
/** A logout's result: why the login couldn't log out, when its container had to be removed instead. */
|
|
128
193
|
export const logoutSchema = z.strictObject({ failure: z.string().optional() });
|
|
129
194
|
|
|
130
|
-
/** Who ran a command, as the client reports it in these headers. */
|
|
195
|
+
/** Who ran a command, and with which tsPLC, as the client reports it in these headers. */
|
|
131
196
|
export const clientHeaders = {
|
|
132
197
|
user: "x-tsplc-user",
|
|
133
198
|
host: "x-tsplc-host",
|
|
199
|
+
version: "x-tsplc-version",
|
|
200
|
+
/** The client's `workerScriptsDigest`, which tells a local build from a release of its version. */
|
|
201
|
+
scripts: "x-tsplc-scripts",
|
|
134
202
|
} as const;
|
|
135
203
|
|
|
204
|
+
/** The header each agent response names the agent's tsPLC version in; agents before 0.3.0 send none. */
|
|
205
|
+
export const agentVersionHeader = "x-tsplc-agent-version";
|
|
206
|
+
|
|
207
|
+
/** Why a client and an agent of different tsPLC versions don't deploy, and what to do about it. */
|
|
208
|
+
export const versionMismatch = (versions: {
|
|
209
|
+
agent: string | null;
|
|
210
|
+
client: string;
|
|
211
|
+
}) =>
|
|
212
|
+
`The plant agent runs ${versions.agent ? `tsPLC ${versions.agent}` : "a tsPLC older than 0.3.0"}, but this deployment comes from tsPLC ${versions.client}. A release is compiled by the agent's own CODESYS scripts, so the two must match: update the plant agent to ${versions.client} and run tsplc-agent setup there${versions.agent ? `, or deploy with tsPLC ${versions.agent}` : ""}`;
|
|
213
|
+
|
|
136
214
|
/** Who sent a request, as the client reports it. */
|
|
137
215
|
export const clientIdentity = (request: Request) => {
|
|
138
216
|
const header = (name: string) =>
|
|
@@ -141,6 +219,15 @@ export const clientIdentity = (request: Request) => {
|
|
|
141
219
|
};
|
|
142
220
|
export type ClientIdentity = ReturnType<typeof clientIdentity>;
|
|
143
221
|
|
|
222
|
+
/** The tsPLC a request's client runs, as it reports it; clients before 0.3.0 report none. */
|
|
223
|
+
export const clientRelease = (request: Request) => ({
|
|
224
|
+
version:
|
|
225
|
+
request.headers.get(clientHeaders.version)?.slice(0, 50) || undefined,
|
|
226
|
+
scripts:
|
|
227
|
+
request.headers.get(clientHeaders.scripts)?.slice(0, 100) || undefined,
|
|
228
|
+
});
|
|
229
|
+
export type ClientRelease = ReturnType<typeof clientRelease>;
|
|
230
|
+
|
|
144
231
|
/** A line of the agent's `deployments.jsonl`. */
|
|
145
232
|
export const deploymentSchema = z.strictObject({
|
|
146
233
|
at: z.iso.datetime(),
|