@flisk-sh/flisk-import 0.10.0 → 0.11.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
@@ -14,11 +14,11 @@ flisk import . --yes # write service.json files and project setup
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 install's data:** each named volume gets `name`, the Docker volume `docker compose up` created (`<project>_<volume>`, its own `name:`, or an `external` volume), so `flisk up` mounts the same data with no copy (needs the Flisk release with flisk#2964).
16
16
  - **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.
17
- - **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
+ - **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 the same host address as in Compose: `"80:80"` stays on every interface, `"127.0.0.1:5432:5432"` on loopback, and another address or a `${VAR:-default}` host side as `publish`), `network_mode: service:<name>` (`sidecarOf`) and `restart`.
18
18
  - **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.
19
19
  - **Keeps:** a `command` or `entrypoint` on an image as the `CMD`/`ENTRYPOINT` of a small `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`.
20
20
  - **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.
21
- - **Does not start anything.** Run `flisk up` with Flisk core **1.4.24 or later**. The importer does not check the core version.
21
+ - **Does not start anything.** Run `flisk up` with Flisk core **1.4.26 or later** (`publish` needs it). The importer does not check the core version.
22
22
  - **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.`).
23
23
 
24
24
  See [docs/import.md](docs/import.md) for the full mapping and limits. `flisk import` is the core CLI command backed by this package.
@@ -120,33 +120,65 @@ function containerOf(raw) {
120
120
  return { ...(Object.keys(container).length ? { container } : {}), dropped };
121
121
  }
122
122
  /** One published port: container port, host port when Compose names a literal one, and protocol. */
123
+ /** A `publish` value Flisk accepts: digits, dots, colons and `${NAME:-default}` with a port or IPv4 address:port default. */
124
+ const PUBLISH = /^(?:[0-9.:]|\$\{[A-Za-z_][A-Za-z0-9_]*:-[0-9.:]+\})+$/;
125
+ /** The host address and port of a published port. Compose publishes on every interface unless an address is given. */
126
+ function hostSideOf(port, hostIp, hostPort) {
127
+ const hostNumber = hostPort && /^\d+$/.test(hostPort) ? Number.parseInt(hostPort, 10) : undefined;
128
+ const keepHostPort = hostNumber !== undefined && hostNumber >= 1 && hostNumber <= 65535 && hostNumber !== port
129
+ ? { hostPort: hostNumber }
130
+ : {};
131
+ if (hostPort?.includes("${")) {
132
+ const publish = hostIp ? `${hostIp}:${hostPort}` : hostPort;
133
+ return PUBLISH.test(publish) ? { publish } : undefined;
134
+ }
135
+ if (hostIp === undefined || hostIp === "" || hostIp === "0.0.0.0") {
136
+ // A bare container port ("3000") gets a random host port from Compose; Flisk keeps it on 127.0.0.1.
137
+ return hostPort === undefined ? {} : { ...keepHostPort, bind: "0.0.0.0" };
138
+ }
139
+ if (hostIp === "127.0.0.1")
140
+ return keepHostPort;
141
+ const publish = `${hostIp}:${hostNumber ?? port}`;
142
+ return /^\d{1,3}(\.\d{1,3}){3}:\d+$/.test(publish) ? { publish } : undefined;
143
+ }
123
144
  function publishedPort(entry) {
124
145
  const port = containerPort(entry);
125
146
  if (port === undefined)
126
147
  return undefined;
127
- let host;
128
148
  let protocol = "tcp";
149
+ let hostSide;
129
150
  if (isObj(entry)) {
130
- const published = typeof entry.published === "string" ? Number(entry.published) : entry.published;
131
- if (typeof published === "number" && Number.isInteger(published))
132
- host = published;
151
+ const published = entry.published === undefined ? undefined : String(entry.published);
152
+ const hostIp = typeof entry.host_ip === "string" ? entry.host_ip : undefined;
133
153
  if (entry.protocol === "udp")
134
154
  protocol = "udp";
155
+ hostSide = hostSideOf(port, hostIp, published);
135
156
  }
136
157
  else if (typeof entry === "string") {
137
158
  const [mapping, proto] = entry.split("/");
138
159
  if (proto === "udp")
139
160
  protocol = "udp";
140
- const parts = (mapping ?? "").split(":");
141
- const hostPart = parts.length >= 2 ? parts[parts.length - 2] : undefined;
142
- if (hostPart && /^\d+$/.test(hostPart))
143
- host = Number.parseInt(hostPart, 10);
161
+ // [HOST_IP:][HOST_PORT:]CONTAINER_PORT, where the host side may hold ${VAR:-default}, which has colons of its own.
162
+ const match = (mapping ?? "").match(/^(?:(.*):)?([^:]+)$/);
163
+ const host = match?.[1];
164
+ if (host === undefined)
165
+ hostSide = hostSideOf(port, undefined, undefined);
166
+ else if (host.includes("${"))
167
+ hostSide = hostSideOf(port, undefined, host);
168
+ else {
169
+ const at = host.lastIndexOf(":");
170
+ hostSide =
171
+ at === -1
172
+ ? hostSideOf(port, undefined, host)
173
+ : hostSideOf(port, host.slice(0, at), host.slice(at + 1));
174
+ }
175
+ }
176
+ else {
177
+ hostSide = {};
144
178
  }
145
179
  return {
146
180
  port,
147
- ...(host !== undefined && host >= 1 && host <= 65535 && host !== port
148
- ? { hostPort: host }
149
- : {}),
181
+ ...(hostSide ?? {}),
150
182
  ...(protocol === "udp" ? { protocol } : {}),
151
183
  };
152
184
  }
@@ -438,13 +470,13 @@ registerDetector({
438
470
  if (ports.length > 1 && selectedPort === undefined) {
439
471
  skips.push({
440
472
  ...base,
441
- reason: `service "${name}" lists several ports (${ports.join(", ")}); no single HTTP container port could be identified, so none is routed (all are published on 127.0.0.1 as extraPorts)`,
473
+ reason: `service "${name}" lists several ports (${ports.join(", ")}); no single HTTP container port could be identified, so none is routed (all stay published, as extraPorts)`,
442
474
  });
443
475
  }
444
476
  else if (ports.length > 1) {
445
477
  skips.push({
446
478
  ...base,
447
- reason: `service "${name}" publishes extra container ports (${ports.filter((port) => port !== selectedPort).join(", ")}); Flisk routes only ${selectedPort} and publishes the others on 127.0.0.1 as extraPorts`,
479
+ reason: `service "${name}" publishes extra container ports (${ports.filter((port) => port !== selectedPort).join(", ")}); Flisk routes only ${selectedPort} and keeps the others published as extraPorts`,
448
480
  });
449
481
  }
450
482
  const dependsOn = Array.isArray(raw.depends_on)
@@ -477,7 +509,7 @@ registerDetector({
477
509
  reason: `service "${name}" ${key} ${JSON.stringify(raw[key])} has values Flisk does not accept; not used`,
478
510
  });
479
511
  }
480
- // Every published port Flisk does not route stays published, on 127.0.0.1 (service.json extraPorts).
512
+ // Every published port Flisk does not route stays published on the same host address (service.json extraPorts).
481
513
  const proxied = selectedPort !== undefined && !NON_HTTP_PORTS.has(selectedPort);
482
514
  const extraPorts = (Array.isArray(raw.ports) ? raw.ports : [])
483
515
  .map(publishedPort)
package/dist/write.js CHANGED
@@ -237,8 +237,16 @@ export function planWrites(plan, force) {
237
237
  const extraPorts = sidecarOf ? [] : (service.extraPorts ?? []);
238
238
  if (sidecarOf && service.extraPorts?.length)
239
239
  notes.push(`ports not written: a sidecar has none of its own; publish them on "${sidecarOf}"`);
240
- if (extraPorts.length)
241
- notes.push(`published on 127.0.0.1 only: ${extraPorts.map((p) => `${p.hostPort ?? p.port}->${p.port}/${p.protocol ?? "tcp"}`).join(", ")}`);
240
+ const portText = (p) => `${p.publish ?? p.hostPort ?? p.port}->${p.port}/${p.protocol ?? "tcp"}`;
241
+ const loopback = extraPorts.filter((p) => !p.bind && !p.publish);
242
+ const everywhere = extraPorts.filter((p) => p.bind === "0.0.0.0");
243
+ const fromCompose = extraPorts.filter((p) => p.publish);
244
+ if (loopback.length)
245
+ notes.push(`published on 127.0.0.1 only: ${loopback.map(portText).join(", ")}`);
246
+ if (everywhere.length)
247
+ notes.push(`published on every interface, as in Compose: ${everywhere.map(portText).join(", ")} (flisk doctor lists these)`);
248
+ if (fromCompose.length)
249
+ notes.push(`published as Compose wrote the host side: ${fromCompose.map(portText).join(", ")}`);
242
250
  // Bind mounts are relative to the service directory in service.json, and must stay inside the project.
243
251
  const serviceDir = dirname(path);
244
252
  const volumes = (service.volumes ?? []).flatMap((volume) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flisk-sh/flisk-import",
3
- "version": "0.10.0",
3
+ "version": "0.11.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",