UNPKG

@mindconnect/mindconnect-nodejs

Version:

MindConnect Library for NodeJS (community based)

67 lines 4 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 path = require("path"); const __1 = require("../../"); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const color = command_utils_1.getColor("cyan"); exports.default = (program) => { program .command("onboard") .alias("ob") .option("-c, --config <agentconfig>", "config file with agent configuration", "agentconfig.json") .option("-r, --cert [privatekey]", "required for agents with RSA_3072 profile. create with: openssl genrsa -out private.key 3072") .option("-y, --retry <number>", "retry attempts before giving up", 3) .option("-v, --verbose", "verbose output") .description(color("onboard the agent with configuration stored in the config file")) .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); const configFile = path.resolve(options.config); command_utils_1.verboseLog(`Onboarding the agent using the configuration stored in: ${color(configFile)}.`, options.verbose); if (!fs.existsSync(configFile)) { throw new Error(`Can't find file ${configFile}`); } const configuration = require(configFile); const profile = utils_1.checkCertificate(configuration, options); const agentFolder = utils_1.getAgentDir(path.dirname(options.config)); command_utils_1.verboseLog(`Using .mc folder for agent: ${color(agentFolder)}`, options.verbose); const agent = new __1.MindConnectAgent(configuration, undefined, agentFolder); if (profile) { agent.SetupAgentCertificate(fs.readFileSync(options.cert)); } if (!agent.IsOnBoarded()) { yield __1.retry(options.retry, () => agent.OnBoard(), 300, command_utils_1.retrylog("OnBoard")); console_1.log(`Your agent with id ${color(agent.ClientId())} was succesfully onboarded.`); } else { console_1.log(`Your agent with id ${color(agent.ClientId())} was already onboarded.`); command_utils_1.verboseLog(`Offboard the agent in the mindsphere UI and delete the .mc/${color(agent.ClientId() + ".json")} file to onboard again.`, options.verbose); } } catch (err) { command_utils_1.errorLog(err, options.verbose); } }))(); }) .on("--help", () => { console_1.log("\n Examples:\n"); console_1.log(` mc ob \t\t\t\tuses default ${color("agentconfig.json")}`); console_1.log(` mc onboard --config agent.json \tuses specified configuration file`); console_1.log(` mc onboard --config agent.json --cert private.key \tuses specified key for RSA_3072 profile`); }); }; //# sourceMappingURL=mc-onboard.js.map