Skip to content

Commit

Permalink
Release changes from #386 (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardopirovano authored Feb 6, 2024
2 parents d92237d + 1f5837e commit 96ea3f5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN apt-get update \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-khmeros fonts-kacst fonts-freefont-ttf libxss1 \
&& apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-khmeros fonts-kacst fonts-freefont-ttf libxss1 nginx \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

Expand Down
2 changes: 2 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getBaseAndHeadCommitShas } from "./utils/get-base-and-head-commit-shas"
import { getCodeChangeEvent } from "./utils/get-code-change-event";
import { getInputs } from "./utils/get-inputs";
import { initLogger, setLogLevel, shortSha } from "./utils/logger.utils";
import { spinUpProxyIfNeeded } from "./utils/proxy";
import { ResultsReporter } from "./utils/results-reporter";
import { waitForDeploymentUrl } from "./utils/wait-for-deployment-url";

Expand Down Expand Up @@ -144,6 +145,7 @@ export const runMeticulousTestsAction = async (): Promise<void> => {
: appUrl;

if (urlToTestAgainst != null) {
spinUpProxyIfNeeded(urlToTestAgainst, logger);
await throwIfCannotConnectToOrigin(urlToTestAgainst);
}

Expand Down
12 changes: 1 addition & 11 deletions src/utils/__tests__/get-inputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("getInputs", () => {

expect(getInputs()).toEqual({
...EXPECTED_DEFAULT_VALUES,
appUrl: "https://example.com/",
appUrl: "https://example.com",
localhostAliases: "app1.local,app2.local",
parallelTasks: 5,
testsFile: "tests.json",
Expand Down Expand Up @@ -125,16 +125,6 @@ describe("getInputs", () => {
expect(() => getInputs()).toThrowError();
});

it("handles rewriting localhost urls to the docker bridge IP", () => {
setupDefaultEnvVars();
process.env.APP_URL = "https://localhost/app";

expect(getInputs()).toEqual({
...EXPECTED_DEFAULT_VALUES,
appUrl: "https://172.17.0.1/app",
});
});

const setupDefaultEnvVars = () => {
process.env.API_TOKEN = "mock-api-token";
process.env.GITHUB_TOKEN = "mock-github-token";
Expand Down
2 changes: 1 addition & 1 deletion src/utils/add-localhost-aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve4 } from "dns/promises";
import { appendFile } from "fs/promises";
import { METICULOUS_LOGGER_NAME } from "@alwaysmeticulous/common";
import log from "loglevel";
import { DOCKER_BRIDGE_NETWORK_GATEWAY } from "./get-inputs";
import { DOCKER_BRIDGE_NETWORK_GATEWAY } from "./proxy";

const HOSTS_FILE = "/etc/hosts";

Expand Down
15 changes: 3 additions & 12 deletions src/utils/check-connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Socket } from "net";
import { DOCKER_BRIDGE_NETWORK_GATEWAY } from "./get-inputs";

export const throwIfCannotConnectToOrigin = async (url: string) => {
const { hostname, port, protocol, origin } = new URL(url);
Expand All @@ -8,18 +7,10 @@ export const throwIfCannotConnectToOrigin = async (url: string) => {
port != null && port != "" ? Number(port) : defaultPortForProtocol;
const connectionAccepted = await canConnectTo(hostname, portNumber);
if (!connectionAccepted) {
const rewrittenHostname = hostname.replace(
DOCKER_BRIDGE_NETWORK_GATEWAY,
"127.0.0.1"
);
const rewrittenOrigin = origin.replace(
DOCKER_BRIDGE_NETWORK_GATEWAY,
"127.0.0.1"
);
throw new Error(
`Could not connect to '${rewrittenHostname}:${portNumber}'. Please check:\n\n` +
`1. The server running at '${rewrittenOrigin}' has fully started by the time the Meticulous action starts. You may need to add a 'sleep 30' after starting the server to ensure that this is the case.\n` +
`2. The server running at '${rewrittenOrigin}' is using tcp instead of tcp6. You can use 'netstat -tulpen' to see what addresses and ports it is bound to.\n\n`
`Could not connect to '${hostname}:${portNumber}'. Please check:\n\n` +
`1. The server running at '${origin}' has fully started by the time the Meticulous action starts. You may need to add a 'sleep 30' after starting the server to ensure that this is the case.\n` +
`2. The server running at '${origin}' is using tcp instead of tcp6. You can use 'netstat -tulpen' to see what addresses and ports it is bound to.\n\n`
);
}
};
Expand Down
21 changes: 2 additions & 19 deletions src/utils/get-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const getInputs = () => {
required: true,
type: "string",
});
const appUrl_ = getInputFromEnv({
const appUrl = getInputFromEnv({
name: "app-url",
required: false,
type: "string",
Expand Down Expand Up @@ -63,7 +63,7 @@ export const getInputs = () => {
type: "string-array",
});

if (appUrl_ != null && appUrl_ != "" && useDeploymentUrl === true) {
if (appUrl != null && appUrl != "" && useDeploymentUrl === true) {
throw new Error("Cannot use both app-url and use-deployment-url");
}

Expand All @@ -80,8 +80,6 @@ export const getInputs = () => {
);
}

const appUrl = appUrl_ ? handleLocalhostUrl(appUrl_) : appUrl_;

return {
apiToken,
githubToken,
Expand All @@ -97,18 +95,3 @@ export const getInputs = () => {
allowedEnvironments,
};
};

export const DOCKER_BRIDGE_NETWORK_GATEWAY = "172.17.0.1";

// Swaps "localhost" with the IP address of the Docker host
const handleLocalhostUrl = (appUrl: string): string => {
try {
const url = new URL(appUrl);
if (url.hostname === "localhost" || url.hostname === "127.0.0.1") {
url.hostname = DOCKER_BRIDGE_NETWORK_GATEWAY;
}
return url.toString();
} catch (error) {
return appUrl;
}
};
41 changes: 41 additions & 0 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { execSync } from "child_process";
import { mkdirSync, writeFileSync } from "fs";
import * as Sentry from "@sentry/node";
import { Logger } from "loglevel";

export const DOCKER_BRIDGE_NETWORK_GATEWAY = "172.17.0.1";

// We are running in a docker container and need to proxy to localhost.
// We previously just rewrote the app URL to point to the host IP, but
// this breaks some sites that behave specially on localhost (e.g.
// allowing auth to go ahead with HTTPS).
export const spinUpProxyIfNeeded = (appUrl: string, logger: Logger): void => {
try {
const url = new URL(appUrl);
if (url.hostname === "localhost" || url.hostname === "127.0.0.1") {
const defaultPortForProtocol = url.protocol === "https:" ? 443 : 80;
const port = url.port || defaultPortForProtocol;
const nginxConfig = `
events {}
http {
server {
listen ${port};
server_name localhost;
location / {
proxy_pass http://${DOCKER_BRIDGE_NETWORK_GATEWAY}:${port};
}
}
}
`;
mkdirSync("/etc/nginx", { recursive: true });
writeFileSync("/etc/nginx/nginx.conf", nginxConfig);
execSync("service nginx restart");
logger.info(
`Successfully set up a proxy to host machine on port ${port}`
);
}
} catch (error) {
Sentry.captureException(error);
logger.error("Error while spinning up proxy", error);
}
};

0 comments on commit 96ea3f5

Please sign in to comment.