Public: anyone can read this; the crew can change it.
forge.json validates a service app's ports and refuses a clash with another app #16778
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15078-forge-json-validates-a-service-app-s-por → main 0 commits fork 3 threads · 3 unresolved
4 files
changed +113
−1
docs/APPS.md +2 −1
| @@ -2059,7 +2059,8 @@ wait: started is up. `src/apps/supervise.ts` restarts it when it exits, after | ||
| 2059 | 2059 | 1 s, 2 s, 4 s … never more than 60 s (a run of 60 s starts the count over), and |
| 2060 | 2060 | the app page says so ("service restarting in 8 s after exit code 1"). Stopping |
| 2061 | 2061 | or archiving the app stops the process and its children and ends the |
| 2062 | restarts. Exposing the ports and syncing volumes are not built yet (issue #391). | |
| 2062 | restarts. A deploy is refused when another app's service already holds a declared port, naming both (`deployPortRefusal`); an app's own previews and production never clash. Only services live in this forge process hold ports, so a stopped or asleep service holds none, and the answer depends on what is awake. | |
| 2063 | Exposing the ports and syncing volumes are not built yet (issue #391). | |
| 2063 | 2064 | |
| 2064 | 2065 | ## The access log |
| 2065 | 2066 |
src/apps/runtime_bringup.ts +5
| @@ -19,6 +19,7 @@ import { exportTree, readFileAt } from "./checkout.ts"; | ||
| 19 | 19 | import { Stopwatch } from "./timing.ts"; |
| 20 | 20 | import { ensureEnvData, envDataDir, envDir, envPull, reseedEmpty, waitedEmptyNote } from "./data.ts"; |
| 21 | 21 | import { parseManifest } from "./manifest.ts"; |
| 22 | import { deployPortRefusal } from "./service.ts"; | |
| 22 | 23 | import { releaseAt } from "./release_at.ts"; |
| 23 | 24 | import { identityKey } from "./grants.ts"; |
| 24 | 25 | import { startProcess, type StartInput } from "./process.ts"; |
| @@ -55,6 +56,10 @@ export async function bringUp( | ||
| 55 | 56 | : manifest.error.message, |
| 56 | 57 | }; |
| 57 | 58 | } |
| 59 | if (manifest.value.service != null) { | |
| 60 | const refusal = deployPortRefusal(state, manifest.value.service.ports, rt.envs.values()); | |
| 61 | if (refusal !== null) return { ok: false, error: refusal }; | |
| 62 | } | |
| 58 | 63 | const dir = join( |
| 59 | 64 | envDir(rt.app.config.dataDir, state.app.id, state.env), |
| 60 | 65 | run.commitOid.slice(0, 7), |
src/apps/service.ts +49
| @@ -112,3 +112,52 @@ export function parseService(app: Json): Result<ServiceSpec, { message: string } | ||
| 112 | 112 | volumes, |
| 113 | 113 | }); |
| 114 | 114 | } |
| 115 | ||
| 116 | /** One service environment already holding ports on this forge. */ | |
| 117 | export interface PortHolder { | |
| 118 | /** Its name for a person: `<slug>` or `<slug> (<env>)`. */ | |
| 119 | readonly name: string; | |
| 120 | readonly ports: readonly number[]; | |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * The first declared port another service already holds, as a refusal naming both | |
| 125 | * (`mine` and the holder). Ports are one host's, so a preview clashes with its | |
| 126 | * own production too. `null` when none clash. | |
| 127 | */ | |
| 128 | export function portClash(mine: string, ports: readonly ServicePort[], holders: readonly PortHolder[]): string | null { | |
| 129 | for (const { port } of ports) { | |
| 130 | const held = holders.find((h) => h.ports.includes(port)); | |
| 131 | if (held !== undefined) return `port ${port} is already held by ${held.name}; ${mine} cannot declare it`; | |
| 132 | } | |
| 133 | return null; | |
| 134 | } | |
| 135 | ||
| 136 | /** The part of an environment the port check reads. */ | |
| 137 | export interface PortEnv { | |
| 138 | readonly env: string; | |
| 139 | readonly app: { readonly id: string | number; readonly slug: string }; | |
| 140 | readonly live?: { readonly manifest: { readonly service?: Pick<ServiceSpec, "ports"> | null | undefined } } | undefined; | |
| 141 | } | |
| 142 | ||
| 143 | const nameOf = (e: PortEnv): string => (e.env === "production" ? e.app.slug : `${e.app.slug} (${e.env})`); | |
| 144 | ||
| 145 | /** | |
| 146 | * Other apps' live services as port holders. An app's own environments are skipped: | |
| 147 | * a preview and its production are one app, and a redeploy replaces what it holds. | |
| 148 | */ | |
| 149 | export function portHolders(self: PortEnv, all: Iterable<PortEnv>): PortHolder[] { | |
| 150 | const holders: PortHolder[] = []; | |
| 151 | for (const other of all) { | |
| 152 | const service = other.live?.manifest.service; | |
| 153 | if (other.app.id === self.app.id || service == null) continue; | |
| 154 | holders.push({ name: nameOf(other), ports: service.ports.map((p) => p.port) }); | |
| 155 | } | |
| 156 | return holders; | |
| 157 | } | |
| 158 | ||
| 159 | /** The deploy refusal for `self` declaring `ports` against the other apps, or `null`. */ | |
| 160 | export function deployPortRefusal(self: PortEnv, ports: readonly ServicePort[], all: Iterable<PortEnv>): string | null { | |
| 161 | const clash = portClash(nameOf(self), ports, portHolders(self, all)); | |
| 162 | return clash === null ? null : `forge.json: ${clash}`; | |
| 163 | } |
test/service-ports.test.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { parseManifest } from "../src/apps/manifest.ts"; | |
| 3 | import { portClash } from "../src/apps/service.ts"; | |
| 4 | ||
| 5 | const declare = (ports: unknown) => | |
| 6 | parseManifest(JSON.stringify({ app: { kind: "service", ports, run: "true" } }), undefined); | |
| 7 | ||
| 8 | test("valid service ports parse", () => { | |
| 9 | const m = declare([1024, 25565, 65535]); | |
| 10 | expect(m.ok && m.value.service?.ports.map((p) => p.port)).toEqual([1024, 25565, 65535]); | |
| 11 | }); | |
| 12 | ||
| 13 | test("ports out of range, fractional or not numbers are refused", () => { | |
| 14 | for (const bad of [[1023], [65536], [0], [80.5], ["25565"], [-1], []]) expect(declare(bad).ok).toBe(false); | |
| 15 | }); | |
| 16 | ||
| 17 | test("a port named twice is refused", () => { | |
| 18 | const m = declare([25565, 25565]); | |
| 19 | expect(!m.ok && m.error.kind === "invalid" && m.error.message).toContain("25565 twice"); | |
| 20 | }); | |
| 21 | ||
| 22 | test("a clash with another app is refused, naming both", () => { | |
| 23 | const ports = [{ port: 25565, protocol: "tcp" as const }]; | |
| 24 | const holders = [{ name: "mc", ports: [25565] }, { name: "other", ports: [9000] }]; | |
| 25 | const said = portClash("proxy", ports, holders); | |
| 26 | expect(said).toContain("proxy"); | |
| 27 | expect(said).toContain("mc"); | |
| 28 | expect(said).toContain("25565"); | |
| 29 | expect(portClash("proxy", [{ port: 25566, protocol: "tcp" }], holders)).toBeNull(); | |
| 30 | expect(portClash("proxy", ports, [])).toBeNull(); | |
| 31 | }); | |
| 32 | ||
| 33 | import { deployPortRefusal, portHolders, type PortEnv } from "../src/apps/service.ts"; | |
| 34 | ||
| 35 | const svc = (ports: number[]) => ({ manifest: { service: { ports: ports.map((port) => ({ port, protocol: "tcp" as const })) } } }); | |
| 36 | const env = (id: number, slug: string, name: string, live?: NonNullable<PortEnv["live"]>): PortEnv => ({ env: name, app: { id, slug }, live }); | |
| 37 | const mine = [{ port: 25565, protocol: "tcp" as const }]; | |
| 38 | ||
| 39 | test("a preview of an app is not refused for its own production's ports", () => { | |
| 40 | const prod = env(1, "mc", "production", svc([25565])); | |
| 41 | const preview = env(1, "mc", "pull-3"); | |
| 42 | expect(deployPortRefusal(preview, mine, [prod, preview])).toBeNull(); | |
| 43 | expect(portHolders(preview, [prod, preview])).toEqual([]); | |
| 44 | }); | |
| 45 | ||
| 46 | test("another app's live service refuses, naming both with the env", () => { | |
| 47 | const preview = env(1, "proxy", "pull-3"); | |
| 48 | const said = deployPortRefusal(preview, mine, [env(2, "mc", "production", svc([25565])), preview]); | |
| 49 | expect(said).toBe("forge.json: port 25565 is already held by mc; proxy (pull-3) cannot declare it"); | |
| 50 | expect(portHolders(preview, [env(2, "mc", "pull-9", svc([1, 2]))])).toEqual([{ name: "mc (pull-9)", ports: [1, 2] }]); | |
| 51 | }); | |
| 52 | ||
| 53 | test("environments with no live service, or no live process, hold nothing", () => { | |
| 54 | const self = env(1, "proxy", "production"); | |
| 55 | const web = { manifest: { service: null } }; | |
| 56 | expect(deployPortRefusal(self, mine, [env(2, "web", "production", web), env(3, "mc", "production")])).toBeNull(); | |
| 57 | }); |
new above
offline
This loop includes the same app's other environments (production vs preview). A preview of a service app will always clash with its own production. Skip
other.app.id === state.app.id, or give previews their own ports. Move the loop into a named function beside portClash so it can be tested.