@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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsplc/agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "The tsPLC plant agent: deploys a program and its server to a controller on the plant network, and holds an engineering login for reads, writes and forces.",
|
|
@@ -31,21 +31,31 @@
|
|
|
31
31
|
"files": [
|
|
32
32
|
"dist",
|
|
33
33
|
"src",
|
|
34
|
-
"!**/*.test.ts"
|
|
34
|
+
"!**/*.test.ts",
|
|
35
|
+
"!**/*.fixtures.ts",
|
|
36
|
+
"!**/test_*.py",
|
|
37
|
+
"!**/__pycache__/**",
|
|
38
|
+
"!**/*.pyc",
|
|
39
|
+
"!**/tests/**"
|
|
35
40
|
],
|
|
36
41
|
"engines": {
|
|
37
42
|
"bun": ">=1.4.3"
|
|
38
43
|
},
|
|
39
44
|
"dependencies": {
|
|
40
|
-
"@tsplc/build": "0.
|
|
41
|
-
"@tsplc/config": "0.
|
|
42
|
-
"@tsplc/debug": "0.
|
|
43
|
-
"@tsplc/iec": "0.
|
|
45
|
+
"@tsplc/build": "0.3.0",
|
|
46
|
+
"@tsplc/config": "0.3.0",
|
|
47
|
+
"@tsplc/debug": "0.3.0",
|
|
48
|
+
"@tsplc/iec": "0.3.0",
|
|
44
49
|
"zod": "4.3.6"
|
|
45
50
|
},
|
|
46
51
|
"bin": {
|
|
47
52
|
"tsplc-agent": "./dist/cli.js"
|
|
48
53
|
},
|
|
54
|
+
"buildEntries": {
|
|
55
|
+
"./dist/cli.js": {
|
|
56
|
+
"source": "./src/cli.ts"
|
|
57
|
+
}
|
|
58
|
+
},
|
|
49
59
|
"publishConfig": {
|
|
50
60
|
"access": "public"
|
|
51
61
|
}
|
package/src/agent.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { timingSafeEqual } from "node:crypto";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { loginCommandSchema } from "@tsplc/build";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
+
import type { CodesysSettings } from "./codesys";
|
|
5
6
|
import { createDebugging } from "./debugging";
|
|
6
7
|
import { createDeployments } from "./deploy";
|
|
7
8
|
import { discoverControllers, networkInterfaces } from "./discovery";
|
|
@@ -10,11 +11,16 @@ import { createLogin } from "./login";
|
|
|
10
11
|
import { createOperations } from "./operations";
|
|
11
12
|
import {
|
|
12
13
|
type AgentStatus,
|
|
14
|
+
agentVersionHeader,
|
|
13
15
|
clientIdentity,
|
|
16
|
+
clientRelease,
|
|
17
|
+
compareRequestSchema,
|
|
14
18
|
credentialsSchema,
|
|
15
19
|
discoverSchema,
|
|
16
20
|
targetSchema,
|
|
21
|
+
tsplcVersion,
|
|
17
22
|
} from "./protocol";
|
|
23
|
+
import { createReleaseStore, readDeployRequest } from "./releases";
|
|
18
24
|
import { storedCredentials } from "./secrets";
|
|
19
25
|
import { createServerSupervisor } from "./server-process";
|
|
20
26
|
import { openState } from "./state";
|
|
@@ -33,6 +39,10 @@ export interface AgentOptions {
|
|
|
33
39
|
readToken?: string;
|
|
34
40
|
/** The deployed server's health route; without it a server that stays up 5 s counts as started. */
|
|
35
41
|
serverHealthUrl?: string;
|
|
42
|
+
/** Where a release's `serverTokenEnv` is read from; this process's environment by default. */
|
|
43
|
+
environment?: Readonly<Record<string, string | undefined>>;
|
|
44
|
+
/** Experimental: downloads without a baseline import their POUs in one PLCopenXML import. */
|
|
45
|
+
bulkImport?: boolean;
|
|
36
46
|
log: (message: string) => void;
|
|
37
47
|
}
|
|
38
48
|
|
|
@@ -49,9 +59,12 @@ export const createAgent = async (options: AgentOptions) => {
|
|
|
49
59
|
server.start(join(state.paths.releases, deployed.server));
|
|
50
60
|
}
|
|
51
61
|
const operations = createOperations(log);
|
|
62
|
+
const codesys: CodesysSettings = { bulkImport: options.bulkImport ?? false };
|
|
63
|
+
const releases = createReleaseStore(state.paths.releases);
|
|
52
64
|
const login = createLogin({
|
|
53
65
|
state,
|
|
54
66
|
operations,
|
|
67
|
+
codesys,
|
|
55
68
|
onAudit: (entry) => debugging.record(entry),
|
|
56
69
|
log,
|
|
57
70
|
});
|
|
@@ -59,11 +72,13 @@ export const createAgent = async (options: AgentOptions) => {
|
|
|
59
72
|
state,
|
|
60
73
|
readLogin: login.read,
|
|
61
74
|
serverHealthUrl: options.serverHealthUrl,
|
|
75
|
+
environment: options.environment ?? { ...process.env },
|
|
62
76
|
log,
|
|
63
77
|
});
|
|
64
78
|
const deployments = createDeployments({
|
|
65
79
|
state,
|
|
66
80
|
operations,
|
|
81
|
+
codesys,
|
|
67
82
|
server,
|
|
68
83
|
debugging,
|
|
69
84
|
log,
|
|
@@ -102,10 +117,25 @@ export const createAgent = async (options: AgentOptions) => {
|
|
|
102
117
|
}
|
|
103
118
|
case "GET /variables":
|
|
104
119
|
return Response.json((await state.requireRelease()).manifest.variables);
|
|
120
|
+
case "POST /releases/compare": {
|
|
121
|
+
const { buckets } = compareRequestSchema.parse(await request.json());
|
|
122
|
+
const comparison = await releases.compare(
|
|
123
|
+
buckets,
|
|
124
|
+
(await state.release())?.id ?? null,
|
|
125
|
+
);
|
|
126
|
+
// The answer lists every stored path in the differing buckets, which compresses tenfold; fetch inflates it.
|
|
127
|
+
return new Response(Bun.gzipSync(JSON.stringify(comparison)), {
|
|
128
|
+
headers: {
|
|
129
|
+
"Content-Type": "application/json",
|
|
130
|
+
"Content-Encoding": "gzip",
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
}
|
|
105
134
|
case "POST /deploy":
|
|
106
135
|
return deployments.deploy(
|
|
107
|
-
|
|
136
|
+
() => readDeployRequest(releases, request),
|
|
108
137
|
clientIdentity(request),
|
|
138
|
+
clientRelease(request),
|
|
109
139
|
);
|
|
110
140
|
case "POST /login":
|
|
111
141
|
return login.open(
|
|
@@ -125,63 +155,73 @@ export const createAgent = async (options: AgentOptions) => {
|
|
|
125
155
|
}
|
|
126
156
|
};
|
|
127
157
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
)
|
|
144
|
-
|
|
158
|
+
const respond = async (request: Request): Promise<Response> => {
|
|
159
|
+
const { pathname, search, searchParams } = new URL(request.url);
|
|
160
|
+
const debugPath = pathname.startsWith("/debug/");
|
|
161
|
+
// The UI's relative links need the trailing slash.
|
|
162
|
+
if (pathname === "/debug") {
|
|
163
|
+
return new Response(null, {
|
|
164
|
+
status: 308,
|
|
165
|
+
headers: { Location: `/debug/${search}` },
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
// The page and its script hold no data, and a browser loads the script without the token.
|
|
169
|
+
if (
|
|
170
|
+
request.method === "GET" &&
|
|
171
|
+
(pathname === "/debug/" || pathname === "/debug/ui.js")
|
|
172
|
+
) {
|
|
173
|
+
return debugging.handle(request, pathname.slice("/debug".length));
|
|
174
|
+
}
|
|
175
|
+
// EventSource can't send headers, so the debugger also takes `?token=`.
|
|
176
|
+
const presented =
|
|
177
|
+
/^Bearer (.+)$/.exec(request.headers.get("authorization") ?? "")?.[1] ??
|
|
178
|
+
(debugPath ? searchParams.get("token") : null) ??
|
|
179
|
+
"";
|
|
180
|
+
const full = matches(presented, options.token);
|
|
181
|
+
const read =
|
|
182
|
+
options.readToken !== undefined &&
|
|
183
|
+
matches(presented, options.readToken) &&
|
|
184
|
+
(debugPath || (request.method === "GET" && pathname === "/status"));
|
|
185
|
+
if (!full && !read) {
|
|
186
|
+
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
if (debugPath) {
|
|
190
|
+
return await debugging.handle(request, pathname.slice("/debug".length));
|
|
145
191
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
options.readToken !== undefined &&
|
|
154
|
-
matches(presented, options.readToken) &&
|
|
155
|
-
(debugPath || (request.method === "GET" && pathname === "/status"));
|
|
156
|
-
if (!full && !read) {
|
|
157
|
-
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
192
|
+
return await route(request);
|
|
193
|
+
} catch (error) {
|
|
194
|
+
if (error instanceof AgentError) {
|
|
195
|
+
return Response.json(
|
|
196
|
+
{ error: error.message },
|
|
197
|
+
{ status: error.status },
|
|
198
|
+
);
|
|
158
199
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
pathname.slice("/debug".length),
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
return await route(request);
|
|
167
|
-
} catch (error) {
|
|
168
|
-
if (error instanceof AgentError) {
|
|
169
|
-
return Response.json(
|
|
170
|
-
{ error: error.message },
|
|
171
|
-
{ status: error.status },
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
if (error instanceof z.ZodError) {
|
|
175
|
-
return Response.json(
|
|
176
|
-
{ error: z.prettifyError(error) },
|
|
177
|
-
{ status: 400 },
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
log(
|
|
181
|
-
`${request.method} ${request.url} failed: ${error instanceof Error ? error.stack : String(error)}`,
|
|
200
|
+
if (error instanceof z.ZodError) {
|
|
201
|
+
return Response.json(
|
|
202
|
+
{ error: z.prettifyError(error) },
|
|
203
|
+
{ status: 400 },
|
|
182
204
|
);
|
|
183
|
-
return Response.json({ error: errorMessage(error) }, { status: 500 });
|
|
184
205
|
}
|
|
206
|
+
log(
|
|
207
|
+
`${request.method} ${request.url} failed: ${error instanceof Error ? error.stack : String(error)}`,
|
|
208
|
+
);
|
|
209
|
+
return Response.json({ error: errorMessage(error) }, { status: 500 });
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
/** Answers `request`, naming the agent's tsPLC version so a client can refuse a mismatched agent. */
|
|
215
|
+
fetch: async (request: Request) => {
|
|
216
|
+
const response = await respond(request);
|
|
217
|
+
// A proxied debugger response's headers are immutable.
|
|
218
|
+
const headers = new Headers(response.headers);
|
|
219
|
+
headers.set(agentVersionHeader, tsplcVersion);
|
|
220
|
+
return new Response(response.body, {
|
|
221
|
+
status: response.status,
|
|
222
|
+
statusText: response.statusText,
|
|
223
|
+
headers,
|
|
224
|
+
});
|
|
185
225
|
},
|
|
186
226
|
/** Stops the debugger and the supervised server; a login container keeps running for the next agent. */
|
|
187
227
|
stop: async () => {
|
package/src/cli.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { homedir } from "node:os";
|
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
5
|
import { configureDocker, configureGateway } from "@tsplc/build";
|
|
6
|
+
import { tcpPort } from "@tsplc/config/build";
|
|
6
7
|
import { z } from "zod";
|
|
7
8
|
import { createAgent } from "./agent.ts";
|
|
8
9
|
import {
|
|
@@ -12,7 +13,7 @@ import {
|
|
|
12
13
|
} from "./secrets.ts";
|
|
13
14
|
import { askCredentials, askSecret } from "./terminal.ts";
|
|
14
15
|
|
|
15
|
-
const usage = `tsplc-agent setup|serve [--listen host:port] [--state directory] [--server-health url]
|
|
16
|
+
const usage = `tsplc-agent setup|serve [--listen host:port] [--state directory] [--server-health url] [--bulk-import]
|
|
16
17
|
tsplc-agent secrets set|delete <name> [--service ${agentService}]
|
|
17
18
|
tsplc-agent secrets set-controller
|
|
18
19
|
|
|
@@ -22,6 +23,8 @@ serve Serves deployments and logins. --listen is required: use the machine's T
|
|
|
22
23
|
at least 32 characters, such as one from \`openssl rand -hex 32\`. An optional
|
|
23
24
|
TSPLC_AGENT_READ_TOKEN, as long, only opens the debugger at /debug and GET /status.
|
|
24
25
|
Each comes from its environment variable, or else from \`secrets set\` under ${agentService}.
|
|
26
|
+
--bulk-import (experimental) brings a download without a baseline's POUs into CODESYS
|
|
27
|
+
in one PLCopenXML import, importing one at a time if CODESYS reports any problem.
|
|
25
28
|
secrets Stores a value a release's secrets name, typed without echo, in Bun.secrets under
|
|
26
29
|
--service, or deletes it. set-controller stores the controller user, which deployments
|
|
27
30
|
and logins then use when they send none. Run it as the user the agent runs as.`;
|
|
@@ -33,6 +36,7 @@ const { values, positionals } = parseArgs({
|
|
|
33
36
|
listen: { type: "string" },
|
|
34
37
|
state: { type: "string" },
|
|
35
38
|
"server-health": { type: "string" },
|
|
39
|
+
"bulk-import": { type: "boolean" },
|
|
36
40
|
service: { type: "string" },
|
|
37
41
|
},
|
|
38
42
|
});
|
|
@@ -48,9 +52,10 @@ if (command === "setup") {
|
|
|
48
52
|
await configureDocker();
|
|
49
53
|
await configureGateway();
|
|
50
54
|
} else if (command === "serve") {
|
|
51
|
-
const [, hostname,
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
const [, hostname, portText] = /^(.+):(\d+)$/.exec(values.listen ?? "") ?? [];
|
|
56
|
+
const port = tcpPort.safeParse(Number(portText));
|
|
57
|
+
if (!hostname || !port.success) {
|
|
58
|
+
throw new Error("--listen host:port is required, with a TCP port.");
|
|
54
59
|
}
|
|
55
60
|
const token = z
|
|
56
61
|
.string({
|
|
@@ -73,11 +78,13 @@ if (command === "setup") {
|
|
|
73
78
|
token,
|
|
74
79
|
readToken,
|
|
75
80
|
serverHealthUrl: values["server-health"],
|
|
81
|
+
bulkImport: values["bulk-import"] ?? false,
|
|
82
|
+
environment: process.env,
|
|
76
83
|
log: (message) => console.log(`${new Date().toISOString()} ${message}`),
|
|
77
84
|
});
|
|
78
85
|
const server = Bun.serve({
|
|
79
86
|
hostname,
|
|
80
|
-
port:
|
|
87
|
+
port: port.data,
|
|
81
88
|
fetch: agent.fetch,
|
|
82
89
|
// Deployments stream for minutes and upload a server executable.
|
|
83
90
|
idleTimeout: 0,
|
package/src/client.ts
CHANGED
|
@@ -1,31 +1,60 @@
|
|
|
1
1
|
import { hostname, userInfo } from "node:os";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
type LoginCommand,
|
|
4
|
+
loginResultSchema,
|
|
5
|
+
type Rollback,
|
|
6
|
+
workerScriptsDigest,
|
|
7
|
+
} from "@tsplc/build";
|
|
3
8
|
import { z } from "zod";
|
|
4
9
|
import {
|
|
10
|
+
agentVersionHeader,
|
|
11
|
+
type Comparison,
|
|
12
|
+
type Credentials,
|
|
5
13
|
clientHeaders,
|
|
14
|
+
comparisonSchema,
|
|
6
15
|
controllerSchema,
|
|
7
|
-
type Credentials,
|
|
8
16
|
type DeployOptions,
|
|
9
17
|
errorSchema,
|
|
10
18
|
eventSchema,
|
|
11
19
|
logoutSchema,
|
|
12
20
|
networkInterfaceSchema,
|
|
13
21
|
type ReleaseManifest,
|
|
22
|
+
type ReleaseUpload,
|
|
14
23
|
releaseSchema,
|
|
24
|
+
releaseUploadType,
|
|
15
25
|
statusSchema,
|
|
16
|
-
uploadEncoding,
|
|
17
26
|
type Target,
|
|
18
27
|
targetSchema,
|
|
28
|
+
tsplcVersion,
|
|
19
29
|
variableSchema,
|
|
30
|
+
versionMismatch,
|
|
20
31
|
} from "./protocol";
|
|
32
|
+
import {
|
|
33
|
+
bucketDigests,
|
|
34
|
+
encodeUpload,
|
|
35
|
+
fileBucket,
|
|
36
|
+
type ReleaseContent,
|
|
37
|
+
shortDigest,
|
|
38
|
+
} from "./transfer";
|
|
21
39
|
|
|
22
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* A failed agent operation; `downloadRequired` means only a full download can deploy, and
|
|
42
|
+
* `rollback` what became of the controller after a download failed mid-transfer.
|
|
43
|
+
*/
|
|
23
44
|
export class AgentOperationError extends Error {
|
|
45
|
+
readonly downloadRequired: boolean;
|
|
46
|
+
readonly rollback: Rollback | undefined;
|
|
47
|
+
|
|
24
48
|
constructor(
|
|
25
49
|
message: string,
|
|
26
|
-
|
|
50
|
+
{
|
|
51
|
+
downloadRequired = false,
|
|
52
|
+
rollback,
|
|
53
|
+
}: { downloadRequired?: boolean; rollback?: Rollback } = {},
|
|
27
54
|
) {
|
|
28
55
|
super(message);
|
|
56
|
+
this.downloadRequired = downloadRequired;
|
|
57
|
+
this.rollback = rollback;
|
|
29
58
|
}
|
|
30
59
|
}
|
|
31
60
|
|
|
@@ -37,17 +66,25 @@ export interface ReleaseFile {
|
|
|
37
66
|
|
|
38
67
|
/** Talks to a plant agent at `url`, such as its Tailscale name and port. */
|
|
39
68
|
export const createAgentClient = (options: { url: string; token: string }) => {
|
|
40
|
-
// The agent records who deployed and who changed the controller.
|
|
41
|
-
const identity =
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
69
|
+
// The agent records who deployed and who changed the controller, and refuses another tsPLC version.
|
|
70
|
+
const identity = workerScriptsDigest()
|
|
71
|
+
.then(
|
|
72
|
+
(scripts) => ({ [clientHeaders.scripts]: scripts }),
|
|
73
|
+
// The digest only tells local builds apart; an installation that can't read its scripts still deploys.
|
|
74
|
+
() => ({}),
|
|
75
|
+
)
|
|
76
|
+
.then((scripts) => ({
|
|
77
|
+
[clientHeaders.user]: userInfo().username,
|
|
78
|
+
[clientHeaders.host]: hostname(),
|
|
79
|
+
[clientHeaders.version]: tsplcVersion,
|
|
80
|
+
...scripts,
|
|
81
|
+
}));
|
|
45
82
|
const call = async (path: string, init: RequestInit = {}) => {
|
|
46
83
|
const response = await fetch(new URL(path, options.url), {
|
|
47
84
|
...init,
|
|
48
85
|
headers: {
|
|
49
86
|
...init.headers,
|
|
50
|
-
...identity,
|
|
87
|
+
...(await identity),
|
|
51
88
|
Authorization: `Bearer ${options.token}`,
|
|
52
89
|
},
|
|
53
90
|
}).catch((cause: unknown) => {
|
|
@@ -109,12 +146,19 @@ export const createAgentClient = (options: { url: string; token: string }) => {
|
|
|
109
146
|
} else if (event.kind === "done") {
|
|
110
147
|
return event.result;
|
|
111
148
|
} else {
|
|
112
|
-
throw new AgentOperationError(event.message,
|
|
149
|
+
throw new AgentOperationError(event.message, {
|
|
150
|
+
downloadRequired: event.downloadRequired,
|
|
151
|
+
rollback: event.rollback,
|
|
152
|
+
});
|
|
113
153
|
}
|
|
114
154
|
}
|
|
115
155
|
}
|
|
116
156
|
};
|
|
117
157
|
|
|
158
|
+
/** What the agent holds of a release with these bucket digests. */
|
|
159
|
+
const compare = (buckets: string[]) =>
|
|
160
|
+
send("POST", "/releases/compare", { buckets }, comparisonSchema);
|
|
161
|
+
|
|
118
162
|
return {
|
|
119
163
|
status: () => get("/status", statusSchema),
|
|
120
164
|
interfaces: () => get("/interfaces", z.array(networkInterfaceSchema)),
|
|
@@ -127,21 +171,36 @@ export const createAgentClient = (options: { url: string; token: string }) => {
|
|
|
127
171
|
settings: DeployOptions,
|
|
128
172
|
onProgress: (message: string) => void,
|
|
129
173
|
) => {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
new Blob([Bun.gzipSync(await file.content.bytes())]),
|
|
139
|
-
file.path,
|
|
174
|
+
// Before uploading: an agent of another version, including one from before version checks,
|
|
175
|
+
// would compile the release with scripts this client doesn't expect.
|
|
176
|
+
const agentVersion = (await call("/status")).headers.get(
|
|
177
|
+
agentVersionHeader,
|
|
178
|
+
);
|
|
179
|
+
if (agentVersion !== tsplcVersion) {
|
|
180
|
+
throw new AgentOperationError(
|
|
181
|
+
versionMismatch({ agent: agentVersion, client: tsplcVersion }),
|
|
140
182
|
);
|
|
141
183
|
}
|
|
184
|
+
const files = await Promise.all(
|
|
185
|
+
release.files.map(async ({ path, content }) => ({
|
|
186
|
+
path,
|
|
187
|
+
content: await content.bytes(),
|
|
188
|
+
})),
|
|
189
|
+
);
|
|
190
|
+
const digests = files.map(({ path, content }) => ({
|
|
191
|
+
path,
|
|
192
|
+
digest: shortDigest(content),
|
|
193
|
+
}));
|
|
194
|
+
const buckets = bucketDigests(digests);
|
|
195
|
+
const body = await releaseUpload(
|
|
196
|
+
{ manifest: release.manifest, options: settings, buckets },
|
|
197
|
+
files,
|
|
198
|
+
await compare(buckets),
|
|
199
|
+
onProgress,
|
|
200
|
+
);
|
|
142
201
|
return releaseSchema.parse(
|
|
143
202
|
await operation(
|
|
144
|
-
await call("/deploy", { method: "POST", body
|
|
203
|
+
await call("/deploy", { method: "POST", ...body }),
|
|
145
204
|
onProgress,
|
|
146
205
|
),
|
|
147
206
|
);
|
|
@@ -166,3 +225,35 @@ export const createAgentClient = (options: { url: string; token: string }) => {
|
|
|
166
225
|
};
|
|
167
226
|
};
|
|
168
227
|
export type AgentClient = ReturnType<typeof createAgentClient>;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* A release upload built on the release the agent compared it with: in each bucket that differs,
|
|
231
|
+
* the files the agent lacks and the paths to drop. Without a base, every file.
|
|
232
|
+
*/
|
|
233
|
+
const releaseUpload = async (
|
|
234
|
+
header: Pick<ReleaseUpload, "manifest" | "options" | "buckets">,
|
|
235
|
+
files: readonly ReleaseContent[],
|
|
236
|
+
{ base, changed }: Comparison,
|
|
237
|
+
onProgress: (message: string) => void,
|
|
238
|
+
) => {
|
|
239
|
+
const stored = new Map(changed.map(({ bucket, files }) => [bucket, files]));
|
|
240
|
+
const uploaded =
|
|
241
|
+
base === null
|
|
242
|
+
? files
|
|
243
|
+
: files.filter(({ path, content }) => {
|
|
244
|
+
const held = stored.get(fileBucket(path));
|
|
245
|
+
return held !== undefined && held[path] !== shortDigest(content);
|
|
246
|
+
});
|
|
247
|
+
const paths = new Set(files.map(({ path }) => path));
|
|
248
|
+
const remove = [...stored.values()].flatMap((held) =>
|
|
249
|
+
Object.keys(held).filter((path) => !paths.has(path)),
|
|
250
|
+
);
|
|
251
|
+
const body = await encodeUpload(
|
|
252
|
+
{ ...header, base, remove } satisfies ReleaseUpload,
|
|
253
|
+
uploaded,
|
|
254
|
+
);
|
|
255
|
+
onProgress(
|
|
256
|
+
`Uploading ${uploaded.length} of ${files.length} files (${Math.ceil(body.length / 1024)} KB)`,
|
|
257
|
+
);
|
|
258
|
+
return { headers: { "Content-Type": releaseUploadType }, body };
|
|
259
|
+
};
|
package/src/codesys.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
2
1
|
import { join } from "node:path";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { captureDocker, gatewayImage } from "@tsplc/build";
|
|
3
|
+
import { resolveConfig } from "@tsplc/config/build";
|
|
5
4
|
import type { Credentials, Target } from "./protocol";
|
|
6
5
|
import type { StoredRelease } from "./releases";
|
|
7
6
|
|
|
7
|
+
/** How the agent runs CODESYS, whichever release it deploys. */
|
|
8
|
+
export interface CodesysSettings {
|
|
9
|
+
/** Downloads without a baseline import their POUs in one PLCopenXML import; experimental. */
|
|
10
|
+
bulkImport: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
8
13
|
/** The gateway reaches controllers through this Docker network. */
|
|
9
14
|
const network = "tsplc-agent";
|
|
10
15
|
/**
|
|
@@ -23,10 +28,11 @@ const credentialNames = {
|
|
|
23
28
|
passwordEnv: "TSPLC_DEVICE_PASSWORD",
|
|
24
29
|
};
|
|
25
30
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
/** Network and gateway commands return at once; one still running after this has hung. */
|
|
32
|
+
const dockerTimeout = 60_000;
|
|
33
|
+
|
|
34
|
+
const docker = async (...args: string[]) =>
|
|
35
|
+
(await captureDocker(args, dockerTimeout)).stdout.trim();
|
|
30
36
|
|
|
31
37
|
const ensureNetwork = async (name: string, ...options: string[]) => {
|
|
32
38
|
if (
|
|
@@ -76,34 +82,27 @@ export const ensureGateway = async () => {
|
|
|
76
82
|
}
|
|
77
83
|
};
|
|
78
84
|
|
|
79
|
-
/**
|
|
80
|
-
export const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
)
|
|
84
|
-
process.env[credentialNames.usernameEnv] = credentials.username;
|
|
85
|
-
process.env[credentialNames.passwordEnv] = credentials.password;
|
|
86
|
-
try {
|
|
87
|
-
return await run();
|
|
88
|
-
} finally {
|
|
89
|
-
delete process.env[credentialNames.usernameEnv];
|
|
90
|
-
delete process.env[credentialNames.passwordEnv];
|
|
91
|
-
}
|
|
92
|
-
};
|
|
85
|
+
/** The controller user as the variables one operation's container passes to CODESYS. */
|
|
86
|
+
export const credentialEnvironment = (credentials: Credentials) => ({
|
|
87
|
+
[credentialNames.usernameEnv]: credentials.username,
|
|
88
|
+
[credentialNames.passwordEnv]: credentials.password,
|
|
89
|
+
});
|
|
93
90
|
|
|
94
91
|
/**
|
|
95
|
-
* The release's build config, aimed at `target` through the agent's gateway
|
|
96
|
-
* baseline in `controllerDirectory` outlives releases: an online
|
|
97
|
-
* last download.
|
|
92
|
+
* The release's build config, aimed at `target` through the agent's gateway, with the agent's own
|
|
93
|
+
* CODESYS settings. The download baseline in `controllerDirectory` outlives releases: an online
|
|
94
|
+
* change compares against the last download.
|
|
98
95
|
*/
|
|
99
96
|
export const controllerConfig = (
|
|
100
97
|
release: StoredRelease,
|
|
101
98
|
target: Target,
|
|
102
99
|
controllerDirectory: string,
|
|
100
|
+
settings: CodesysSettings,
|
|
103
101
|
) => ({
|
|
104
102
|
...resolveConfig(
|
|
105
103
|
{
|
|
106
104
|
...release.manifest.config,
|
|
105
|
+
...(settings.bulkImport && { bulkImport: true }),
|
|
107
106
|
online: {
|
|
108
107
|
network: codesysNetwork,
|
|
109
108
|
gateway,
|
package/src/debugging.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
loadManifest,
|
|
9
9
|
timelineEntries,
|
|
10
10
|
} from "@tsplc/debug";
|
|
11
|
+
import { debugManifestFile } from "@tsplc/debug/manifest";
|
|
11
12
|
import { errorMessage } from "./errors";
|
|
12
13
|
import { readJsonl } from "./logs";
|
|
13
14
|
import { type Audit, auditSchema, deploymentSchema } from "./protocol";
|
|
@@ -32,6 +33,7 @@ export const createDebugging = (context: {
|
|
|
32
33
|
state: AgentState;
|
|
33
34
|
readLogin: (expressions: string[]) => Promise<Record<string, string>>;
|
|
34
35
|
serverHealthUrl: string | undefined;
|
|
36
|
+
environment: Readonly<Record<string, string | undefined>>;
|
|
35
37
|
log: (message: string) => void;
|
|
36
38
|
}) => {
|
|
37
39
|
const { state, log } = context;
|
|
@@ -45,14 +47,18 @@ export const createDebugging = (context: {
|
|
|
45
47
|
state.paths.releases,
|
|
46
48
|
release.id,
|
|
47
49
|
);
|
|
48
|
-
const manifestFile = join(directory,
|
|
50
|
+
const manifestFile = join(directory, debugManifestFile);
|
|
49
51
|
if (!existsSync(manifestFile)) {
|
|
50
52
|
return null;
|
|
51
53
|
}
|
|
52
54
|
const manifest = await loadManifest(manifestFile);
|
|
53
55
|
const serverToken =
|
|
54
56
|
releaseManifest.serverTokenEnv &&
|
|
55
|
-
(await releaseVariable(
|
|
57
|
+
(await releaseVariable(
|
|
58
|
+
releaseManifest,
|
|
59
|
+
releaseManifest.serverTokenEnv,
|
|
60
|
+
context.environment,
|
|
61
|
+
));
|
|
56
62
|
const debug = await createDebugger({
|
|
57
63
|
manifest,
|
|
58
64
|
adapter: createLoginAdapter({
|