diff --git a/hole-punch-interop/src/compose-runner.ts b/hole-punch-interop/src/compose-runner.ts index c90e1a292..6c9f40eff 100644 --- a/hole-punch-interop/src/compose-runner.ts +++ b/hole-punch-interop/src/compose-runner.ts @@ -5,6 +5,7 @@ import {exec as execStd} from 'child_process'; import util from 'util'; import {ComposeSpecification} from "../compose-spec/compose-spec"; import {stringify} from 'yaml'; +import {sanitizeComposeName} from "./lib"; const exec = util.promisify(execStd); @@ -26,7 +27,7 @@ export async function run(namespace: string, compose: ComposeSpecification, logD // Create compose.yaml file // Some docker compose environments don't like the name field to have special characters - const sanitizedComposeName = compose?.name.replace(/[^a-zA-Z0-9_-]/g, "_") + const sanitizedComposeName = sanitizeComposeName(compose.name) await fs.writeFile(path.join(dir, "compose.yaml"), stringify({ ...compose, name: sanitizedComposeName })) const stdoutLogFile = path.join(logDir, `${sanitizedComposeName}.stdout`); diff --git a/hole-punch-interop/src/generator.ts b/hole-punch-interop/src/generator.ts index f27f04854..bfd7f453c 100644 --- a/hole-punch-interop/src/generator.ts +++ b/hole-punch-interop/src/generator.ts @@ -2,6 +2,7 @@ import sqlite3 from "sqlite3"; import {open} from "sqlite"; import {Version} from "../versions"; import {ComposeSpecification} from "../compose-spec/compose-spec"; +import {sanitizeComposeName} from "./lib"; function buildExtraEnv(timeoutOverride: { [key: string]: number }, test1ID: string, test2ID: string): { [key: string]: string @@ -85,10 +86,24 @@ function buildSpec(containerImages: { [key: string]: () => string }, { return null } + let internetNetworkName = `${sanitizeComposeName(name)}_internet` + + let startupScriptFn = (actor: "alice" | "bob") => (` + set -ex; + + ROUTER_IP=$$(dig +short ${actor}_router) + INTERNET_SUBNET=$$(curl --silent --unix-socket /var/run/docker.sock http://localhost/networks | jq -r '.[] | select(.Name == \"${internetNetworkName}\") | .IPAM.Config[0].Subnet') + + ip route add $$INTERNET_SUBNET via $$ROUTER_IP dev eth0 + + hole-punch-client + `); + return { name, services: { relay: { + depends_on: ["redis"], image: relayImageId, init: true, environment: { @@ -112,7 +127,7 @@ function buildSpec(containerImages: { [key: string]: () => string }, { depends_on: ["relay", "alice_router"], image: containerImages[aliceImage](), init: true, - command: ["/bin/sh", "-c", "set -ex; ip route add $(curl --silent --unix-socket /var/run/docker.sock http://localhost/networks | jq -r '.[] | select(.Name | contains(\"internet\")) | .IPAM.Config[0].Subnet') via $(dig +short alice_router) dev eth0; hole-punch-client"], + command: ["/bin/sh", "-c", startupScriptFn("alice")], environment: { TRANSPORT: transport, MODE: "dial" @@ -138,7 +153,7 @@ function buildSpec(containerImages: { [key: string]: () => string }, { depends_on: ["relay", "bob_router"], image: containerImages[bobImage](), init: true, - command: ["/bin/sh", "-c", `set -ex; ip route add $(curl --silent --unix-socket /var/run/docker.sock http://localhost/networks | jq -r '.[] | select(.Name | contains(\"internet\")) | .IPAM.Config[0].Subnet') via $(dig +short bob_router) dev eth0; hole-punch-client`], + command: ["/bin/sh", "-c", startupScriptFn("bob")], environment: { TRANSPORT: transport, MODE: "listen" diff --git a/hole-punch-interop/src/lib.ts b/hole-punch-interop/src/lib.ts index d3fbca217..f0993a7c3 100644 --- a/hole-punch-interop/src/lib.ts +++ b/hole-punch-interop/src/lib.ts @@ -118,3 +118,7 @@ export const markdownTable = (table: string[][]): string => { return body; }; + +export function sanitizeComposeName(name: string) { + return name.replace(/[^a-zA-Z0-9_-]/g, "_"); +}