Skip to content

Commit

Permalink
Bug/teardown podman (#1022)
Browse files Browse the repository at this point in the history
* chore: fixed undefined network  before promise result

* chore: fix teardown before spawning end on user action

* bugfix: added interval to keep process running and avoid exit when using podman provider

* chore: apply feedback for review

* doc: added comment to explain why we prevent throw on previous ran commands
  • Loading branch information
l0r1s authored May 9, 2023
1 parent 8b1f696 commit 7e2d9e5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
8 changes: 6 additions & 2 deletions javascript/packages/cli/src/actions/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export async function spawn(
credsFile: string | undefined,
cmdOpts: any,
program: any,
): Promise<Network> {
setGlobalNetwork: (network: Network) => void,
): Promise<void> {
const opts = { ...program.parent.opts(), ...cmdOpts };
const dir = opts.dir || "";
const force = opts.force || false;
Expand Down Expand Up @@ -93,8 +94,11 @@ export async function spawn(
force,
inCI,
silent: false,
setGlobalNetwork,
};
const network = await start(creds, config, options);
network.showNetworkInfo(config.settings?.provider);
return network;
// keep the process running
// eslint-disable-next-line @typescript-eslint/no-empty-function
setInterval(() => {}, 1000);
}
7 changes: 6 additions & 1 deletion javascript/packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ const program = new Command("zombienet");
let network: Network | undefined;
let alreadyTryToStop = false;

const setGlobalNetwork = (globalNetwork: Network) => {
network = globalNetwork;
};

async function handleTermination(userInterrupted = false) {
process.env.terminating = "1";
if (network && !alreadyTryToStop) {
alreadyTryToStop = true;
if (userInterrupted) console.log("Ctrl+c detected...");
debug("removing namespace: " + network.namespace);
await network.dumpLogs();
console.log(decorators.blue("Tearing down network..."));
await network.stop();
}
}
Expand Down Expand Up @@ -154,7 +159,7 @@ function asyncAction(cmd: Function) {
(async () => {
try {
if (cmd.name == "spawn") {
network = await cmd(...args);
await cmd(...args, setGlobalNetwork);
} else {
await cmd(...args);
}
Expand Down
4 changes: 4 additions & 0 deletions javascript/packages/orchestrator/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface OrcOptionsInterface {
dir?: string;
force?: boolean;
silent?: boolean; // Mute logging output
setGlobalNetwork?: (network: Network) => void;
}

export async function start(
Expand Down Expand Up @@ -161,6 +162,9 @@ export async function start(
if (networkSpec.settings.node_spawn_timeout)
client.timeout = networkSpec.settings.node_spawn_timeout;
network = new Network(client, namespace, tmpDir.path);
if (options?.setGlobalNetwork) {
options.setGlobalNetwork(network);
}

const zombieTable = new CreateLogTable({
head: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class PodmanClient extends Client {
localMagicFilepath: string;
remoteDir: string;
dataDir: string;
isTearingDown: boolean;

constructor(configPath: string, namespace: string, tmpDir: string) {
super(configPath, namespace, tmpDir, "podman", "podman");
Expand All @@ -66,6 +67,7 @@ export class PodmanClient extends Client {
this.localMagicFilepath = `${tmpDir}/finished.txt`;
this.remoteDir = DEFAULT_REMOTE_DIR;
this.dataDir = DEFAULT_DATA_DIR;
this.isTearingDown = false;
}

async validateAccess(): Promise<boolean> {
Expand Down Expand Up @@ -177,6 +179,7 @@ export class PodmanClient extends Client {
}

async destroyNamespace(): Promise<void> {
this.isTearingDown = true;
// get pod names
let args = [
"pod",
Expand All @@ -189,11 +192,11 @@ export class PodmanClient extends Client {
let result = await this.runCommand(args, { scoped: false });

// now remove the pods
args = ["pod", "rm", "-f", ...result.stdout.split("\n")];
args = ["pod", "rm", "-f", "-i", ...result.stdout.split("\n")];
result = await this.runCommand(args, { scoped: false });

// now remove the pnetwork
args = ["network", "rm", this.namespace];
args = ["network", "rm", "-f", this.namespace];
result = await this.runCommand(args, { scoped: false });
}

Expand Down Expand Up @@ -297,10 +300,14 @@ export class PodmanClient extends Client {
stdout,
};
} catch (error) {
console.log(
`\n ${decorators.red("Error: ")} \t ${decorators.bright(error)}\n`,
);
throw error;
// We prevent previous commands ran to throw error when we are tearing down the network.
if (!this.isTearingDown) {
console.log(
`\n ${decorators.red("Error: ")} \t ${decorators.bright(error)}\n`,
);
throw error;
}
return { exitCode: 0, stdout: "" };
}
}

Expand Down

0 comments on commit 7e2d9e5

Please sign in to comment.