diff --git a/javascript/packages/orchestrator/src/network.ts b/javascript/packages/orchestrator/src/network.ts index 990d3f070..e29cca48b 100644 --- a/javascript/packages/orchestrator/src/network.ts +++ b/javascript/packages/orchestrator/src/network.ts @@ -153,7 +153,7 @@ export class Network { async dumpLogs(showLogPath: boolean = true): Promise { const logsPath = this.tmpDir + "/logs"; // create dump directory in local temp - fs.mkdirSync(logsPath); + if (!fs.existsSync(logsPath)) fs.mkdirSync(logsPath); const paraNodes: NetworkNode[] = Object.values(this.paras).reduce( (memo: NetworkNode[], value) => memo.concat(value.nodes), [], diff --git a/javascript/packages/orchestrator/src/orchestrator.ts b/javascript/packages/orchestrator/src/orchestrator.ts index db8cf046e..c45b06c58 100644 --- a/javascript/packages/orchestrator/src/orchestrator.ts +++ b/javascript/packages/orchestrator/src/orchestrator.ts @@ -264,8 +264,9 @@ export async function start( // Check if the chain spec is in raw format // Could be if the chain_spec_path was set - const chainSpecContent = require(chainSpecFullPathPlain); + const chainSpecContent = readAndParseChainSpec(chainSpecFullPathPlain); const relayChainSpecIsRaw = Boolean(chainSpecContent.genesis?.raw); + client.chainId = chainSpecContent.id; const parachainFilesPromiseGenerator = async (parachain: Parachain) => { @@ -380,11 +381,11 @@ export async function start( // ensure chain raw is ok try { - const chainRawContent = require(chainSpecFullPath); - debug(`Chain name: ${chainRawContent.name}`); + const chainSpecContent = readAndParseChainSpec(chainSpecFullPathPlain); + debug(`Chain name: ${chainSpecContent.name}`); new CreateLogTable({ colWidths: [120], doubleBorder: true }).pushToPrint([ - [`Chain name: ${decorators.green(chainRawContent.name)}`], + [`Chain name: ${decorators.green(chainSpecContent.name)}`], ]); } catch (err) { console.log( @@ -671,7 +672,6 @@ export async function start( // add bootnodes to chain spec await addBootNodes(chainSpecFullPath, bootnodes); // flush require cache since we change the chain-spec - delete require.cache[require.resolve(chainSpecFullPath)]; if (client.providerName === "kubernetes") { // cache the chainSpec with bootnodes diff --git a/javascript/packages/orchestrator/src/providers/k8s/chain-spec.ts b/javascript/packages/orchestrator/src/providers/k8s/chain-spec.ts index 5987644e3..31e38ef7b 100644 --- a/javascript/packages/orchestrator/src/providers/k8s/chain-spec.ts +++ b/javascript/packages/orchestrator/src/providers/k8s/chain-spec.ts @@ -1,4 +1,5 @@ import { sleep } from "@zombienet/utils"; +import { promises as fsPromises, writeFileSync } from "fs"; import { DEFAULT_CHAIN_SPEC, DEFAULT_CHAIN_SPEC_COMMAND, @@ -6,9 +7,8 @@ import { } from "../../constants"; import { getClient } from "../client"; import { createTempNodeDef, genNodeDef } from "./dynResourceDefinition"; -const debug = require("debug")("zombie::kube::chain-spec"); -const fs = require("fs").promises; +const debug = require("debug")("zombie::kube::chain-spec"); export async function setupChainSpec( namespace: string, @@ -21,7 +21,7 @@ export async function setupChainSpec( // 2: User provide the chainSpecCommand (without the --raw option) const client = getClient(); if (chainConfig.chainSpecPath) { - await fs.copyFile(chainConfig.chainSpecPath, chainFullPath); + await fsPromises.copyFile(chainConfig.chainSpecPath, chainFullPath); } else { if (chainConfig.chainSpecCommand) { const { defaultImage, chainSpecCommand } = chainConfig; @@ -139,7 +139,7 @@ export async function getChainSpecRaw( if (result.exitCode === 0 && result.stdout.length > 0) { // TODO: remove this debug when we get this fixed. debug(result.stdout); - fs.writeFileSync(chainFullPath, result.stdout); + writeFileSync(chainFullPath, result.stdout); isValid = true; } } catch (_) {} diff --git a/javascript/packages/orchestrator/src/providers/native/chain-spec.ts b/javascript/packages/orchestrator/src/providers/native/chain-spec.ts index afa46c5d2..56bd4105b 100644 --- a/javascript/packages/orchestrator/src/providers/native/chain-spec.ts +++ b/javascript/packages/orchestrator/src/providers/native/chain-spec.ts @@ -1,4 +1,6 @@ import { sleep } from "@zombienet/utils"; +import { promises as fsPromises } from "fs"; +import { readAndParseChainSpec } from "../../chain-spec"; import { DEFAULT_CHAIN_SPEC, DEFAULT_CHAIN_SPEC_COMMAND, @@ -6,9 +8,8 @@ import { } from "../../constants"; import { getClient } from "../client"; import { createTempNodeDef, genNodeDef } from "./dynResourceDefinition"; -const debug = require("debug")("zombie::native::chain-spec"); -const fs = require("fs").promises; +const debug = require("debug")("zombie::native::chain-spec"); export async function setupChainSpec( namespace: string, @@ -22,7 +23,7 @@ export async function setupChainSpec( const client = getClient(); if (chainConfig.chainSpecPath) { // copy file to temp to use - await fs.copyFile(chainConfig.chainSpecPath, chainFullPath); + await fsPromises.copyFile(chainConfig.chainSpecPath, chainFullPath); } else { if (chainConfig.chainSpecCommand) { const { defaultImage, chainSpecCommand } = chainConfig; @@ -41,7 +42,7 @@ export async function setupChainSpec( const podDef = await genNodeDef(namespace, node); await client.spawnFromDef(podDef); - await fs.copyFile(plainChainSpecOutputFilePath, chainFullPath); + await fsPromises.copyFile(plainChainSpecOutputFilePath, chainFullPath); } } } @@ -90,7 +91,7 @@ export async function getChainSpecRaw( // let's add some extra checks here to ensure we are ok. let isValid = false; try { - require(chainFullPath); + readAndParseChainSpec(chainFullPath); isValid = true; } catch (e) { debug(e); diff --git a/javascript/packages/orchestrator/src/providers/native/nativeClient.ts b/javascript/packages/orchestrator/src/providers/native/nativeClient.ts index 79d318586..fc9ce4d87 100644 --- a/javascript/packages/orchestrator/src/providers/native/nativeClient.ts +++ b/javascript/packages/orchestrator/src/providers/native/nativeClient.ts @@ -131,10 +131,17 @@ export class NativeClient extends Client { return memo; }, memo); - if (pids.length > 0) { - args.push(`kill -9 ${pids.join(" ")}`); + const result = await this.runCommand( + ["bash", "-c", `ps ax| awk '{print $1}'| grep -E '${pids.join("|")}'`], + { allowFail: true }, + ); + if (result.exitCode === 0) { + const pidsToKill = result.stdout.split("\n"); + if (pidsToKill.length > 0) { + args.push(`kill -9 ${pids.join(" ")}`); - await this.runCommand(args); + await this.runCommand(args); + } } } diff --git a/javascript/packages/orchestrator/src/providers/podman/chain-spec.ts b/javascript/packages/orchestrator/src/providers/podman/chain-spec.ts index 462bfae83..025976178 100644 --- a/javascript/packages/orchestrator/src/providers/podman/chain-spec.ts +++ b/javascript/packages/orchestrator/src/providers/podman/chain-spec.ts @@ -8,7 +8,7 @@ import { getClient } from "../client"; import { createTempNodeDef, genNodeDef } from "./dynResourceDefinition"; const debug = require("debug")("zombie::podman::chain-spec"); -const fs = require("fs").promises; +const { copyFileSync, readFileSync, promises } = require("fs"); export async function setupChainSpec( namespace: string, @@ -22,7 +22,7 @@ export async function setupChainSpec( const client = getClient(); if (chainConfig.chainSpecPath) { // copy file to temp to use - await fs.copyFile(chainConfig.chainSpecPath, chainFullPath); + copyFileSync(chainConfig.chainSpecPath, chainFullPath); } else { if (chainConfig.chainSpecCommand) { const { defaultImage, chainSpecCommand } = chainConfig; @@ -46,7 +46,7 @@ export async function setupChainSpec( debug("copy file from pod"); const podChainPath = `${client.tmpDir}/${podName}${plainChainSpecOutputFilePath}`; - await fs.copyFile(podChainPath, chainFullPath); + copyFileSync(podChainPath, chainFullPath); } } } @@ -103,8 +103,12 @@ export async function getChainSpecRaw( let isValid = false; try { let content = require(chainFullPath); + const chainSpecContentTest = readFileSync(chainFullPath); + JSON.parse(chainSpecContentTest.toString()); isValid = true; - } catch (_) {} + } catch (e) { + debug(e); + } if (!isValid) { try { @@ -117,10 +121,12 @@ export async function getChainSpecRaw( if (result.exitCode === 0 && result.stdout.length > 0) { // TODO: remove this debug when we get this fixed. debug(result.stdout); - fs.writeFileSync(chainFullPath, result.stdout); + promises.writeFileSync(chainFullPath, result.stdout); isValid = true; } - } catch (_) {} + } catch (e) { + debug(e); + } } if (!isValid) throw new Error(`Invalid chain spec raw file generated.`); diff --git a/javascript/packages/orchestrator/src/providers/podman/podmanClient.ts b/javascript/packages/orchestrator/src/providers/podman/podmanClient.ts index 55f02c5a7..5c63db63b 100644 --- a/javascript/packages/orchestrator/src/providers/podman/podmanClient.ts +++ b/javascript/packages/orchestrator/src/providers/podman/podmanClient.ts @@ -386,20 +386,36 @@ export class PodmanClient extends Client { if (keystore) { const keystoreRemoteDir = `${dataPath.hostPath.path}/chains/${chainSpecId}/keystore`; - debug("keystoreRemoteDir", keystoreRemoteDir); await makeDir(keystoreRemoteDir, true); + const keystoreIsEmpty = + (await fs.readdir(keystoreRemoteDir).length) === 0; + if (!keystoreIsEmpty) + await this.runCommand( + ["unshare", "chmod", "-R", "o+w", keystoreRemoteDir], + { scoped: false }, + ); + debug("keystoreRemoteDir", keystoreRemoteDir); // inject keys await fseCopy(keystore, keystoreRemoteDir); debug("keys injected"); } + const cfgDirIsEmpty = + (await fs.readdir(`${this.tmpDir}/${name}/cfg`).length) === 0; + if (!cfgDirIsEmpty && filesToCopy.length) { + await this.runCommand( + ["unshare", "chmod", "-R", "o+w", `${this.tmpDir}/${name}/cfg`], + { scoped: false }, + ); + } // copy files to volumes for (const fileMap of filesToCopy) { const { localFilePath, remoteFilePath } = fileMap; - await fs.copyFile( - localFilePath, - `${this.tmpDir}/${name}${remoteFilePath}`, + debug( + `copyFile ${localFilePath} to ${this.tmpDir}/${name}${remoteFilePath}`, ); + await fs.cp(localFilePath, `${this.tmpDir}/${name}${remoteFilePath}`); + debug("copied!"); } await this.createResource(podDef, false, false);