balena-cli
Version:
The official balena Command Line Interface
155 lines (148 loc) • 6.76 kB
JavaScript
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");
const helpers_1 = require("../../utils/helpers");
class DeviceInitCmd extends core_1.Command {
async run() {
const { flags: options } = await this.parse(DeviceInitCmd);
const { promisify } = await Promise.resolve().then(() => require('util'));
const rimraf = promisify(await Promise.resolve().then(() => require('rimraf')));
const tmp = await Promise.resolve().then(() => require('tmp'));
const tmpNameAsync = promisify(tmp.tmpName);
tmp.setGracefulCleanup();
const { downloadOSImage } = await Promise.resolve().then(() => require('../../utils/cloud'));
const { getApplication } = await Promise.resolve().then(() => require('../../utils/sdk'));
const Logger = await Promise.resolve().then(() => require('../../utils/logger'));
const logger = Logger.getLogger();
const balena = (0, lazy_1.getBalenaSdk)();
const application = options.fleet
? await getApplication(balena, options.fleet, {
$select: ['id', 'slug'],
$expand: {
is_for__device_type: {
$select: 'slug',
},
},
})
: await (await Promise.resolve().then(() => require('../../utils/patterns'))).selectApplication();
const deviceUuid = balena.models.device.generateUniqueKey();
console.info(`Registering to ${application.slug}: ${deviceUuid}`);
await balena.models.device.register(application.id, deviceUuid);
const device = await balena.models.device.get(deviceUuid);
const tmpPath = (await tmpNameAsync());
try {
logger.logDebug(`Downloading OS image...`);
const osVersion = options['os-version'] || 'default';
const deviceType = application.is_for__device_type[0].slug;
await downloadOSImage(deviceType, tmpPath, osVersion);
logger.logDebug(`Configuring OS image...`);
await this.configureOsImage(tmpPath, device.uuid, options);
logger.logDebug(`Writing OS image...`);
await this.writeOsImage(tmpPath, deviceType, options);
}
catch (e) {
try {
logger.logDebug(`Process failed, removing device ${device.uuid}`);
await balena.models.device.remove(device.uuid);
}
catch (e) {
}
throw e;
}
finally {
logger.logDebug(`Removing temporary OS image download...`);
await rimraf(tmpPath);
}
console.log('Done');
return device.uuid;
}
async configureOsImage(path, uuid, options) {
const configureCommand = ['os', 'configure', path, '--device', uuid];
if (options.config) {
configureCommand.push('--config', options.config);
}
else if (options.advanced) {
configureCommand.push('--advanced');
}
if (options['provisioning-key-name']) {
configureCommand.push('--provisioning-key-name', options['provisioning-key-name']);
}
if (options['provisioning-key-expiry-date']) {
configureCommand.push('--provisioning-key-expiry-date', options['provisioning-key-expiry-date']);
}
await (0, helpers_1.runCommand)(configureCommand);
}
async writeOsImage(path, deviceType, options) {
const osInitCommand = ['os', 'initialize', path, '--type', deviceType];
if (options.yes) {
osInitCommand.push('--yes');
}
if (options.drive) {
osInitCommand.push('--drive', options.drive);
}
await (0, helpers_1.runCommand)(osInitCommand);
}
}
DeviceInitCmd.description = (0, lazy_1.stripIndent) `
Initialize a device with balenaOS.
Register a new device in the selected fleet, download the OS image for the
fleet's default device type, configure the image and write it to an SD card.
This command effectively combines several other balena CLI commands in one,
namely:
'balena device register'
'balena os download'
'balena os build-config' or 'balena config generate'
'balena os configure'
'balena os local flash'
Possible arguments for the '--fleet', '--os-version' and '--drive' options can
be listed respectively with the commands:
'balena fleet list'
'balena os versions'
'balena util available-drives'
If the '--fleet' or '--drive' options are omitted, interactive menus will be
presented with values to choose from. If the '--os-version' option is omitted,
the latest released OS version for the fleet's default device type will be used.
${messages_1.applicationIdInfo.split('\n').join('\n\t\t')}
Image configuration questions will be asked interactively unless a pre-configured
'config.json' file is provided with the '--config' option. The file can be
generated with the 'balena config generate' or 'balena os build-config' commands.
`;
DeviceInitCmd.examples = [
'$ balena device init',
'$ balena device init -f myorg/myfleet',
'$ balena device init --fleet myFleet --os-version 2.101.7 --drive /dev/disk5 --config config.json --yes',
'$ balena device init --fleet myFleet --os-version 2.83.21+rev1.prod --drive /dev/disk5 --config config.json --yes',
];
DeviceInitCmd.flags = {
fleet: cf.fleet,
yes: cf.yes,
advanced: core_1.Flags.boolean({
char: 'v',
description: 'show advanced configuration options',
}),
'os-version': core_1.Flags.string({
description: (0, lazy_1.stripIndent) `
exact version number, or a valid semver range,
or 'latest' (includes pre-releases),
or 'default' (excludes pre-releases if at least one stable version is available),
or 'recommended' (excludes pre-releases, will fail if only pre-release versions are available),
or 'menu' (will show the interactive menu)
`,
}),
drive: cf.drive,
config: core_1.Flags.string({
description: 'path to the config JSON file, see `balena os build-config`',
}),
'provisioning-key-name': core_1.Flags.string({
description: 'custom key name assigned to generated provisioning api key',
}),
'provisioning-key-expiry-date': core_1.Flags.string({
description: 'expiry date assigned to generated provisioning api key (format: YYYY-MM-DD)',
}),
};
DeviceInitCmd.authenticated = true;
exports.default = DeviceInitCmd;
//# sourceMappingURL=init.js.map
;