@mindconnect/mindconnect-nodejs
Version:
MindConnect Library for NodeJS (community based)
75 lines • 4.05 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 __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");
exports.default = (program) => {
program
.command("offboard-agent")
.alias("of")
.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("offboards the 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 configFile = path.resolve(options.config);
const configuration = require(configFile);
const agentid = configuration.content.clientId;
agentid || utils_1.throwError("invalid configuration in the file");
const ag = sdk.GetAgentManagementClient();
yield __1.retry(options.retry, () => ag.OffboardAgent(`${agentid}`), 500, command_utils_1.retrylog("OffboardAgent", color));
console.log(`\nAgent offboarded`);
console.log(`Run ${color("mc agent-status")} command to see the status of your agent`);
console.log(`You have to run ${color("mc renew-agent")} command before you can onboard this agent again!`);
command_utils_1.agentConfigLog({
gateway: sdk.GetGateway(),
host: "gateway",
tenant: sdk.GetTenant(),
agentid,
color
});
}
catch (err) {
command_utils_1.errorLog(err, options.verbose);
}
}))();
})
.on("--help", () => {
console_1.log("\n Examples:\n");
console_1.log(` mc offboard-agent --config agent.json --passkey passkey... \t offboard agent with agent.json configuration`);
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 of --help for full description)", true);
!options.config && command_utils_1.errorLog("you have to provide a filename for the agent configuration", true);
!fs.existsSync(options.config) && utils_1.throwError(`the config file ${color(options.config)} doesn't exist.`);
}
//# sourceMappingURL=mc-offboard-agent.js.map