UNPKG

@mindconnect/mindconnect-nodejs

Version:

MindConnect Library for NodeJS (community based)

112 lines 6.97 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 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"); const green = command_utils_1.getColor("green"); const red = command_utils_1.getColor("red"); const yellow = command_utils_1.getColor("yellow"); exports.default = (program) => { program .command("agent-status") .alias("as") .option("-c, --config <agentconfig>", "config file with agent configuration", "agentconfig.json") .option("-k, --passkey <passkey>", "passkey") .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(`displays the agent status and agent onboarding status *`)) .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(`Agent Status for the agent configuration in: ${color(configFile)}.`, options.verbose); if (!fs.existsSync(configFile)) { throw new Error(`Can't find file ${configFile}`); } const agentFolder = utils_1.getAgentDir(path.dirname(options.config)); command_utils_1.verboseLog(`Using .mc folder for agent: ${color(agentFolder)}`, options.verbose); const configuration = require(configFile); const profile = utils_1.checkCertificate(configuration, options); const agent = new __1.MindConnectAgent(configuration, undefined, agentFolder); if (profile) { agent.SetupAgentCertificate(fs.readFileSync(options.cert)); } coloredStatusLog(agent); if (!options.passkey) { command_utils_1.errorLog("you have to provide a passkey to get mindsphere boarding status and online status (run mc as --help for full description)", true); } const auth = utils_1.loadAuth(); const sdk = new sdk_1.MindSphereSdk(auth.gateway, utils_1.decrypt(auth, options.passkey), auth.tenant); const agentMgmt = sdk.GetAgentManagementClient(); const onlinestatus = yield utils_1.retry(options.retry, () => __awaiter(void 0, void 0, void 0, function* () { return yield agentMgmt.GetAgentOnlineStatus(agent.ClientId()); }), 300, command_utils_1.retrylog("GetAgentStatus")); onlineLog(onlinestatus); const boardingstatus = yield utils_1.retry(options.retry, () => __awaiter(void 0, void 0, void 0, function* () { return yield agentMgmt.GetOnboardingStatus(agent.ClientId()); }), 300, command_utils_1.retrylog("GetBoardingStatus")); const { status } = boardingstatus; if (status) { coloredBoardingStatusLog(boardingstatus); } if (status === "ONBOARDED" && agent.HasDataSourceConfiguration()) { command_utils_1.verboseLog(color("Data Source Configuration\n"), options.verbose); command_utils_1.verboseLog(JSON.stringify(yield agent.GetDataSourceConfiguration(), null, 2), options.verbose); } if (status === "ONBOARDED" && agent.HasDataMappings()) { command_utils_1.verboseLog(color("Data Mappings\n"), options.verbose); command_utils_1.verboseLog(JSON.stringify(yield agent.GetDataMappings(), null, 2), options.verbose); } } catch (err) { command_utils_1.errorLog(err, options.verbose); } }))(); }) .on("--help", () => { console_1.log("\n Examples:\n"); console_1.log(` mc agent-status \t\t\t\t\tuses default ${color("agentconfig.json")}`); console_1.log(` mc agent-status --config agent.json \t\tuses specified configuration file`); console_1.log(` mc agent-status --cert private.key \t\t\tuses specified key for RSA_3072 profile`); console_1.log(` mc agent-status --passkey mypasskey \t\tdisplays also the online agent information`); console_1.log(` mc agent-status --passkey mypasskey --verbose \tdisplays additionally the mappings and configuration`); command_utils_1.serviceCredentialLog(); }); }; const coloredStatusLog = (agent) => { console_1.log(`\nAgent status, local information (from .mc folder):\nAgent Id: ${color(agent.ClientId())} is ${agent.IsOnBoarded() ? green("onboarded") : red("not onboarded")}, data source is ${agent.HasDataSourceConfiguration() ? green("configured") : red("not configured")}, mappings are ${agent.HasDataMappings() ? green("configured") : red("not configured")}.`); }; const coloredBoardingStatusLog = (boardingstatus) => { let color; if (boardingstatus.status === sdk_1.AgentManagementModels.OnboardingStatus.StatusEnum.ONBOARDING) { color = yellow; } else if (boardingstatus.status === sdk_1.AgentManagementModels.OnboardingStatus.StatusEnum.NOTONBOARDED) { color = red; } else { color = green; } console_1.log(`Agent is ${color("" + boardingstatus.status)}.`); }; const onlineLog = (onlinestatus) => { if (onlinestatus) { console_1.log(`Online Status: ${onlinestatus.status === sdk_1.AgentManagementModels.OnlineStatus.StatusEnum.OFFLINE ? red("OFFLINE") : green("ONLINE")} since: ${color("" + onlinestatus.since)}`); } }; //# sourceMappingURL=mc-agent-status.js.map