@tsplc/build 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 +1052 -836
- package/dist/cli.js.map +27 -24
- package/dist/images.js +165 -0
- package/dist/images.js.map +12 -0
- package/dist/index.js +888 -668
- package/dist/index.js.map +23 -20
- package/dist/libraries.js +4 -3
- package/dist/libraries.js.map +3 -3
- package/docker/Dockerfile.dockerignore +0 -4
- package/docker/Dockerfile.source +5 -0
- package/docker/build-image +30 -26
- package/docker/config.ts +1 -1
- package/docker/install/wine-signal/README.md +1 -1
- package/docker/runner.ts +9 -7
- package/package.json +24 -8
- package/python/build.py +231 -20
- package/python/codesys_online.py +121 -10
- package/python/worker.py +2 -0
- package/src/artifacts.ts +9 -7
- package/src/build.ts +9 -4
- package/src/cli.ts +11 -20
- package/src/controller-online.ts +145 -61
- package/src/controller-protocol.ts +48 -5
- package/src/controller-session.ts +386 -147
- package/src/device-tree.ts +1 -1
- package/src/docker-build.ts +80 -98
- package/src/docker-context.ts +56 -3
- package/src/docker-run.ts +56 -3
- package/src/engineering-settings.ts +3 -3
- package/src/host/codesys-host.ts +3 -3
- package/src/host/codesys-worker.ts +77 -3
- package/src/images.ts +105 -0
- package/src/import-request.ts +1 -0
- package/src/index.ts +30 -16
- package/src/io.ts +1 -1
- package/src/libraries.ts +3 -2
- package/src/project-operations.ts +48 -0
- package/src/run.ts +9 -6
- package/src/sources.ts +2 -2
- package/docker/install/wine-signal/check.sh +0 -34
- package/docker/install/wine-signal/tests/avx-registers.c +0 -53
- package/docker/install/wine-signal/tests/frame-and-handler.c +0 -192
- package/docker/install/wine-signal/tests/loader-i386.c +0 -8
- package/docker/install/wine-signal/tests/signal-return.c +0 -47
- package/python/test_build.py +0 -579
- package/python/test_codesys_login.py +0 -139
- package/python/test_codesys_online.py +0 -259
- package/python/test_installer_packages.py +0 -217
- package/python/test_installer_verification.py +0 -307
- package/python/test_worker.py +0 -154
|
@@ -1,28 +1,39 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { appendFileSync } from "node:fs";
|
|
2
3
|
import { cp, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
4
|
import { resolve } from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
controllerBudgets,
|
|
7
|
+
type ResolvedConfig,
|
|
8
|
+
sha256Digest,
|
|
9
|
+
} from "@tsplc/config/build";
|
|
4
10
|
import { z } from "zod";
|
|
5
|
-
import { sha256Digest, type ResolvedConfig } from "@tsplc/config/build";
|
|
6
11
|
import {
|
|
7
12
|
type ControllerCredentials,
|
|
8
13
|
controllerResultSchema,
|
|
9
14
|
controllerTarget,
|
|
15
|
+
DownloadRequiredError,
|
|
10
16
|
explicitDownload,
|
|
11
17
|
failureReason,
|
|
12
18
|
loginCommandSchema,
|
|
19
|
+
requiresDownload,
|
|
13
20
|
sameBaseline,
|
|
21
|
+
TransferFailedError,
|
|
14
22
|
tracebackFailure,
|
|
23
|
+
transferRollback,
|
|
15
24
|
warmOperationSchema,
|
|
16
25
|
} from "./controller-protocol";
|
|
26
|
+
import { writeDeviceTree } from "./device-tree";
|
|
17
27
|
import {
|
|
18
28
|
createCodesysWorker,
|
|
29
|
+
type PhasedBudget,
|
|
19
30
|
withWorkerLock,
|
|
20
31
|
withWorkerShutdown,
|
|
32
|
+
workerPhase,
|
|
21
33
|
writeJson,
|
|
22
34
|
} from "./host/codesys-worker";
|
|
23
|
-
import { importRequest } from "./import-request";
|
|
24
|
-
import { writeDeviceTree } from "./device-tree";
|
|
25
35
|
import { toWindowsPath } from "./host/windows-path";
|
|
36
|
+
import { importRequest } from "./import-request";
|
|
26
37
|
import { loadSources } from "./sources";
|
|
27
38
|
|
|
28
39
|
// Vendor code/data IDs are .NET Guid values derived from the compiler's data;
|
|
@@ -73,46 +84,118 @@ const projectFiles = async (
|
|
|
73
84
|
);
|
|
74
85
|
};
|
|
75
86
|
|
|
76
|
-
/**
|
|
77
|
-
const
|
|
87
|
+
/** A build output directory's controller state: download baseline generations, jobs and results. */
|
|
88
|
+
export const controllerDirectory = (outDir: string) =>
|
|
89
|
+
resolve(outDir, ".codesys/controller");
|
|
90
|
+
|
|
91
|
+
/** Operation `operationId`'s own output directory, which keeps its progress and descriptions. */
|
|
92
|
+
const jobOutput = (outDir: string, operationId: string) =>
|
|
93
|
+
resolve(controllerDirectory(outDir), "jobs", operationId);
|
|
94
|
+
|
|
95
|
+
/** The progress CODESYS reports for operation `operationId`, one timestamped line per step. */
|
|
96
|
+
export const jobProgressLog = (outDir: string, operationId: string) =>
|
|
97
|
+
resolve(controllerDirectory(jobOutput(outDir, operationId)), "progress.log");
|
|
98
|
+
|
|
99
|
+
const missingFile = (error: unknown) =>
|
|
100
|
+
error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Whether a download baseline exists for an online change: `missing` before the first download,
|
|
104
|
+
* `malformed` when its pointer or committed generation can't be read. A valid baseline can still
|
|
105
|
+
* belong to another image, template or target, which the operation itself refuses.
|
|
106
|
+
*/
|
|
107
|
+
export const controllerBaseline = async (
|
|
108
|
+
outDir: string,
|
|
109
|
+
): Promise<"valid" | "missing" | "malformed"> => {
|
|
110
|
+
const root = controllerDirectory(outDir);
|
|
111
|
+
const pointer = await readFile(resolve(root, "current.json"), "utf8").then(
|
|
112
|
+
(text) => ({ text }),
|
|
113
|
+
(error: unknown) => ({ error }),
|
|
114
|
+
);
|
|
115
|
+
if ("error" in pointer) {
|
|
116
|
+
return missingFile(pointer.error) ? "missing" : "malformed";
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
const { generation } = pointerSchema.parse(JSON.parse(pointer.text));
|
|
120
|
+
baselineSchema.parse(
|
|
121
|
+
JSON.parse(
|
|
122
|
+
await readFile(
|
|
123
|
+
resolve(root, "generations", generation, "manifest.json"),
|
|
124
|
+
"utf8",
|
|
125
|
+
),
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
return "valid";
|
|
129
|
+
} catch {
|
|
130
|
+
return "malformed";
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Where CODESYS opens and edits an operation's projects inside the builder container: its own
|
|
136
|
+
* filesystem. The output directory is a bind mount of the plant PC's disk, through which each of
|
|
137
|
+
* the many small writes CODESYS makes saving and compiling a large project is slow. Only a
|
|
138
|
+
* committed generation is copied out to it.
|
|
139
|
+
*/
|
|
140
|
+
export const containerScratch = "/tmp/tsplc-controller";
|
|
141
|
+
|
|
142
|
+
/** The committed generation, as its pointer and manifest name it. */
|
|
143
|
+
const readCommitted = async (root: string) => {
|
|
144
|
+
const pointer = pointerSchema.parse(
|
|
145
|
+
JSON.parse(await readFile(resolve(root, "current.json"), "utf8")),
|
|
146
|
+
);
|
|
147
|
+
const directory = resolve(root, "generations", pointer.generation);
|
|
148
|
+
const baseline = baselineSchema.parse(
|
|
149
|
+
JSON.parse(await readFile(resolve(directory, "manifest.json"), "utf8")),
|
|
150
|
+
);
|
|
151
|
+
if (baseline.generation !== pointer.generation) {
|
|
152
|
+
throw new DownloadRequiredError(
|
|
153
|
+
`Downloaded baseline pointer and manifest disagree; ${explicitDownload}`,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
return { baseline, project: resolve(directory, "project") };
|
|
157
|
+
};
|
|
158
|
+
type Committed = Awaited<ReturnType<typeof readCommitted>>;
|
|
159
|
+
|
|
160
|
+
/** The committed generation, once proven to match this image, template and target. */
|
|
161
|
+
const committedBaseline = async (
|
|
78
162
|
root: string,
|
|
79
163
|
expected: Pick<
|
|
80
164
|
z.infer<typeof baselineSchema>,
|
|
81
165
|
"imageId" | "templateHash" | "target"
|
|
82
166
|
>,
|
|
83
167
|
) => {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const previous = resolve(root, "generations", pointer.generation);
|
|
88
|
-
const baseline = baselineSchema.parse(
|
|
89
|
-
JSON.parse(await readFile(resolve(previous, "manifest.json"), "utf8")),
|
|
90
|
-
);
|
|
91
|
-
if (
|
|
92
|
-
baseline.generation !== pointer.generation ||
|
|
93
|
-
!sameBaseline(baseline, expected)
|
|
94
|
-
) {
|
|
95
|
-
throw new Error(
|
|
168
|
+
const committed = await readCommitted(root);
|
|
169
|
+
if (!sameBaseline(committed.baseline, expected)) {
|
|
170
|
+
throw new DownloadRequiredError(
|
|
96
171
|
`Downloaded baseline uses a different image, template, controller or device tree; ${explicitDownload}`,
|
|
97
172
|
);
|
|
98
173
|
}
|
|
99
|
-
|
|
174
|
+
return committed;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Copies the committed project to `destination`, where CODESYS may write to it, and proves the
|
|
179
|
+
* copy holds the files that were committed, which reads the output directory only once.
|
|
180
|
+
*/
|
|
181
|
+
const copyCommitted = async (committed: Committed, destination: string) => {
|
|
182
|
+
await rm(destination, { recursive: true, force: true });
|
|
183
|
+
await cp(committed.project, destination, { recursive: true });
|
|
100
184
|
if (
|
|
101
|
-
JSON.stringify(await projectFiles(
|
|
102
|
-
JSON.stringify(baseline.files)
|
|
185
|
+
JSON.stringify(await projectFiles(destination)) !==
|
|
186
|
+
JSON.stringify(committed.baseline.files)
|
|
103
187
|
) {
|
|
104
|
-
throw new
|
|
188
|
+
throw new DownloadRequiredError(
|
|
105
189
|
`Saved online project/reference files changed; ${explicitDownload}`,
|
|
106
190
|
);
|
|
107
191
|
}
|
|
108
|
-
return { baseline, project };
|
|
109
192
|
};
|
|
110
193
|
|
|
111
194
|
/** A CODESYS worker that handles the `online` request written to `requestDirectory` when `run`. */
|
|
112
195
|
interface OnlineWorker {
|
|
113
196
|
requestDirectory: string;
|
|
114
|
-
run: () => Promise<unknown>;
|
|
115
|
-
/** A
|
|
197
|
+
run: (budget: PhasedBudget) => Promise<unknown>;
|
|
198
|
+
/** A verified copy of baseline generation `baseline` in the scratch directory, which the worker has open. */
|
|
116
199
|
prepared?: { baseline: string; generation: string };
|
|
117
200
|
}
|
|
118
201
|
|
|
@@ -120,24 +203,27 @@ interface OnlineWorker {
|
|
|
120
203
|
const oneShotWorker = (config: ResolvedConfig): OnlineWorker => {
|
|
121
204
|
const worker = createCodesysWorker(config);
|
|
122
205
|
return {
|
|
123
|
-
requestDirectory:
|
|
124
|
-
run: () =>
|
|
206
|
+
requestDirectory: controllerDirectory(config.outDir),
|
|
207
|
+
run: (budget) =>
|
|
125
208
|
withWorkerShutdown(config, worker, () =>
|
|
126
209
|
withWorkerLock(config, async () =>
|
|
127
|
-
worker.request(
|
|
128
|
-
await worker.launch(),
|
|
129
|
-
{ action: "online" },
|
|
130
|
-
config.timeouts.build,
|
|
131
|
-
),
|
|
210
|
+
worker.request(await worker.launch(), { action: "online" }, budget),
|
|
132
211
|
),
|
|
133
212
|
),
|
|
134
213
|
};
|
|
135
214
|
};
|
|
136
215
|
|
|
216
|
+
const restoreResultSchema = z.object({
|
|
217
|
+
restored: z.boolean(),
|
|
218
|
+
controllerIds: z.strictObject({ codeId: guid, dataId: guid }),
|
|
219
|
+
state: z.string(),
|
|
220
|
+
});
|
|
221
|
+
|
|
137
222
|
/**
|
|
138
223
|
* Called inside the builder container, while its output-directory flock is held or, for a warm
|
|
139
224
|
* `worker`, while the agent runs nothing else. Without `credentials`, CODESYS reads the controller
|
|
140
|
-
* user from the environment variables `config.online` names.
|
|
225
|
+
* user from the environment variables `config.online` names. CODESYS works on projects in
|
|
226
|
+
* `scratch`; a download or online change the controller confirms is committed to the output directory.
|
|
141
227
|
*/
|
|
142
228
|
export const runControllerSession = async (
|
|
143
229
|
config: ResolvedConfig,
|
|
@@ -145,106 +231,221 @@ export const runControllerSession = async (
|
|
|
145
231
|
options: {
|
|
146
232
|
imageId: string;
|
|
147
233
|
operationId: string;
|
|
234
|
+
scratch: string;
|
|
148
235
|
initializeUser?: boolean;
|
|
149
236
|
read?: string[];
|
|
150
237
|
credentials?: ControllerCredentials;
|
|
151
238
|
},
|
|
152
239
|
worker?: OnlineWorker,
|
|
153
240
|
) => {
|
|
154
|
-
|
|
241
|
+
const settings = config.online;
|
|
242
|
+
if (!settings) {
|
|
155
243
|
throw new Error("Controller operations require online settings");
|
|
156
244
|
}
|
|
157
|
-
|
|
245
|
+
if (options.initializeUser && operation !== "download") {
|
|
246
|
+
throw new DownloadRequiredError(
|
|
247
|
+
"Initial user creation requires an explicit download",
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
const root = controllerDirectory(config.outDir);
|
|
158
251
|
await mkdir(root, { recursive: true });
|
|
159
252
|
const resultPath = resolve(root, "result.json");
|
|
160
253
|
await rm(resultPath, { force: true });
|
|
161
254
|
const templateHash = sha256(await readFile(config.template));
|
|
162
255
|
// A different image, template, controller or device tree needs a fresh download from the template.
|
|
163
|
-
const target = controllerTarget(config,
|
|
256
|
+
const target = controllerTarget(config, settings);
|
|
164
257
|
const expected = { imageId: options.imageId, templateHash, target };
|
|
165
|
-
// A download reuses the baseline when it still fits, and imports only what changed since.
|
|
166
|
-
const previous =
|
|
167
|
-
operation === "download"
|
|
168
|
-
? await readBaseline(root, expected).catch(() => undefined)
|
|
169
|
-
: await readBaseline(root, expected);
|
|
170
|
-
const baseline = previous?.baseline;
|
|
171
|
-
if (options.initializeUser && operation !== "download") {
|
|
172
|
-
throw new Error("Initial user creation requires an explicit download");
|
|
173
|
-
}
|
|
174
|
-
const opened = worker?.prepared;
|
|
175
|
-
const prepared =
|
|
176
|
-
operation !== "inspect" && opened?.baseline === baseline?.generation
|
|
177
|
-
? opened
|
|
178
|
-
: undefined;
|
|
179
|
-
const generation = prepared?.generation ?? randomUUID();
|
|
180
|
-
const candidate = resolve(root, "generations", generation);
|
|
181
|
-
const project = resolve(candidate, "project", "Application.project");
|
|
182
|
-
await mkdir(resolve(candidate, "project"), { recursive: true });
|
|
183
|
-
// Opens a copy of the baseline unless the worker opened one ahead; a download without a baseline saves the template here.
|
|
184
|
-
if (previous && !prepared) {
|
|
185
|
-
await cp(previous.project, resolve(candidate, "project"), {
|
|
186
|
-
recursive: true,
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
258
|
const jobConfig = {
|
|
190
259
|
...config,
|
|
191
|
-
outDir:
|
|
260
|
+
outDir: jobOutput(config.outDir, options.operationId),
|
|
192
261
|
};
|
|
193
262
|
// The job directory keeps this operation's progress and descriptions; the worker its mailbox.
|
|
194
|
-
const jobDirectory =
|
|
263
|
+
const jobDirectory = controllerDirectory(jobConfig.outDir);
|
|
264
|
+
await mkdir(jobDirectory, { recursive: true });
|
|
265
|
+
const progressLog = jobProgressLog(config.outDir, options.operationId);
|
|
266
|
+
// The host's steps join CODESYS's in the log the agent relays, in the same format.
|
|
267
|
+
const progress = (message: string) =>
|
|
268
|
+
appendFileSync(
|
|
269
|
+
progressLog,
|
|
270
|
+
`${new Date().toISOString().replace(/\.\d+Z$/, "Z")} ${message}\n`,
|
|
271
|
+
);
|
|
195
272
|
const online = worker ?? oneShotWorker(jobConfig);
|
|
196
273
|
const { requestDirectory } = online;
|
|
197
|
-
await mkdir(jobDirectory, { recursive: true });
|
|
198
274
|
await mkdir(requestDirectory, { recursive: true });
|
|
199
|
-
|
|
275
|
+
const opened = online.prepared;
|
|
276
|
+
progress("Reading the committed download");
|
|
277
|
+
// A download reuses the baseline when it still fits, and imports only what changed since.
|
|
278
|
+
const committed = await committedBaseline(root, expected).catch(
|
|
279
|
+
(error: unknown) => {
|
|
280
|
+
if (operation === "download") {
|
|
281
|
+
return undefined;
|
|
282
|
+
}
|
|
283
|
+
throw error;
|
|
284
|
+
},
|
|
285
|
+
);
|
|
286
|
+
const prepared =
|
|
287
|
+
operation !== "inspect" &&
|
|
288
|
+
committed &&
|
|
289
|
+
opened?.baseline === committed.baseline.generation
|
|
290
|
+
? opened
|
|
291
|
+
: undefined;
|
|
292
|
+
const generation = prepared?.generation ?? randomUUID();
|
|
293
|
+
const candidate = resolve(options.scratch, "generations", generation);
|
|
294
|
+
const candidateProject = resolve(candidate, "project");
|
|
295
|
+
const project = resolve(candidateProject, "Application.project");
|
|
200
296
|
// Only this container reads it; the worker deletes it once CODESYS has the user.
|
|
201
297
|
const credentialsFile =
|
|
202
298
|
options.credentials && `/tmp/tsplc-credentials-${options.operationId}.json`;
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
299
|
+
const writeCredentials = async () => {
|
|
300
|
+
if (credentialsFile) {
|
|
301
|
+
await writeFile(credentialsFile, JSON.stringify(options.credentials), {
|
|
302
|
+
mode: 0o600,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
const phase = resolve(requestDirectory, "phase");
|
|
307
|
+
const budget: PhasedBudget = {
|
|
308
|
+
file: phase,
|
|
309
|
+
...controllerBudgets(config.timeouts),
|
|
310
|
+
overdue: progress,
|
|
311
|
+
};
|
|
312
|
+
const windowsPath = (path: string) =>
|
|
313
|
+
toWindowsPath(path, config.codesys.wine);
|
|
314
|
+
/** Runs one request in the worker and returns the result it wrote. */
|
|
315
|
+
const runRequest = async (request: Record<string, unknown>) => {
|
|
316
|
+
await rm(resolve(requestDirectory, "result.json"), { force: true });
|
|
317
|
+
await rm(phase, { force: true });
|
|
318
|
+
await writeCredentials();
|
|
319
|
+
await writeFile(
|
|
320
|
+
resolve(requestDirectory, "request.json"),
|
|
321
|
+
JSON.stringify(
|
|
322
|
+
{
|
|
323
|
+
...request,
|
|
324
|
+
online: settings,
|
|
325
|
+
progress: await windowsPath(progressLog),
|
|
326
|
+
phase: await windowsPath(phase),
|
|
327
|
+
...(credentialsFile && {
|
|
328
|
+
credentialsFile: await windowsPath(credentialsFile),
|
|
329
|
+
}),
|
|
330
|
+
},
|
|
331
|
+
null,
|
|
332
|
+
2,
|
|
237
333
|
),
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
334
|
+
);
|
|
335
|
+
await online.run(budget);
|
|
336
|
+
return JSON.parse(
|
|
337
|
+
await readFile(resolve(requestDirectory, "result.json"), "utf8"),
|
|
338
|
+
);
|
|
339
|
+
};
|
|
340
|
+
/**
|
|
341
|
+
* After a download failed mid-transfer, the controller may hold neither application. Downloads
|
|
342
|
+
* the committed one again unless the controller still runs it, and describes the outcome. One
|
|
343
|
+
* made with another builder image or template is restored too, as the first download after
|
|
344
|
+
* updating tsPLC needs; one made for another controller or device tree never is.
|
|
345
|
+
*/
|
|
346
|
+
const rollBack = async (failure: unknown) => {
|
|
347
|
+
const reason = `The download failed during the transfer: ${failureReason(failure)}`;
|
|
348
|
+
const previous = await readCommitted(root).then(
|
|
349
|
+
(committed) =>
|
|
350
|
+
JSON.stringify(committed.baseline.target) === JSON.stringify(target)
|
|
351
|
+
? committed
|
|
352
|
+
: undefined,
|
|
353
|
+
() => undefined,
|
|
354
|
+
);
|
|
355
|
+
if (!previous) {
|
|
356
|
+
return new TransferFailedError(
|
|
357
|
+
`${reason}. This agent has committed no download to this controller to restore, so the controller may hold no application: deploy again with a full download`,
|
|
358
|
+
"unavailable",
|
|
359
|
+
{ cause: failure },
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
const restore = resolve(options.scratch, "restore", options.operationId);
|
|
363
|
+
try {
|
|
364
|
+
progress(
|
|
365
|
+
`${reason}. Restoring the last committed download (generation ${previous.baseline.generation})`,
|
|
366
|
+
);
|
|
367
|
+
await copyCommitted(previous, resolve(restore, "project"));
|
|
368
|
+
const restored = restoreResultSchema.parse(
|
|
369
|
+
await runRequest({
|
|
370
|
+
operation: "restore",
|
|
371
|
+
project: await windowsPath(
|
|
372
|
+
resolve(restore, "project", "Application.project"),
|
|
373
|
+
),
|
|
374
|
+
baseline: previous.baseline,
|
|
375
|
+
}),
|
|
376
|
+
);
|
|
377
|
+
if (!restored.restored) {
|
|
378
|
+
return new TransferFailedError(
|
|
379
|
+
`${reason}. The controller still runs the previous application`,
|
|
380
|
+
"intact",
|
|
381
|
+
{ cause: failure },
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
const changed =
|
|
385
|
+
JSON.stringify(restored.controllerIds) !==
|
|
386
|
+
JSON.stringify(previous.baseline.controllerIds);
|
|
387
|
+
return new TransferFailedError(
|
|
388
|
+
`${reason}. The controller runs the previous application again, downloaded from the last committed download (generation ${previous.baseline.generation})${changed ? "; its code/data identity changed, so the next deployment needs a full download" : ""}`,
|
|
389
|
+
"restored",
|
|
390
|
+
{ cause: failure },
|
|
391
|
+
);
|
|
392
|
+
} catch (restoreFailure) {
|
|
393
|
+
return new TransferFailedError(
|
|
394
|
+
`${reason}. Restoring the previous application failed too: ${failureReason(restoreFailure)}. The controller may hold no application: deploy again with a full download`,
|
|
395
|
+
"failed",
|
|
396
|
+
{ cause: failure },
|
|
397
|
+
);
|
|
398
|
+
} finally {
|
|
399
|
+
await rm(restore, { recursive: true, force: true });
|
|
400
|
+
}
|
|
241
401
|
};
|
|
242
|
-
await writeFile(
|
|
243
|
-
resolve(requestDirectory, "request.json"),
|
|
244
|
-
JSON.stringify(request, null, 2),
|
|
245
|
-
);
|
|
246
402
|
try {
|
|
247
|
-
|
|
403
|
+
// Opens a copy of the baseline unless the worker opened one ahead; a download without one saves the template here.
|
|
404
|
+
const previous =
|
|
405
|
+
committed && !prepared
|
|
406
|
+
? await (async () => {
|
|
407
|
+
progress("Copying the committed project");
|
|
408
|
+
return copyCommitted(committed, candidateProject).then(
|
|
409
|
+
() => committed,
|
|
410
|
+
async (error: unknown) => {
|
|
411
|
+
if (operation !== "download") {
|
|
412
|
+
throw error;
|
|
413
|
+
}
|
|
414
|
+
progress(
|
|
415
|
+
`${failureReason(error)}; downloading from the template`,
|
|
416
|
+
);
|
|
417
|
+
await rm(candidateProject, { recursive: true, force: true });
|
|
418
|
+
return undefined;
|
|
419
|
+
},
|
|
420
|
+
);
|
|
421
|
+
})()
|
|
422
|
+
: committed;
|
|
423
|
+
await mkdir(candidateProject, { recursive: true });
|
|
424
|
+
const baseline = previous?.baseline;
|
|
425
|
+
progress("Loading the program's sources");
|
|
426
|
+
const sources = operation === "inspect" ? null : await loadSources(config);
|
|
427
|
+
const request = {
|
|
428
|
+
libraryReferences: config.libraryReferences,
|
|
429
|
+
// Only a download without a baseline starts from the template; the others keep the downloaded project's tree.
|
|
430
|
+
...(operation === "download" &&
|
|
431
|
+
!previous && {
|
|
432
|
+
template: await windowsPath(config.template),
|
|
433
|
+
templateHash,
|
|
434
|
+
// Kept across jobs: generating a device tree takes minutes, and the same inputs make the same one.
|
|
435
|
+
caches: await windowsPath(root),
|
|
436
|
+
...(config.deviceTree && {
|
|
437
|
+
deviceTree: await writeDeviceTree(config.deviceTree, jobDirectory),
|
|
438
|
+
devices: await windowsPath(jobDirectory),
|
|
439
|
+
}),
|
|
440
|
+
}),
|
|
441
|
+
...(await importRequest(config, sources)),
|
|
442
|
+
operation,
|
|
443
|
+
initializeUser: options.initializeUser ?? false,
|
|
444
|
+
controller: config.controller,
|
|
445
|
+
project: await windowsPath(project),
|
|
446
|
+
baseline,
|
|
447
|
+
read: options.read ?? [],
|
|
448
|
+
};
|
|
248
449
|
const result = z
|
|
249
450
|
.object({
|
|
250
451
|
applicationGuid: guid,
|
|
@@ -254,9 +455,15 @@ export const runControllerSession = async (
|
|
|
254
455
|
values: z.record(z.string(), z.string()),
|
|
255
456
|
})
|
|
256
457
|
.parse(
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
458
|
+
await runRequest(request).catch(async (failure: unknown) => {
|
|
459
|
+
if (
|
|
460
|
+
operation !== "download" ||
|
|
461
|
+
(await workerPhase(phase)) !== "transfer"
|
|
462
|
+
) {
|
|
463
|
+
throw failure;
|
|
464
|
+
}
|
|
465
|
+
throw await rollBack(failure);
|
|
466
|
+
}),
|
|
260
467
|
);
|
|
261
468
|
const receipt = controllerResultSchema.parse({
|
|
262
469
|
...result,
|
|
@@ -270,10 +477,20 @@ export const runControllerSession = async (
|
|
|
270
477
|
generation: operation === "inspect" ? baseline?.generation : generation,
|
|
271
478
|
});
|
|
272
479
|
if (operation !== "inspect") {
|
|
273
|
-
const
|
|
480
|
+
const committedDirectory = resolve(root, "generations", generation);
|
|
481
|
+
/** Keeps the candidate in the output directory, where it outlives the container. */
|
|
482
|
+
const retain = async (message: string) => {
|
|
483
|
+
await cp(candidateProject, resolve(committedDirectory, "project"), {
|
|
484
|
+
recursive: true,
|
|
485
|
+
});
|
|
486
|
+
return new Error(
|
|
487
|
+
`${message}; candidate is retained for recovery in ${committedDirectory}`,
|
|
488
|
+
);
|
|
489
|
+
};
|
|
490
|
+
const files = await projectFiles(candidateProject);
|
|
274
491
|
if (!Object.keys(files).some((name) => name.endsWith(".compileinfo"))) {
|
|
275
|
-
throw
|
|
276
|
-
"CODESYS acknowledged the operation but no downloaded compiler reference was saved
|
|
492
|
+
throw await retain(
|
|
493
|
+
"CODESYS acknowledged the operation but no downloaded compiler reference was saved",
|
|
277
494
|
);
|
|
278
495
|
}
|
|
279
496
|
const bootIds = Object.keys(files).filter((name) =>
|
|
@@ -293,15 +510,20 @@ export const runControllerSession = async (
|
|
|
293
510
|
!files[bootReference] ||
|
|
294
511
|
files[bootReference] === sha256(new Uint8Array()) ||
|
|
295
512
|
files[bootReference] !== files[downloadedReference] ||
|
|
296
|
-
!(
|
|
297
|
-
|
|
298
|
-
)
|
|
513
|
+
!(await readFile(resolve(candidateProject, bootIds[0] ?? ""))).equals(
|
|
514
|
+
expectedBootIds,
|
|
515
|
+
)
|
|
299
516
|
) {
|
|
300
|
-
throw
|
|
301
|
-
"CODESYS acknowledged the operation but matching boot reference was not saved
|
|
517
|
+
throw await retain(
|
|
518
|
+
"CODESYS acknowledged the operation but matching boot reference was not saved",
|
|
302
519
|
);
|
|
303
520
|
}
|
|
304
|
-
|
|
521
|
+
progress("Committing the downloaded project");
|
|
522
|
+
await rm(committedDirectory, { recursive: true, force: true });
|
|
523
|
+
await cp(candidateProject, resolve(committedDirectory, "project"), {
|
|
524
|
+
recursive: true,
|
|
525
|
+
});
|
|
526
|
+
await writeJson(resolve(committedDirectory, "manifest.json"), {
|
|
305
527
|
...receipt,
|
|
306
528
|
objects: sources?.objects,
|
|
307
529
|
files,
|
|
@@ -319,16 +541,14 @@ export const runControllerSession = async (
|
|
|
319
541
|
if (credentialsFile) {
|
|
320
542
|
await rm(credentialsFile, { force: true });
|
|
321
543
|
}
|
|
322
|
-
//
|
|
544
|
+
// CODESYS closed the candidate, and an opened project this operation didn't use.
|
|
545
|
+
await rm(candidate, { recursive: true, force: true });
|
|
323
546
|
if (opened && opened !== prepared) {
|
|
324
|
-
await rm(resolve(
|
|
547
|
+
await rm(resolve(options.scratch, "generations", opened.generation), {
|
|
325
548
|
recursive: true,
|
|
326
549
|
force: true,
|
|
327
550
|
});
|
|
328
551
|
}
|
|
329
|
-
if (operation === "inspect") {
|
|
330
|
-
await rm(candidate, { recursive: true, force: true });
|
|
331
|
-
}
|
|
332
552
|
}
|
|
333
553
|
};
|
|
334
554
|
|
|
@@ -343,16 +563,17 @@ export const serveControllerLogin = async (
|
|
|
343
563
|
if (!config.online) {
|
|
344
564
|
throw new Error("Controller logins require online settings");
|
|
345
565
|
}
|
|
346
|
-
const root =
|
|
347
|
-
const
|
|
566
|
+
const root = controllerDirectory(config.outDir);
|
|
567
|
+
const committed = await committedBaseline(root, {
|
|
348
568
|
imageId: options.imageId,
|
|
349
569
|
templateHash: sha256(await readFile(config.template)),
|
|
350
570
|
target: controllerTarget(config, config.online),
|
|
351
571
|
});
|
|
572
|
+
const { baseline } = committed;
|
|
352
573
|
const jobConfig = { ...config, outDir: resolve(root, "login") };
|
|
353
574
|
await rm(jobConfig.outDir, { recursive: true, force: true });
|
|
354
575
|
// CODESYS writes into an open project, and the committed baseline must not change.
|
|
355
|
-
await
|
|
576
|
+
await copyCommitted(committed, resolve(jobConfig.outDir, "project"));
|
|
356
577
|
const worker = createCodesysWorker(jobConfig);
|
|
357
578
|
await withWorkerShutdown(jobConfig, worker, () =>
|
|
358
579
|
withWorkerLock(jobConfig, async () => {
|
|
@@ -432,34 +653,42 @@ export const serveWarmController = async (
|
|
|
432
653
|
) => {
|
|
433
654
|
const warmConfig = {
|
|
434
655
|
...config,
|
|
435
|
-
outDir: resolve(config.outDir, "
|
|
656
|
+
outDir: resolve(controllerDirectory(config.outDir), "warm"),
|
|
436
657
|
};
|
|
437
658
|
// One container owns this directory at a time; a stopped one can leave its worker session behind.
|
|
438
659
|
await rm(warmConfig.outDir, { recursive: true, force: true });
|
|
660
|
+
await rm(containerScratch, { recursive: true, force: true });
|
|
439
661
|
const worker = createCodesysWorker(warmConfig);
|
|
440
|
-
|
|
441
|
-
const
|
|
662
|
+
// CODESYS can die mid-operation; the next request, such as restoring after a failed transfer, starts it again.
|
|
663
|
+
const session = { current: await worker.launch() };
|
|
664
|
+
const running = async () => {
|
|
665
|
+
if (!worker.alive(session.current)) {
|
|
666
|
+
session.current = await worker.launch();
|
|
667
|
+
}
|
|
668
|
+
return session.current;
|
|
669
|
+
};
|
|
670
|
+
const root = controllerDirectory(config.outDir);
|
|
442
671
|
const online: OnlineWorker = {
|
|
443
|
-
requestDirectory:
|
|
444
|
-
run: () =>
|
|
445
|
-
worker.request(
|
|
672
|
+
requestDirectory: controllerDirectory(warmConfig.outDir),
|
|
673
|
+
run: async (budget) =>
|
|
674
|
+
worker.request(await running(), { action: "online" }, budget),
|
|
446
675
|
};
|
|
447
|
-
/** Opens a copy of the committed baseline, so the next
|
|
676
|
+
/** Opens a verified copy of the committed baseline, so the next operation needn't copy and open one. */
|
|
448
677
|
const prepare = async () => {
|
|
449
|
-
const
|
|
450
|
-
|
|
451
|
-
);
|
|
452
|
-
if (!pointer) {
|
|
678
|
+
const committed = await readCommitted(root).catch(() => undefined);
|
|
679
|
+
if (!committed) {
|
|
453
680
|
return;
|
|
454
681
|
}
|
|
455
|
-
const baseline = pointerSchema.parse(JSON.parse(pointer)).generation;
|
|
456
682
|
const generation = randomUUID();
|
|
457
|
-
const project = resolve(
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
683
|
+
const project = resolve(
|
|
684
|
+
containerScratch,
|
|
685
|
+
"generations",
|
|
686
|
+
generation,
|
|
687
|
+
"project",
|
|
688
|
+
);
|
|
689
|
+
await copyCommitted(committed, project);
|
|
461
690
|
await worker.request(
|
|
462
|
-
|
|
691
|
+
await running(),
|
|
463
692
|
{
|
|
464
693
|
action: "prepare",
|
|
465
694
|
project: await toWindowsPath(
|
|
@@ -469,7 +698,7 @@ export const serveWarmController = async (
|
|
|
469
698
|
},
|
|
470
699
|
config.timeouts.build,
|
|
471
700
|
);
|
|
472
|
-
online.prepared = { baseline, generation };
|
|
701
|
+
online.prepared = { baseline: committed.baseline.generation, generation };
|
|
473
702
|
};
|
|
474
703
|
// A failed prepare only costs the next operation the time to open the project itself.
|
|
475
704
|
const prepareNext = () =>
|
|
@@ -486,7 +715,7 @@ export const serveWarmController = async (
|
|
|
486
715
|
fetch: async (request) => {
|
|
487
716
|
if (request.method === "GET") {
|
|
488
717
|
return Response.json(
|
|
489
|
-
worker.alive(session)
|
|
718
|
+
worker.alive(session.current)
|
|
490
719
|
? { ok: true }
|
|
491
720
|
: { ok: false, error: "CODESYS exited" },
|
|
492
721
|
);
|
|
@@ -500,6 +729,7 @@ export const serveWarmController = async (
|
|
|
500
729
|
{
|
|
501
730
|
imageId: options.imageId,
|
|
502
731
|
operationId: body.operationId,
|
|
732
|
+
scratch: containerScratch,
|
|
503
733
|
initializeUser: body.initializeUser,
|
|
504
734
|
credentials: body.credentials,
|
|
505
735
|
},
|
|
@@ -507,17 +737,26 @@ export const serveWarmController = async (
|
|
|
507
737
|
).catch((error: unknown) => {
|
|
508
738
|
// CODESYS crashes generating an online change that moves a block holding a network
|
|
509
739
|
// client, such as a job with a service call. The controller keeps its running program.
|
|
510
|
-
if (
|
|
740
|
+
if (
|
|
741
|
+
body.operation !== "online-change" ||
|
|
742
|
+
worker.alive(session.current)
|
|
743
|
+
) {
|
|
511
744
|
throw error;
|
|
512
745
|
}
|
|
513
|
-
throw new
|
|
746
|
+
throw new DownloadRequiredError(
|
|
514
747
|
`CODESYS crashed on this online change, as it does when one moves a block that holds a network client; ${explicitDownload}`,
|
|
515
748
|
{ cause: error },
|
|
516
749
|
);
|
|
517
750
|
});
|
|
518
751
|
return Response.json({ ok: true, result });
|
|
519
752
|
} catch (error) {
|
|
520
|
-
|
|
753
|
+
const rollback = transferRollback(error);
|
|
754
|
+
return Response.json({
|
|
755
|
+
ok: false,
|
|
756
|
+
error: failureReason(error),
|
|
757
|
+
...(requiresDownload(error) && { downloadRequired: true }),
|
|
758
|
+
...(rollback && { rollback }),
|
|
759
|
+
});
|
|
521
760
|
} finally {
|
|
522
761
|
online.prepared = undefined;
|
|
523
762
|
preparing = prepareNext();
|