@tsplc/build 0.4.4 → 0.5.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/dist/cli.js +392 -272
- package/dist/cli.js.map +12 -11
- package/dist/images.js +70 -11
- package/dist/images.js.map +4 -4
- package/dist/index.js +388 -253
- package/dist/index.js.map +11 -10
- package/package.json +4 -4
- package/src/builder-image.ts +94 -0
- package/src/controller-online.ts +13 -4
- package/src/docker-build.ts +50 -20
- package/src/docker-context.ts +158 -13
- package/src/docker-run.ts +7 -1
- package/src/images.ts +23 -2
- package/src/index.ts +5 -0
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.
|
|
18
|
+
"version": "0.5.0",
|
|
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.
|
|
58
|
-
"@tsplc/typegen": "0.
|
|
59
|
-
"@tsplc/iec": "0.
|
|
57
|
+
"@tsplc/config": "0.5.0",
|
|
58
|
+
"@tsplc/typegen": "0.5.0",
|
|
59
|
+
"@tsplc/iec": "0.5.0"
|
|
60
60
|
},
|
|
61
61
|
"bin": {
|
|
62
62
|
"tsplc-build": "./dist/cli.js"
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { builderRecipe, containerManifest } from "./docker-context.ts";
|
|
4
|
+
import { captureDocker } from "./docker-run.ts";
|
|
5
|
+
import { builderImage, recipeLabel } from "./images.ts";
|
|
6
|
+
|
|
7
|
+
/** Where the image's recipe wrote the manifest of the third-party packages it installed. */
|
|
8
|
+
const imageManifest = "/opt/codesys-build/package.json";
|
|
9
|
+
|
|
10
|
+
/** Image inspection and reading one file from it return at once; one still running has hung. */
|
|
11
|
+
const dockerTimeout = 60_000;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Whether the builder image can run this installation's code. `current` was built from this
|
|
15
|
+
* installation's recipe. `missing` and `outdated` need a new image. `unverified` was built before
|
|
16
|
+
* images carried their recipe: its third-party packages match, but its vendor installation can't
|
|
17
|
+
* be told apart from this recipe's, so it is used as it is until the next image build.
|
|
18
|
+
*/
|
|
19
|
+
export type BuilderImageStatus =
|
|
20
|
+
| { kind: "current" }
|
|
21
|
+
| { kind: "missing" }
|
|
22
|
+
| { kind: "outdated"; reason: string }
|
|
23
|
+
| { kind: "unverified"; reason: string };
|
|
24
|
+
|
|
25
|
+
const labelsSchema = z.record(z.string(), z.string()).nullable();
|
|
26
|
+
|
|
27
|
+
/** Why an image needs rebuilding, or null when its status leaves it usable. */
|
|
28
|
+
export const setupReason = (status: BuilderImageStatus) => {
|
|
29
|
+
switch (status.kind) {
|
|
30
|
+
case "missing":
|
|
31
|
+
return `the CODESYS builder image ${builderImage} is missing`;
|
|
32
|
+
case "outdated":
|
|
33
|
+
return status.reason;
|
|
34
|
+
case "current":
|
|
35
|
+
case "unverified":
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The builder image's status against this installation's recipe. Throws when Docker can't be
|
|
42
|
+
* reached, which says nothing about the image.
|
|
43
|
+
*/
|
|
44
|
+
export const builderImageStatus = async (
|
|
45
|
+
image: string = builderImage,
|
|
46
|
+
): Promise<BuilderImageStatus> => {
|
|
47
|
+
const docker = async (...args: string[]) =>
|
|
48
|
+
(await captureDocker(args, dockerTimeout)).stdout.trim();
|
|
49
|
+
if (!(await docker("image", "ls", "--quiet", image))) {
|
|
50
|
+
return { kind: "missing" };
|
|
51
|
+
}
|
|
52
|
+
const labels = labelsSchema.parse(
|
|
53
|
+
JSON.parse(
|
|
54
|
+
await docker(
|
|
55
|
+
"image",
|
|
56
|
+
"inspect",
|
|
57
|
+
"--format",
|
|
58
|
+
"{{json .Config.Labels}}",
|
|
59
|
+
image,
|
|
60
|
+
),
|
|
61
|
+
),
|
|
62
|
+
);
|
|
63
|
+
const recipe = labels?.[recipeLabel];
|
|
64
|
+
if (recipe !== undefined) {
|
|
65
|
+
return recipe === (await builderRecipe())
|
|
66
|
+
? { kind: "current" }
|
|
67
|
+
: {
|
|
68
|
+
kind: "outdated",
|
|
69
|
+
reason: `the CODESYS builder image ${image} was built from another recipe: another CODESYS installation, Bun or third-party packages`,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const installed: unknown = JSON.parse(
|
|
73
|
+
await docker(
|
|
74
|
+
"run",
|
|
75
|
+
"--rm",
|
|
76
|
+
"--network",
|
|
77
|
+
"none",
|
|
78
|
+
"--entrypoint",
|
|
79
|
+
"cat",
|
|
80
|
+
image,
|
|
81
|
+
imageManifest,
|
|
82
|
+
),
|
|
83
|
+
);
|
|
84
|
+
if (!isDeepStrictEqual(installed, await containerManifest())) {
|
|
85
|
+
return {
|
|
86
|
+
kind: "outdated",
|
|
87
|
+
reason: `the CODESYS builder image ${image} installed other third-party packages than this tsPLC needs`,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
kind: "unverified",
|
|
92
|
+
reason: `the CODESYS builder image ${image} predates recipe labels, so whether its CODESYS installation is this tsPLC's can't be checked; its third-party packages match`,
|
|
93
|
+
};
|
|
94
|
+
};
|
package/src/controller-online.ts
CHANGED
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
} from "./controller-protocol.ts";
|
|
29
29
|
import { controllerDirectory, jobProgressLog } from "./controller-session.ts";
|
|
30
30
|
import { controllerFailureFile } from "./diagnostics.ts";
|
|
31
|
+
import { containerCodeDigest } from "./docker-context.ts";
|
|
31
32
|
import {
|
|
32
33
|
type ContainerEnvironment,
|
|
33
34
|
captureDocker,
|
|
@@ -423,8 +424,16 @@ const warmController = async (
|
|
|
423
424
|
const timeout = config.timeouts.hostCommand;
|
|
424
425
|
const { imageId, target } = connection;
|
|
425
426
|
const settings = createHash("sha256")
|
|
426
|
-
// Each operation brings its own timeouts, so a release that changes them needn't restart
|
|
427
|
-
.
|
|
427
|
+
// Each operation brings its own timeouts, so a release that changes them needn't restart
|
|
428
|
+
// CODESYS. The container runs this installation's code, so another installation's replaces it.
|
|
429
|
+
.update(
|
|
430
|
+
JSON.stringify({
|
|
431
|
+
imageId,
|
|
432
|
+
target,
|
|
433
|
+
docker: config.docker,
|
|
434
|
+
code: await containerCodeDigest(),
|
|
435
|
+
}),
|
|
436
|
+
)
|
|
428
437
|
.digest("hex");
|
|
429
438
|
const label = `tsplc.warm=${settings}`;
|
|
430
439
|
const running = async () =>
|
|
@@ -443,7 +452,7 @@ const warmController = async (
|
|
|
443
452
|
// One whose CODESYS died, which its server reports, is replaced too.
|
|
444
453
|
if (!(await running()) || !(await answers(name, warmPort))) {
|
|
445
454
|
report(
|
|
446
|
-
"Starting CODESYS: no running builder matches this image, controller and Docker settings, which takes minutes",
|
|
455
|
+
"Starting CODESYS: no running builder matches this image, tsPLC, controller and Docker settings, which takes minutes",
|
|
447
456
|
);
|
|
448
457
|
// A container for other settings may still commit the last deployment, which a rollback restores.
|
|
449
458
|
await settleWarmController(config, name);
|
|
@@ -533,7 +542,7 @@ export const startWarmController = async (
|
|
|
533
542
|
/**
|
|
534
543
|
* A download or online change in builder container `name`, which keeps CODESYS running between
|
|
535
544
|
* operations so each skips starting the container, Wine and CODESYS. The first operation, or one
|
|
536
|
-
* with a new image, target or Docker settings, starts it. Credentials go to CODESYS for this
|
|
545
|
+
* with a new image, tsPLC installation, target or Docker settings, starts it. Credentials go to CODESYS for this
|
|
537
546
|
* operation only. A download that fails mid-transfer throws a `TransferFailedError` naming its rollback.
|
|
538
547
|
*/
|
|
539
548
|
export const updateWarmController = async (
|
package/src/docker-build.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { resolve } from "node:path";
|
|
|
5
5
|
import type { ResolvedConfig } from "@tsplc/config/build";
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
import { readBuild } from "./artifacts";
|
|
8
|
+
import { builderImageStatus, setupReason } from "./builder-image.ts";
|
|
8
9
|
import {
|
|
9
10
|
buildFailureFile,
|
|
10
11
|
buildFailureSchema,
|
|
@@ -22,6 +23,7 @@ import {
|
|
|
22
23
|
buildBuilderImage,
|
|
23
24
|
builderImage,
|
|
24
25
|
buildGatewayImage,
|
|
26
|
+
buildRuntimeImage,
|
|
25
27
|
gatewayImage,
|
|
26
28
|
publishedImage,
|
|
27
29
|
runtimeImage,
|
|
@@ -63,10 +65,16 @@ const pullPublished = async (image: string, progress: ProgressStream) => {
|
|
|
63
65
|
return true;
|
|
64
66
|
};
|
|
65
67
|
|
|
66
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* Install the IDE and its prerequisites in the Docker builder image, unless the image was already
|
|
70
|
+
* built from this installation's recipe: containers run the installed tsPLC's code, so the image
|
|
71
|
+
* changes only with its recipe. `keepUnverified` also keeps an image from before recipe labels
|
|
72
|
+
* whose third-party packages match, which can't be shown to need a new image.
|
|
73
|
+
*/
|
|
67
74
|
export const configureDocker = async ({
|
|
68
75
|
progress = "stdout",
|
|
69
|
-
|
|
76
|
+
keepUnverified = false,
|
|
77
|
+
}: ProgressOptions & { keepUnverified?: boolean } = {}) => {
|
|
70
78
|
const image = builderImage;
|
|
71
79
|
const { stdout } = await captureDocker(
|
|
72
80
|
["info", "--format", "{{.OSType}}/{{.Architecture}}"],
|
|
@@ -85,7 +93,24 @@ export const configureDocker = async ({
|
|
|
85
93
|
"The builder requires a Linux Docker server on ARM64 or AMD64",
|
|
86
94
|
);
|
|
87
95
|
}
|
|
88
|
-
|
|
96
|
+
const status = await builderImageStatus(image);
|
|
97
|
+
if (status.kind === "current") {
|
|
98
|
+
progressLog(progress)(
|
|
99
|
+
`The Docker builder (${image}) was built from this tsPLC's recipe; keeping it.`,
|
|
100
|
+
);
|
|
101
|
+
return image;
|
|
102
|
+
}
|
|
103
|
+
if (status.kind === "unverified" && keepUnverified) {
|
|
104
|
+
progressLog(progress)(`Keeping the Docker builder: ${status.reason}.`);
|
|
105
|
+
return image;
|
|
106
|
+
}
|
|
107
|
+
progressLog(progress)(
|
|
108
|
+
`Rebuilding the Docker builder: ${status.kind === "unverified" ? status.reason : setupReason(status)}.`,
|
|
109
|
+
);
|
|
110
|
+
if (
|
|
111
|
+
(await pullPublished(image, progress)) &&
|
|
112
|
+
(await builderImageStatus(image)).kind === "current"
|
|
113
|
+
) {
|
|
89
114
|
return image;
|
|
90
115
|
}
|
|
91
116
|
progressLog(progress)(
|
|
@@ -111,30 +136,35 @@ export const configureDocker = async ({
|
|
|
111
136
|
export const configureRuntime = async ({
|
|
112
137
|
progress = "stdout",
|
|
113
138
|
}: ProgressOptions = {}) => {
|
|
114
|
-
const context = resolve(packageDirectory, "docker/control-linux-sl");
|
|
115
139
|
progressLog(progress)(`Preparing the local PLC runtime (${runtimeImage}).`);
|
|
116
|
-
await
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
],
|
|
127
|
-
(status) =>
|
|
128
|
-
`Runtime image build failed (${status}). Review the image-build output`,
|
|
129
|
-
{ progress, environment: {} },
|
|
130
|
-
);
|
|
140
|
+
await buildRuntimeImage({
|
|
141
|
+
tag: runtimeImage,
|
|
142
|
+
docker: (args) =>
|
|
143
|
+
runDocker(
|
|
144
|
+
args,
|
|
145
|
+
(status) =>
|
|
146
|
+
`Runtime image build failed (${status}). Review the image-build output`,
|
|
147
|
+
{ progress, environment: {} },
|
|
148
|
+
),
|
|
149
|
+
});
|
|
131
150
|
return runtimeImage;
|
|
132
151
|
};
|
|
133
152
|
|
|
134
|
-
/**
|
|
153
|
+
/**
|
|
154
|
+
* Build the CODESYS Edge Gateway that builder containers reach a plant controller through, unless
|
|
155
|
+
* it is already there: its tag names its version, so a recipe change comes with a new tag.
|
|
156
|
+
*/
|
|
135
157
|
export const configureGateway = async ({
|
|
136
158
|
progress = "stdout",
|
|
137
159
|
}: ProgressOptions = {}) => {
|
|
160
|
+
if (
|
|
161
|
+
(
|
|
162
|
+
await captureDocker(["image", "ls", "--quiet", gatewayImage], 60_000)
|
|
163
|
+
).stdout.trim()
|
|
164
|
+
) {
|
|
165
|
+
progressLog(progress)(`The CODESYS gateway (${gatewayImage}) is present.`);
|
|
166
|
+
return gatewayImage;
|
|
167
|
+
}
|
|
138
168
|
if (await pullPublished(gatewayImage, progress)) {
|
|
139
169
|
return gatewayImage;
|
|
140
170
|
}
|
package/src/docker-context.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
cp,
|
|
4
|
+
mkdir,
|
|
5
|
+
mkdtemp,
|
|
6
|
+
readdir,
|
|
7
|
+
readFile,
|
|
8
|
+
rm,
|
|
9
|
+
stat,
|
|
10
|
+
writeFile,
|
|
11
|
+
} from "node:fs/promises";
|
|
2
12
|
import { createRequire } from "node:module";
|
|
3
13
|
import { tmpdir } from "node:os";
|
|
4
|
-
import { basename, dirname, resolve } from "node:path";
|
|
14
|
+
import { basename, dirname, posix, relative, resolve, sep } from "node:path";
|
|
5
15
|
import { z } from "zod";
|
|
6
16
|
import { packageDirectory } from "./package-path.ts";
|
|
7
17
|
|
|
@@ -9,6 +19,7 @@ const includeSource = (source: string) => {
|
|
|
9
19
|
const name = basename(source);
|
|
10
20
|
return (
|
|
11
21
|
name !== "__pycache__" &&
|
|
22
|
+
name !== ".DS_Store" &&
|
|
12
23
|
!name.endsWith(".pyc") &&
|
|
13
24
|
!name.endsWith(".test.ts") &&
|
|
14
25
|
!name.endsWith(".fixtures.ts") &&
|
|
@@ -16,20 +27,121 @@ const includeSource = (source: string) => {
|
|
|
16
27
|
);
|
|
17
28
|
};
|
|
18
29
|
|
|
19
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* The workspace packages container code imports, each copied with the files Bun runs it from,
|
|
32
|
+
* to `directory` in the context and `image` in the image.
|
|
33
|
+
*/
|
|
20
34
|
const containerPackages = [
|
|
21
35
|
{
|
|
22
36
|
entry: "@tsplc/config/build",
|
|
23
37
|
directory: "shared-config",
|
|
38
|
+
image: "/opt/tsplc-config",
|
|
24
39
|
files: ["src", "templates", "package.json"],
|
|
25
40
|
},
|
|
26
41
|
{
|
|
27
42
|
entry: "@tsplc/iec",
|
|
28
43
|
directory: "shared-iec",
|
|
44
|
+
image: "/opt/tsplc-iec",
|
|
29
45
|
files: ["src", "package.json"],
|
|
30
46
|
},
|
|
31
47
|
];
|
|
32
48
|
|
|
49
|
+
/** Where the image keeps this package. */
|
|
50
|
+
const imagePackage = "/opt/codesys-build";
|
|
51
|
+
/**
|
|
52
|
+
* This package's files that the image runs and a container may take from the installed package
|
|
53
|
+
* instead: everything the runner imports and the CODESYS scripts. The entrypoint stays the image's.
|
|
54
|
+
*/
|
|
55
|
+
const packageCode = ["src", "python", "docker/runner.ts", "docker/config.ts"];
|
|
56
|
+
|
|
57
|
+
const packageRoot = (entry: string) =>
|
|
58
|
+
resolve(dirname(createRequire(import.meta.url).resolve(entry)), "..");
|
|
59
|
+
|
|
60
|
+
/** A host file or directory and the path in the image it replaces. */
|
|
61
|
+
export interface ContainerMount {
|
|
62
|
+
source: string;
|
|
63
|
+
target: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The code a builder container runs, as this installation has it, and where the image keeps its
|
|
68
|
+
* own copy. Mounted over the image's, it makes a container run this installation's tsPLC whatever
|
|
69
|
+
* tsPLC built the image, so only a change of the image's recipe needs a new image.
|
|
70
|
+
*/
|
|
71
|
+
export const containerCode = (): ContainerMount[] => [
|
|
72
|
+
...packageCode.map((path) => ({
|
|
73
|
+
source: resolve(packageDirectory, path),
|
|
74
|
+
target: posix.join(imagePackage, path),
|
|
75
|
+
})),
|
|
76
|
+
...containerPackages.flatMap(({ entry, image, files }) =>
|
|
77
|
+
files.map((file) => ({
|
|
78
|
+
source: resolve(packageRoot(entry), file),
|
|
79
|
+
target: posix.join(image, file),
|
|
80
|
+
})),
|
|
81
|
+
),
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
/** Every file under `path`, or `path` itself, that `include` keeps, by its path relative to `root`. */
|
|
85
|
+
const listFiles = async (
|
|
86
|
+
root: string,
|
|
87
|
+
path: string,
|
|
88
|
+
include: (relativePath: string) => boolean,
|
|
89
|
+
): Promise<string[]> => {
|
|
90
|
+
const name = relative(root, path).split(sep).join("/");
|
|
91
|
+
if (!include(name)) {
|
|
92
|
+
return [];
|
|
93
|
+
}
|
|
94
|
+
if (!(await stat(path)).isDirectory()) {
|
|
95
|
+
return [name];
|
|
96
|
+
}
|
|
97
|
+
const entries = await readdir(path);
|
|
98
|
+
return (
|
|
99
|
+
await Promise.all(
|
|
100
|
+
entries.map((entry) => listFiles(root, resolve(path, entry), include)),
|
|
101
|
+
)
|
|
102
|
+
).flat();
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** A digest of named texts and files, which changes when a name or a file's content does. */
|
|
106
|
+
const digestFiles = async (
|
|
107
|
+
files: { name: string; path: string }[],
|
|
108
|
+
texts: Record<string, string>,
|
|
109
|
+
) => {
|
|
110
|
+
const hash = createHash("sha256");
|
|
111
|
+
for (const [name, text] of Object.entries(texts)) {
|
|
112
|
+
hash.update(`${name}\0${text}\0`);
|
|
113
|
+
}
|
|
114
|
+
for (const file of [...files].sort((a, b) => a.name.localeCompare(b.name))) {
|
|
115
|
+
hash.update(`${file.name}\0`);
|
|
116
|
+
hash.update(await readFile(file.path));
|
|
117
|
+
hash.update("\0");
|
|
118
|
+
}
|
|
119
|
+
return hash.digest("hex");
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* A digest of the code `containerCode` mounts, which a long-running container is labelled with so
|
|
124
|
+
* that one running other code is replaced.
|
|
125
|
+
*/
|
|
126
|
+
export const containerCodeDigest = async () =>
|
|
127
|
+
digestFiles(
|
|
128
|
+
(
|
|
129
|
+
await Promise.all(
|
|
130
|
+
containerCode().map(async ({ source, target }) =>
|
|
131
|
+
(
|
|
132
|
+
await listFiles(dirname(source), source, (path) =>
|
|
133
|
+
includeSource(basename(path)),
|
|
134
|
+
)
|
|
135
|
+
).map((name) => ({
|
|
136
|
+
name: posix.join(posix.dirname(target), name),
|
|
137
|
+
path: resolve(dirname(source), name),
|
|
138
|
+
})),
|
|
139
|
+
),
|
|
140
|
+
)
|
|
141
|
+
).flat(),
|
|
142
|
+
{},
|
|
143
|
+
);
|
|
144
|
+
|
|
33
145
|
const manifestDependencies = z.object({
|
|
34
146
|
name: z.string(),
|
|
35
147
|
dependencies: z.record(z.string(), z.string()).default({}),
|
|
@@ -67,6 +179,47 @@ const readManifest = async (directory: string) =>
|
|
|
67
179
|
JSON.parse(await readFile(resolve(directory, "package.json"), "utf8")),
|
|
68
180
|
);
|
|
69
181
|
|
|
182
|
+
/** The manifest of the third-party packages the image installs, as its recipe writes it. */
|
|
183
|
+
export const containerManifest = async () => {
|
|
184
|
+
const manifests = [await readManifest(packageDirectory)];
|
|
185
|
+
for (const { entry } of containerPackages) {
|
|
186
|
+
manifests.push(await readManifest(packageRoot(entry)));
|
|
187
|
+
}
|
|
188
|
+
return { type: "module", dependencies: containerDependencies(manifests) };
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The files of the image's recipe that its containers can't take from the installed package: the
|
|
193
|
+
* Dockerfile, the entrypoint and the installation's scripts and data. Tests and checks of the
|
|
194
|
+
* installation's own tools stay out, as the published package leaves them out.
|
|
195
|
+
*/
|
|
196
|
+
const recipeFile = (path: string) =>
|
|
197
|
+
path === "docker" ||
|
|
198
|
+
path === "docker/Dockerfile" ||
|
|
199
|
+
path === "docker/entrypoint.sh" ||
|
|
200
|
+
((path === "docker/install" || path.startsWith("docker/install/")) &&
|
|
201
|
+
includeSource(basename(path)) &&
|
|
202
|
+
!path.split("/").includes("tests") &&
|
|
203
|
+
path !== "docker/install/wine-signal/check.sh");
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The builder image's recipe: what it installs and how. An image built from another recipe has
|
|
207
|
+
* another vendor installation or other third-party packages, so its containers can't run this
|
|
208
|
+
* installation's code, while one built from this recipe runs any tsPLC with this recipe.
|
|
209
|
+
*/
|
|
210
|
+
export const builderRecipe = async () =>
|
|
211
|
+
digestFiles(
|
|
212
|
+
(await recipeFiles()).map((name) => ({
|
|
213
|
+
name,
|
|
214
|
+
path: resolve(packageDirectory, name),
|
|
215
|
+
})),
|
|
216
|
+
{ manifest: JSON.stringify(await containerManifest()) },
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
/** The package's files `builderRecipe` covers, beside the third-party manifest. */
|
|
220
|
+
export const recipeFiles = () =>
|
|
221
|
+
listFiles(packageDirectory, resolve(packageDirectory, "docker"), recipeFile);
|
|
222
|
+
|
|
70
223
|
export async function prepareDockerContext() {
|
|
71
224
|
const context = await mkdtemp(resolve(tmpdir(), "tsplc-docker-"));
|
|
72
225
|
try {
|
|
@@ -80,13 +233,8 @@ export async function prepareDockerContext() {
|
|
|
80
233
|
},
|
|
81
234
|
);
|
|
82
235
|
}
|
|
83
|
-
const manifests = [await readManifest(packageDirectory)];
|
|
84
236
|
for (const { entry, directory, files } of containerPackages) {
|
|
85
|
-
const root =
|
|
86
|
-
dirname(createRequire(import.meta.url).resolve(entry)),
|
|
87
|
-
"..",
|
|
88
|
-
);
|
|
89
|
-
manifests.push(await readManifest(root));
|
|
237
|
+
const root = packageRoot(entry);
|
|
90
238
|
await mkdir(resolve(context, directory));
|
|
91
239
|
for (const file of files) {
|
|
92
240
|
await cp(resolve(root, file), resolve(context, directory, file), {
|
|
@@ -97,10 +245,7 @@ export async function prepareDockerContext() {
|
|
|
97
245
|
}
|
|
98
246
|
await writeFile(
|
|
99
247
|
resolve(context, "package.json"),
|
|
100
|
-
JSON.stringify(
|
|
101
|
-
type: "module",
|
|
102
|
-
dependencies: containerDependencies(manifests),
|
|
103
|
-
}),
|
|
248
|
+
JSON.stringify(await containerManifest()),
|
|
104
249
|
);
|
|
105
250
|
return context;
|
|
106
251
|
} catch (cause) {
|
package/src/docker-run.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { copyFile, cp, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import { basename, posix, relative, resolve, sep } from "node:path";
|
|
5
5
|
import { promisify } from "node:util";
|
|
6
6
|
import type { ResolvedConfig } from "@tsplc/config/build";
|
|
7
|
+
import { containerCode } from "./docker-context.ts";
|
|
7
8
|
|
|
8
9
|
const execute = promisify(execFile);
|
|
9
10
|
|
|
@@ -184,7 +185,8 @@ export const writeOperationRequest = async (
|
|
|
184
185
|
* Write the builder container's request and return the `docker run` options that precede
|
|
185
186
|
* the image; the caller chooses `--rm` or `--detach`. The request names only container paths;
|
|
186
187
|
* host paths enter through these mounts, and host-only Docker, CODESYS and runtime settings
|
|
187
|
-
* stay outside.
|
|
188
|
+
* stay outside. The container runs this installation's code and CODESYS scripts, mounted over
|
|
189
|
+
* the image's copy, so a tsPLC update needs no new image unless the image's recipe changed.
|
|
188
190
|
*/
|
|
189
191
|
export const prepareContainerRun = async (
|
|
190
192
|
config: ResolvedConfig,
|
|
@@ -221,5 +223,9 @@ export const prepareContainerRun = async (
|
|
|
221
223
|
bindMount(config.template, "/template.project", true),
|
|
222
224
|
"--mount",
|
|
223
225
|
bindMount(config.outDir, "/output"),
|
|
226
|
+
...containerCode().flatMap(({ source, target }) => [
|
|
227
|
+
"--mount",
|
|
228
|
+
bindMount(source, target, true),
|
|
229
|
+
]),
|
|
224
230
|
];
|
|
225
231
|
};
|
package/src/images.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { copyFile, cp, mkdir, mkdtemp, rm } from "node:fs/promises";
|
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { dockerImage } from "@tsplc/config/build";
|
|
5
|
-
import { prepareDockerContext } from "./docker-context.ts";
|
|
5
|
+
import { builderRecipe, prepareDockerContext } from "./docker-context.ts";
|
|
6
6
|
import { packageDirectory } from "./package-path.ts";
|
|
7
7
|
|
|
8
8
|
export const builderImage = dockerImage;
|
|
@@ -30,8 +30,11 @@ export const setupPlatformFor = (server: string): SetupPlatform | null => {
|
|
|
30
30
|
return null;
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
+
/** The label an image built by `buildBuilderImage` carries its `builderRecipe` in. */
|
|
34
|
+
export const recipeLabel = "tsplc.recipe";
|
|
35
|
+
|
|
33
36
|
/**
|
|
34
|
-
* Builds the builder image from the full recipe. `installer` is a local CODESYS installer,
|
|
37
|
+
* Builds the builder image from the full recipe, labelled with it. `installer` is a local CODESYS installer,
|
|
35
38
|
* or null to download the official one; `winetricksCache` seeds the prerequisite downloads.
|
|
36
39
|
* `docker` runs one Docker client command, so each caller keeps its own terminal handling.
|
|
37
40
|
*/
|
|
@@ -71,6 +74,8 @@ export async function buildBuilderImage(options: {
|
|
|
71
74
|
`SETUP_PLATFORM=${options.setupPlatform}`,
|
|
72
75
|
"--build-arg",
|
|
73
76
|
`INSTALLER_SOURCE=${options.installer ? "installer-local" : "installer-download"}`,
|
|
77
|
+
"--label",
|
|
78
|
+
`${recipeLabel}=${await builderRecipe()}`,
|
|
74
79
|
"--progress",
|
|
75
80
|
"plain",
|
|
76
81
|
"--build-context",
|
|
@@ -89,6 +94,22 @@ export async function buildBuilderImage(options: {
|
|
|
89
94
|
}
|
|
90
95
|
}
|
|
91
96
|
|
|
97
|
+
/** Builds the local CODESYS Control for Linux SL runtime, the device a production build targets. */
|
|
98
|
+
export const buildRuntimeImage = (options: {
|
|
99
|
+
tag: string;
|
|
100
|
+
docker: (args: string[]) => Promise<void>;
|
|
101
|
+
}) =>
|
|
102
|
+
options.docker([
|
|
103
|
+
"build",
|
|
104
|
+
"--platform",
|
|
105
|
+
"linux/amd64",
|
|
106
|
+
"--progress",
|
|
107
|
+
"plain",
|
|
108
|
+
"-t",
|
|
109
|
+
options.tag,
|
|
110
|
+
resolve(packageDirectory, "docker/control-linux-sl"),
|
|
111
|
+
]);
|
|
112
|
+
|
|
92
113
|
export const buildGatewayImage = (options: {
|
|
93
114
|
tag: string;
|
|
94
115
|
docker: (args: string[]) => Promise<void>;
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,11 @@ export {
|
|
|
9
9
|
} from "@tsplc/config/build";
|
|
10
10
|
export { readBuild } from "./artifacts.ts";
|
|
11
11
|
export { buildProject } from "./build";
|
|
12
|
+
export {
|
|
13
|
+
type BuilderImageStatus,
|
|
14
|
+
builderImageStatus,
|
|
15
|
+
setupReason,
|
|
16
|
+
} from "./builder-image.ts";
|
|
12
17
|
export {
|
|
13
18
|
type ControllerLogin,
|
|
14
19
|
findControllerLogin,
|