@intentius/chant-lexicon-docker 0.124.98 → 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 -250
  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,359 +0,0 @@
1
- /**
2
- * Pure export helpers for the docker lexicon (#4388): turn what the Engine API
3
- * returns for a compose project's containers back into the compose document a
4
- * user would have written, and map it to import IR through `DockerParser`. All
5
- * Engine I/O stays in the caller (`../export-resources`).
6
- *
7
- * A compose project is the set of containers labelled
8
- * `com.docker.compose.project=<name>`; each service is the container labelled
9
- * `com.docker.compose.service`, the lowest `container-number` when scaled.
10
- * One-off containers (`compose run`) are skipped.
11
- *
12
- * Configs. Outside swarm, a top-level config is not an Engine object. A
13
- * `content` config is copied into the container when it is created, and a
14
- * `file` config is bind-mounted; neither is named anywhere the Engine keeps.
15
- * What the container does keep is its command, so the files a service is
16
- * told to read (`--config=/etc/otelcol-contrib/config.yaml`) are the
17
- * candidates, and the caller reads each through
18
- * `GET /containers/{id}/archive?path=…`, which works for both forms and for a
19
- * remote Engine whose host paths are out of reach. A candidate that reads back
20
- * as one text file becomes a `content` config mounted at that path, named
21
- * `<service>-<file name>` (the original key is not recoverable). A candidate
22
- * inside a volume or a directory bind mount belongs to that mount and is not
23
- * read.
24
- *
25
- * The strip. By default fields the image supplies (its command, entrypoint,
26
- * environment, labels, working dir and user) are dropped, so a service reaches
27
- * the shape it was declared in; `verbatim` keeps them. Compose's own
28
- * `com.docker.compose.*` labels are always dropped: compose writes them, and a
29
- * declaration that set them would fight it.
30
- *
31
- * Ownership. The docker serializer stamps no marker, so under `owned` only
32
- * services carrying the label marker (`app.kubernetes.io/managed-by: chant`,
33
- * set with `defaultLabels`) are exported, and the caller logs that once.
34
- */
35
-
36
- import type { ExportedTemplate, ResourceSelector } from "@intentius/chant/lexicon";
37
- import type { ParseContext, TemplateIR } from "@intentius/chant/import/parser";
38
- import { hasOwnershipMarker, LABEL_OWNERSHIP_KEYS } from "@intentius/chant/ownership";
39
- import { DockerParser } from "./parser";
40
- import { dockerResultToTemplate } from "./adapter";
41
-
42
- export const PROJECT_LABEL = "com.docker.compose.project";
43
- export const SERVICE_LABEL = "com.docker.compose.service";
44
- const NUMBER_LABEL = "com.docker.compose.container-number";
45
- const ONEOFF_LABEL = "com.docker.compose.oneoff";
46
- const DEPENDS_LABEL = "com.docker.compose.depends_on";
47
-
48
- type Json = Record<string, unknown>;
49
-
50
- /** A container's or an image's healthcheck, durations in nanoseconds. */
51
- export interface HealthcheckInspect {
52
- Test?: string[];
53
- Interval?: number;
54
- Timeout?: number;
55
- Retries?: number;
56
- StartPeriod?: number;
57
- }
58
-
59
- /** What `GET /containers/{id}/json` returns, as far as export reads it. */
60
- export interface ContainerInspect {
61
- Id?: string;
62
- Name?: string;
63
- Config?: {
64
- Image?: string;
65
- Cmd?: string[] | null;
66
- Entrypoint?: string[] | null;
67
- Env?: string[] | null;
68
- Labels?: Record<string, string> | null;
69
- WorkingDir?: string;
70
- User?: string;
71
- Healthcheck?: HealthcheckInspect | null;
72
- };
73
- HostConfig?: {
74
- PortBindings?: Record<string, Array<{ HostIp?: string; HostPort?: string }> | null> | null;
75
- RestartPolicy?: { Name?: string; MaximumRetryCount?: number } | null;
76
- };
77
- Mounts?: Array<{ Type?: string; Name?: string; Source?: string; Destination?: string; RW?: boolean }>;
78
- NetworkSettings?: { Networks?: Record<string, unknown> | null };
79
- }
80
-
81
- /** What `GET /images/{name}/json` returns, as far as export reads it. */
82
- export interface ImageInspect {
83
- Config?: {
84
- Cmd?: string[] | null;
85
- Entrypoint?: string[] | null;
86
- Env?: string[] | null;
87
- Labels?: Record<string, string> | null;
88
- WorkingDir?: string;
89
- User?: string;
90
- Healthcheck?: HealthcheckInspect | null;
91
- };
92
- }
93
-
94
- /** One service as read from the Engine: its container, its image, and the config files read out of it. */
95
- export interface LiveService {
96
- name: string;
97
- container: ContainerInspect;
98
- image?: ImageInspect;
99
- /** Config target path → the text read from the container. */
100
- files: Record<string, string>;
101
- }
102
-
103
- function same(a: unknown, b: unknown): boolean {
104
- return JSON.stringify(a ?? null) === JSON.stringify(b ?? null);
105
- }
106
-
107
- /**
108
- * The containers to export, one per service: compose-labelled for `project`,
109
- * not one-off, and the lowest container number when a service is scaled.
110
- * `summaries` is what `GET /containers/json` returns.
111
- */
112
- export function serviceContainers(project: string, summaries: Array<{ Id?: string; Labels?: Record<string, string> | null }>): Map<string, string> {
113
- const best = new Map<string, { id: string; n: number }>();
114
- for (const s of summaries) {
115
- const labels = s.Labels ?? {};
116
- if (labels[PROJECT_LABEL] !== project || !s.Id) continue;
117
- if ((labels[ONEOFF_LABEL] ?? "").toLowerCase() === "true") continue;
118
- const service = labels[SERVICE_LABEL];
119
- if (!service) continue;
120
- const n = Number(labels[NUMBER_LABEL] ?? "1") || 1;
121
- const prev = best.get(service);
122
- if (!prev || n < prev.n) best.set(service, { id: s.Id, n });
123
- }
124
- return new Map([...best].sort(([a], [b]) => a.localeCompare(b)).map(([service, { id }]) => [service, id]));
125
- }
126
-
127
- function under(path: string, dir: string): boolean {
128
- const d = dir.endsWith("/") ? dir : `${dir}/`;
129
- return path.startsWith(d);
130
- }
131
-
132
- /**
133
- * The paths a container's command names that may be config files: an
134
- * absolute path with a file extension, alone or as a `--flag=` value (an
135
- * otel `file:` URI included). A path inside a volume, or inside a directory
136
- * bind mount, belongs to that mount and is left out.
137
- */
138
- export function configCandidates(container: ContainerInspect): string[] {
139
- const args = [...(container.Config?.Entrypoint ?? []), ...(container.Config?.Cmd ?? [])];
140
- const mounts = container.Mounts ?? [];
141
- const out: string[] = [];
142
- for (const arg of args) {
143
- let value = arg.startsWith("-") && arg.includes("=") ? arg.slice(arg.indexOf("=") + 1) : arg;
144
- if (value.startsWith("file:")) value = value.slice("file:".length);
145
- if (!/^\/[^\s,;:]+$/.test(value)) continue;
146
- const base = value.split("/").pop()!;
147
- if (!/\.[A-Za-z0-9]+$/.test(base)) continue;
148
- if (mounts.some((m) => m.Destination && under(value, m.Destination))) continue;
149
- if (!out.includes(value)) out.push(value);
150
- }
151
- return out;
152
- }
153
-
154
- function stripProjectPrefix(project: string, name: string): string | undefined {
155
- return name.startsWith(`${project}_`) ? name.slice(project.length + 1) : undefined;
156
- }
157
-
158
- function envMap(env: string[]): Json {
159
- const out: Json = {};
160
- for (const e of env) {
161
- const i = e.indexOf("=");
162
- if (i === -1) out[e] = null;
163
- else out[e.slice(0, i)] = e.slice(i + 1);
164
- }
165
- return out;
166
- }
167
-
168
- function duration(ns: number | undefined): string | undefined {
169
- if (!ns) return undefined;
170
- const ms = ns / 1e6;
171
- return ms % 1000 === 0 ? `${ms / 1000}s` : `${ms}ms`;
172
- }
173
-
174
- function portList(bindings: NonNullable<ContainerInspect["HostConfig"]>["PortBindings"]): string[] {
175
- const out: string[] = [];
176
- for (const [key, binds] of Object.entries(bindings ?? {})) {
177
- const [port, proto] = key.split("/");
178
- const suffix = proto && proto !== "tcp" ? `/${proto}` : "";
179
- if (!binds || binds.length === 0) {
180
- out.push(`${port}${suffix}`);
181
- continue;
182
- }
183
- for (const b of binds) {
184
- const ip = b.HostIp && b.HostIp !== "0.0.0.0" && b.HostIp !== "::" ? `${b.HostIp}:` : "";
185
- out.push(b.HostPort ? `${ip}${b.HostPort}:${port}${suffix}` : `${port}${suffix}`);
186
- }
187
- }
188
- // The Engine may list `::` and `0.0.0.0` bindings of one port separately.
189
- return [...new Set(out)];
190
- }
191
-
192
- function dependsOn(label: string | undefined): unknown {
193
- if (!label) return undefined;
194
- const entries = label
195
- .split(",")
196
- .map((e) => e.split(":"))
197
- .filter(([name]) => !!name);
198
- if (entries.length === 0) return undefined;
199
- if (entries.every(([, condition, restart]) => (condition ?? "service_started") === "service_started" && restart !== "true")) {
200
- return entries.map(([name]) => name);
201
- }
202
- return Object.fromEntries(
203
- entries.map(([name, condition, restart]) => [name, { condition: condition || "service_started", ...(restart === "true" ? { restart: true } : {}) }]),
204
- );
205
- }
206
-
207
- /** The compose service a container was created from, and the config files it mounts. */
208
- function serviceFromContainer(
209
- project: string,
210
- live: LiveService,
211
- verbatim: boolean,
212
- ): { service: Json; configs: Array<{ name: string; target: string; content: string }>; volumes: string[]; networks: string[] } {
213
- const c = live.container.Config ?? {};
214
- const img = live.image?.Config ?? {};
215
- const keep = (live: unknown, image: unknown) => verbatim || !same(live, image);
216
- const service: Json = {};
217
-
218
- if (c.Image) service.image = c.Image;
219
- if (c.Entrypoint && c.Entrypoint.length > 0 && keep(c.Entrypoint, img.Entrypoint)) service.entrypoint = c.Entrypoint;
220
- if (c.Cmd && c.Cmd.length > 0 && keep(c.Cmd, img.Cmd)) service.command = c.Cmd;
221
-
222
- const imageEnv = new Set(img.Env ?? []);
223
- const env = (c.Env ?? []).filter((e) => verbatim || !imageEnv.has(e));
224
- if (env.length > 0) service.environment = envMap(env);
225
-
226
- const labels: Record<string, string> = {};
227
- for (const [k, v] of Object.entries(c.Labels ?? {})) {
228
- if (k.startsWith("com.docker.compose.")) continue;
229
- if (!verbatim && img.Labels?.[k] === v) continue;
230
- labels[k] = v;
231
- }
232
- if (Object.keys(labels).length > 0) service.labels = labels;
233
-
234
- if (c.WorkingDir && keep(c.WorkingDir, img.WorkingDir ?? "")) service.working_dir = c.WorkingDir;
235
- if (c.User && keep(c.User, img.User ?? "")) service.user = c.User;
236
-
237
- if (c.Healthcheck?.Test && c.Healthcheck.Test.length > 0 && keep(c.Healthcheck, img.Healthcheck)) {
238
- const h = c.Healthcheck;
239
- service.healthcheck = {
240
- test: h.Test,
241
- ...(duration(h.Interval) ? { interval: duration(h.Interval) } : {}),
242
- ...(duration(h.Timeout) ? { timeout: duration(h.Timeout) } : {}),
243
- ...(h.Retries ? { retries: h.Retries } : {}),
244
- ...(duration(h.StartPeriod) ? { start_period: duration(h.StartPeriod) } : {}),
245
- };
246
- }
247
-
248
- const ports = portList(live.container.HostConfig?.PortBindings);
249
- if (ports.length > 0) service.ports = ports;
250
-
251
- const restart = live.container.HostConfig?.RestartPolicy;
252
- if (restart?.Name && restart.Name !== "no") {
253
- service.restart = restart.Name === "on-failure" && restart.MaximumRetryCount ? `on-failure:${restart.MaximumRetryCount}` : restart.Name;
254
- }
255
-
256
- const deps = dependsOn(c.Labels?.[DEPENDS_LABEL]);
257
- if (deps !== undefined) service.depends_on = deps;
258
-
259
- // Mounts: a config read at a mount's destination is that config, not a volume.
260
- const volumes: string[] = [];
261
- const mountList: string[] = [];
262
- for (const m of live.container.Mounts ?? []) {
263
- if (!m.Destination || live.files[m.Destination] !== undefined) continue;
264
- const ro = m.RW === false ? ":ro" : "";
265
- if (m.Type === "volume" && m.Name) {
266
- const key = stripProjectPrefix(project, m.Name);
267
- if (key) {
268
- volumes.push(key);
269
- mountList.push(`${key}:${m.Destination}${ro}`);
270
- } else if (/^[0-9a-f]{64}$/.test(m.Name)) {
271
- mountList.push(m.Destination);
272
- } else {
273
- volumes.push(m.Name);
274
- mountList.push(`${m.Name}:${m.Destination}${ro}`);
275
- }
276
- } else if (m.Type === "bind" && m.Source) {
277
- mountList.push(`${m.Source}:${m.Destination}${ro}`);
278
- }
279
- }
280
- if (mountList.length > 0) service.volumes = mountList;
281
-
282
- const networks: string[] = [];
283
- for (const net of Object.keys(live.container.NetworkSettings?.Networks ?? {})) {
284
- networks.push(stripProjectPrefix(project, net) ?? net);
285
- }
286
- const declaredNets = networks.filter((n) => n !== "default");
287
- if (declaredNets.length > 0) service.networks = networks;
288
-
289
- const configs = Object.entries(live.files).map(([target, content]) => {
290
- const base = target.split("/").pop()!.replace(/\.[^.]*$/, "");
291
- return { name: `${live.name}-${base}`, target, content };
292
- });
293
- if (configs.length > 0) service.configs = configs.map(({ name, target }) => ({ source: name, target }));
294
-
295
- return { service, configs, volumes, networks: declaredNets };
296
- }
297
-
298
- /**
299
- * The compose document a project's services were created from. External
300
- * volumes and networks (not prefixed with the project name) are declared
301
- * `external`.
302
- */
303
- export function composeFromLive(project: string, services: LiveService[], opts: { verbatim?: boolean } = {}): Json {
304
- const doc: { services: Json; configs: Json; volumes: Json; networks: Json } = { services: {}, configs: {}, volumes: {}, networks: {} };
305
- for (const live of services) {
306
- const { service, configs, volumes, networks } = serviceFromContainer(project, live, opts.verbatim ?? false);
307
- doc.services[live.name] = service;
308
- for (const cfg of configs) doc.configs[cfg.name] = { content: cfg.content };
309
- for (const v of volumes) {
310
- const owned = (live.container.Mounts ?? []).some((m) => m.Name === `${project}_${v}`);
311
- doc.volumes[v] = owned ? {} : { external: true };
312
- }
313
- for (const n of networks) {
314
- const owned = Object.keys(live.container.NetworkSettings?.Networks ?? {}).includes(`${project}_${n}`);
315
- doc.networks[n] = owned ? {} : { external: true };
316
- }
317
- }
318
- const out: Json = { services: doc.services };
319
- for (const section of ["volumes", "networks", "configs"] as const) {
320
- if (Object.keys(doc[section]).length > 0) out[section] = doc[section];
321
- }
322
- return out;
323
- }
324
-
325
- /** Whether a container carries chant's label marker. */
326
- export function isChantOwned(container: ContainerInspect): boolean {
327
- return hasOwnershipMarker(container.Config?.Labels ?? undefined, LABEL_OWNERSHIP_KEYS);
328
- }
329
-
330
- /**
331
- * Build export IR from one compose project's services, through
332
- * {@link DockerParser} as the single compose → IR mapping. `owned` keeps only
333
- * services carrying chant's marker; `selector` narrows by IR type (`service`,
334
- * `config`, …) and compose key. `reparse(context)` parses the same document
335
- * again with a `ParseContext`, so `chant import --from` hands a collector
336
- * config to the otel importer, as a file import does (#3373).
337
- */
338
- export function buildExportFromProject(
339
- project: string,
340
- services: LiveService[],
341
- opts: { verbatim?: boolean; selector?: ResourceSelector; owned?: boolean } = {},
342
- ): ExportedTemplate {
343
- const kept = opts.owned ? services.filter((s) => isChantOwned(s.container)) : services;
344
- if (kept.length === 0) return { resources: [], parameters: [] };
345
- const doc = composeFromLive(project, kept, { verbatim: opts.verbatim });
346
- const parse = (context?: ParseContext): TemplateIR =>
347
- select(dockerResultToTemplate(new DockerParser().parseDocument(doc, context)), opts.selector);
348
- return { ...parse(), reparse: (context) => parse(context) };
349
- }
350
-
351
- function select(ir: TemplateIR, selector: ResourceSelector | undefined): TemplateIR {
352
- if (!selector || (selector.type === undefined && selector.name === undefined)) return ir;
353
- return {
354
- ...ir,
355
- resources: ir.resources.filter(
356
- (r) => (selector.type === undefined || r.type === selector.type) && (selector.name === undefined || r.logicalId === selector.name),
357
- ),
358
- };
359
- }
@@ -1,137 +0,0 @@
1
- import { describe, test, expect } from "vitest";
2
- import { readFileSync } from "fs";
3
- import { join } from "path";
4
- import { DockerParser, DockerfileParser } from "./parser";
5
-
6
- const testdata = (file: string) =>
7
- readFileSync(join(import.meta.dirname, "testdata", file), "utf8");
8
-
9
- describe("DockerParser — services", () => {
10
- test("image extracted correctly", () => {
11
- const yaml = `services:\n api:\n image: nginx:1.25\n`;
12
- const { entities } = new DockerParser().parse(yaml);
13
- const svc = entities.find((e) => e.name === "api");
14
- expect(svc?.kind).toBe("service");
15
- expect((svc?.props as any).image).toBe("nginx:1.25");
16
- });
17
-
18
- test("ports array preserved", () => {
19
- const yaml = `services:\n web:\n image: nginx:1.25\n ports:\n - "80:80"\n - "443:443"\n`;
20
- const { entities } = new DockerParser().parse(yaml);
21
- const svc = entities.find((e) => e.name === "web");
22
- expect((svc?.props as any).ports).toEqual(["80:80", "443:443"]);
23
- });
24
-
25
- test("environment map preserved", () => {
26
- const yaml = `services:\n app:\n image: node:20\n environment:\n NODE_ENV: production\n PORT: "3000"\n`;
27
- const { entities } = new DockerParser().parse(yaml);
28
- const svc = entities.find((e) => e.name === "app");
29
- expect((svc?.props as any).environment).toEqual({ NODE_ENV: "production", PORT: "3000" });
30
- });
31
-
32
- test("volumes list preserved", () => {
33
- const yaml = `services:\n db:\n image: postgres:16\n volumes:\n - "pg-data:/var/lib/postgresql/data"\n`;
34
- const { entities } = new DockerParser().parse(yaml);
35
- const svc = entities.find((e) => e.name === "db");
36
- expect((svc?.props as any).volumes).toEqual(["pg-data:/var/lib/postgresql/data"]);
37
- });
38
-
39
- test("depends_on list preserved", () => {
40
- const yaml = `services:\n api:\n image: node:20\n depends_on:\n - db\n - cache\n`;
41
- const { entities } = new DockerParser().parse(yaml);
42
- const svc = entities.find((e) => e.name === "api");
43
- expect((svc?.props as any).depends_on).toEqual(["db", "cache"]);
44
- });
45
-
46
- test("restart string preserved", () => {
47
- const yaml = `services:\n app:\n image: myapp:latest\n restart: unless-stopped\n`;
48
- const { entities } = new DockerParser().parse(yaml);
49
- const svc = entities.find((e) => e.name === "app");
50
- expect((svc?.props as any).restart).toBe("unless-stopped");
51
- });
52
-
53
- test("healthcheck object preserved", () => {
54
- const { entities } = new DockerParser().parse(testdata("webapp.yaml"));
55
- const svc = entities.find((e) => e.name === "api");
56
- const hc = (svc?.props as any).healthcheck;
57
- expect(hc).toBeDefined();
58
- expect(hc.interval).toBe("30s");
59
- expect(hc.retries).toBe(3);
60
- });
61
- });
62
-
63
- describe("DockerParser — top-level sections", () => {
64
- test("top-level volumes: section → VolumeIR entities", () => {
65
- const { entities } = new DockerParser().parse(testdata("simple.yaml"));
66
- const vol = entities.find((e) => e.kind === "volume" && e.name === "webdata");
67
- expect(vol).toBeDefined();
68
- expect((vol?.props as any).driver).toBe("local");
69
- });
70
-
71
- test("top-level networks: section → NetworkIR entities", () => {
72
- const { entities } = new DockerParser().parse(testdata("full.yaml"));
73
- const net = entities.find((e) => e.kind === "network" && e.name === "backend");
74
- expect(net).toBeDefined();
75
- expect((net?.props as any).driver).toBe("bridge");
76
- });
77
-
78
- test("top-level configs: section → ConfigIR entities", () => {
79
- const { entities } = new DockerParser().parse(testdata("full.yaml"));
80
- const cfg = entities.find((e) => e.kind === "config" && e.name === "app-config");
81
- expect(cfg).toBeDefined();
82
- expect((cfg?.props as any).file).toBe("./config/app.conf");
83
- });
84
-
85
- test("top-level secrets: section → SecretIR entities", () => {
86
- const { entities } = new DockerParser().parse(testdata("full.yaml"));
87
- const sec = entities.find((e) => e.kind === "secret" && e.name === "db-password");
88
- expect(sec).toBeDefined();
89
- expect((sec?.props as any).file).toBe("./secrets/db-password.txt");
90
- });
91
-
92
- test("returns empty entities for empty compose", () => {
93
- const result = new DockerParser().parse("");
94
- expect(result.entities).toHaveLength(0);
95
- expect(result.warnings).toHaveLength(0);
96
- });
97
- });
98
-
99
- describe("DockerfileParser", () => {
100
- test("single-stage: stages[0].from correct", () => {
101
- const content = `FROM node:20-alpine\nRUN npm ci\nCMD ["node", "index.js"]\n`;
102
- const result = new DockerfileParser().parse("builder", content);
103
- expect(result.kind).toBe("dockerfile");
104
- expect(result.stages).toHaveLength(1);
105
- expect(result.stages[0].from).toBe("node:20-alpine");
106
- });
107
-
108
- test("multi-stage: two stages with correct from/as", () => {
109
- const content = [
110
- "FROM node:20-alpine AS builder",
111
- "RUN npm ci",
112
- "FROM nginx:1.25 AS runner",
113
- "COPY --from=builder /app/dist /usr/share/nginx/html",
114
- ].join("\n");
115
- const result = new DockerfileParser().parse("app", content);
116
- expect(result.stages).toHaveLength(2);
117
- expect(result.stages[0].from).toBe("node:20-alpine");
118
- expect(result.stages[0].as).toBe("builder");
119
- expect(result.stages[1].from).toBe("nginx:1.25");
120
- expect(result.stages[1].as).toBe("runner");
121
- });
122
-
123
- test("parses multiple instructions within a stage", () => {
124
- const content = `FROM ubuntu:22.04\nRUN apt-get update\nCOPY . /app\nWORKDIR /app\n`;
125
- const result = new DockerfileParser().parse("app", content);
126
- expect(result.stages[0].instructions.length).toBeGreaterThanOrEqual(2);
127
- const runInstr = result.stages[0].instructions.find((i) => i.instruction === "RUN");
128
- expect(runInstr?.value).toBe("apt-get update");
129
- });
130
-
131
- test("skips comments and blank lines", () => {
132
- const content = `# This is a comment\nFROM alpine:3.18\n\n# Another comment\nRUN echo hello\n`;
133
- const result = new DockerfileParser().parse("test", content);
134
- expect(result.stages[0].from).toBe("alpine:3.18");
135
- expect(result.stages[0].instructions).toHaveLength(1);
136
- });
137
- });