UNPKG

@zombienet/orchestrator

Version:

ZombieNet aim to be a testing framework for substrate based blockchains, providing a simple cli tool that allow users to spawn and test ephemeral Substrate based networks

109 lines (108 loc) 5.51 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupChainSpec = setupChainSpec; exports.getChainSpecRaw = getChainSpecRaw; const utils_1 = require("@zombienet/utils"); const constants_1 = require("../../constants"); const client_1 = require("../client"); const dynResourceDefinition_1 = require("./dynResourceDefinition"); const debug = require("debug")("zombie::podman::chain-spec"); const { copyFileSync, readFileSync, promises } = require("fs"); function setupChainSpec(namespace, chainConfig, chainName, chainFullPath) { return __awaiter(this, void 0, void 0, function* () { // We have two options to get the chain-spec file, neither should use the `raw` file/argument // 1: User provide the file (we DON'T expect the raw file) // 2: User provide the chainSpecCommand (without the --raw option) const client = (0, client_1.getClient)(); if (chainConfig.chainSpecPath) { // copy file to temp to use copyFileSync(chainConfig.chainSpecPath, chainFullPath); } else { if (chainConfig.chainSpecCommand) { const { defaultImage, chainSpecCommand } = chainConfig; const plainChainSpecOutputFilePath = client.remoteDir + "/" + constants_1.DEFAULT_CHAIN_SPEC.replace(/{{chainName}}/gi, chainName); // set output of command const fullCommand = `${chainSpecCommand.replace(/{{chainName}}/gi, chainName)} > ${plainChainSpecOutputFilePath}`; const node = yield (0, dynResourceDefinition_1.createTempNodeDef)("temp", defaultImage, chainName, fullCommand); const podDef = yield (0, dynResourceDefinition_1.genNodeDef)(namespace, node); const podName = podDef.metadata.name; yield client.spawnFromDef(podDef); debug("copy file from pod"); const podChainPath = `${client.tmpDir}/${podName}${plainChainSpecOutputFilePath}`; copyFileSync(podChainPath, chainFullPath); } } }); } function getChainSpecRaw(namespace, image, chainName, chainSpecCommand, chainFullPath) { return __awaiter(this, void 0, void 0, function* () { const plainPath = chainFullPath.replace(".json", "-plain.json"); const client = (0, client_1.getClient)(); const remoteChainSpecFullPath = client.remoteDir + "/" + constants_1.DEFAULT_CHAIN_SPEC.replace(/{{chainName}}/, chainName); const remoteChainSpecRawFullPath = client.remoteDir + "/" + constants_1.DEFAULT_CHAIN_SPEC_RAW.replace(/{{chainName}}/, chainName); const chainSpecCommandRaw = chainSpecCommand.replace(/{{chainName}}/gi, remoteChainSpecFullPath); const fullCommand = `${chainSpecCommandRaw} --raw > ${remoteChainSpecRawFullPath}`; const node = yield (0, dynResourceDefinition_1.createTempNodeDef)("temp", image, chainName, fullCommand); const podDef = yield (0, dynResourceDefinition_1.genNodeDef)(namespace, node); const podName = podDef.metadata.name; yield client.spawnFromDef(podDef, [ { localFilePath: plainPath, remoteFilePath: remoteChainSpecFullPath, }, ]); // let's just wait 2 secs yield (0, utils_1.sleep)(2000); debug("Getting the raw chain spec file from pod to the local environment."); yield client.copyFileFromPod(podName, remoteChainSpecRawFullPath, chainFullPath, podName); // We had some issues where the `raw` file is empty // let's add some extra checks here to ensure we are ok. let isValid = false; try { const chainSpecContentTest = readFileSync(chainFullPath); JSON.parse(chainSpecContentTest.toString()); isValid = true; } catch (e) { debug(e); } if (!isValid) { try { const result = yield client.runCommand([ "exec", `${podName}_pod-${podName}`, "cat", remoteChainSpecRawFullPath, ]); if (result.exitCode === 0 && result.stdout.length > 0) { // TODO: remove this debug when we get this fixed. debug(result.stdout); promises.writeFileSync(chainFullPath, result.stdout); isValid = true; } } catch (e) { debug(e); } } if (!isValid) throw new Error(`Invalid chain spec raw file generated.`); yield client.putLocalMagicFile(podName, podName); }); }