UNPKG

@mindconnect/mindconnect-nodejs

Version:

MindConnect Library for NodeJS (community based)

103 lines 5.86 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 }); const console_1 = require("console"); const fs = require("fs"); const test_utils_1 = require("../../../test/test-utils"); const sdk_1 = require("../../api/sdk"); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const color = command_utils_1.getColor("magenta"); exports.default = (program) => { program .command("create-agent") .alias("ca") .option("-c, --config <agentconfig>", "config file for agent configuration") .option("-r, --cert [privatekey]", "required for agents with RSA_3072 profile. create with: openssl genrsa -out private.key 3072") .option("-n, --name <name>", "agent name", `Agent${Date.now()}`) .option("-p, --parentid <name>", "parent asset id") .option("-f, --profile <profile>", "security profile [SHARED_SECRET|RSA_3072]", "SHARED_SECRET") .option("-k, --passkey <passkey>", "passkey") .option("-y, --retry <number>", "retry attempts before giving up", 3) .option("-v, --verbose", "verbose output") .description(color("create an agent in the mindsphere *")) .action(options => { (() => __awaiter(void 0, void 0, void 0, function* () { try { command_utils_1.homeDirLog(options.verbose, color); command_utils_1.proxyLog(options.verbose, color); checkRequiredParamaters(options); const auth = utils_1.loadAuth(); const sdk = new sdk_1.MindSphereSdk({ tenant: auth.tenant, gateway: auth.gateway, basicAuth: utils_1.decrypt(auth, options.passkey) }); const am = sdk.GetAssetManagementClient(); const ag = sdk.GetAgentManagementClient(); const parent = options.parentid ? yield am.GetAsset(options.parentid) : yield am.GetRootAsset(); const asset = { name: options.name, parentId: parent.assetId, description: "Agent created via mindconnect CLI", typeId: "core.mclib" }; command_utils_1.verboseLog("creating asset:", options.verbose); const createdAsset = (yield utils_1.retry(options.retry, () => am.PostAsset(asset), 500, command_utils_1.retrylog("PostAsset", color))); command_utils_1.verboseLog("creating agent:", options.verbose); const createdAgent = (yield utils_1.retry(options.retry, () => ag.PostAgent({ name: createdAsset.name, entityId: `${createdAsset.assetId}`, securityProfile: options.profile }), 500, command_utils_1.retrylog("PostAgent", color))); for (let index = 0; index < options.retry; index++) { const bc = yield ag.GetBoardingConfiguration(`${createdAgent.id}`); if (!bc.content) { yield test_utils_1.sleep((index + 1) * 1000); continue; } command_utils_1.verboseLog(JSON.stringify(bc, null, 2), options.verbose); fs.writeFileSync(options.config, JSON.stringify(bc)); break; } if (createdAgent.securityProfile === sdk_1.AgentManagementModels.AgentUpdate.SecurityProfileEnum.RSA3072) { console.log("\nConfigure your certificate by running\n"); console.log("\t" + color(`openssl genrsa -out ${options.config}.key 3072`)); } command_utils_1.agentConfigLog({ gateway: sdk.GetGateway(), host: "gateway", tenant: sdk.GetTenant(), agentid: `${createdAgent.id}`, color }); } catch (err) { command_utils_1.errorLog(err, options.verbose); } }))(); }) .on("--help", () => { console_1.log("\n Examples:\n"); console_1.log(` mc create-agent --config agent.json --passkey passkey... \t create agent with default parameters`); command_utils_1.serviceCredentialLog(); }); }; function checkRequiredParamaters(options) { !options.passkey && command_utils_1.errorLog("you have to provide a passkey to get the service token (run mc ca --help for full description)", true); !options.config && command_utils_1.errorLog("you have to provide a filename for the agent configuration", true); options.profile !== "SHARED_SECRET" && options.profile !== "RSA_3072" && command_utils_1.errorLog("invalid security profile (SHARED_SECRET, RSA_3072 only)", true); fs.existsSync(options.config) && utils_1.throwError(`the config file ${color(options.config)} already exists.`); } //# sourceMappingURL=mc-create-agent.js.map