@intentius/chant-lexicon-docker 0.124.97 → 0.124.99

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.
Files changed (241) hide show
  1. package/dist/codegen/docs-cli.d.ts +0 -1
  2. package/dist/codegen/docs.d.ts +0 -1
  3. package/dist/codegen/generate-cli.d.ts +0 -1
  4. package/dist/codegen/generate-compose.d.ts +0 -1
  5. package/dist/codegen/generate-dockerfile.d.ts +0 -1
  6. package/dist/codegen/generate.d.ts +0 -1
  7. package/dist/codegen/naming.d.ts +0 -1
  8. package/dist/codegen/package.d.ts +0 -1
  9. package/dist/codegen/patches.d.ts +0 -1
  10. package/dist/codegen/versions.d.ts +0 -1
  11. package/dist/compose-key.d.ts +0 -1
  12. package/dist/composites/catalog.d.ts +0 -1
  13. package/dist/composites/docker-otel-collector.d.ts +0 -1
  14. package/dist/composites/docker-web-service.d.ts +0 -1
  15. package/dist/composites/index.d.ts +0 -1
  16. package/dist/coverage.d.ts +0 -1
  17. package/dist/default-labels.d.ts +0 -1
  18. package/dist/detect.d.ts +0 -1
  19. package/dist/dockerfile-order.d.ts +0 -1
  20. package/dist/export-resources.d.ts +0 -1
  21. package/dist/generated/index.d.ts +0 -1
  22. package/dist/generated/runtime.d.ts +0 -1
  23. package/dist/import/adapter.d.ts +0 -1
  24. package/dist/import/adapter.js +4 -2
  25. package/dist/import/embedded.d.ts +0 -1
  26. package/dist/import/engine.d.ts +0 -1
  27. package/dist/import/generator.d.ts +9 -2
  28. package/dist/import/generator.js +23 -1
  29. package/dist/import/live-export.d.ts +0 -1
  30. package/dist/import/parser.d.ts +0 -1
  31. package/dist/import/testdata/tar.d.ts +0 -1
  32. package/dist/index.d.ts +0 -1
  33. package/dist/integrity.json +2 -2
  34. package/dist/interpolation.d.ts +0 -1
  35. package/dist/lint/audit-catalog.d.ts +0 -1
  36. package/dist/lint/audit-lineage.d.ts +0 -1
  37. package/dist/lint/post-synth/add-url-checksum.d.ts +0 -1
  38. package/dist/lint/post-synth/apt-no-recommends.d.ts +0 -1
  39. package/dist/lint/post-synth/apt-update-alone.d.ts +0 -1
  40. package/dist/lint/post-synth/cache-busting-copy.d.ts +0 -1
  41. package/dist/lint/post-synth/copy-from-stage.d.ts +0 -1
  42. package/dist/lint/post-synth/docker-helpers.d.ts +0 -1
  43. package/dist/lint/post-synth/index.d.ts +0 -1
  44. package/dist/lint/post-synth/no-latest-image.d.ts +0 -1
  45. package/dist/lint/post-synth/no-root-user.d.ts +0 -1
  46. package/dist/lint/post-synth/prefer-copy.d.ts +0 -1
  47. package/dist/lint/post-synth/relative-workdir.d.ts +0 -1
  48. package/dist/lint/post-synth/repeated-cmd.d.ts +0 -1
  49. package/dist/lint/post-synth/root-after-user.d.ts +0 -1
  50. package/dist/lint/post-synth/ssh-port-exposed.d.ts +0 -1
  51. package/dist/lint/post-synth/unused-global-arg.d.ts +0 -1
  52. package/dist/lint/post-synth/unused-volume.d.ts +0 -1
  53. package/dist/lint/rules/data/deprecated-images.d.ts +0 -1
  54. package/dist/lint/rules/data/known-base-images.d.ts +0 -1
  55. package/dist/lint/rules/index.d.ts +0 -1
  56. package/dist/lint/rules/no-latest-tag.d.ts +0 -1
  57. package/dist/lint/rules/per-kind-order.d.ts +0 -1
  58. package/dist/list-artifacts.d.ts +0 -1
  59. package/dist/lsp/completions.d.ts +0 -1
  60. package/dist/lsp/hover.d.ts +0 -1
  61. package/dist/manifest.json +1 -1
  62. package/dist/package-cli.d.ts +0 -1
  63. package/dist/plugin.d.ts +0 -1
  64. package/dist/serializer.d.ts +0 -1
  65. package/dist/spec/fetch-compose.d.ts +0 -1
  66. package/dist/spec/fetch-engine.d.ts +0 -1
  67. package/dist/spec/parse-compose.d.ts +0 -1
  68. package/dist/spec/parse-engine.d.ts +0 -1
  69. package/dist/validate-cli.d.ts +0 -1
  70. package/dist/validate.d.ts +0 -1
  71. package/dist/variables.d.ts +0 -1
  72. package/package.json +4 -5
  73. package/dist/codegen/docs-cli.d.ts.map +0 -1
  74. package/dist/codegen/docs.d.ts.map +0 -1
  75. package/dist/codegen/generate-cli.d.ts.map +0 -1
  76. package/dist/codegen/generate-compose.d.ts.map +0 -1
  77. package/dist/codegen/generate-dockerfile.d.ts.map +0 -1
  78. package/dist/codegen/generate.d.ts.map +0 -1
  79. package/dist/codegen/naming.d.ts.map +0 -1
  80. package/dist/codegen/package.d.ts.map +0 -1
  81. package/dist/codegen/patches.d.ts.map +0 -1
  82. package/dist/codegen/versions.d.ts.map +0 -1
  83. package/dist/compose-key.d.ts.map +0 -1
  84. package/dist/composites/catalog.d.ts.map +0 -1
  85. package/dist/composites/docker-otel-collector.d.ts.map +0 -1
  86. package/dist/composites/docker-web-service.d.ts.map +0 -1
  87. package/dist/composites/index.d.ts.map +0 -1
  88. package/dist/coverage.d.ts.map +0 -1
  89. package/dist/default-labels.d.ts.map +0 -1
  90. package/dist/detect.d.ts.map +0 -1
  91. package/dist/dockerfile-order.d.ts.map +0 -1
  92. package/dist/export-resources.d.ts.map +0 -1
  93. package/dist/generated/index.d.ts.map +0 -1
  94. package/dist/generated/runtime.d.ts.map +0 -1
  95. package/dist/import/adapter.d.ts.map +0 -1
  96. package/dist/import/embedded.d.ts.map +0 -1
  97. package/dist/import/engine.d.ts.map +0 -1
  98. package/dist/import/generator.d.ts.map +0 -1
  99. package/dist/import/live-export.d.ts.map +0 -1
  100. package/dist/import/parser.d.ts.map +0 -1
  101. package/dist/import/testdata/tar.d.ts.map +0 -1
  102. package/dist/index.d.ts.map +0 -1
  103. package/dist/interpolation.d.ts.map +0 -1
  104. package/dist/lint/audit-catalog.d.ts.map +0 -1
  105. package/dist/lint/audit-lineage.d.ts.map +0 -1
  106. package/dist/lint/post-synth/add-url-checksum.d.ts.map +0 -1
  107. package/dist/lint/post-synth/apt-no-recommends.d.ts.map +0 -1
  108. package/dist/lint/post-synth/apt-update-alone.d.ts.map +0 -1
  109. package/dist/lint/post-synth/cache-busting-copy.d.ts.map +0 -1
  110. package/dist/lint/post-synth/copy-from-stage.d.ts.map +0 -1
  111. package/dist/lint/post-synth/docker-helpers.d.ts.map +0 -1
  112. package/dist/lint/post-synth/index.d.ts.map +0 -1
  113. package/dist/lint/post-synth/no-latest-image.d.ts.map +0 -1
  114. package/dist/lint/post-synth/no-root-user.d.ts.map +0 -1
  115. package/dist/lint/post-synth/prefer-copy.d.ts.map +0 -1
  116. package/dist/lint/post-synth/relative-workdir.d.ts.map +0 -1
  117. package/dist/lint/post-synth/repeated-cmd.d.ts.map +0 -1
  118. package/dist/lint/post-synth/root-after-user.d.ts.map +0 -1
  119. package/dist/lint/post-synth/ssh-port-exposed.d.ts.map +0 -1
  120. package/dist/lint/post-synth/unused-global-arg.d.ts.map +0 -1
  121. package/dist/lint/post-synth/unused-volume.d.ts.map +0 -1
  122. package/dist/lint/rules/data/deprecated-images.d.ts.map +0 -1
  123. package/dist/lint/rules/data/known-base-images.d.ts.map +0 -1
  124. package/dist/lint/rules/index.d.ts.map +0 -1
  125. package/dist/lint/rules/no-latest-tag.d.ts.map +0 -1
  126. package/dist/lint/rules/per-kind-order.d.ts.map +0 -1
  127. package/dist/list-artifacts.d.ts.map +0 -1
  128. package/dist/lsp/completions.d.ts.map +0 -1
  129. package/dist/lsp/hover.d.ts.map +0 -1
  130. package/dist/package-cli.d.ts.map +0 -1
  131. package/dist/plugin.d.ts.map +0 -1
  132. package/dist/serializer.d.ts.map +0 -1
  133. package/dist/spec/fetch-compose.d.ts.map +0 -1
  134. package/dist/spec/fetch-engine.d.ts.map +0 -1
  135. package/dist/spec/parse-compose.d.ts.map +0 -1
  136. package/dist/spec/parse-engine.d.ts.map +0 -1
  137. package/dist/validate-cli.d.ts.map +0 -1
  138. package/dist/validate.d.ts.map +0 -1
  139. package/dist/variables.d.ts.map +0 -1
  140. package/src/codegen/__snapshots__/okf.test.ts.snap +0 -555
  141. package/src/codegen/docs-cli.ts +0 -10
  142. package/src/codegen/docs.ts +0 -171
  143. package/src/codegen/generate-cli.ts +0 -36
  144. package/src/codegen/generate-compose.ts +0 -21
  145. package/src/codegen/generate-dockerfile.ts +0 -21
  146. package/src/codegen/generate.test.ts +0 -115
  147. package/src/codegen/generate.ts +0 -258
  148. package/src/codegen/naming.test.ts +0 -81
  149. package/src/codegen/naming.ts +0 -54
  150. package/src/codegen/okf.test.ts +0 -36
  151. package/src/codegen/package.ts +0 -59
  152. package/src/codegen/patches.ts +0 -42
  153. package/src/codegen/versions.ts +0 -46
  154. package/src/compose-key.ts +0 -31
  155. package/src/composites/catalog.test.ts +0 -39
  156. package/src/composites/catalog.ts +0 -118
  157. package/src/composites/docker-otel-collector.ts +0 -83
  158. package/src/composites/docker-web-service.test.ts +0 -34
  159. package/src/composites/docker-web-service.ts +0 -75
  160. package/src/composites/index.ts +0 -9
  161. package/src/coverage.test.ts +0 -33
  162. package/src/coverage.ts +0 -54
  163. package/src/default-labels.test.ts +0 -85
  164. package/src/default-labels.ts +0 -72
  165. package/src/detect.ts +0 -7
  166. package/src/dockerfile-order.ts +0 -20
  167. package/src/docs-examples.test.ts +0 -141
  168. package/src/export-resources.ts +0 -90
  169. package/src/generated/index.d.ts +0 -407
  170. package/src/generated/index.ts +0 -410
  171. package/src/generated/runtime.ts +0 -4
  172. package/src/import/adapter.ts +0 -99
  173. package/src/import/embedded-live.test.ts +0 -248
  174. package/src/import/embedded.ts +0 -83
  175. package/src/import/engine.ts +0 -107
  176. package/src/import/generator.test.ts +0 -134
  177. package/src/import/generator.ts +0 -202
  178. package/src/import/live-export.test.ts +0 -271
  179. package/src/import/live-export.ts +0 -359
  180. package/src/import/parser.test.ts +0 -137
  181. package/src/import/parser.ts +0 -269
  182. package/src/import/roundtrip.test.ts +0 -257
  183. package/src/import/testdata/configs-secrets.yaml +0 -15
  184. package/src/import/testdata/full.yaml +0 -43
  185. package/src/import/testdata/names-and-fields.yaml +0 -44
  186. package/src/import/testdata/simple.yaml +0 -9
  187. package/src/import/testdata/tar.ts +0 -24
  188. package/src/import/testdata/webapp.yaml +0 -41
  189. package/src/index.ts +0 -32
  190. package/src/interpolation.test.ts +0 -41
  191. package/src/interpolation.ts +0 -76
  192. package/src/lint/audit-catalog.ts +0 -28
  193. package/src/lint/audit-lineage.ts +0 -48
  194. package/src/lint/post-synth/add-url-checksum.ts +0 -38
  195. package/src/lint/post-synth/apt-no-recommends.ts +0 -43
  196. package/src/lint/post-synth/apt-update-alone.ts +0 -39
  197. package/src/lint/post-synth/cache-busting-copy.ts +0 -65
  198. package/src/lint/post-synth/copy-from-stage.ts +0 -54
  199. package/src/lint/post-synth/docker-helpers.ts +0 -194
  200. package/src/lint/post-synth/index.ts +0 -33
  201. package/src/lint/post-synth/no-latest-image.ts +0 -36
  202. package/src/lint/post-synth/no-root-user.ts +0 -38
  203. package/src/lint/post-synth/post-synth.test.ts +0 -330
  204. package/src/lint/post-synth/prefer-copy.ts +0 -53
  205. package/src/lint/post-synth/relative-workdir.ts +0 -38
  206. package/src/lint/post-synth/repeated-cmd.ts +0 -35
  207. package/src/lint/post-synth/root-after-user.ts +0 -44
  208. package/src/lint/post-synth/ssh-port-exposed.ts +0 -68
  209. package/src/lint/post-synth/unused-global-arg.ts +0 -38
  210. package/src/lint/post-synth/unused-volume.ts +0 -49
  211. package/src/lint/rules/data/deprecated-images.ts +0 -28
  212. package/src/lint/rules/data/known-base-images.ts +0 -20
  213. package/src/lint/rules/index.ts +0 -6
  214. package/src/lint/rules/no-latest-tag.ts +0 -63
  215. package/src/lint/rules/per-kind-order.ts +0 -130
  216. package/src/lint/rules/rules.test.ts +0 -170
  217. package/src/list-artifacts.test.ts +0 -141
  218. package/src/list-artifacts.ts +0 -164
  219. package/src/lsp/completions.test.ts +0 -40
  220. package/src/lsp/completions.ts +0 -21
  221. package/src/lsp/hover.test.ts +0 -35
  222. package/src/lsp/hover.ts +0 -39
  223. package/src/package-cli.ts +0 -25
  224. package/src/plugin.test.ts +0 -121
  225. package/src/plugin.ts +0 -268
  226. package/src/serializer.test.ts +0 -545
  227. package/src/serializer.ts +0 -444
  228. package/src/skills/chant-docker-patterns.md +0 -222
  229. package/src/skills/chant-docker.md +0 -156
  230. package/src/spec/fetch-compose.ts +0 -35
  231. package/src/spec/fetch-engine.ts +0 -25
  232. package/src/spec/parse-compose.ts +0 -195
  233. package/src/spec/parse-engine.ts +0 -59
  234. package/src/typecheck.e2e.test.ts +0 -125
  235. package/src/typed-props.test.ts +0 -60
  236. package/src/validate-cli.ts +0 -19
  237. package/src/validate.test.ts +0 -16
  238. package/src/validate.ts +0 -44
  239. package/src/variables.test.ts +0 -32
  240. package/src/variables.ts +0 -47
  241. /package/{src → dist}/generated/lexicon-docker.json +0 -0
@@ -1,248 +0,0 @@
1
- /**
2
- * A `DockerOtelCollector`'s config through `chant import` (#3373, #4388): the
3
- * config's `content` is handed to the otel importer, and a live import
4
- * (`chant import --from`, against a fake Engine API) gives the same otel
5
- * source a file import of the build's `docker-compose.yml` gives, and both
6
- * give what `chant import config.yaml` gives for the config itself.
7
- */
8
-
9
- import { describe, expect, test } from "vitest";
10
- import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "fs";
11
- import { join, relative, resolve } from "path";
12
- import { load } from "js-yaml";
13
- import { importCommand, liveImportFromPlugins } from "@intentius/chant/cli/commands/import";
14
- import type { LexiconPlugin } from "@intentius/chant/lexicon";
15
- import { build } from "@intentius/chant/build";
16
- import { EmbeddedImports } from "@intentius/chant/import/embedded";
17
- import { COLLECTOR_CONFIG_PATH, otelPlugin, otelSerializer } from "@intentius/chant-lexicon-otel";
18
- import { OtelCollectorParser } from "@intentius/chant-lexicon-otel/import/parser";
19
- import { OtelCollectorGenerator } from "@intentius/chant-lexicon-otel/import/generator";
20
- import { dockerPlugin } from "../plugin";
21
- import { dockerSerializer } from "../serializer";
22
- import { exportResources } from "../export-resources";
23
- import type { DockerEngine } from "./engine";
24
- import { DockerParser } from "./parser";
25
- import { tarOf } from "./testdata/tar";
26
-
27
- type Json = Record<string, unknown>;
28
-
29
- const pkgDir = resolve(import.meta.dirname, "../..");
30
-
31
- const SOURCE = `import { DockerOtelCollector } from "@intentius/chant-lexicon-docker";
32
- import { OtlpHttpExporter } from "@intentius/chant-lexicon-otel";
33
-
34
- export const otel = DockerOtelCollector({
35
- exporters: [new OtlpHttpExporter({ endpoint: "https://otlp.example.com" })],
36
- });
37
- `;
38
-
39
- function walk(dir: string, root: string, out: Record<string, string>): void {
40
- for (const entry of readdirSync(dir)) {
41
- const path = join(dir, entry);
42
- if (statSync(path).isDirectory()) walk(path, root, out);
43
- else out[relative(root, path)] = readFileSync(path, "utf-8");
44
- }
45
- }
46
-
47
- function project(): { dir: string; srcDir: string } {
48
- const dir = mkdtempSync(join(pkgDir, ".roundtrip-tmp-"));
49
- writeFileSync(join(dir, "package.json"), '{ "name": "docker-embedded", "private": true, "type": "module" }\n');
50
- writeFileSync(join(dir, "chant.config.ts"), 'export default { lexicons: ["docker", "otel"] };\n');
51
- const srcDir = join(dir, "src");
52
- mkdirSync(srcDir);
53
- return { dir, srcDir };
54
- }
55
-
56
- function files(srcDir: string): Record<string, string> {
57
- const out: Record<string, string> = {};
58
- walk(srcDir, srcDir, out);
59
- return out;
60
- }
61
-
62
- function composeOf(out: unknown): string {
63
- if (typeof out === "string") return out;
64
- const o = out as { primary?: string; files?: Record<string, string> };
65
- if (typeof o.primary === "string") return o.primary;
66
- const yml = Object.entries(o.files ?? {}).find(([p]) => /compose/.test(p));
67
- if (!yml) throw new Error("no compose file in the build");
68
- return yml[1];
69
- }
70
-
71
- /** The build's compose document for `source`. */
72
- async function compose(source: string): Promise<{ text: string; doc: Json }> {
73
- const { dir, srcDir } = project();
74
- try {
75
- writeFileSync(join(srcDir, "main.ts"), source);
76
- const result = await build(srcDir, [dockerSerializer, otelSerializer]);
77
- expect(result.errors).toEqual([]);
78
- const text = composeOf(result.outputs.get("docker"));
79
- return { text, doc: load(text) as Json };
80
- } finally {
81
- rmSync(dir, { recursive: true, force: true });
82
- }
83
- }
84
-
85
- /** The collector's service key and its config text. */
86
- function collector(doc: Json): { service: string; config: string } {
87
- const services = doc.services as Record<string, Json>;
88
- const [service, svc] = Object.entries(services).find(([, s]) => Array.isArray(s.configs))!;
89
- const source = (svc.configs as Json[])[0]!.source as string;
90
- return { service, config: (doc.configs as Record<string, Json>)[source]!.content as string };
91
- }
92
-
93
- /** `chant import config.yaml`: the otel modules for the config alone. */
94
- function standalone(text: string): Record<string, string> {
95
- return Object.fromEntries(new OtelCollectorGenerator().generate(new OtelCollectorParser().parse(text)).map((f) => [f.path, f.content]));
96
- }
97
-
98
- /** The modules under `dir/`, keyed by their path inside it. */
99
- function under(all: Record<string, string>, dir: string): Record<string, string> {
100
- return Object.fromEntries(Object.entries(all).filter(([p]) => p.startsWith(`${dir}/`)).map(([p, c]) => [p.slice(dir.length + 1), c]));
101
- }
102
-
103
- /**
104
- * A fake Engine holding the collector's container as compose creates it: the
105
- * service's command, ports and restart policy, compose's labels, and the
106
- * config's content in the container at its target.
107
- */
108
- function engine(project: string, service: string, svc: Json, configText: string): DockerEngine {
109
- const target = ((svc.configs as Json[])[0]!.target as string) ?? COLLECTOR_CONFIG_PATH;
110
- const portBindings: Record<string, Array<{ HostIp: string; HostPort: string }>> = {};
111
- for (const p of (svc.ports as string[]) ?? []) {
112
- const [host, rest] = p.split(":");
113
- const [port, proto] = rest!.split("/");
114
- portBindings[`${port}/${proto ?? "tcp"}`] = [{ HostIp: "", HostPort: host! }];
115
- }
116
- const labels = {
117
- "com.docker.compose.project": project,
118
- "com.docker.compose.service": service,
119
- "com.docker.compose.container-number": "1",
120
- "com.docker.compose.oneoff": "False",
121
- "com.docker.compose.config-hash": "abc",
122
- };
123
- const inspect = {
124
- Id: "c1",
125
- Name: `/${project}-${service}-1`,
126
- Config: {
127
- Image: svc.image,
128
- Cmd: svc.command,
129
- Entrypoint: ["/otelcol-contrib"],
130
- Env: ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],
131
- Labels: { ...labels, "org.opencontainers.image.title": "otelcol-contrib" },
132
- WorkingDir: "",
133
- User: "10001",
134
- },
135
- HostConfig: { PortBindings: portBindings, RestartPolicy: { Name: svc.restart, MaximumRetryCount: 0 } },
136
- Mounts: [],
137
- NetworkSettings: { Networks: { [`${project}_default`]: {} } },
138
- };
139
- const image = {
140
- Config: {
141
- Cmd: ["--config", "/etc/otelcol-contrib/config.yaml"],
142
- Entrypoint: ["/otelcol-contrib"],
143
- Env: ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],
144
- Labels: { "org.opencontainers.image.title": "otelcol-contrib" },
145
- User: "10001",
146
- },
147
- };
148
- const ok = (body: unknown) => ({ status: 200, body: Buffer.from(JSON.stringify(body)) });
149
- return async (path) => {
150
- const url = new URL(`http://docker${path}`);
151
- if (url.pathname === "/containers/json") {
152
- const filters = JSON.parse(url.searchParams.get("filters") ?? "{}") as { label?: string[] };
153
- expect(filters.label).toEqual([`com.docker.compose.project=${project}`]);
154
- return ok([{ Id: "c1", Labels: labels }]);
155
- }
156
- if (url.pathname === "/containers/c1/json") return ok(inspect);
157
- if (url.pathname.startsWith("/images/")) return ok(image);
158
- if (url.pathname === "/containers/c1/archive") {
159
- if (url.searchParams.get("path") !== target) return { status: 404, body: Buffer.from('{"message":"not found"}') };
160
- return { status: 200, body: tarOf([target.split("/").pop()!, configText]) };
161
- }
162
- return { status: 404, body: Buffer.alloc(0) };
163
- };
164
- }
165
-
166
- describe("DockerOtelCollector through chant import (#3373, #4388)", () => {
167
- test("a live import gives the otel source a file import gives, and builds back to the same config", async () => {
168
- const { text: composeText, doc } = await compose(SOURCE);
169
- const { service, config } = collector(doc);
170
- const expected = standalone(config);
171
- expect(Object.keys(expected).length).toBeGreaterThan(0);
172
-
173
- const fileProject = project();
174
- const liveProject = project();
175
- try {
176
- // File import of the build's docker-compose.yml.
177
- const templatePath = join(fileProject.dir, "docker-compose.yml");
178
- writeFileSync(templatePath, composeText);
179
- const fileResult = await importCommand({ templatePath, output: fileProject.srcDir, lexicon: "docker" });
180
- expect(fileResult.error).toBeUndefined();
181
- expect(fileResult.warnings.filter((w) => w.includes("kept as written"))).toEqual([]);
182
- const fromFile = files(fileProject.srcDir);
183
-
184
- // Live import from a fake Engine.
185
- const svc = (doc.services as Record<string, Json>)[service]!;
186
- const plugin: LexiconPlugin = { ...dockerPlugin, exportResources: (options) => exportResources(options, engine("test", service, svc, config)) };
187
- const liveResult = await liveImportFromPlugins([plugin, otelPlugin], {
188
- environment: "test",
189
- output: liveProject.srcDir,
190
- lexicon: "docker",
191
- force: true,
192
- });
193
- expect(liveResult.error).toBeUndefined();
194
- expect(liveResult.success).toBe(true);
195
- expect(liveResult.warnings.filter((w) => w.includes("kept as written"))).toEqual([]);
196
- const fromLive = files(liveProject.srcDir);
197
-
198
- const dir = service.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
199
- expect(under(fromLive, dir)).toEqual(expected);
200
- expect(under(fromFile, dir)).toEqual(expected);
201
- expect(fromLive["main.ts"]).toContain('import { collectorYaml } from "@intentius/chant-lexicon-otel";');
202
- expect(fromLive["main.ts"]).toContain("content: collectorYaml(");
203
-
204
- // The imported source builds back to the same service and config.
205
- const result = await build(liveProject.srcDir, [dockerSerializer, otelSerializer]);
206
- expect(result.errors).toEqual([]);
207
- const rebuilt = { doc: load(composeOf(result.outputs.get("docker"))) as Json };
208
- const after = collector(rebuilt.doc);
209
- expect(load(after.config)).toEqual(load(config));
210
- const before = (doc.services as Record<string, Json>)[service]!;
211
- const back = (rebuilt.doc.services as Record<string, Json>)[after.service]!;
212
- expect(back.image).toBe(before.image);
213
- expect(back.command).toEqual(before.command);
214
- expect(back.ports).toEqual(before.ports);
215
- expect(back.restart).toBe(before.restart);
216
- expect((back.configs as Json[])[0]!.target).toBe(COLLECTOR_CONFIG_PATH);
217
- } finally {
218
- rmSync(fileProject.dir, { recursive: true, force: true });
219
- rmSync(liveProject.dir, { recursive: true, force: true });
220
- }
221
- });
222
-
223
- test("without a context the export keeps the content as read, and offers a reparse", async () => {
224
- const { doc } = await compose(SOURCE);
225
- const { service, config } = collector(doc);
226
- const svc = (doc.services as Record<string, Json>)[service]!;
227
- const ir = await exportResources({ environment: "test" }, engine("test", service, svc, config));
228
- const cfg = ir.resources.find((r) => r.type === "config")!;
229
- expect(cfg.properties.content).toBe(config);
230
- expect(typeof ir.reparse).toBe("function");
231
- });
232
-
233
- test("only a config's content that is a document is offered", () => {
234
- const probe = new EmbeddedImports([], { quiet: true });
235
- const compose = [
236
- "services:",
237
- " app:",
238
- " image: x",
239
- " configs: [{ source: motd, target: /etc/motd }, { source: settings, target: /etc/app.yaml }, { source: fromfile }]",
240
- "configs:",
241
- " motd: { content: \"hello there\" }",
242
- " settings: { content: \"a: 1\\n\" }",
243
- " fromfile: { file: ./x.yaml }",
244
- ].join("\n");
245
- new DockerParser().parse(compose, { embedded: probe });
246
- expect(probe.offered.map((c) => [c.location, c.directory])).toEqual([["config settings content", "app"]]);
247
- });
248
- });
@@ -1,83 +0,0 @@
1
- /**
2
- * Where a Compose file embeds another lexicon's content, for `chant import`
3
- * (#3373, #4388).
4
- *
5
- * A top-level config with `content` is a file Compose writes into each
6
- * container that mounts it. The parser offers that text to core's
7
- * embedded-content resolver, as k8s offers a ConfigMap value (#2962) and fly
8
- * a machine file. `DockerOtelCollector` carries its collector config this way,
9
- * at the otel lexicon's `COLLECTOR_CONFIG_PATH`, so a file or live import of
10
- * the project gives the same otel source `chant import config.yaml` gives.
11
- *
12
- * Which lexicon imports the content is decided by core at run time; the hint
13
- * below only names the owner a warning suggests when none is installed.
14
- */
15
-
16
- import type { EmbeddedContent, EmbeddedContentResolver } from "@intentius/chant/import/embedded";
17
- import { embeddedDocument } from "@intentius/chant/import/embedded";
18
- import { COLLECTOR_CONFIG_PATH } from "@intentius/chant-lexicon-otel";
19
-
20
- function isRecord(v: unknown): v is Record<string, unknown> {
21
- return typeof v === "object" && v !== null && !Array.isArray(v);
22
- }
23
-
24
- const COLLECTOR = { lexicon: "otel", what: "an OpenTelemetry Collector config" } as const;
25
-
26
- function hintFor(targets: string[], doc: unknown): EmbeddedContent["expectedOwner"] {
27
- if (targets.includes(COLLECTOR_CONFIG_PATH)) return COLLECTOR;
28
- if (isRecord(doc) && isRecord(doc.service) && isRecord(doc.service.pipelines)) return COLLECTOR;
29
- return undefined;
30
- }
31
-
32
- /** Each service's mounts of top-level configs: config name, then the services and targets that mount it. */
33
- function mountsByConfig(services: Record<string, unknown> | undefined): Map<string, Array<{ service: string; target: string }>> {
34
- const out = new Map<string, Array<{ service: string; target: string }>>();
35
- for (const [service, raw] of Object.entries(services ?? {})) {
36
- if (!isRecord(raw) || !Array.isArray(raw.configs)) continue;
37
- for (const c of raw.configs) {
38
- const source = typeof c === "string" ? c : isRecord(c) && typeof c.source === "string" ? c.source : undefined;
39
- if (!source) continue;
40
- const target = isRecord(c) && typeof c.target === "string" ? c.target : `/${source}`;
41
- const list = out.get(source) ?? [];
42
- list.push({ service, target });
43
- out.set(source, list);
44
- }
45
- }
46
- return out;
47
- }
48
-
49
- /**
50
- * Offer each top-level config's `content` to `embedded`, replacing the value
51
- * an owner imports with its reference. `configs` maps config name to its IR
52
- * properties and is changed in place with fresh copies; `services` is the
53
- * compose document's services section, read for where each config is mounted.
54
- *
55
- * The owner's modules go in a directory named after the one service that
56
- * mounts the config, else after the config, so a file import and a live
57
- * import (which cannot recover the config's key) write them to the same place.
58
- */
59
- export function delegateEmbedded(
60
- configs: Map<string, Record<string, unknown>>,
61
- services: Record<string, unknown> | undefined,
62
- embedded: EmbeddedContentResolver,
63
- ): void {
64
- const mounts = mountsByConfig(services);
65
- for (const [name, props] of configs) {
66
- const text = props.content;
67
- if (typeof text !== "string") continue;
68
- const doc = embeddedDocument(text);
69
- if (!isRecord(doc)) continue;
70
- const users = mounts.get(name) ?? [];
71
- const owners = new Set(users.map((u) => u.service));
72
- const ref = embedded.resolve({
73
- host: "docker",
74
- hostType: "config",
75
- location: `config ${name} content`,
76
- directory: owners.size === 1 ? [...owners][0]! : name,
77
- text,
78
- document: doc,
79
- expectedOwner: hintFor(users.map((u) => u.target), doc),
80
- });
81
- if (ref) configs.set(name, { ...props, content: ref });
82
- }
83
- }
@@ -1,107 +0,0 @@
1
- /**
2
- * A read-only Docker Engine API client for live export (#4388), and the tar
3
- * reader for `GET /containers/{id}/archive`.
4
- *
5
- * The client is a single function, `(path) => { status, body }`, so tests hand
6
- * export a fake Engine and never need a daemon. The default one speaks HTTP
7
- * over the socket `DOCKER_HOST` names (`unix://…`, or plain `tcp://host:port`),
8
- * else `/var/run/docker.sock`, with the unversioned API paths the daemon
9
- * serves at its own version.
10
- */
11
-
12
- import { request } from "node:http";
13
-
14
- /** One GET against the Engine API. `path` starts with `/`. */
15
- export type DockerEngine = (path: string, signal?: AbortSignal) => Promise<{ status: number; body: Buffer }>;
16
-
17
- const DEFAULT_SOCKET = "/var/run/docker.sock";
18
-
19
- /** Where `DOCKER_HOST` points: a unix socket path, or a host and port. */
20
- export function engineAddress(dockerHost = process.env.DOCKER_HOST): { socketPath: string } | { host: string; port: number } {
21
- if (!dockerHost) return { socketPath: DEFAULT_SOCKET };
22
- if (dockerHost.startsWith("unix://")) return { socketPath: dockerHost.slice("unix://".length) };
23
- if (dockerHost.startsWith("tcp://")) {
24
- if (process.env.DOCKER_TLS_VERIFY) {
25
- throw new Error("docker live export: DOCKER_TLS_VERIFY is set, and a TLS Engine endpoint is not supported; use a unix socket or a plain tcp:// endpoint");
26
- }
27
- const url = new URL(`http://${dockerHost.slice("tcp://".length)}`);
28
- return { host: url.hostname, port: Number(url.port || 2375) };
29
- }
30
- throw new Error(`docker live export: DOCKER_HOST ${dockerHost} is not a unix:// or tcp:// endpoint`);
31
- }
32
-
33
- /** The Engine at `DOCKER_HOST` (or the default socket). */
34
- export function defaultDockerEngine(dockerHost?: string): DockerEngine {
35
- const address = engineAddress(dockerHost);
36
- return (path, signal) =>
37
- new Promise((resolve, reject) => {
38
- const req = request({ ...address, path, method: "GET", headers: { Host: "docker" }, signal }, (res) => {
39
- const chunks: Buffer[] = [];
40
- res.on("data", (c: Buffer) => chunks.push(c));
41
- res.on("end", () => resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks) }));
42
- res.on("error", reject);
43
- });
44
- req.on("error", reject);
45
- req.end();
46
- });
47
- }
48
-
49
- /** One entry of a tar stream. `type` is the ustar typeflag (`0` file, `5` directory, …). */
50
- export interface TarEntry {
51
- name: string;
52
- type: string;
53
- data: Buffer;
54
- }
55
-
56
- function field(block: Buffer, start: number, length: number): string {
57
- const raw = block.subarray(start, start + length);
58
- const end = raw.indexOf(0);
59
- return raw.subarray(0, end === -1 ? raw.length : end).toString("utf8");
60
- }
61
-
62
- /** The entries of an uncompressed tar stream. PAX and GNU long-name records are applied to the entry after them. */
63
- export function readTar(tar: Buffer): TarEntry[] {
64
- const out: TarEntry[] = [];
65
- let offset = 0;
66
- let longName: string | undefined;
67
- while (offset + 512 <= tar.length) {
68
- const header = tar.subarray(offset, offset + 512);
69
- if (header.every((b) => b === 0)) break;
70
- const size = parseInt(field(header, 124, 12).trim() || "0", 8);
71
- const type = field(header, 156, 1) || "0";
72
- const prefix = field(header, 345, 155);
73
- let name = prefix ? `${prefix}/${field(header, 0, 100)}` : field(header, 0, 100);
74
- const data = tar.subarray(offset + 512, offset + 512 + size);
75
- offset += 512 + Math.ceil(size / 512) * 512;
76
-
77
- if (type === "x") {
78
- const path = /(?:^|\n)\d+ path=([^\n]*)\n/.exec(data.toString("utf8"));
79
- if (path) longName = path[1];
80
- continue;
81
- }
82
- if (type === "L") {
83
- longName = data.toString("utf8").replace(/\0+$/, "");
84
- continue;
85
- }
86
- if (type === "g") continue;
87
- if (longName !== undefined) {
88
- name = longName;
89
- longName = undefined;
90
- }
91
- out.push({ name, type: type === "\0" ? "0" : type, data: Buffer.from(data) });
92
- }
93
- return out;
94
- }
95
-
96
- /**
97
- * The text of the one regular file an archive holds, or undefined when it
98
- * holds anything else (a directory, a link, several entries) or the bytes
99
- * are not UTF-8 text.
100
- */
101
- export function singleFileText(tar: Buffer): string | undefined {
102
- const entries = readTar(tar);
103
- if (entries.length !== 1 || entries[0]!.type !== "0") return undefined;
104
- const bytes = entries[0]!.data;
105
- const text = bytes.toString("utf8");
106
- return Buffer.from(text, "utf8").equals(bytes) ? text : undefined;
107
- }
@@ -1,134 +0,0 @@
1
- import { describe, test, expect } from "vitest";
2
- import { DockerGenerator } from "./generator";
3
- import type { ServiceIR, VolumeIR, NetworkIR, ConfigIR, SecretIR, DockerfileIR } from "./parser";
4
-
5
- describe("DockerGenerator", () => {
6
- test("generates Service from IR with image", () => {
7
- const entities: ServiceIR[] = [
8
- { kind: "service", name: "api", props: { image: "nginx:1.25" } },
9
- ];
10
- const { source } = new DockerGenerator().generate(entities);
11
- expect(source).toContain("import { Service }");
12
- expect(source).toContain("new Service(");
13
- expect(source).toContain("nginx:1.25");
14
- });
15
-
16
- test("generates Service with ports and environment", () => {
17
- const entities: ServiceIR[] = [
18
- {
19
- kind: "service",
20
- name: "api",
21
- props: {
22
- image: "node:20",
23
- ports: ["3000:3000"],
24
- environment: { NODE_ENV: "production" },
25
- },
26
- },
27
- ];
28
- const { source } = new DockerGenerator().generate(entities);
29
- expect(source).toContain("3000:3000");
30
- expect(source).toContain("NODE_ENV");
31
- expect(source).toContain("production");
32
- });
33
-
34
- test("generates Config entity", () => {
35
- const entities: ConfigIR[] = [
36
- { kind: "config", name: "app-config", props: { file: "./config/app.conf" } },
37
- ];
38
- const { source } = new DockerGenerator().generate(entities);
39
- expect(source).toContain("import { DockerConfig, composeKey }");
40
- // app-config is not an identifier, so the key is set explicitly (#3937).
41
- expect(source).toContain('export const appConfig = composeKey("app-config", new DockerConfig(');
42
- expect(source).toContain("app.conf");
43
- });
44
-
45
- test("generates Secret entity", () => {
46
- const entities: SecretIR[] = [
47
- { kind: "secret", name: "db-password", props: { file: "./secrets/db.txt" } },
48
- ];
49
- const { source } = new DockerGenerator().generate(entities);
50
- expect(source).toContain("import { DockerSecret, composeKey }");
51
- expect(source).toContain('export const dbPassword = composeKey("db-password", new DockerSecret(');
52
- });
53
-
54
- test("single-stage Dockerfile generates flat props (existing behaviour)", () => {
55
- const entities: DockerfileIR[] = [
56
- {
57
- kind: "dockerfile",
58
- name: "builder",
59
- stages: [
60
- {
61
- from: "node:20-alpine",
62
- instructions: [
63
- { instruction: "RUN", value: "npm ci" },
64
- { instruction: "CMD", value: '["node", "index.js"]' },
65
- ],
66
- },
67
- ],
68
- },
69
- ];
70
- const { source } = new DockerGenerator().generate(entities);
71
- expect(source).toContain("import { Dockerfile }");
72
- expect(source).toContain("new Dockerfile(");
73
- expect(source).toContain("node:20-alpine");
74
- expect(source).not.toContain('"stages"');
75
- });
76
-
77
- test("multi-stage Dockerfile generates stages: array form", () => {
78
- const entities: DockerfileIR[] = [
79
- {
80
- kind: "dockerfile",
81
- name: "app",
82
- stages: [
83
- { from: "node:20-alpine", as: "builder", instructions: [{ instruction: "RUN", value: "npm ci" }] },
84
- { from: "nginx:1.25", as: "runner", instructions: [] },
85
- ],
86
- },
87
- ];
88
- const { source } = new DockerGenerator().generate(entities);
89
- expect(source).toContain("stages");
90
- expect(source).toContain("node:20-alpine");
91
- expect(source).toContain("nginx:1.25");
92
- });
93
-
94
- test("import line includes only needed types", () => {
95
- const entities: VolumeIR[] = [
96
- { kind: "volume", name: "data", props: {} },
97
- ];
98
- const { source } = new DockerGenerator().generate(entities);
99
- expect(source).toContain("import { Volume }");
100
- expect(source).not.toContain("Service");
101
- expect(source).not.toContain("Network");
102
- });
103
-
104
- test("generates correct imports for multiple entity types", () => {
105
- const entities = [
106
- { kind: "service" as const, name: "api", props: { image: "nginx:1.25" } },
107
- {
108
- kind: "dockerfile" as const,
109
- name: "build",
110
- stages: [{ from: "node:20", instructions: [] }],
111
- },
112
- ];
113
- const { source } = new DockerGenerator().generate(entities);
114
- expect(source).toContain("Dockerfile");
115
- expect(source).toContain("Service");
116
- });
117
-
118
- test("sanitizes kebab-case names to camelCase", () => {
119
- const entities: ServiceIR[] = [
120
- { kind: "service", name: "my-web-server", props: { image: "nginx:1.25" } },
121
- ];
122
- const { source } = new DockerGenerator().generate(entities);
123
- expect(source).toContain("myWebServer");
124
- });
125
-
126
- test("generates Network entity", () => {
127
- const entities: NetworkIR[] = [
128
- { kind: "network", name: "backend", props: { driver: "bridge" } },
129
- ];
130
- const { source } = new DockerGenerator().generate(entities);
131
- expect(source).toContain("import { Network }");
132
- expect(source).toContain("new Network(");
133
- });
134
- });