@mindconnect/mindconnect-nodejs
Version:
NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)
70 lines • 3.83 kB
JavaScript
;
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 utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
let color = (0, command_utils_1.getColor)("magenta");
exports.default = (program) => {
program
.command("renew-agent")
.alias("rn")
.option("-c, --config <agentconfig>", "config file for agent configuration")
.option("-k, --passkey <passkey>", "passkey")
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-v, --verbose", "verbose output")
.description(color("renews the agent secrets *"))
.action((options) => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
checkRequiredParamaters(options);
const sdk = (0, command_utils_1.getSdk)(options);
color = (0, command_utils_1.adjustColor)(color, options);
(0, command_utils_1.homeDirLog)(options.verbose, color);
(0, command_utils_1.proxyLog)(options.verbose, color);
const configFile = path.resolve(options.config);
const configuration = require(configFile);
const agentid = configuration.content.clientId;
agentid || (0, utils_1.throwError)("invalid configuration in the file");
const ag = sdk.GetAgentManagementClient();
const bc = yield ag.GetBoardingConfiguration(`${agentid}`, { retry: options.retry });
(0, command_utils_1.verboseLog)(JSON.stringify(bc), options.verbose);
!bc.content && (0, utils_1.throwError)("Invalid configuration recieved from mindsphere.");
console.log(`\nAgent configuration renewed`);
fs.writeFileSync(options.config, JSON.stringify(bc));
(0, command_utils_1.agentConfigLog)({
gateway: sdk.GetGateway(),
host: "gateway",
tenant: sdk.GetTenant(),
agentid,
color,
});
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
(0, console_1.log)("\n Examples:\n");
(0, console_1.log)(` mdsp renew-agent --config agent.json --passkey passkey... \t renew agent secrets in agent.json configuration`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkRequiredParamaters(options) {
!options.passkey &&
(0, command_utils_1.errorLog)("you have to provide a passkey to get the service token (run mdsp rn --help for full description)", true);
!options.config && (0, command_utils_1.errorLog)("you have to provide a filename for the agent configuration", true);
!fs.existsSync(options.config) && (0, utils_1.throwError)(`the config file ${color(options.config)} doesn't exist.`);
}
//# sourceMappingURL=mc-renew-agent.js.map