flisk-import 0.6.0 → 0.7.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/README.md CHANGED
@@ -10,12 +10,13 @@ flisk import . --yes # write service.json files and project setup
10
10
 
11
11
  ## What to expect
12
12
 
13
- - **Reads one file:** the root `docker-compose.yml`. Not `compose.yaml`, override files, nested Compose files, Dockerfiles on their own, `package.json`, or Procfiles.
13
+ - **Reads one file:** the root `docker-compose.yml`, or `compose.yaml`, `compose.yml` or `docker-compose.yaml` when that is absent. Not override files, nested Compose files, Dockerfiles on their own, `package.json`, or Procfiles.
14
14
  - **Writes:** one bring-your-own `service.json` per Compose service, plus `.flisk/project.json` if the repo is not a Flisk project yet. It never edits the Compose file or overwrites a `service.json` without `--force`.
15
15
  - **Keeps:** the service names (`db` stays `db`, so it stays the hostname), the build context and Dockerfile (or the image), the container port as written (3000, 80, 5432), `depends_on`, and an HTTP health path. Databases and brokers are private, with no proxy route.
16
16
  - **Runtime keys:** named volumes and bind mounts inside the project (`volumes`), `cap_add`, `cap_drop`, `init`, `read_only`, `user`, `security_opt` and `shm_size` (`container`), extra published ports (`extraPorts`, on 127.0.0.1 only), `network_mode: service:<name>` (`sidecarOf`) and `restart`.
17
17
  - **Environment:** values that cannot be secrets go into the committed `service.json` `env`: numbers, `true`/`false`, another imported service's address without a user, password or query (`http://invidious:3000`, `redis:6379`), and pure `${VAR}` references. Every other name, and any name that looks like a credential (`PASSWORD`, `TOKEN`, `KEY`, ...), goes into `secrets.required`, with its literal value copied to the project `.env`, which is added to `.gitignore`. Values in `.env` already are kept.
18
- - **Drops, and says so:** `command`, `env_file` and every other untranslated key; anonymous volumes and paths outside the project; `network_mode` other than `service:<name>`; services with `profiles`, `extends`, an out-of-tree build or a `${VAR}` image.
18
+ - **Keeps:** a `command` on an image as the `CMD` of a one-line `Dockerfile` next to `service.json` (Flisk sets no container command), a `healthcheck` as `healthCheck.command`, a published database port as `extraPorts`, and `restart: on-failure:N` as `on-failure`.
19
+ - **Drops, and says so:** `command` on a service with a `build`, `env_file` and every other untranslated key; anonymous volumes and paths outside the project; `network_mode` other than `service:<name>`; services with `profiles`, `extends`, an out-of-tree build or a `${VAR}` image.
19
20
  - **Does not start anything.** Run `flisk up` with Flisk core **1.4.24 or later**. The importer does not check the core version.
20
21
  - **Exit codes:** `0` when the plan succeeds (`--dry-run`, files written, or all `service.json` files already exist), `1` when no root `docker-compose.yml` or mappable service is found, and `2` when the write prompt is declined or closed without an answer (`Nothing written.`).
21
22
 
@@ -44,6 +44,7 @@ const TRANSLATED_KEYS = new Set([
44
44
  "network_mode",
45
45
  "volumes",
46
46
  "restart",
47
+ "command",
47
48
  ]);
48
49
  const CAPABILITY = /^[A-Z_]+$/;
49
50
  const USER = /^[A-Za-z0-9_.:-]+$/;
@@ -236,6 +237,55 @@ function commandOf(command) {
236
237
  return command.join(" ");
237
238
  return undefined;
238
239
  }
240
+ /** Compose `command` as the argument vector the container runs; a string goes through a shell, as Compose runs it. */
241
+ function containerCommandOf(command) {
242
+ if (typeof command === "string" && command.trim())
243
+ return ["sh", "-c", command];
244
+ if (Array.isArray(command) && command.length > 0 && command.every((c) => typeof c === "string"))
245
+ return command;
246
+ return undefined;
247
+ }
248
+ /** Seconds from a Compose duration such as `30s` or `2m`; undefined for anything it cannot read exactly. */
249
+ function secondsOf(value) {
250
+ if (typeof value === "number" && value > 0)
251
+ return value;
252
+ const match = typeof value === "string" ? value.match(/^(\d+)(s|m)?$/) : null;
253
+ if (!match)
254
+ return undefined;
255
+ const seconds = Number(match[1]) * (match[2] === "m" ? 60 : 1);
256
+ return seconds > 0 ? seconds : undefined;
257
+ }
258
+ /** Compose `healthcheck` as a probe command (exit 0 is ready). `NONE` and `disable` give no probe. */
259
+ function healthProbeOf(healthcheck) {
260
+ if (!isObj(healthcheck) || healthcheck.disable === true)
261
+ return undefined;
262
+ const test = healthcheck.test;
263
+ let command;
264
+ if (typeof test === "string")
265
+ command = ["sh", "-c", test];
266
+ else if (Array.isArray(test) && test.every((t) => typeof t === "string")) {
267
+ const [kind, ...rest] = test;
268
+ if (kind === "CMD" && rest.length > 0)
269
+ command = rest;
270
+ else if (kind === "CMD-SHELL" && rest.length > 0)
271
+ command = ["sh", "-c", rest.join(" ")];
272
+ }
273
+ if (!command)
274
+ return undefined;
275
+ const interval = secondsOf(healthcheck.interval);
276
+ const timeout = secondsOf(healthcheck.timeout);
277
+ const retries = typeof healthcheck.retries === "number" &&
278
+ Number.isInteger(healthcheck.retries) &&
279
+ healthcheck.retries > 0
280
+ ? healthcheck.retries
281
+ : undefined;
282
+ return {
283
+ command,
284
+ ...(interval ? { interval } : {}),
285
+ ...(timeout ? { timeout } : {}),
286
+ ...(retries ? { retries } : {}),
287
+ };
288
+ }
239
289
  function healthPathOf(healthcheck) {
240
290
  if (!isObj(healthcheck) || !Array.isArray(healthcheck.test))
241
291
  return undefined;
@@ -255,7 +305,7 @@ function healthPathOf(healthcheck) {
255
305
  }
256
306
  registerDetector({
257
307
  id: "compose",
258
- claim: (relPath) => relPath === "docker-compose.yml",
308
+ claim: (relPath) => /^(docker-)?compose\.ya?ml$/.test(relPath),
259
309
  parse(absPath, relPath) {
260
310
  const composeDir = dirname(relPath) === "." ? "" : dirname(relPath);
261
311
  const base = { file: relPath, detector: "compose" };
@@ -363,6 +413,8 @@ registerDetector({
363
413
  ? Object.keys(raw.depends_on)
364
414
  : [];
365
415
  const command = commandOf(raw.command);
416
+ const containerCommand = containerCommandOf(raw.command);
417
+ const healthProbe = healthProbeOf(raw.healthcheck);
366
418
  const envKeys = envNames(raw.environment);
367
419
  const env = envValues(raw.environment);
368
420
  const envRefs = envReferences(raw.environment);
@@ -384,9 +436,10 @@ registerDetector({
384
436
  });
385
437
  }
386
438
  // Every published port Flisk does not route stays published, on 127.0.0.1 (service.json extraPorts).
439
+ const proxied = selectedPort !== undefined && !NON_HTTP_PORTS.has(selectedPort);
387
440
  const extraPorts = (Array.isArray(raw.ports) ? raw.ports : [])
388
441
  .map(publishedPort)
389
- .filter((p) => p !== undefined && p.port !== selectedPort);
442
+ .filter((p) => p !== undefined && (p.port !== selectedPort || !proxied));
390
443
  let sidecarOf;
391
444
  if (raw.network_mode !== undefined) {
392
445
  const mode = typeof raw.network_mode === "string" ? raw.network_mode : "";
@@ -411,9 +464,19 @@ registerDetector({
411
464
  reason: `service "${name}" volume ${JSON.stringify(entry)} is not translated (an anonymous volume, a path outside the imported directory, or Compose variables); not mounted`,
412
465
  });
413
466
  }
414
- const restart = typeof raw.restart === "string" && RESTART.has(raw.restart)
415
- ? raw.restart
467
+ // `on-failure:5` caps the retries; Flisk has the policy without the cap.
468
+ const restartName = typeof raw.restart === "string"
469
+ ? raw.restart.replace(/^(on-failure):\d+$/, "$1")
470
+ : undefined;
471
+ const restart = restartName !== undefined && RESTART.has(restartName)
472
+ ? restartName
416
473
  : undefined;
474
+ if (restart && restartName !== raw.restart) {
475
+ skips.push({
476
+ ...base,
477
+ reason: `service "${name}" restart ${JSON.stringify(raw.restart)} keeps "${restart}"; the retry limit is not carried over`,
478
+ });
479
+ }
417
480
  if (raw.restart !== undefined && !restart) {
418
481
  skips.push({
419
482
  ...base,
@@ -438,6 +501,8 @@ registerDetector({
438
501
  ...(typeof raw.image === "string" ? { image: raw.image } : {}),
439
502
  ...(dockerfile ? { dockerfile } : {}),
440
503
  ...(command ? { command } : {}),
504
+ ...(containerCommand ? { containerCommand } : {}),
505
+ ...(healthProbe ? { healthCheck: healthProbe } : {}),
441
506
  ...(selectedPort !== undefined ? { port: selectedPort } : {}),
442
507
  ...(healthCheckPath ? { healthCheckPath } : {}),
443
508
  exposeViaProxy,
package/dist/merge.js CHANGED
@@ -71,6 +71,8 @@ function mergeGroup(name, dir, members, resolve) {
71
71
  ...firstOf(ordered, "extraPorts"),
72
72
  ...firstOf(ordered, "volumes"),
73
73
  ...firstOf(ordered, "restart"),
74
+ ...firstOf(ordered, "containerCommand"),
75
+ ...firstOf(ordered, "healthCheck"),
74
76
  ...(ordered.find((c) => c.sidecarOf)?.sidecarOf
75
77
  ? { sidecarOf: resolve(ordered.find((c) => c.sidecarOf)?.sidecarOf) }
76
78
  : {}),
package/dist/plan.js CHANGED
@@ -7,7 +7,7 @@ import { selectDetectors } from "./registry.js";
7
7
  import { scanTree } from "./scan.js";
8
8
  import { findUnsupported } from "./unsupported.js";
9
9
  const COMPOSE_FILE = "docker-compose.yml";
10
- /** Names `docker compose` also picks up, in its own order of preference; none of them is read. */
10
+ /** Names `docker compose` also picks up, in its own order of preference. They are read only when docker-compose.yml is absent. */
11
11
  const OTHER_COMPOSE_FILES = ["compose.yaml", "compose.yml", "docker-compose.yaml"];
12
12
  /** Files `docker compose up` merges over the main file; none of them is applied. */
13
13
  const OVERRIDE_FILES = [
@@ -16,37 +16,33 @@ const OVERRIDE_FILES = [
16
16
  "docker-compose.override.yaml",
17
17
  "docker-compose.override.yml",
18
18
  ];
19
- /** Read the one supported input, root docker-compose.yml. This function writes nothing. */
19
+ /** Read the one supported input, the root Compose file. This function writes nothing. */
20
20
  export function buildPlan(dir) {
21
21
  const root = resolve(dir);
22
22
  if (!statSync(root, { throwIfNoEntry: false })?.isDirectory()) {
23
23
  throw new Error(`${dir} is not a directory`);
24
24
  }
25
- const composePath = COMPOSE_FILE;
26
25
  const detectors = selectDetectors(["compose"]);
27
26
  const candidates = [];
28
27
  const skips = [];
29
28
  const isFile = (name) => statSync(resolve(root, name), { throwIfNoEntry: false })?.isFile() ?? false;
30
- if (!isFile(composePath)) {
31
- const others = OTHER_COMPOSE_FILES.filter(isFile);
32
- throw new ImportError(`No root docker-compose.yml found in ${root}${others.length ? ` (found ${others.join(", ")})` : ""}; flisk import reads only that file`, others.length
33
- ? [
34
- `Other Compose file names are not read yet. To import ${others[0]}, link it: ln -s ${others[0]} docker-compose.yml`,
35
- ]
36
- : ["Run flisk import in the directory that holds docker-compose.yml"]);
29
+ const present = OTHER_COMPOSE_FILES.filter(isFile);
30
+ const composePath = isFile(COMPOSE_FILE) ? COMPOSE_FILE : present[0];
31
+ if (!composePath) {
32
+ throw new ImportError(`No root docker-compose.yml found in ${root} (nor compose.yaml); flisk import reads only the root Compose file`, ["Run flisk import in the directory that holds docker-compose.yml (or compose.yaml)"]);
37
33
  }
38
- for (const name of OTHER_COMPOSE_FILES.filter(isFile)) {
34
+ for (const name of present.filter((n) => n !== composePath)) {
39
35
  skips.push({
40
36
  file: name,
41
37
  detector: "compose",
42
- reason: `not read; \`docker compose\` prefers it over ${COMPOSE_FILE}, so what it runs may differ from this plan`,
38
+ reason: `not read; \`docker compose\` prefers it over ${composePath}, so what it runs may differ from this plan`,
43
39
  });
44
40
  }
45
41
  for (const name of OVERRIDE_FILES.filter(isFile)) {
46
42
  skips.push({
47
43
  file: name,
48
44
  detector: "compose",
49
- reason: `not applied; \`docker compose up\` merges it over ${COMPOSE_FILE}, flisk import does not`,
45
+ reason: `not applied; \`docker compose up\` merges it over ${composePath}, flisk import does not`,
50
46
  });
51
47
  }
52
48
  for (const detector of detectors) {
package/dist/write.js CHANGED
@@ -160,6 +160,10 @@ export function planWrites(plan, force) {
160
160
  notes.push("no health path: the service does not serve HTTP (non-HTTP image or job)");
161
161
  if (!isPrivate && !nonHttp && !service.healthCheckPath)
162
162
  notes.push("health path / is assumed: no HTTP health path could be read from Compose. Traefik routes only while it answers 2xx or 3xx");
163
+ // A probe command is the only way to describe a non-HTTP check; a routed service keeps its HTTP path.
164
+ const healthCheck = service.healthCheck && !(healthCheckPath && service.healthCheckPath)
165
+ ? service.healthCheck
166
+ : undefined;
163
167
  // Values that cannot be secrets (numbers, booleans, another service's address, pure ${VAR} references) go into the
164
168
  // committed service.json `env` (Flisk 1.4.24). Every other name is a secret: Flisk injects it from the gitignored .env.
165
169
  const validNames = service.envKeys.filter((key) => ENV_NAME.test(key));
@@ -201,12 +205,27 @@ export function planWrites(plan, force) {
201
205
  notes.push(`image "${service.image}" is not written: with build and image, Flisk builds from ${service.dockerfile} and does not pull the tag`);
202
206
  // With both build and image, Compose builds the Dockerfile and tags the result as `image`.
203
207
  // Omit `image` from service.json so Flisk builds from the Dockerfile instead of pulling the tag.
204
- const source = service.dockerfile
205
- ? {
206
- buildContext: relative(dirname(path), resolve(plan.root, service.dir)).split("\\").join("/") || ".",
207
- dockerfile: service.dockerfile,
208
- }
209
- : { image: service.image };
208
+ // Flisk has no container `command`, so a Compose `command` on an image becomes the CMD of a one-line Dockerfile.
209
+ const files = [];
210
+ const wrapWithCommand = !service.dockerfile && service.image && service.containerCommand;
211
+ if (wrapWithCommand) {
212
+ files.push({
213
+ path: resolve(dirname(path), "Dockerfile"),
214
+ content: `# From the Compose \`command\` of this service (flisk import). Flisk sets no container command, so it lives here.\nFROM ${service.image}\nCMD ${JSON.stringify(service.containerCommand)}\n`,
215
+ });
216
+ notes.push(`command ${JSON.stringify(service.containerCommand)} is written as the CMD of a Dockerfile on ${service.image}`);
217
+ }
218
+ else if (service.dockerfile && service.containerCommand) {
219
+ notes.push(`command ${JSON.stringify(service.containerCommand)} not written: set it as CMD in ${service.dockerfile}`);
220
+ }
221
+ const source = wrapWithCommand
222
+ ? { buildContext: ".", dockerfile: "Dockerfile" }
223
+ : service.dockerfile
224
+ ? {
225
+ buildContext: relative(dirname(path), resolve(plan.root, service.dir)).split("\\").join("/") || ".",
226
+ dockerfile: service.dockerfile,
227
+ }
228
+ : { image: service.image };
210
229
  // A sidecar shares another written service's network; one whose host is not written stays a normal service.
211
230
  const sidecarOf = service.sidecarOf && service.sidecarOf !== service.name && names.has(service.sidecarOf)
212
231
  ? service.sidecarOf
@@ -240,6 +259,7 @@ export function planWrites(plan, force) {
240
259
  stack,
241
260
  port,
242
261
  ...(healthCheckPath ? { healthCheckPath } : {}),
262
+ ...(healthCheck ? { healthCheck } : {}),
243
263
  ...source,
244
264
  ...(isPrivate ? { exposeViaProxy: false } : {}),
245
265
  ...(dependsOn.length ? { dependsOn } : {}),
@@ -261,7 +281,7 @@ export function planWrites(plan, force) {
261
281
  }
262
282
  : {}),
263
283
  };
264
- items.push({ service, path, manifest, status, notes });
284
+ items.push({ service, path, manifest, files, status, notes });
265
285
  }
266
286
  return {
267
287
  projectRoot,
@@ -283,6 +303,8 @@ export function applyWrites(writes) {
283
303
  continue;
284
304
  mkdirSync(dirname(item.path), { recursive: true });
285
305
  writeFileSync(item.path, `${JSON.stringify(item.manifest, null, 2)}\n`);
306
+ for (const file of item.files)
307
+ writeFileSync(file.path, file.content);
286
308
  written.push(item);
287
309
  }
288
310
  const keys = Object.keys(writes.env.add);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flisk-import",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Import a repository's root docker-compose.yml into Flisk service.json files, or register its Tiltfile.",
5
5
  "type": "module",
6
6
  "license": "MIT",