Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions packages/config/src/bin/dev-proxy-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import concurrently, { Command } from 'concurrently';
import fs from 'fs';
import os from 'os';
import path from 'path';
import treeKill from 'tree-kill';
import { LogType, fecLogger as fecLoggerDefault } from '@redhat-cloud-services/frontend-components-config-utilities';
import { getCdnPath, setEnv, validateFECConfig } from './common';
import serveChrome, { CONTAINER_NAME as CHROME_CONTAINTER_NAME, ContainerRuntime, checkContainerRuntime } from './serve-chrome';
Expand All @@ -23,6 +22,26 @@ const IOP_CUSTOM_ROUTES_CONTAINER_PATH = '/config/custom_routes.iop.json';
let execBin: ContainerRuntime | undefined = undefined;
let debug: boolean = false;

function killProcessTree(pid: number) {
try {
const children = execSync(`pgrep -P ${pid}`, { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'] })
.trim()
.split('\n')
.filter(Boolean)
.map(Number);
for (const child of children) {
killProcessTree(child);
}
} catch (_) {
// pgrep exits 1 when no children found
}
try {
process.kill(pid, 'SIGKILL');
} catch (_) {
// process already dead
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

interface RouteConfig {
url: string;
is_chrome?: boolean;
Expand Down Expand Up @@ -63,7 +82,7 @@ function stopContainer(containerName: string) {
try {
let isRunning: boolean = true;
try {
execSync(`${execBin} inspect -f '{{.State.Running}}' ${containerName}\n`).toString().trim().toLowerCase() === 'true';
isRunning = execSync(`${execBin} inspect -f '{{.State.Running}}' ${containerName}\n`).toString().trim().toLowerCase() === 'true';
} catch (error) {
isRunning = false;
}
Expand Down Expand Up @@ -258,19 +277,31 @@ async function devProxyScript(
const cleanup = () => {
commands.forEach((cmd) => {
if (cmd.pid) {
treeKill(cmd.pid, 'SIGKILL');
killProcessTree(cmd.pid);
}
});
if (waitOnProcess?.pid) {
treeKill(waitOnProcess.pid, 'SIGKILL');
killProcessTree(waitOnProcess.pid);
}
stopContainer(DEV_PROXY_CONTAINER_NAME);
removeContainer(DEV_PROXY_CONTAINER_NAME);
if (SPAFallback) {
stopContainer(CHROME_CONTAINTER_NAME);
removeContainer(CHROME_CONTAINTER_NAME);
}
console.log('\n');
};

const handleSignal = () => {
process.off('SIGINT', handleSignal);
process.off('SIGTERM', handleSignal);
cleanup();
process.exit(0);
};

process.on('SIGINT', handleSignal);
process.on('SIGTERM', handleSignal);

try {
const outputPath = webpackConfig.output.path;
const proxyEnvVar = process.env.HCC_ENV === 'stage' ? '-e HTTPS_PROXY=$RH_PROXY_URL' : '';
Expand Down Expand Up @@ -338,7 +369,7 @@ async function devProxyScript(
prefixColor: 'bgGreen',
},
{
command: `${execBin} run -d -e HCC_ENV=${process.env.HCC_ENV} -e HCC_ENV_URL=${process.env.HCC_ENV_URL} ${proxyEnvVar} ${iopEnvVar} ${iopCustomRoutesEnvVar} -p ${argv.port || 1337}:${DEV_PROXY_CONTAINER_PORT} -v "${routesConfigPath}:/config/routes.json:ro,z" ${iopCustomRoutesMountVar} --name ${DEV_PROXY_CONTAINER_NAME} ${DEV_PROXY_IMAGE_REPO}${DEV_PROXY_IMAGE_TAG} ${proxyVerbose}`,
command: `${execBin} run --rm -d -e HCC_ENV=${process.env.HCC_ENV} -e HCC_ENV_URL=${process.env.HCC_ENV_URL} ${proxyEnvVar} ${iopEnvVar} ${iopCustomRoutesEnvVar} -p ${argv.port || 1337}:${DEV_PROXY_CONTAINER_PORT} -v "${routesConfigPath}:/config/routes.json:ro,z" ${iopCustomRoutesMountVar} --name ${DEV_PROXY_CONTAINER_NAME} ${DEV_PROXY_IMAGE_REPO}${DEV_PROXY_IMAGE_TAG} ${proxyVerbose}`,
name: 'PROXY',
env: { RH_PROXY_URL: PROXY_URL },
prefixColor: 'bgMagenta',
Expand Down Expand Up @@ -372,7 +403,7 @@ async function devProxyScript(
},
() => {
cleanup();
process.exit(0);
process.exit(1);
},
);
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/bin/serve-chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async function startServer(image: string, tag: string, serverPort: number) {
} catch (error) {
fecLogger(LogType.info, 'No existing chrome container found');
}
const runCommand = `${execBin} run -p ${serverPort}:${CONTAINER_PORT} --name ${CONTAINER_NAME} ${image}:${tag}`;
const runCommand = `${execBin} run --rm -p ${serverPort}:${CONTAINER_PORT} --name ${CONTAINER_NAME} ${image}:${tag}`;
Comment thread
charlesmulder marked this conversation as resolved.
Resolved
fecLogger(LogType.debug, `Spawn ${runCommand}`);
const child = spawn(runCommand, [], {
stdio: 'ignore',
Expand Down
Loading