UNPKG

balena-cli

Version:

The official balena Command Line Interface

185 lines (176 loc) • 8.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const cf = require("../../utils/common-flags"); const lazy_1 = require("../../utils/lazy"); const messages_1 = require("../../utils/messages"); class ConfigGenerateCmd extends core_1.Command { constructor() { super(...arguments); this.missingDeviceOrAppMessage = (0, lazy_1.stripIndent) ` Either a device or a fleet must be specified. See the help page for examples: $ balena help config generate `; this.deviceTypeNotAllowedMessage = 'The --deviceType option can only be used alongside the --fleet option'; } async getApplication(balena, fleet) { const { getApplication } = await Promise.resolve().then(() => require('../../utils/sdk')); return await getApplication(balena, fleet, { $select: 'slug', $expand: { is_for__device_type: { $select: 'slug' }, }, }); } async run() { const { flags: options } = await this.parse(ConfigGenerateCmd); const balena = (0, lazy_1.getBalenaSdk)(); await this.validateOptions(options); let resourceDeviceType; let application = null; let device = null; if (options.device != null) { const rawDevice = await balena.models.device.get(options.device, { $expand: { is_of__device_type: { $select: 'slug' } }, }); if (!rawDevice.belongs_to__application) { const { ExpectedError } = await Promise.resolve().then(() => require('../../errors')); throw new ExpectedError((0, lazy_1.stripIndent) ` Device ${options.device} does not appear to belong to an accessible fleet. Try with a different device, or use '--fleet' instead of '--device'.`); } device = rawDevice; resourceDeviceType = device.is_of__device_type[0].slug; } else { application = await this.getApplication(balena, options.fleet); resourceDeviceType = application.is_for__device_type[0].slug; } const deviceType = options.deviceType || resourceDeviceType; if (options.fleet && options.deviceType) { const helpers = await Promise.resolve().then(() => require('../../utils/helpers')); if (!(await helpers.areDeviceTypesCompatible(resourceDeviceType, deviceType))) { const { ExpectedError } = await Promise.resolve().then(() => require('../../errors')); throw new ExpectedError(`Device type ${options.deviceType} is incompatible with fleet ${options.fleet}`); } } const deviceManifest = await balena.models.config.getDeviceTypeManifestBySlug(deviceType); const { validateSecureBootOptionAndWarn } = await Promise.resolve().then(() => require('../../utils/config')); await validateSecureBootOptionAndWarn(options.secureBoot, deviceType, options.version); const answers = await (0, lazy_1.getCliForm)().run(deviceManifest.options, { override: { ...options, app: options.fleet, application: options.fleet }, }); answers.version = options.version; answers.developmentMode = options.dev; answers.secureBoot = options.secureBoot; answers.provisioningKeyName = options['provisioning-key-name']; answers.provisioningKeyExpiryDate = options['provisioning-key-expiry-date']; const { generateDeviceConfig, generateApplicationConfig } = await Promise.resolve().then(() => require('../../utils/config')); let config; if (device) { config = await generateDeviceConfig(device, options.deviceApiKey || options['generate-device-api-key'] || undefined, answers); } else if (application) { answers.deviceType = deviceType; config = await generateApplicationConfig(application, answers); } if (options.output != null) { const fs = await Promise.resolve().then(() => require('fs')); await fs.promises.writeFile(options.output, JSON.stringify(config)); } const prettyjson = await Promise.resolve().then(() => require('prettyjson')); console.log(prettyjson.render(config)); } async validateOptions(options) { const { ExpectedError } = await Promise.resolve().then(() => require('../../errors')); if (options.device == null && options.fleet == null) { throw new ExpectedError(this.missingDeviceOrAppMessage); } if (!options.fleet && options.deviceType) { throw new ExpectedError(this.deviceTypeNotAllowedMessage); } const { normalizeOsVersion } = await Promise.resolve().then(() => require('../../utils/normalization')); options.version = normalizeOsVersion(options.version); const { validateDevOptionAndWarn } = await Promise.resolve().then(() => require('../../utils/config')); await validateDevOptionAndWarn(options.dev, options.version); } } ConfigGenerateCmd.description = (0, lazy_1.stripIndent) ` Generate a config.json file. Generate a config.json file for a device or fleet. The target balenaOS version must be specified with the --version option. ${messages_1.devModeInfo.split('\n').join('\n\t\t')} ${messages_1.secureBootInfo.split('\n').join('\n\t\t')} To configure an image for a fleet of mixed device types, use the --fleet option alongside the --deviceType option to specify the target device type. To avoid interactive questions, specify a command line option for each question that would otherwise be asked. ${messages_1.applicationIdInfo.split('\n').join('\n\t\t')} `; ConfigGenerateCmd.examples = [ '$ balena config generate --device 7cf02a6 --version 2.12.7', '$ balena config generate --device 7cf02a6 --version 2.12.7 --generate-device-api-key', '$ balena config generate --device 7cf02a6 --version 2.12.7 --deviceApiKey <existingDeviceKey>', '$ balena config generate --device 7cf02a6 --version 2.12.7 --output config.json', '$ balena config generate --fleet myorg/fleet --version 2.12.7 --dev', '$ balena config generate --fleet myorg/fleet --version 2.12.7 --secureBoot', '$ balena config generate --fleet myorg/fleet --version 2.12.7 --deviceType fincm3', '$ balena config generate --fleet myorg/fleet --version 2.12.7 --output config.json', '$ balena config generate --fleet myorg/fleet --version 2.12.7 --network wifi --wifiSsid mySsid --wifiKey abcdefgh --appUpdatePollInterval 15', ]; ConfigGenerateCmd.flags = { version: core_1.Flags.string({ description: 'a balenaOS version', required: true, }), fleet: { ...cf.fleet, exclusive: ['device'] }, dev: cf.dev, secureBoot: cf.secureBoot, device: { ...cf.device, exclusive: [ 'fleet', 'provisioning-key-name', 'provisioning-key-expiry-date', ], }, deviceApiKey: core_1.Flags.string({ description: 'custom device key - note that this is only supported on balenaOS 2.0.3+', char: 'k', }), deviceType: core_1.Flags.string({ description: "device type slug (run 'balena device-type list' for possible values)", }), 'generate-device-api-key': core_1.Flags.boolean({ description: 'generate a fresh device key for the device', }), output: core_1.Flags.string({ description: 'path of output file', char: 'o', }), network: core_1.Flags.string({ description: 'the network type to use: ethernet or wifi', options: ['ethernet', 'wifi'], }), wifiSsid: core_1.Flags.string({ description: 'the wifi ssid to use (used only if --network is set to wifi)', }), wifiKey: core_1.Flags.string({ description: 'the wifi key to use (used only if --network is set to wifi)', }), appUpdatePollInterval: core_1.Flags.string({ description: 'supervisor cloud polling interval in minutes (e.g. for device variables)', }), 'provisioning-key-name': core_1.Flags.string({ description: 'custom key name assigned to generated provisioning api key', exclusive: ['device'], }), 'provisioning-key-expiry-date': core_1.Flags.string({ description: 'expiry date assigned to generated provisioning api key (format: YYYY-MM-DD)', exclusive: ['device'], }), }; ConfigGenerateCmd.authenticated = true; exports.default = ConfigGenerateCmd; //# sourceMappingURL=generate.js.map