Skip to content

Commit

Permalink
feat: ✨ Add check for existing containers
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Jan 31, 2025
1 parent 036605b commit afdaf79
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion packages/cli/src/internal/launcherCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import fs from "node:fs";
import path from "node:path";
import { importAsyncConfig, parseZombieConfigForBins } from "../lib/configReader";
import { checkAlreadyRunning, downloadBinsIfMissing, promptAlreadyRunning } from "./fileCheckers";
import Docker from "dockerode";
import { select } from "@inquirer/prompts";

export async function commonChecks(env: Environment) {
const globalConfig = await importAsyncConfig();
Expand Down Expand Up @@ -50,7 +52,35 @@ async function devBinCheck(env: Environment) {
}

if (env.foundation.launchSpec[0].useDocker) {
return;
const docker = new Docker();
const imageName = env.foundation.launchSpec[0].binPath;

console.log(`Checking if ${imageName} is running...`);
const matchingContainers = (
await docker.listContainers({
filters: { ancestor: [imageName] },
})
).flat();

if (matchingContainers.length === 0) {
return;
}

if (!process.env.CI) {
await promptKillContainers(matchingContainers);
return;
}

const runningContainers = matchingContainers.map(({ Id, Ports }) => ({
Id: Id.slice(0, 12),
Ports: Ports.map(({ PublicPort, PrivatePort }) =>
PublicPort ? `${PublicPort} -> ${PrivatePort}` : `${PrivatePort}`
).join(", "),
}));

console.table(runningContainers);

throw new Error(`${imageName} is already running, aborting`);
}

const binName = path.basename(env.foundation.launchSpec[0].binPath);
Expand All @@ -59,6 +89,43 @@ async function devBinCheck(env: Environment) {
await downloadBinsIfMissing(env.foundation.launchSpec[0].binPath);
}

async function promptKillContainers(matchingContainers: Docker.ContainerInfo[]) {
const answer = await select({
message: `The following containers are already running image ${matchingContainers[0].Image}: ${matchingContainers.map(({ Id }) => Id).join(", ")}\n Would you like to kill them?`,
choices: [
{ name: "🪓 Kill containers", value: "kill" },
{ name: "👋 Quit", value: "goodbye" },
],
});

if (answer === "goodbye") {
console.log("Goodbye!");
process.exit(0);
}

if (answer === "kill") {
const docker = new Docker();
for (const { Id } of matchingContainers) {
const container = docker.getContainer(Id);
await container.stop();
await container.remove();
}

const containers = await docker.listContainers({
filters: { ancestor: matchingContainers.map(({ Image }) => Image) },
});

if (containers.length > 0) {
console.error(
`The following containers are still running: ${containers.map(({ Id }) => Id).join(", ")}`
);
process.exit(1);
}

return;
}
}

export async function executeScript(scriptCommand: string, args?: string) {
const scriptsDir = (await importAsyncConfig()).scriptsDir;

Expand Down

0 comments on commit afdaf79

Please sign in to comment.