UNPKG

@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)

289 lines 16 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 __1 = require("../.."); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const path = require("path"); let color = (0, command_utils_1.getColor)("magenta"); exports.default = (program) => { program .command("oe-app-deploy") .alias("oead") .option("-m, --mode [list|create|update|accept|remove|template|info|check]", "list | create | update | accept | remove | template | info | check", "list") .option("-i, --id <id>", "the installation task id") .option("-d, --deviceid <deviceid>", "deviceid to filter") .option("-r, --releaseid <releaseid>", "software release id") .option("-f, --file <file>", ".mdsp.json file with app data") .option("-s, --status <status> [closed|open]", "closed | open") .option("-o, --overwrite", "overwrite template file if it already exists") .option("-k, --passkey <passkey>", "passkey") .option("-y, --retry <number>", "retry attempts before giving up", "3") .option("-v, --verbose", "verbose output") .description(color("list, create, update app installation task(s) (open edge) *")) .action((options) => { (() => __awaiter(void 0, void 0, void 0, function* () { try { checkRequiredParameters(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); switch (options.mode) { case "list": yield listInstalls(sdk, options); break; case "template": yield createTemplateInstallationTask(options, sdk); yield createTemplateRemovalTask(options, sdk); yield createTemplateTaskStatus(options, sdk); console.log("Edit the file(s) before submitting it to MindSphere."); break; case "create": yield createInstallationTask(options, sdk); break; case "remove": yield createRemovalTask(options, sdk); break; case "update": yield updateTaskStatus(options, sdk); break; case "check": yield checkTermAndConditions(options, sdk); break; case "accept": yield acceptTermAndConditions(options, sdk); break; case "info": yield taskInstInfo(options, sdk); break; default: throw Error(`no such option: ${options.mode}`); } } catch (err) { (0, command_utils_1.errorLog)(err, options.verbose); } }))(); }) .on("--help", () => { (0, console_1.log)("\n Examples:\n"); (0, console_1.log)(` mdsp oe-app-deploy --mode list --deviceid "7d018c..." \n\tlist all installation and removal tasks of a specified device.`); (0, console_1.log)(` mdsp oe-app-deploy --mode template \n\tcreate template files to define an app installation/removal task.`); (0, console_1.log)(` mdsp oe-app-deploy --mode create --file edge.install.app.mdsp.json \n\tcreates a new installation app taks.`); (0, console_1.log)(` mdsp oe-app-deploy --mode remove --file edge.remove.app.mdsp.json \n\tcreates a new removal app task.`); (0, console_1.log)(` mdsp oe-app-deploy --mode update --id "7d018c..." --file edge.app.status.mdsp.json \n\tupdate an installation/removal task from status template file.`); (0, console_1.log)(` mdsp oe-app-deploy --mode info --id <id>\n\tget details of an installation task.`); (0, console_1.log)(` mdsp oe-app-deploy --mode check --deviceid <deviceid> --releaseid <releaseid> \n\tcheck terms and condition of a software release on a a specific device.`); (0, console_1.log)(` mdsp oe-app-deploy --mode accept --deviceid <deviceid> --releaseid <releaseid> \n\taccept terms and condition of a software release on a a specific device.`); (0, command_utils_1.serviceCredentialLog)(); }); }; function checkRequiredParameters(options) { options.mode === "list" && !options.deviceid && (0, command_utils_1.errorLog)("you have to provide the device id to list all the app installation/removal tasks (see mdsp oe-app-deploy --help for more details)", true); options.mode === "create" && !options.file && (0, command_utils_1.errorLog)("you have to provide a file with the task data to create a new installation task (see mdsp oe-app-deploy --help for more details)", true); options.mode === "remove" && !options.file && (0, command_utils_1.errorLog)("you have to provide a file with the task data to create a new deployment workflow (see mdsp oe-app-deploy --help for more details)", true); options.mode === "update" && !options.id && (0, command_utils_1.errorLog)("you have to provide the id of the installation/removal task to update it (see mdsp oe-app-deploy --help for more details)", true); options.mode === "check" && !options.deviceid && (0, command_utils_1.errorLog)("you have to provide the deviceid to check for terms and conditions (see mdsp oe-app-deploy --help for more details)", true); options.mode === "check" && !options.releaseid && (0, command_utils_1.errorLog)("you have to provide the releaseid to check for terms and conditions (see mdsp oe-app-deploy --help for more details)", true); options.mode === "accept" && !options.deviceid && (0, command_utils_1.errorLog)("you have to provide the deviceid to accept the terms and conditions (see mdsp oe-app-deploy --help for more details)", true); options.mode === "accept" && !options.releaseid && (0, command_utils_1.errorLog)("you have to provide the releaseid to accept the terms and conditions (see mdsp oe-app-deploy --help for more details)", true); options.mode === "info" && !options.id && (0, command_utils_1.errorLog)("you have to provide the id of the installation task (see mdsp oe-app-deploy --help for more details)", true); } function listInstalls(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const appDeploymentClient = sdk.GetEdgeDeploymentClient(); // Parse all options const deviceid = options.deviceid; let page = 0; let iTaskPage; let rslt_table = []; do { iTaskPage = (yield (0, __1.retry)(options.retry, () => appDeploymentClient.GetInstallationTasks(deviceid, options.status, 100, page))); iTaskPage.content = iTaskPage.content || []; iTaskPage.page = iTaskPage.page || { totalPages: 0 }; rslt_table = rslt_table.concat(iTaskPage.content); } while (page++ < (iTaskPage.page.totalPages || 0)); // Print out the table console.log("App installation tasks"); console.table(rslt_table, ["id", "createdAt", "softwareType", "softwareId", "softwareReleaseId", "currentState"]); console.log(`${color(rslt_table.length)} app installation task(s) listed.\n`); page = 0; let rTaskPage; let rslt_table_2 = []; do { rTaskPage = (yield (0, __1.retry)(options.retry, () => appDeploymentClient.GetRemovalTasks(deviceid, options.status, 100, page))); rTaskPage.content = rTaskPage.content || []; rTaskPage.page = rTaskPage.page || { totalPages: 0 }; rslt_table_2 = rslt_table_2.concat(rTaskPage.content); } while (page++ < (rTaskPage.page.totalPages || 0)); // Print out the table console.log("Removal Tasks"); console.table(rslt_table, ["id", "createdAt", "softwareType", "softwareId", "softwareReleaseId", "currentState"]); console.log(`${color(rslt_table_2.length)} app removal task(s) listed.\n`); }); } function createTemplateInstallationTask(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const template = { deviceId: "7d018c...", softwareId: "7d018c...", softwareReleaseId: "7d018c...", customData: { sampleKey1: "sampleValue1", sampleKey2: "sampleValue2", }, }; (0, command_utils_1.verboseLog)(template, options.verbose); writeInstallTaskTemplateToFile(options, template); }); } function writeInstallTaskTemplateToFile(options, templateType) { const fileName = options.file || `edge.install.app.mdsp.json`; const filePath = path.resolve(fileName); fs.existsSync(filePath) && !options.overwrite && (0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `); fs.writeFileSync(filePath, JSON.stringify(templateType, null, 2)); console.log(`The app installation task template was written into ${color(filePath)} run \n\n\tmdsp oe-app-deploy --mode create --file ${fileName} \n\nto create a new instalation task.\n`); } function createTemplateRemovalTask(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const templateType = { deviceId: "7d018c...", softwareId: "7d018c...", softwareReleaseId: "7d018c...", customData: { sampleKey1: "sampleValue1", sampleKey2: "sampleValue2", }, }; (0, command_utils_1.verboseLog)(templateType, options.verbose); writeRemovalTaskTemplateToFile(options, templateType); }); } function writeRemovalTaskTemplateToFile(options, templateType) { const fileName = options.file || `edge.remove.app.mdsp.json`; const filePath = path.resolve(fileName); fs.existsSync(filePath) && !options.overwrite && (0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `); fs.writeFileSync(filePath, JSON.stringify(templateType, null, 2)); console.log(`The workflow instance template was written into ${color(filePath)} run \n\n\tmdsp oe-app-deploy --mode remove --file ${fileName} \n\nto create a new removal task.\n`); } function createTemplateTaskStatus(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const template = { state: "DOWNLOAD", progress: 0.4, message: "Task status updated as DOWNLOAD", details: { sampleKey1: "sampleValue1", sampleKey2: "sampleValue2", }, }; (0, command_utils_1.verboseLog)(template, options.verbose); writeTaskStatusTemplateToFile(options, template); }); } function writeTaskStatusTemplateToFile(options, templateType) { const fileName = options.file || `edge.app.status.mdsp.json`; const filePath = path.resolve(fileName); fs.existsSync(filePath) && !options.overwrite && (0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `); fs.writeFileSync(filePath, JSON.stringify(templateType, null, 2)); console.log(`The app installation task status template was written into ${color(filePath)} run \n\n\tmdsp oe-app-deploy --mode update --id <id> --file ${fileName} \n\nto update the instalation task.\n`); } function taskInstInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { let info = null; const id = options.id ? options.id : `${options.id}`; info = (yield (0, __1.retry)(options.retry, () => sdk.GetEdgeDeploymentClient().GetInstallationTask(id))); console.log(`Task with id ${id}:`); console.log(JSON.stringify(info, null, 4)); }); } function updateTaskStatus(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const data = JSON.parse(file.toString()); const id = options.id ? options.id : `${options.id}`; yield sdk.GetEdgeDeploymentClient().PatchInstallationTask(id, data); console.log(`updated the status of the installation task ${color(id)} as specified in ${color(filePath)}`); }); } function checkTermAndConditions(options, sdk) { return __awaiter(this, void 0, void 0, function* () { let info = null; const deviceid = options.deviceid ? options.deviceid : `${options.deviceid}`; const releaseid = options.releaseid ? options.releaseid : `${options.releaseid}`; info = (yield (0, __1.retry)(options.retry, () => sdk.GetEdgeDeploymentClient().GetTermsAndConditions(deviceid, releaseid))); if (info.firstAccepted) { console.log(`Terms and conditions accepted on ${color(info.firstAccepted)}.\n`); } else { console.log(`Terms and conditions are not ccepted for deviceid: ${deviceid} and releaseid: ${releaseid}.\n`); } }); } function acceptTermAndConditions(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const deviceid = options.deviceid ? options.deviceid : `${options.deviceid}`; const releaseid = options.releaseid ? options.releaseid : `${options.releaseid}`; yield (0, __1.retry)(options.retry, () => sdk.GetEdgeDeploymentClient().PostAcceptTermsAndConditions({ deviceId: deviceid, releaseId: releaseid, })); }); } function createRemovalTask(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const data = JSON.parse(file.toString()); const inst = yield sdk.GetEdgeDeploymentClient().PostRemovalTask(data); (0, command_utils_1.verboseLog)(JSON.stringify(inst, null, 2), options.verbose); console.log(`created a new app removal task on the device ${color(inst.deviceId)} as specified by the file ${color(filePath)}`); }); } function createInstallationTask(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const data = JSON.parse(file.toString()); const inst = yield sdk.GetEdgeDeploymentClient().PostInstallationTask(data); (0, command_utils_1.verboseLog)(JSON.stringify(inst, null, 2), options.verbose); console.log(`created a new app installation task as specified by the file ${color(filePath)}`); }); } //# sourceMappingURL=oe-app-deployment.js.map