Skip to content

Commit

Permalink
Extract startupScriptFn
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Sep 15, 2023
1 parent 8f142d7 commit e4f653e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion hole-punch-interop/src/compose-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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`);
Expand Down
19 changes: 17 additions & 2 deletions hole-punch-interop/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand All @@ -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"
Expand All @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions hole-punch-interop/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "_");
}

0 comments on commit e4f653e

Please sign in to comment.