@tsplc/build 0.5.9 → 0.5.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +338 -324
- package/dist/cli.js.map +12 -12
- package/dist/index.js +478 -416
- package/dist/index.js.map +12 -12
- package/package.json +4 -4
- package/python/codesys_online.py +20 -10
- package/python/worker.py +1 -0
- package/src/controller-online.ts +117 -28
- package/src/controller-protocol.ts +35 -4
- package/src/controller-session.ts +117 -41
- package/src/host/codesys-worker.ts +4 -1
- package/src/index.ts +8 -0
- package/src/online-change-check.ts +10 -9
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"url": "git+https://github.com/rip-technologies/tsplc.git",
|
|
16
16
|
"directory": "packages/build"
|
|
17
17
|
},
|
|
18
|
-
"version": "0.5.
|
|
18
|
+
"version": "0.5.10",
|
|
19
19
|
"type": "module",
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
@@ -54,9 +54,9 @@
|
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"zod": "4.3.6",
|
|
57
|
-
"@tsplc/config": "0.5.
|
|
58
|
-
"@tsplc/typegen": "0.5.
|
|
59
|
-
"@tsplc/iec": "0.5.
|
|
57
|
+
"@tsplc/config": "0.5.10",
|
|
58
|
+
"@tsplc/typegen": "0.5.10",
|
|
59
|
+
"@tsplc/iec": "0.5.10"
|
|
60
60
|
},
|
|
61
61
|
"bin": {
|
|
62
62
|
"tsplc-build": "./dist/cli.js"
|
package/python/codesys_online.py
CHANGED
|
@@ -35,10 +35,12 @@ def fail_on_download_errors(engine):
|
|
|
35
35
|
raise CompilerError(errors)
|
|
36
36
|
|
|
37
37
|
|
|
38
|
-
def download_required(message):
|
|
39
|
-
"""A refusal only a full download resolves; the worker marks its reply so callers can offer one
|
|
38
|
+
def download_required(message, messages=()):
|
|
39
|
+
"""A refusal only a full download resolves; the worker marks its reply so callers can offer one,
|
|
40
|
+
and passes on `messages`, CODESYS's reasons in its own words."""
|
|
40
41
|
error = RuntimeError(message)
|
|
41
42
|
error.download_required = True
|
|
43
|
+
error.messages = list(messages)
|
|
42
44
|
return error
|
|
43
45
|
|
|
44
46
|
|
|
@@ -46,7 +48,8 @@ CANNOT_APPLY = 'CODESYS cannot apply this source as an online change; use an exp
|
|
|
46
48
|
|
|
47
49
|
|
|
48
50
|
def refusal_with_reasons(diagnostics):
|
|
49
|
-
|
|
51
|
+
reasons = [item['text'] for item in diagnostics]
|
|
52
|
+
return download_required('\n'.join([CANNOT_APPLY + ':'] + reasons), reasons)
|
|
50
53
|
|
|
51
54
|
|
|
52
55
|
def fail_on_online_change_errors(engine):
|
|
@@ -476,7 +479,8 @@ def run_online(engine, request, log, prepared=None, keep=None):
|
|
|
476
479
|
"""`log` is CODESYS's output, where it writes the prompts it answers. `prepared` is a project a
|
|
477
480
|
warm worker opened ahead; an online change of that project uses it. A request to finish later
|
|
478
481
|
replies once the controller runs the application, and hands `keep` the open project and its newly
|
|
479
|
-
generated tree, for `finish_online` to save.
|
|
482
|
+
generated tree, for `finish_online` to save. A review goes the online change's way up to CODESYS's
|
|
483
|
+
verdict, refusing as it would, and then logs out, having transferred nothing."""
|
|
480
484
|
progress = request_progress(request)
|
|
481
485
|
progress('Starting; processor count {0}'.format(Environment.ProcessorCount))
|
|
482
486
|
if request.get('finishLater') and keep is None:
|
|
@@ -486,6 +490,7 @@ def run_online(engine, request, log, prepared=None, keep=None):
|
|
|
486
490
|
prepared.close()
|
|
487
491
|
return restore_previous(engine, request, log, progress)
|
|
488
492
|
previous = request.get('baseline')
|
|
493
|
+
changes_online = request['operation'] in ('online-change', 'review')
|
|
489
494
|
project, complete_tree = open_operation_project(engine, request, prepared, progress)
|
|
490
495
|
application = project.active_application
|
|
491
496
|
online_app = None
|
|
@@ -510,7 +515,7 @@ def run_online(engine, request, log, prepared=None, keep=None):
|
|
|
510
515
|
online_app = engine.online.create_online_application(application)
|
|
511
516
|
core = core_application(online_app)
|
|
512
517
|
if not previous or str(application.guid) != previous['applicationGuid']:
|
|
513
|
-
if
|
|
518
|
+
if changes_online:
|
|
514
519
|
raise download_required('Online change requires the saved downloaded application; use an explicit download')
|
|
515
520
|
raise RuntimeError('Online change requires the saved downloaded application')
|
|
516
521
|
progress('Checking downloaded controller identity and state')
|
|
@@ -524,7 +529,7 @@ def run_online(engine, request, log, prepared=None, keep=None):
|
|
|
524
529
|
raise download_required('Downloaded compiler reference differs from the controller; use an explicit download')
|
|
525
530
|
progress('Downloaded compiler reference loaded')
|
|
526
531
|
state = controller_state(core)
|
|
527
|
-
if
|
|
532
|
+
if changes_online and state != 'run':
|
|
528
533
|
raise RuntimeError('Online change requires a running application')
|
|
529
534
|
if request['operation'] == 'inspect':
|
|
530
535
|
return {'applicationGuid': str(application.guid), 'controllerIds': observed,
|
|
@@ -577,10 +582,15 @@ def run_online(engine, request, log, prepared=None, keep=None):
|
|
|
577
582
|
if controller_ids(core) != previous['controllerIds']:
|
|
578
583
|
raise RuntimeError('Controller application changed before the online change')
|
|
579
584
|
expected = controller_ids(core)
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
585
|
+
changed = not application.is_uptodate
|
|
586
|
+
if changed and not application.is_online_change_possible:
|
|
587
|
+
raise download_required(CANNOT_APPLY + '. CODESYS gave no reason; the usual causes are a '
|
|
588
|
+
'changed task configuration, a variable whose type changed, or a changed device.')
|
|
589
|
+
if request['operation'] == 'review':
|
|
590
|
+
progress('CODESYS can apply this as an online change; the review transferred nothing')
|
|
591
|
+
core.Logout()
|
|
592
|
+
return {}
|
|
593
|
+
if changed:
|
|
584
594
|
progress('Generating online-change code')
|
|
585
595
|
generate_online_code(application)
|
|
586
596
|
fail_on_online_code_errors(engine)
|
package/python/worker.py
CHANGED
|
@@ -147,6 +147,7 @@ def serve(engine, directory):
|
|
|
147
147
|
response["diagnostics"] = error.diagnostics
|
|
148
148
|
if getattr(error, "download_required", False):
|
|
149
149
|
response["downloadRequired"] = True
|
|
150
|
+
response["messages"] = getattr(error, "messages", [])
|
|
150
151
|
write_json(engine, os.path.join(directory, command["id"] + ".json"), response)
|
|
151
152
|
if command["action"] == "stop":
|
|
152
153
|
return
|
package/src/controller-online.ts
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
imageIdSchema,
|
|
19
19
|
type LoginCommand,
|
|
20
20
|
loginResultSchema,
|
|
21
|
-
|
|
21
|
+
reviewReplySchema,
|
|
22
22
|
rollbackSchema,
|
|
23
23
|
sameBaseline,
|
|
24
24
|
TransferFailedError,
|
|
@@ -39,6 +39,10 @@ import {
|
|
|
39
39
|
writeOperationRequest,
|
|
40
40
|
} from "./docker-run";
|
|
41
41
|
import { builderImage } from "./images";
|
|
42
|
+
import {
|
|
43
|
+
type OnlineChangeFinding,
|
|
44
|
+
onlineChangeFindingSchema,
|
|
45
|
+
} from "./online-change-check";
|
|
42
46
|
|
|
43
47
|
/** Where an operation reads the controller user's environment variables from; this process's environment by default. */
|
|
44
48
|
export interface CredentialSource {
|
|
@@ -205,6 +209,8 @@ const loginReplySchema = z.discriminatedUnion("ok", [
|
|
|
205
209
|
ok: z.literal(false),
|
|
206
210
|
error: z.string(),
|
|
207
211
|
downloadRequired: z.literal(true).optional(),
|
|
212
|
+
findings: z.array(onlineChangeFindingSchema).optional(),
|
|
213
|
+
messages: z.array(z.string()).optional(),
|
|
208
214
|
rollback: rollbackSchema.optional(),
|
|
209
215
|
}),
|
|
210
216
|
]);
|
|
@@ -276,7 +282,10 @@ const callContainer = async (
|
|
|
276
282
|
}
|
|
277
283
|
// Builder images from before `downloadRequired` existed only end such a refusal with these words.
|
|
278
284
|
throw reply.downloadRequired || reply.error.endsWith(explicitDownload)
|
|
279
|
-
? new DownloadRequiredError(reply.error
|
|
285
|
+
? new DownloadRequiredError(reply.error, {
|
|
286
|
+
findings: reply.findings,
|
|
287
|
+
messages: reply.messages,
|
|
288
|
+
})
|
|
280
289
|
: new Error(reply.error);
|
|
281
290
|
}
|
|
282
291
|
return reply.result;
|
|
@@ -540,23 +549,26 @@ export const startWarmController = async (
|
|
|
540
549
|
);
|
|
541
550
|
};
|
|
542
551
|
|
|
552
|
+
/** What a warm operation takes beside its config: the controller user and where its progress goes. */
|
|
553
|
+
interface WarmOptions {
|
|
554
|
+
credentials?: ControllerCredentials;
|
|
555
|
+
/** Receives each step CODESYS reports while the operation runs. */
|
|
556
|
+
onProgress?: (message: string) => void;
|
|
557
|
+
}
|
|
558
|
+
|
|
543
559
|
/**
|
|
544
|
-
*
|
|
560
|
+
* Runs `operation` in warm builder container `name`, which keeps CODESYS running between
|
|
545
561
|
* operations so each skips starting the container, Wine and CODESYS. The first operation, or one
|
|
546
|
-
* with a new image, tsPLC installation, target or Docker settings, starts it. Credentials go to
|
|
547
|
-
* operation only.
|
|
562
|
+
* with a new image, tsPLC installation, target or Docker settings, starts it. Credentials go to
|
|
563
|
+
* CODESYS for this operation only. Returns the container's result, unchecked, and what it was run
|
|
564
|
+
* against; a refusal only a download resolves throws a `DownloadRequiredError` with its reasons.
|
|
548
565
|
*/
|
|
549
|
-
|
|
566
|
+
const warmOperation = async (
|
|
550
567
|
config: ResolvedConfig,
|
|
551
568
|
name: string,
|
|
552
|
-
operation:
|
|
553
|
-
options: {
|
|
554
|
-
|
|
555
|
-
credentials?: ControllerCredentials;
|
|
556
|
-
/** Receives each step CODESYS reports while this operation runs. */
|
|
557
|
-
onProgress?: (message: string) => void;
|
|
558
|
-
} = {},
|
|
559
|
-
): Promise<ControllerResult> => {
|
|
569
|
+
operation: z.input<typeof warmOperationSchema>["operation"],
|
|
570
|
+
options: WarmOptions & { initializeUser: boolean },
|
|
571
|
+
) => {
|
|
560
572
|
const report = options.onProgress ?? (() => {});
|
|
561
573
|
const connection = await controllerConnection(config);
|
|
562
574
|
const operationId = randomUUID();
|
|
@@ -586,7 +598,7 @@ export const updateWarmController = async (
|
|
|
586
598
|
operation,
|
|
587
599
|
operationId,
|
|
588
600
|
request,
|
|
589
|
-
initializeUser: options.initializeUser
|
|
601
|
+
initializeUser: options.initializeUser,
|
|
590
602
|
credentials: options.credentials,
|
|
591
603
|
} satisfies z.input<typeof warmOperationSchema>).catch((error: unknown) => {
|
|
592
604
|
const message = `${failureReason(error)}. Inspect ${diagnostics} for diagnostics`;
|
|
@@ -594,23 +606,100 @@ export const updateWarmController = async (
|
|
|
594
606
|
if (rollback) {
|
|
595
607
|
throw new TransferFailedError(message, rollback, { cause: error });
|
|
596
608
|
}
|
|
597
|
-
throw
|
|
598
|
-
? new DownloadRequiredError(message, {
|
|
609
|
+
throw error instanceof DownloadRequiredError
|
|
610
|
+
? new DownloadRequiredError(message, {
|
|
611
|
+
cause: error,
|
|
612
|
+
findings: error.findings,
|
|
613
|
+
messages: error.messages,
|
|
614
|
+
})
|
|
599
615
|
: new Error(message, { cause: error });
|
|
600
616
|
});
|
|
601
|
-
return
|
|
602
|
-
controllerResultSchema.parse(result),
|
|
603
|
-
{
|
|
604
|
-
operationId,
|
|
605
|
-
operation,
|
|
606
|
-
imageId: connection.imageId,
|
|
607
|
-
templateHash: connection.templateHash,
|
|
608
|
-
target: connection.target,
|
|
609
|
-
},
|
|
610
|
-
diagnostics,
|
|
611
|
-
);
|
|
617
|
+
return { result, connection, operationId, diagnostics };
|
|
612
618
|
} finally {
|
|
613
619
|
await stopFollowing();
|
|
614
620
|
await rm(directory, { recursive: true, force: true });
|
|
615
621
|
}
|
|
616
622
|
};
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* What an online change to `config`'s sources would do, found without applying it: tsPLC's check
|
|
626
|
+
* of the sources against the committed download, then CODESYS's own verdict from an incremental
|
|
627
|
+
* build against the controller's downloaded reference. The controller and its baseline are left
|
|
628
|
+
* as they were.
|
|
629
|
+
*/
|
|
630
|
+
export interface OnlineChangeReview {
|
|
631
|
+
/** Whether CODESYS can apply the sources as an online change. */
|
|
632
|
+
possible: boolean;
|
|
633
|
+
/** tsPLC's findings: what it refuses, and what the change restarts. */
|
|
634
|
+
findings: OnlineChangeFinding[];
|
|
635
|
+
/**
|
|
636
|
+
* Why it can't, beside the findings: CODESYS's reasons in its own words when it gave them, else
|
|
637
|
+
* the refusal itself, such as a committed download this change can't follow. Empty when it can,
|
|
638
|
+
* or when the findings say why.
|
|
639
|
+
*/
|
|
640
|
+
messages: string[];
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Reviews an online change in warm builder container `name`: everything `updateWarmController`
|
|
645
|
+
* does up to CODESYS's verdict, which logs in to the controller and out again. Nothing is
|
|
646
|
+
* transferred to the controller and no download baseline is committed.
|
|
647
|
+
*/
|
|
648
|
+
export const reviewWarmController = async (
|
|
649
|
+
config: ResolvedConfig,
|
|
650
|
+
name: string,
|
|
651
|
+
options: WarmOptions,
|
|
652
|
+
): Promise<OnlineChangeReview> => {
|
|
653
|
+
try {
|
|
654
|
+
const { result } = await warmOperation(config, name, "review", {
|
|
655
|
+
...options,
|
|
656
|
+
initializeUser: false,
|
|
657
|
+
});
|
|
658
|
+
return {
|
|
659
|
+
possible: true,
|
|
660
|
+
findings: reviewReplySchema.parse(result).findings,
|
|
661
|
+
messages: [],
|
|
662
|
+
};
|
|
663
|
+
} catch (error) {
|
|
664
|
+
if (!(error instanceof DownloadRequiredError)) {
|
|
665
|
+
throw error;
|
|
666
|
+
}
|
|
667
|
+
const explained =
|
|
668
|
+
error.messages.length > 0 ||
|
|
669
|
+
error.findings.some((finding) => finding.refuses);
|
|
670
|
+
return {
|
|
671
|
+
possible: false,
|
|
672
|
+
findings: error.findings,
|
|
673
|
+
messages: explained ? error.messages : [failureReason(error.cause)],
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* A download or online change in warm builder container `name` (see `warmOperation`). A download
|
|
680
|
+
* that fails mid-transfer throws a `TransferFailedError` naming its rollback.
|
|
681
|
+
*/
|
|
682
|
+
export const updateWarmController = async (
|
|
683
|
+
config: ResolvedConfig,
|
|
684
|
+
name: string,
|
|
685
|
+
operation: "download" | "online-change",
|
|
686
|
+
options: WarmOptions & { initializeUser?: boolean } = {},
|
|
687
|
+
): Promise<ControllerResult> => {
|
|
688
|
+
const { result, connection, operationId, diagnostics } = await warmOperation(
|
|
689
|
+
config,
|
|
690
|
+
name,
|
|
691
|
+
operation,
|
|
692
|
+
{ ...options, initializeUser: options.initializeUser ?? false },
|
|
693
|
+
);
|
|
694
|
+
return checkReceipt(
|
|
695
|
+
controllerResultSchema.parse(result),
|
|
696
|
+
{
|
|
697
|
+
operationId,
|
|
698
|
+
operation,
|
|
699
|
+
imageId: connection.imageId,
|
|
700
|
+
templateHash: connection.templateHash,
|
|
701
|
+
target: connection.target,
|
|
702
|
+
},
|
|
703
|
+
diagnostics,
|
|
704
|
+
);
|
|
705
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// What the host and the builder container exchange for controller operations: the receipt,
|
|
2
|
-
// the target a baseline is bound to, warm and login commands, and how failures
|
|
2
|
+
// the target a baseline is bound to, warm and login commands, a review's reply, and how failures
|
|
3
|
+
// are worded.
|
|
3
4
|
import {
|
|
4
5
|
controllerSchema,
|
|
5
6
|
type ResolvedConfig,
|
|
@@ -8,6 +9,10 @@ import {
|
|
|
8
9
|
} from "@tsplc/config/build";
|
|
9
10
|
import { z } from "zod";
|
|
10
11
|
import { deviceTreeStructure } from "./device-tree";
|
|
12
|
+
import {
|
|
13
|
+
type OnlineChangeFinding,
|
|
14
|
+
onlineChangeFindingSchema,
|
|
15
|
+
} from "./online-change-check";
|
|
11
16
|
|
|
12
17
|
export const imageIdSchema = z.string().regex(/^sha256:[a-f0-9]{64}$/);
|
|
13
18
|
const endpointSchema = z.strictObject({
|
|
@@ -46,9 +51,12 @@ export const controllerCredentialsSchema = z.strictObject({
|
|
|
46
51
|
});
|
|
47
52
|
export type ControllerCredentials = z.infer<typeof controllerCredentialsSchema>;
|
|
48
53
|
|
|
49
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* One operation for the warm builder container; `request` is the container path of its config. A
|
|
56
|
+
* `review` goes an online change's way up to CODESYS's verdict and changes nothing.
|
|
57
|
+
*/
|
|
50
58
|
export const warmOperationSchema = z.strictObject({
|
|
51
|
-
operation: z.enum(["download", "online-change"]),
|
|
59
|
+
operation: z.enum(["download", "online-change", "review"]),
|
|
52
60
|
operationId: z.uuid(),
|
|
53
61
|
request: z.string().nonempty(),
|
|
54
62
|
initializeUser: z.boolean(),
|
|
@@ -117,14 +125,37 @@ export const sameBaseline = (state: Baseline, expected: Baseline) =>
|
|
|
117
125
|
/** Ends the message of every refusal that only a full download resolves. */
|
|
118
126
|
export const explicitDownload = "use an explicit download";
|
|
119
127
|
|
|
128
|
+
/** Why only a full download will do: tsPLC's findings and CODESYS's reasons, in its own words. */
|
|
129
|
+
export interface DownloadReasons {
|
|
130
|
+
findings: OnlineChangeFinding[];
|
|
131
|
+
messages: string[];
|
|
132
|
+
}
|
|
133
|
+
|
|
120
134
|
/**
|
|
121
135
|
* A refusal that only a full download resolves, so callers can offer one instead of failing.
|
|
122
|
-
* Worker and container replies carry it as `downloadRequired: true
|
|
136
|
+
* Worker and container replies carry it as `downloadRequired: true`, with the reasons they have.
|
|
123
137
|
*/
|
|
124
138
|
export class DownloadRequiredError extends Error {
|
|
125
139
|
readonly downloadRequired = true;
|
|
140
|
+
/** tsPLC's findings about the change, refusals and restarts, when its check ran. */
|
|
141
|
+
readonly findings: OnlineChangeFinding[];
|
|
142
|
+
/** CODESYS's reasons, when it gave them. */
|
|
143
|
+
readonly messages: string[];
|
|
144
|
+
constructor(
|
|
145
|
+
message: string,
|
|
146
|
+
options: ErrorOptions & Partial<DownloadReasons> = {},
|
|
147
|
+
) {
|
|
148
|
+
super(message, options);
|
|
149
|
+
this.findings = options.findings ?? [];
|
|
150
|
+
this.messages = options.messages ?? [];
|
|
151
|
+
}
|
|
126
152
|
}
|
|
127
153
|
|
|
154
|
+
/** What a warm container replies once a review finds CODESYS can apply the change. */
|
|
155
|
+
export const reviewReplySchema = z.strictObject({
|
|
156
|
+
findings: z.array(onlineChangeFindingSchema),
|
|
157
|
+
});
|
|
158
|
+
|
|
128
159
|
/**
|
|
129
160
|
* What became of the controller after a download failed mid-transfer: it still ran the previous
|
|
130
161
|
* application (`intact`), runs it again from the last committed download (`restored`), or may hold
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
failureReason,
|
|
20
20
|
loginCommandSchema,
|
|
21
21
|
requiresDownload,
|
|
22
|
+
type reviewReplySchema,
|
|
22
23
|
sameBaseline,
|
|
23
24
|
TransferFailedError,
|
|
24
25
|
tracebackFailure,
|
|
@@ -41,6 +42,7 @@ import {
|
|
|
41
42
|
type CommittedObject,
|
|
42
43
|
committedObjectSchema,
|
|
43
44
|
describeFinding,
|
|
45
|
+
type OnlineChangeFinding,
|
|
44
46
|
onlineChangeFindings,
|
|
45
47
|
} from "./online-change-check";
|
|
46
48
|
import { loadSources } from "./sources";
|
|
@@ -216,7 +218,7 @@ const crashedOnlineChange = (cause: unknown) =>
|
|
|
216
218
|
|
|
217
219
|
/**
|
|
218
220
|
* Refuses, before CODESYS runs, an online change CODESYS would refuse or apply wrongly, naming
|
|
219
|
-
* the variable or task that causes it; reports the state the change restarts.
|
|
221
|
+
* the variable or task that causes it; reports the state the change restarts, and returns it.
|
|
220
222
|
*/
|
|
221
223
|
const checkOnlineChange = (
|
|
222
224
|
baseline: z.infer<typeof baselineSchema>,
|
|
@@ -233,11 +235,13 @@ const checkOnlineChange = (
|
|
|
233
235
|
if (refusals.length) {
|
|
234
236
|
throw new DownloadRequiredError(
|
|
235
237
|
`This change can't be applied as an online change; ${explicitDownload}:\n${refusals.map(describeFinding).join("\n")}`,
|
|
238
|
+
{ findings },
|
|
236
239
|
);
|
|
237
240
|
}
|
|
238
241
|
for (const finding of findings) {
|
|
239
242
|
progress(`Online change: ${describeFinding(finding)}`);
|
|
240
243
|
}
|
|
244
|
+
return findings;
|
|
241
245
|
};
|
|
242
246
|
|
|
243
247
|
/** A CODESYS worker that handles the `online` request written to `requestDirectory` when `run`. */
|
|
@@ -287,6 +291,9 @@ interface SessionOptions {
|
|
|
287
291
|
notices?: string[];
|
|
288
292
|
}
|
|
289
293
|
|
|
294
|
+
/** An operation that ends in a receipt, and a review, which ends in tsPLC's findings. */
|
|
295
|
+
type SessionOperation = "download" | "online-change" | "inspect" | "review";
|
|
296
|
+
|
|
290
297
|
/**
|
|
291
298
|
* Called inside the builder container, while its output-directory flock is held or, for a warm
|
|
292
299
|
* `worker`, while the agent runs nothing else. Without `credentials`, CODESYS reads the controller
|
|
@@ -295,30 +302,67 @@ interface SessionOptions {
|
|
|
295
302
|
*/
|
|
296
303
|
export const runControllerSession = async (
|
|
297
304
|
config: ResolvedConfig,
|
|
298
|
-
operation:
|
|
305
|
+
operation: Exclude<SessionOperation, "review">,
|
|
299
306
|
options: SessionOptions,
|
|
300
307
|
worker?: OnlineWorker,
|
|
301
308
|
) => {
|
|
302
|
-
const session = await
|
|
309
|
+
const session = await receiptSession(config, operation, options, worker);
|
|
303
310
|
await session.finish();
|
|
304
311
|
return session.receipt;
|
|
305
312
|
};
|
|
306
313
|
|
|
314
|
+
/** A session of an operation that ends in a receipt. */
|
|
315
|
+
const receiptSession = async (
|
|
316
|
+
config: ResolvedConfig,
|
|
317
|
+
operation: Exclude<SessionOperation, "review">,
|
|
318
|
+
options: SessionOptions,
|
|
319
|
+
worker?: OnlineWorker,
|
|
320
|
+
) => {
|
|
321
|
+
const session = await controllerSession(config, operation, options, worker);
|
|
322
|
+
if (!("receipt" in session)) {
|
|
323
|
+
throw new Error(`The ${operation} ended without a receipt`);
|
|
324
|
+
}
|
|
325
|
+
return session;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Called inside the builder container like `runControllerSession`: goes an online change's way up
|
|
330
|
+
* to CODESYS's verdict on it, and returns tsPLC's findings. Refuses as the online change would,
|
|
331
|
+
* with a `DownloadRequiredError`; transfers, saves and commits nothing.
|
|
332
|
+
*/
|
|
333
|
+
export const reviewControllerSession = async (
|
|
334
|
+
config: ResolvedConfig,
|
|
335
|
+
options: SessionOptions,
|
|
336
|
+
worker?: OnlineWorker,
|
|
337
|
+
): Promise<OnlineChangeFinding[]> => {
|
|
338
|
+
const session = await controllerSession(config, "review", options, worker);
|
|
339
|
+
if (!("findings" in session)) {
|
|
340
|
+
throw new Error("The review ended with a receipt");
|
|
341
|
+
}
|
|
342
|
+
return session.findings;
|
|
343
|
+
};
|
|
344
|
+
|
|
307
345
|
/**
|
|
308
346
|
* Runs an operation up to the receipt of a controller that runs its application. `finish` then
|
|
309
347
|
* commits the operation's project as the download baseline once CODESYS saved it, and cleans up;
|
|
310
|
-
* until it settles, nothing else may use the worker or the output directory's baseline.
|
|
348
|
+
* until it settles, nothing else may use the worker or the output directory's baseline. A review
|
|
349
|
+
* ends at CODESYS's verdict, with tsPLC's findings, and cleans up at once.
|
|
311
350
|
*/
|
|
312
351
|
const controllerSession = async (
|
|
313
352
|
config: ResolvedConfig,
|
|
314
|
-
operation:
|
|
353
|
+
operation: SessionOperation,
|
|
315
354
|
options: SessionOptions,
|
|
316
355
|
worker?: OnlineWorker,
|
|
317
|
-
)
|
|
356
|
+
): Promise<
|
|
357
|
+
| { receipt: ControllerResult; finish: () => Promise<void> }
|
|
358
|
+
| { findings: OnlineChangeFinding[] }
|
|
359
|
+
> => {
|
|
318
360
|
const settings = config.online;
|
|
319
361
|
if (!settings) {
|
|
320
362
|
throw new Error("Controller operations require online settings");
|
|
321
363
|
}
|
|
364
|
+
// A review checks everything an online change would, up to applying it.
|
|
365
|
+
const onlineChange = operation === "online-change" || operation === "review";
|
|
322
366
|
if (options.initializeUser && operation !== "download") {
|
|
323
367
|
throw new DownloadRequiredError(
|
|
324
368
|
"Initial user creation requires an explicit download",
|
|
@@ -370,10 +414,7 @@ const controllerSession = async (
|
|
|
370
414
|
);
|
|
371
415
|
const deviceSettings =
|
|
372
416
|
config.deviceTree && deviceTreeSettings(config.deviceTree);
|
|
373
|
-
if (
|
|
374
|
-
operation === "online-change" &&
|
|
375
|
-
committed?.baseline.deviceSettings !== deviceSettings
|
|
376
|
-
) {
|
|
417
|
+
if (onlineChange && committed?.baseline.deviceSettings !== deviceSettings) {
|
|
377
418
|
throw new DownloadRequiredError(
|
|
378
419
|
`The device tree's adapter addresses or RPIs changed since the download; ${explicitDownload}`,
|
|
379
420
|
);
|
|
@@ -606,9 +647,10 @@ const controllerSession = async (
|
|
|
606
647
|
const baseline = previous?.baseline;
|
|
607
648
|
progress("Loading the program's sources");
|
|
608
649
|
const sources = operation === "inspect" ? null : await loadSources(config);
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
650
|
+
const findings =
|
|
651
|
+
onlineChange && baseline && sources
|
|
652
|
+
? checkOnlineChange(baseline, sources.objects, config, progress)
|
|
653
|
+
: [];
|
|
612
654
|
const request = {
|
|
613
655
|
libraryReferences: config.libraryReferences,
|
|
614
656
|
// Only a download without a baseline starts from the template; the others keep the downloaded project's tree.
|
|
@@ -634,6 +676,36 @@ const controllerSession = async (
|
|
|
634
676
|
read: options.read ?? [],
|
|
635
677
|
finishLater: online.finishesLater,
|
|
636
678
|
};
|
|
679
|
+
const reply = await runConnected(request).catch(
|
|
680
|
+
async (failure: unknown) => {
|
|
681
|
+
if (
|
|
682
|
+
operation === "online-change" &&
|
|
683
|
+
failure instanceof CodesysExitedError
|
|
684
|
+
) {
|
|
685
|
+
throw crashedOnlineChange(failure);
|
|
686
|
+
}
|
|
687
|
+
// CODESYS's refusal carries its reasons; tsPLC's findings join them.
|
|
688
|
+
if (onlineChange && failure instanceof DownloadRequiredError) {
|
|
689
|
+
throw new DownloadRequiredError(failure.message, {
|
|
690
|
+
cause: failure,
|
|
691
|
+
findings,
|
|
692
|
+
messages: failure.messages,
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
if (
|
|
696
|
+
operation !== "download" ||
|
|
697
|
+
(await workerPhase(phase)) !== "transfer"
|
|
698
|
+
) {
|
|
699
|
+
throw failure;
|
|
700
|
+
}
|
|
701
|
+
throw await rollBack(failure);
|
|
702
|
+
},
|
|
703
|
+
);
|
|
704
|
+
if (operation === "review") {
|
|
705
|
+
z.strictObject({}).parse(reply);
|
|
706
|
+
await cleanUp();
|
|
707
|
+
return { findings };
|
|
708
|
+
}
|
|
637
709
|
const result = z
|
|
638
710
|
.object({
|
|
639
711
|
applicationGuid: guid,
|
|
@@ -642,23 +714,7 @@ const controllerSession = async (
|
|
|
642
714
|
state: z.string(),
|
|
643
715
|
values: z.record(z.string(), z.string()),
|
|
644
716
|
})
|
|
645
|
-
.parse(
|
|
646
|
-
await runConnected(request).catch(async (failure: unknown) => {
|
|
647
|
-
if (
|
|
648
|
-
operation === "online-change" &&
|
|
649
|
-
failure instanceof CodesysExitedError
|
|
650
|
-
) {
|
|
651
|
-
throw crashedOnlineChange(failure);
|
|
652
|
-
}
|
|
653
|
-
if (
|
|
654
|
-
operation !== "download" ||
|
|
655
|
-
(await workerPhase(phase)) !== "transfer"
|
|
656
|
-
) {
|
|
657
|
-
throw failure;
|
|
658
|
-
}
|
|
659
|
-
throw await rollBack(failure);
|
|
660
|
-
}),
|
|
661
|
-
);
|
|
717
|
+
.parse(reply);
|
|
662
718
|
const receipt = controllerResultSchema.parse({
|
|
663
719
|
...result,
|
|
664
720
|
application: result.application ?? baseline?.application,
|
|
@@ -905,18 +961,34 @@ export const serveWarmController = async (
|
|
|
905
961
|
const operate = async (request: Request, notices: string[]) => {
|
|
906
962
|
try {
|
|
907
963
|
const body = warmOperationSchema.parse(await request.json());
|
|
908
|
-
const
|
|
909
|
-
|
|
964
|
+
const operationConfig = await options.loadConfig(body.request);
|
|
965
|
+
const sessionOptions: SessionOptions = {
|
|
966
|
+
imageId: options.imageId,
|
|
967
|
+
engineering: options.engineering,
|
|
968
|
+
operationId: body.operationId,
|
|
969
|
+
scratch: containerScratch,
|
|
970
|
+
initializeUser: body.initializeUser,
|
|
971
|
+
credentials: body.credentials,
|
|
972
|
+
notices,
|
|
973
|
+
};
|
|
974
|
+
if (body.operation === "review") {
|
|
975
|
+
const findings = await reviewControllerSession(
|
|
976
|
+
operationConfig,
|
|
977
|
+
sessionOptions,
|
|
978
|
+
online,
|
|
979
|
+
);
|
|
980
|
+
return {
|
|
981
|
+
reply: Response.json({
|
|
982
|
+
ok: true,
|
|
983
|
+
result: { findings } satisfies z.input<typeof reviewReplySchema>,
|
|
984
|
+
}),
|
|
985
|
+
finish: undefined,
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
const reported = await receiptSession(
|
|
989
|
+
operationConfig,
|
|
910
990
|
body.operation,
|
|
911
|
-
|
|
912
|
-
imageId: options.imageId,
|
|
913
|
-
engineering: options.engineering,
|
|
914
|
-
operationId: body.operationId,
|
|
915
|
-
scratch: containerScratch,
|
|
916
|
-
initializeUser: body.initializeUser,
|
|
917
|
-
credentials: body.credentials,
|
|
918
|
-
notices,
|
|
919
|
-
},
|
|
991
|
+
sessionOptions,
|
|
920
992
|
online,
|
|
921
993
|
).catch((error: unknown) => {
|
|
922
994
|
// A worker that died on an online change crashed generating it.
|
|
@@ -939,6 +1011,10 @@ export const serveWarmController = async (
|
|
|
939
1011
|
ok: false,
|
|
940
1012
|
error: failureReason(error),
|
|
941
1013
|
...(requiresDownload(error) && { downloadRequired: true }),
|
|
1014
|
+
...(error instanceof DownloadRequiredError && {
|
|
1015
|
+
findings: error.findings,
|
|
1016
|
+
messages: error.messages,
|
|
1017
|
+
}),
|
|
942
1018
|
...(rollback && { rollback }),
|
|
943
1019
|
}),
|
|
944
1020
|
finish: undefined,
|
|
@@ -243,6 +243,7 @@ export const createCodesysWorker = (config: ResolvedConfig) => {
|
|
|
243
243
|
error: z.string().optional(),
|
|
244
244
|
diagnostics: z.array(compilerDiagnosticSchema).optional(),
|
|
245
245
|
downloadRequired: z.literal(true).optional(),
|
|
246
|
+
messages: z.array(z.string()).optional(),
|
|
246
247
|
id: z.literal(id),
|
|
247
248
|
ok: z.boolean(),
|
|
248
249
|
result: z.unknown(),
|
|
@@ -260,7 +261,9 @@ export const createCodesysWorker = (config: ResolvedConfig) => {
|
|
|
260
261
|
throw new CodesysCompilerError(message, response.data.diagnostics);
|
|
261
262
|
}
|
|
262
263
|
if (response.data.downloadRequired) {
|
|
263
|
-
throw new DownloadRequiredError(message
|
|
264
|
+
throw new DownloadRequiredError(message, {
|
|
265
|
+
messages: response.data.messages,
|
|
266
|
+
});
|
|
264
267
|
}
|
|
265
268
|
throw new Error(message);
|
|
266
269
|
}
|