From ab8923bb77354754d0ffa522e4dbe5cafa34da6b Mon Sep 17 00:00:00 2001 From: Jeppe Bundgaard Date: Fri, 24 Apr 2026 20:47:18 +0200 Subject: [PATCH] Attach CI runner to edge E2E network --- scripts/edge-gateway-e2e.mjs | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/scripts/edge-gateway-e2e.mjs b/scripts/edge-gateway-e2e.mjs index e3eb0966..e5e3322e 100644 --- a/scripts/edge-gateway-e2e.mjs +++ b/scripts/edge-gateway-e2e.mjs @@ -118,6 +118,73 @@ async function readDefaultGatewayHost() { } } +function composeNetworkName(composeProject) { + return `${composeProject}_default`; +} + +async function readCurrentContainerRef() { + if (process.platform === "win32") { + return null; + } + + const candidates = []; + const envHostname = String(process.env.HOSTNAME || "").trim(); + if (envHostname !== "") { + candidates.push(envHostname); + } + + try { + const hostname = (await fs.readFile("/etc/hostname", "utf8")).trim(); + if (hostname !== "") { + candidates.push(hostname); + } + } catch { + // Not running in a container with /etc/hostname available. + } + + try { + const cgroup = await fs.readFile("/proc/self/cgroup", "utf8"); + const matches = cgroup.match(/[0-9a-f]{64}/gi) || []; + candidates.push(...matches); + } catch { + // cgroup metadata is optional in local development. + } + + return candidates.find((candidate) => /^[a-zA-Z0-9][a-zA-Z0-9_.-]{1,127}$/.test(candidate)) || null; +} + +async function connectCurrentContainerToComposeNetwork(rootDir, composeProject) { + const containerRef = await readCurrentContainerRef(); + if (!containerRef) { + return false; + } + + const inspection = await runCommand("docker", ["inspect", containerRef], { + cwd: rootDir, + allowFailure: true, + }); + if (inspection.code !== 0) { + return false; + } + + const networkName = composeNetworkName(composeProject); + const connection = await runCommand("docker", ["network", "connect", networkName, containerRef], { + cwd: rootDir, + allowFailure: true, + }); + const stderr = String(connection.stderr || ""); + if (connection.code === 0) { + process.stdout.write(`Attached runner container ${containerRef} to ${networkName}.\n`); + return true; + } + + if (/already exists|already connected/i.test(stderr)) { + return true; + } + + return false; +} + async function readComposeServiceHost(rootDir, composeProject, serviceName) { const ps = await runCommand("docker", composeArgs(composeProject, ["ps", "-q", serviceName]), { cwd: rootDir, @@ -156,6 +223,9 @@ async function candidateApiBaseUrls(baseUrl, rootDir, composeProject) { if (["localhost", "127.0.0.1", "::1"].includes(url.hostname)) { if (rootDir && composeProject) { + await connectCurrentContainerToComposeNetwork(rootDir, composeProject); + candidates.push(baseUrlWithHost(normalized, "traefik", "")); + const traefikHost = await readComposeServiceHost(rootDir, composeProject, "traefik"); if (traefikHost) { candidates.push(baseUrlWithHost(normalized, traefikHost, ""));