2020-04-24 13:08:45 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016-2020 Balena Ltd.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as commandOptions from './command-options';
|
|
|
|
|
|
|
|
import { normalizeUuidProp } from '../utils/normalization';
|
|
|
|
import { getBalenaSdk, getVisuals } from '../utils/lazy';
|
2020-06-30 18:26:45 +00:00
|
|
|
import * as _ from 'lodash';
|
|
|
|
|
|
|
|
const getUmountAsync = async () => {
|
|
|
|
const { promisify } = await import('bluebird');
|
|
|
|
const { umount } = await import('umount');
|
|
|
|
return promisify(umount);
|
|
|
|
};
|
2020-04-24 13:08:45 +00:00
|
|
|
|
|
|
|
export const read = {
|
|
|
|
signature: 'config read',
|
|
|
|
description: 'read a device configuration',
|
|
|
|
help: `\
|
|
|
|
Use this command to read the config.json file from the mounted filesystem (e.g. SD card) of a provisioned device"
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ balena config read --type raspberry-pi
|
|
|
|
$ balena config read --type raspberry-pi --drive /dev/disk2\
|
|
|
|
`,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'type',
|
|
|
|
description:
|
|
|
|
'device type (Check available types with `balena devices supported`)',
|
|
|
|
parameter: 'type',
|
|
|
|
alias: 't',
|
|
|
|
required: 'You have to specify a device type',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'drive',
|
|
|
|
description: 'drive',
|
|
|
|
parameter: 'drive',
|
|
|
|
alias: 'd',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
permission: 'user',
|
|
|
|
root: true,
|
2020-06-30 18:26:45 +00:00
|
|
|
async action(_params, options) {
|
|
|
|
const umountAsync = await getUmountAsync();
|
|
|
|
|
|
|
|
const drive =
|
|
|
|
options.drive || (await getVisuals().drive('Select the device drive'));
|
|
|
|
await umountAsync(drive);
|
|
|
|
|
|
|
|
const config = await import('balena-config-json');
|
|
|
|
const configJSON = await config.read(drive, options.type);
|
|
|
|
|
|
|
|
const prettyjson = await import('prettyjson');
|
|
|
|
console.info(prettyjson.render(configJSON));
|
2020-04-24 13:08:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const write = {
|
|
|
|
signature: 'config write <key> <value>',
|
|
|
|
description: 'write a device configuration',
|
|
|
|
help: `\
|
|
|
|
Use this command to write the config.json file to the mounted filesystem (e.g. SD card) of a provisioned device
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ balena config write --type raspberry-pi username johndoe
|
|
|
|
$ balena config write --type raspberry-pi --drive /dev/disk2 username johndoe
|
|
|
|
$ balena config write --type raspberry-pi files.network/settings "..."\
|
|
|
|
`,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'type',
|
|
|
|
description:
|
|
|
|
'device type (Check available types with `balena devices supported`)',
|
|
|
|
parameter: 'type',
|
|
|
|
alias: 't',
|
|
|
|
required: 'You have to specify a device type',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'drive',
|
|
|
|
description: 'drive',
|
|
|
|
parameter: 'drive',
|
|
|
|
alias: 'd',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
permission: 'user',
|
|
|
|
root: true,
|
2020-06-30 18:26:45 +00:00
|
|
|
async action(params, options) {
|
|
|
|
const umountAsync = await getUmountAsync();
|
|
|
|
|
|
|
|
const drive =
|
|
|
|
options.drive || (await getVisuals().drive('Select the device drive'));
|
|
|
|
await umountAsync(drive);
|
|
|
|
|
|
|
|
const config = await import('balena-config-json');
|
|
|
|
const configJSON = await config.read(drive, options.type);
|
|
|
|
|
|
|
|
console.info(`Setting ${params.key} to ${params.value}`);
|
|
|
|
_.set(configJSON, params.key, params.value);
|
|
|
|
|
|
|
|
await umountAsync(drive);
|
|
|
|
|
|
|
|
await config.write(drive, options.type, configJSON);
|
|
|
|
|
|
|
|
console.info('Done');
|
2020-04-24 13:08:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const inject = {
|
|
|
|
signature: 'config inject <file>',
|
|
|
|
description: 'inject a device configuration file',
|
|
|
|
help: `\
|
|
|
|
Use this command to inject a config.json file to the mounted filesystem
|
|
|
|
(e.g. SD card or mounted balenaOS image) of a provisioned device"
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ balena config inject my/config.json --type raspberry-pi
|
|
|
|
$ balena config inject my/config.json --type raspberry-pi --drive /dev/disk2\
|
|
|
|
`,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'type',
|
|
|
|
description:
|
|
|
|
'device type (Check available types with `balena devices supported`)',
|
|
|
|
parameter: 'type',
|
|
|
|
alias: 't',
|
|
|
|
required: 'You have to specify a device type',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'drive',
|
|
|
|
description: 'drive',
|
|
|
|
parameter: 'drive',
|
|
|
|
alias: 'd',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
permission: 'user',
|
|
|
|
root: true,
|
2020-06-30 18:26:45 +00:00
|
|
|
async action(params, options) {
|
|
|
|
const umountAsync = await getUmountAsync();
|
|
|
|
|
|
|
|
const drive =
|
|
|
|
options.drive || (await getVisuals().drive('Select the device drive'));
|
|
|
|
await umountAsync(drive);
|
|
|
|
|
|
|
|
const fs = await import('fs');
|
|
|
|
const configJSON = JSON.parse(
|
|
|
|
await fs.promises.readFile(params.file, 'utf8'),
|
|
|
|
);
|
|
|
|
|
|
|
|
const config = await import('balena-config-json');
|
|
|
|
await config.write(drive, options.type, configJSON);
|
|
|
|
|
|
|
|
console.info('Done');
|
2020-04-24 13:08:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const reconfigure = {
|
|
|
|
signature: 'config reconfigure',
|
|
|
|
description: 'reconfigure a provisioned device',
|
|
|
|
help: `\
|
|
|
|
Use this command to reconfigure a provisioned device
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ balena config reconfigure --type raspberry-pi
|
|
|
|
$ balena config reconfigure --type raspberry-pi --advanced
|
|
|
|
$ balena config reconfigure --type raspberry-pi --drive /dev/disk2\
|
|
|
|
`,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'type',
|
|
|
|
description:
|
|
|
|
'device type (Check available types with `balena devices supported`)',
|
|
|
|
parameter: 'type',
|
|
|
|
alias: 't',
|
|
|
|
required: 'You have to specify a device type',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'drive',
|
|
|
|
description: 'drive',
|
|
|
|
parameter: 'drive',
|
|
|
|
alias: 'd',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'advanced',
|
|
|
|
description: 'show advanced commands',
|
|
|
|
boolean: true,
|
|
|
|
alias: 'v',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
permission: 'user',
|
|
|
|
root: true,
|
2020-06-30 18:26:45 +00:00
|
|
|
async action(_params, options) {
|
|
|
|
const umountAsync = await getUmountAsync();
|
|
|
|
|
|
|
|
const drive =
|
|
|
|
options.drive || (await getVisuals().drive('Select the device drive'));
|
|
|
|
await umountAsync(drive);
|
|
|
|
|
|
|
|
const config = await import('balena-config-json');
|
|
|
|
const { uuid } = await config.read(drive, options.type);
|
|
|
|
await umountAsync(drive);
|
|
|
|
|
|
|
|
let configureCommand = `os configure ${drive} --device ${uuid}`;
|
|
|
|
if (options.advanced) {
|
|
|
|
configureCommand += ' --advanced';
|
|
|
|
}
|
|
|
|
const { runCommand } = await import('../utils/helpers');
|
|
|
|
await runCommand(configureCommand);
|
|
|
|
|
|
|
|
console.info('Done');
|
2020-04-24 13:08:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const generate = {
|
|
|
|
signature: 'config generate',
|
|
|
|
description: 'generate a config.json file',
|
|
|
|
help: `\
|
|
|
|
Use this command to generate a config.json for a device or application.
|
|
|
|
|
|
|
|
Calling this command with the exact version number of the targeted image is required.
|
|
|
|
|
|
|
|
This is interactive by default, but you can do this automatically without interactivity
|
|
|
|
by specifying an option for each question on the command line, if you know the questions
|
|
|
|
that will be asked for the relevant device type.
|
|
|
|
|
|
|
|
In case that you want to configure an image for an application with mixed device types,
|
|
|
|
you can pass the --device-type argument along with --app to specify the target device type.
|
|
|
|
|
|
|
|
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 --device-api-key <existingDeviceKey>
|
|
|
|
$ balena config generate --device 7cf02a6 --version 2.12.7 --output config.json
|
|
|
|
$ balena config generate --app MyApp --version 2.12.7
|
|
|
|
$ balena config generate --app MyApp --version 2.12.7 --device-type fincm3
|
|
|
|
$ balena config generate --app MyApp --version 2.12.7 --output config.json
|
|
|
|
$ balena config generate --app MyApp --version 2.12.7 \
|
|
|
|
--network wifi --wifiSsid mySsid --wifiKey abcdefgh --appUpdatePollInterval 1\
|
|
|
|
`,
|
|
|
|
options: [
|
|
|
|
commandOptions.osVersion,
|
|
|
|
commandOptions.optionalApplication,
|
|
|
|
commandOptions.optionalDevice,
|
|
|
|
commandOptions.optionalDeviceApiKey,
|
|
|
|
commandOptions.optionalDeviceType,
|
|
|
|
{
|
|
|
|
signature: 'generate-device-api-key',
|
|
|
|
description: 'generate a fresh device key for the device',
|
|
|
|
boolean: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'output',
|
|
|
|
description: 'output',
|
|
|
|
parameter: 'output',
|
|
|
|
alias: 'o',
|
|
|
|
},
|
|
|
|
// Options for non-interactive configuration
|
|
|
|
{
|
|
|
|
signature: 'network',
|
|
|
|
description: 'the network type to use: ethernet or wifi',
|
|
|
|
parameter: 'network',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'wifiSsid',
|
|
|
|
description:
|
|
|
|
'the wifi ssid to use (used only if --network is set to wifi)',
|
|
|
|
parameter: 'wifiSsid',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'wifiKey',
|
|
|
|
description:
|
|
|
|
'the wifi key to use (used only if --network is set to wifi)',
|
|
|
|
parameter: 'wifiKey',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
signature: 'appUpdatePollInterval',
|
|
|
|
description:
|
|
|
|
'how frequently (in minutes) to poll for application updates',
|
|
|
|
parameter: 'appUpdatePollInterval',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
permission: 'user',
|
|
|
|
action(_params, options) {
|
|
|
|
normalizeUuidProp(options, 'device');
|
2020-06-23 10:29:03 +00:00
|
|
|
const Bluebird = require('bluebird');
|
2020-04-24 13:08:45 +00:00
|
|
|
const balena = getBalenaSdk();
|
|
|
|
const form = require('resin-cli-form');
|
|
|
|
const prettyjson = require('prettyjson');
|
|
|
|
|
|
|
|
const {
|
|
|
|
generateDeviceConfig,
|
|
|
|
generateApplicationConfig,
|
|
|
|
} = require('../utils/config');
|
|
|
|
const helpers = require('../utils/helpers');
|
2020-04-30 08:45:16 +00:00
|
|
|
const { exitWithExpectedError } = require('../errors');
|
2020-04-24 13:08:45 +00:00
|
|
|
|
|
|
|
if (options.device == null && options.application == null) {
|
|
|
|
exitWithExpectedError(`\
|
|
|
|
You have to pass either a device or an application.
|
|
|
|
|
|
|
|
See the help page for examples:
|
|
|
|
|
|
|
|
$ balena help config generate\
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.application && options.deviceType) {
|
|
|
|
exitWithExpectedError(`\
|
|
|
|
Specifying a different device type is only supported when
|
|
|
|
generating a config for an application:
|
|
|
|
|
|
|
|
* An application, with --app <appname>
|
|
|
|
* A specific device type, with --device-type <deviceTypeSlug>
|
|
|
|
|
|
|
|
See the help page for examples:
|
|
|
|
|
|
|
|
$ balena help config generate\
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
2020-06-23 10:29:03 +00:00
|
|
|
return Bluebird.try(
|
2020-06-15 22:53:07 +00:00
|
|
|
/** @returns {Promise<any>} */ function () {
|
2020-04-24 13:08:45 +00:00
|
|
|
if (options.device != null) {
|
|
|
|
return balena.models.device.get(options.device);
|
|
|
|
}
|
|
|
|
return balena.models.application.get(options.application);
|
|
|
|
},
|
|
|
|
)
|
2020-06-15 22:53:07 +00:00
|
|
|
.then(function (resource) {
|
2020-04-24 13:08:45 +00:00
|
|
|
const deviceType = options.deviceType || resource.device_type;
|
|
|
|
let manifestPromise = balena.models.device.getManifestBySlug(
|
|
|
|
deviceType,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (options.application && options.deviceType) {
|
|
|
|
const app = resource;
|
|
|
|
const appManifestPromise = balena.models.device.getManifestBySlug(
|
|
|
|
app.device_type,
|
|
|
|
);
|
2020-06-15 22:53:07 +00:00
|
|
|
manifestPromise = manifestPromise.tap((paramDeviceType) =>
|
|
|
|
appManifestPromise.then(function (appDeviceType) {
|
2020-04-24 13:08:45 +00:00
|
|
|
if (
|
|
|
|
!helpers.areDeviceTypesCompatible(
|
|
|
|
appDeviceType,
|
|
|
|
paramDeviceType,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new balena.errors.BalenaInvalidDeviceType(
|
|
|
|
`Device type ${options.deviceType} is incompatible with application ${options.application}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return manifestPromise
|
|
|
|
.get('options')
|
|
|
|
.then((
|
|
|
|
formOptions, // Pass params as an override: if there is any param with exactly the same name as a
|
|
|
|
) =>
|
|
|
|
// required option, that value is used (and the corresponding question is not asked)
|
|
|
|
form.run(formOptions, { override: options }),
|
|
|
|
)
|
2020-06-15 22:53:07 +00:00
|
|
|
.then(function (answers) {
|
2020-04-24 13:08:45 +00:00
|
|
|
answers.version = options.version;
|
|
|
|
|
|
|
|
if (resource.uuid != null) {
|
|
|
|
return generateDeviceConfig(
|
|
|
|
resource,
|
|
|
|
options.deviceApiKey || options['generate-device-api-key'],
|
|
|
|
answers,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
answers.deviceType = deviceType;
|
|
|
|
return generateApplicationConfig(resource, answers);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
2020-06-15 22:53:07 +00:00
|
|
|
.then(function (config) {
|
2020-04-24 13:08:45 +00:00
|
|
|
if (options.output != null) {
|
2020-06-23 10:57:57 +00:00
|
|
|
return require('fs').promises.writeFile(
|
|
|
|
options.output,
|
|
|
|
JSON.stringify(config),
|
|
|
|
);
|
2020-04-24 13:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log(prettyjson.render(config));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|