UNPKG

balena-cli

Version:

The official balena Command Line Interface

118 lines (114 loc) 5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const errors_1 = require("../../errors"); const cf = require("../../utils/common-flags"); const lazy_1 = require("../../utils/lazy"); const messages_1 = require("../../utils/messages"); class SupportCmd extends core_1.Command { async run() { var _a, _b; const { args: params, flags: options } = await this.parse(SupportCmd); const balena = (0, lazy_1.getBalenaSdk)(); const ux = (0, lazy_1.getCliUx)(); const enabling = params.action === 'enable'; if (!options.device && !options.fleet) { throw new errors_1.ExpectedError('At least one device or fleet must be specified'); } if (options.duration != null && !enabling) { throw new errors_1.ExpectedError('--duration option is only applicable when enabling support'); } const durationDefault = '24h'; const duration = options.duration || durationDefault; const expiryTs = Date.now() + this.parseDuration(duration); const deviceUuids = ((_a = options.device) === null || _a === void 0 ? void 0 : _a.split(',')) || []; const appNames = ((_b = options.fleet) === null || _b === void 0 ? void 0 : _b.split(',')) || []; const enablingMessage = 'Enabling support access for'; const disablingMessage = 'Disabling support access for'; for (const deviceUuid of deviceUuids) { if (enabling) { ux.action.start(`${enablingMessage} device ${deviceUuid}`); await balena.models.device.grantSupportAccess(deviceUuid, expiryTs); } else if (params.action === 'disable') { ux.action.start(`${disablingMessage} device ${deviceUuid}`); await balena.models.device.revokeSupportAccess(deviceUuid); } ux.action.stop(); } const { getFleetSlug } = await Promise.resolve().then(() => require('../../utils/sdk')); for (const appName of appNames) { const slug = await getFleetSlug(balena, appName); if (enabling) { ux.action.start(`${enablingMessage} fleet ${slug}`); await balena.models.application.grantSupportAccess(slug, expiryTs); } else if (params.action === 'disable') { ux.action.start(`${disablingMessage} fleet ${slug}`); await balena.models.application.revokeSupportAccess(slug); } ux.action.stop(); } if (enabling) { console.log(`Access has been granted for ${duration}, expiring ${new Date(expiryTs).toISOString()}`); } } parseDuration(duration) { const parseErrorMsg = 'Duration must be specified as number followed by h or d, e.g. 24h, 1d'; const unit = duration.slice(duration.length - 1); const amount = Number(duration.substring(0, duration.length - 1)); if (isNaN(amount)) { throw new errors_1.ExpectedError(parseErrorMsg); } let durationMs; if (['h', 'H'].includes(unit)) { durationMs = amount * 60 * 60 * 1000; } else if (['d', 'D'].includes(unit)) { durationMs = amount * 24 * 60 * 60 * 1000; } else { throw new errors_1.ExpectedError(parseErrorMsg); } return durationMs; } } SupportCmd.description = (0, lazy_1.stripIndent) ` Grant or revoke support access for devices or fleets. Grant or revoke balena support agent access to devices or fleets on balenaCloud. (This command does not apply to openBalena.) Access will be automatically revoked once the specified duration has elapsed. Duration defaults to 24h, but can be specified using --duration flag in days or hours, e.g. '12h', '2d'. Both --device and --fleet flags accept multiple values, specified as a comma-separated list (with no spaces). ${messages_1.applicationIdInfo.split('\n').join('\n\t\t')} `; SupportCmd.examples = [ 'balena support enable --device ab346f,cd457a --duration 3d', 'balena support enable --fleet myFleet --duration 12h', 'balena support disable -f myorg/myfleet', ]; SupportCmd.args = { action: core_1.Args.string({ description: 'enable|disable support access', options: ['enable', 'disable'], }), }; SupportCmd.flags = { device: core_1.Flags.string({ description: 'comma-separated list (no spaces) of device UUIDs', char: 'd', }), fleet: { ...cf.fleet, description: 'comma-separated list (no spaces) of fleet names or slugs (preferred)', }, duration: core_1.Flags.string({ description: 'length of time to enable support for, in (h)ours or (d)ays, e.g. 12h, 2d', char: 't', }), }; SupportCmd.authenticated = true; exports.default = SupportCmd; //# sourceMappingURL=index.js.map