@tsplc/build 0.2.17 → 0.2.18
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 +628 -352
- package/dist/cli.js.map +30 -28
- package/dist/index.js +548 -387
- package/dist/index.js.map +27 -25
- package/dist/libraries.js +68 -27
- package/dist/libraries.js.map +3 -3
- package/docker/Dockerfile +2 -0
- package/docker/Dockerfile.dockerignore +4 -1
- package/docker/Dockerfile.source +3 -0
- package/docker/config.ts +10 -5
- package/docker/runner.ts +140 -127
- package/package.json +54 -53
- package/python/build.py +183 -124
- package/python/codesys_online.py +44 -45
- package/python/hardware.py +9 -9
- package/python/test_build.py +3 -3
- package/python/test_codesys_online.py +18 -12
- package/python/test_installer_packages.py +1 -0
- package/python/worker.py +11 -7
- package/src/artifacts.ts +82 -60
- package/src/build.ts +71 -62
- package/src/cli.ts +109 -100
- package/src/controller-online.ts +400 -425
- package/src/controller-protocol.ts +140 -0
- package/src/controller-session.ts +466 -397
- package/src/device-tree.ts +19 -15
- package/src/diagnostics.ts +59 -49
- package/src/docker-build.ts +217 -156
- package/src/docker-context.ts +49 -27
- package/src/docker-run.ts +137 -111
- package/src/engineering-settings.ts +42 -0
- package/src/host/codesys-command.ts +35 -34
- package/src/host/codesys-host.ts +157 -129
- package/src/host/codesys-worker.ts +318 -301
- package/src/host/windows-path.ts +63 -59
- package/src/import-request.ts +24 -0
- package/src/index.ts +37 -31
- package/src/io.ts +152 -116
- package/src/libraries.ts +214 -146
- package/src/package-path.ts +3 -1
- package/src/run.ts +132 -108
- package/src/runtime-status.ts +25 -19
- package/src/sources.ts +264 -225
- package/src/st/declarations.ts +181 -193
- package/src/st/lexer.ts +102 -118
- package/src/st/parse.ts +193 -171
- package/src/st/reader.ts +47 -42
- package/src/__tests__/fixtures/xml/fb-simple.xml +0 -40
- package/src/__tests__/fixtures/xml/fb-state-machine.xml +0 -86
- package/src/__tests__/fixtures/xml/prg-cyclic.xml +0 -44
- package/src/config.ts +0 -12
package/src/run.ts
CHANGED
|
@@ -1,133 +1,157 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
2
1
|
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
2
|
import { tmpdir } from "node:os";
|
|
4
3
|
import { basename, dirname, join } from "node:path";
|
|
5
4
|
import { setTimeout as delay } from "node:timers/promises";
|
|
6
|
-
import { promisify } from "node:util";
|
|
7
5
|
import { z } from "zod";
|
|
8
6
|
import { readBuild } from "./artifacts";
|
|
9
|
-
import type { ResolvedConfig } from "
|
|
7
|
+
import type { ResolvedConfig } from "@tsplc/config/build";
|
|
10
8
|
import { runtimeImage } from "./docker-build";
|
|
9
|
+
import { captureDocker } from "./docker-run";
|
|
11
10
|
import { withWorkerLock } from "./host/codesys-worker";
|
|
12
11
|
import { getApplicationStatus } from "./runtime-status";
|
|
13
12
|
|
|
14
|
-
const execute = promisify(execFile);
|
|
15
13
|
const stateSchema = z.object({
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
Running: z.boolean(),
|
|
15
|
+
StartedAt: z.iso.datetime({ offset: true }),
|
|
18
16
|
});
|
|
19
17
|
|
|
20
18
|
const service = "plc";
|
|
21
19
|
|
|
22
20
|
/** The Docker Compose project of a tsPLC project: `FrameAssembly` runs as `frame-assembly`. */
|
|
23
21
|
export const runtimeProject = (name: string) =>
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
name
|
|
23
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
24
|
+
.toLowerCase()
|
|
25
|
+
.replace(/[^a-z0-9_-]+/g, "-");
|
|
28
26
|
|
|
29
27
|
/** The local PLC: named volumes keep its provisioning and the installed application across restarts. */
|
|
30
28
|
export const runtimeDefinition = (name: string) => ({
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
29
|
+
services: {
|
|
30
|
+
[service]: {
|
|
31
|
+
image: runtimeImage,
|
|
32
|
+
platform: "linux/amd64",
|
|
33
|
+
hostname: `${runtimeProject(name)}-${service}`,
|
|
34
|
+
init: true,
|
|
35
|
+
ports: ["127.0.0.1:4840:4840", "127.0.0.1:11740:11740"],
|
|
36
|
+
cap_add: ["SYS_NICE", "IPC_LOCK"],
|
|
37
|
+
volumes: [
|
|
38
|
+
"control-conf:/conf/codesyscontrol",
|
|
39
|
+
"control-data:/data/codesyscontrol",
|
|
40
|
+
],
|
|
41
|
+
restart: "no",
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
volumes: { "control-conf": {}, "control-data": {} },
|
|
44
45
|
});
|
|
45
46
|
|
|
46
47
|
/** Deploy the completed boot application to the project's local PLC container. */
|
|
47
48
|
export const runProject = async (
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
config: Pick<ResolvedConfig, "outDir" | "timeouts">,
|
|
50
|
+
name: string,
|
|
50
51
|
) =>
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
52
|
+
withWorkerLock(config, async () => {
|
|
53
|
+
// Validate before stopping anything. A missing receipt includes a failed rebuild.
|
|
54
|
+
const build = await readBuild(config.outDir).catch((cause: unknown) => {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`No complete build in ${config.outDir}. Run tsplc-build build first.`,
|
|
57
|
+
{
|
|
58
|
+
cause,
|
|
59
|
+
},
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
const definitionDirectory = await mkdtemp(join(tmpdir(), "tsplc-runtime-"));
|
|
63
|
+
try {
|
|
64
|
+
const composeFile = join(definitionDirectory, "compose.yaml");
|
|
65
|
+
// JSON is valid YAML.
|
|
66
|
+
await writeFile(composeFile, JSON.stringify(runtimeDefinition(name)));
|
|
67
|
+
const docker = (...args: string[]) =>
|
|
68
|
+
captureDocker(args, config.timeouts.runtime);
|
|
69
|
+
const compose = (...args: string[]) =>
|
|
70
|
+
docker(
|
|
71
|
+
"compose",
|
|
72
|
+
"--project-name",
|
|
73
|
+
runtimeProject(name),
|
|
74
|
+
"--file",
|
|
75
|
+
composeFile,
|
|
76
|
+
...args,
|
|
77
|
+
);
|
|
78
|
+
const installer = await readFile(
|
|
79
|
+
new URL("../runtime/install.sh", import.meta.url),
|
|
80
|
+
"utf8",
|
|
81
|
+
);
|
|
82
|
+
console.log(
|
|
83
|
+
`Installing ${build.application} in Docker service ${service}`,
|
|
84
|
+
);
|
|
85
|
+
await compose("stop", service);
|
|
86
|
+
// The helper uses the service's volumes without starting its PLC or publishing
|
|
87
|
+
// ports. Its read-only artifact mount is removed with the helper container.
|
|
88
|
+
await compose(
|
|
89
|
+
"run",
|
|
90
|
+
"--rm",
|
|
91
|
+
"--no-deps",
|
|
92
|
+
"-T",
|
|
93
|
+
"--volume",
|
|
94
|
+
`${dirname(build.output)}:/codesys-build:ro`,
|
|
95
|
+
"--entrypoint",
|
|
96
|
+
"/bin/sh",
|
|
97
|
+
service,
|
|
98
|
+
"-c",
|
|
99
|
+
installer,
|
|
100
|
+
"codesys-build-install",
|
|
101
|
+
build.application,
|
|
102
|
+
basename(build.output, ".app"),
|
|
103
|
+
);
|
|
104
|
+
await compose("up", "--detach", "--no-deps", service);
|
|
105
|
+
const containers = (
|
|
106
|
+
await compose("ps", "--all", "--quiet", service)
|
|
107
|
+
).stdout
|
|
108
|
+
.trim()
|
|
109
|
+
.split(/\s+/)
|
|
110
|
+
.filter(Boolean);
|
|
111
|
+
if (containers.length !== 1 || !containers[0]) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
`Expected one container for Compose service ${service}`,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
const container = containers[0];
|
|
117
|
+
const deadline = Date.now() + config.timeouts.runtime;
|
|
118
|
+
while (true) {
|
|
119
|
+
const state = stateSchema.parse(
|
|
120
|
+
JSON.parse(
|
|
121
|
+
(await docker("inspect", "--format", "{{json .State}}", container))
|
|
122
|
+
.stdout,
|
|
123
|
+
),
|
|
124
|
+
);
|
|
125
|
+
if (!state.Running) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
`Docker service ${service} exited before the application started. Check docker logs ${container}.`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
// Use this container start time, not the host clock or an old "started" event.
|
|
131
|
+
const { stdout: log } = await docker(
|
|
132
|
+
"exec",
|
|
133
|
+
container,
|
|
134
|
+
"/bin/sh",
|
|
135
|
+
"-c",
|
|
136
|
+
"if [ -f /data/codesyscontrol/codesyscontrol.log ]; then cat /data/codesyscontrol/codesyscontrol.log; fi",
|
|
137
|
+
);
|
|
138
|
+
const status = getApplicationStatus(
|
|
139
|
+
log,
|
|
140
|
+
build.application,
|
|
141
|
+
Date.parse(state.StartedAt),
|
|
142
|
+
);
|
|
143
|
+
if (status === "running") {
|
|
144
|
+
console.log(`${build.application} is running in ${service}`);
|
|
145
|
+
return { ...build, container };
|
|
146
|
+
}
|
|
147
|
+
if (status === "failed" || Date.now() >= deadline) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`CODESYS did not start ${build.application}. Runtime log:\n${log.split("\n").slice(-25).join("\n")}`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
await delay(250);
|
|
153
|
+
}
|
|
154
|
+
} finally {
|
|
155
|
+
await rm(definitionDirectory, { force: true, recursive: true });
|
|
156
|
+
}
|
|
157
|
+
});
|
package/src/runtime-status.ts
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
/** Runtime stdout is buffered in Docker. Its log file records start/stop events immediately. */
|
|
2
|
-
export const getApplicationStatus = (
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
2
|
+
export const getApplicationStatus = (
|
|
3
|
+
log: string,
|
|
4
|
+
application: string,
|
|
5
|
+
startedAt: number,
|
|
6
|
+
) => {
|
|
7
|
+
const event = log
|
|
8
|
+
.split(/\r?\n/)
|
|
9
|
+
.reverse()
|
|
10
|
+
.find((line) => {
|
|
11
|
+
const timestamp = Date.parse(line.split(",")[0] ?? "");
|
|
12
|
+
if (!Number.isFinite(timestamp) || timestamp < startedAt) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return (
|
|
16
|
+
line.includes(`<app>${application}</app>`) &&
|
|
17
|
+
/\b(started|stopped|exception|corrupt|failed|denied)\b/i.test(line) &&
|
|
18
|
+
!line.includes("load started")
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
if (!event) {
|
|
22
|
+
return "pending";
|
|
23
|
+
}
|
|
24
|
+
return event.includes(`Application [<app>${application}</app>] started`)
|
|
25
|
+
? "running"
|
|
26
|
+
: "failed";
|
|
21
27
|
};
|