2016-01-04 03:58:51 +00:00
|
|
|
###
|
2018-10-19 14:38:50 +00:00
|
|
|
Copyright 2016-2017 Balena
|
2016-01-04 03:58:51 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
###
|
|
|
|
|
2016-03-21 19:42:54 +00:00
|
|
|
commandOptions = require('./command-options')
|
2018-02-01 15:48:01 +00:00
|
|
|
{ normalizeUuidProp } = require('../utils/normalization')
|
2016-03-21 19:42:54 +00:00
|
|
|
|
2015-11-10 16:53:34 +00:00
|
|
|
exports.read =
|
|
|
|
signature: 'config read'
|
|
|
|
description: 'read a device configuration'
|
|
|
|
help: '''
|
2016-06-13 16:24:19 +00:00
|
|
|
Use this command to read the config.json file from the mounted filesystem (e.g. SD card) of a provisioned device"
|
2015-11-10 16:53:34 +00:00
|
|
|
|
2015-11-10 18:27:01 +00:00
|
|
|
Examples:
|
2015-11-10 16:53:34 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
$ balena config read --type raspberry-pi
|
|
|
|
$ balena config read --type raspberry-pi --drive /dev/disk2
|
2015-11-10 16:53:34 +00:00
|
|
|
'''
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'type'
|
2018-10-19 14:38:50 +00:00
|
|
|
description: 'device type (Check available types with `balena devices supported`)'
|
2015-11-10 16:53:34 +00:00
|
|
|
parameter: 'type'
|
|
|
|
alias: 't'
|
|
|
|
required: 'You have to specify a device type'
|
|
|
|
}
|
|
|
|
{
|
|
|
|
signature: 'drive'
|
|
|
|
description: 'drive'
|
|
|
|
parameter: 'drive'
|
|
|
|
alias: 'd'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
permission: 'user'
|
|
|
|
root: true
|
|
|
|
action: (params, options, done) ->
|
2015-12-07 14:32:24 +00:00
|
|
|
Promise = require('bluebird')
|
2018-10-19 14:38:50 +00:00
|
|
|
config = require('balena-config-json')
|
2015-12-07 14:32:24 +00:00
|
|
|
visuals = require('resin-cli-visuals')
|
2017-03-27 09:14:55 +00:00
|
|
|
umountAsync = Promise.promisify(require('umount').umount)
|
2015-12-07 14:32:24 +00:00
|
|
|
prettyjson = require('prettyjson')
|
|
|
|
|
2015-11-10 16:53:34 +00:00
|
|
|
Promise.try ->
|
|
|
|
return options.drive or visuals.drive('Select the device drive')
|
2017-03-27 09:14:55 +00:00
|
|
|
.tap(umountAsync)
|
2015-11-10 16:53:34 +00:00
|
|
|
.then (drive) ->
|
2015-11-11 14:04:46 +00:00
|
|
|
return config.read(drive, options.type)
|
|
|
|
.tap (configJSON) ->
|
|
|
|
console.info(prettyjson.render(configJSON))
|
2015-11-10 18:27:01 +00:00
|
|
|
.nodeify(done)
|
|
|
|
|
|
|
|
exports.write =
|
|
|
|
signature: 'config write <key> <value>'
|
|
|
|
description: 'write a device configuration'
|
|
|
|
help: '''
|
2016-06-13 16:24:19 +00:00
|
|
|
Use this command to write the config.json file to the mounted filesystem (e.g. SD card) of a provisioned device
|
2015-11-10 18:27:01 +00:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
$ 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 "..."
|
2015-11-10 18:27:01 +00:00
|
|
|
'''
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'type'
|
2018-10-19 14:38:50 +00:00
|
|
|
description: 'device type (Check available types with `balena devices supported`)'
|
2015-11-10 18:27:01 +00:00
|
|
|
parameter: 'type'
|
|
|
|
alias: 't'
|
|
|
|
required: 'You have to specify a device type'
|
|
|
|
}
|
|
|
|
{
|
|
|
|
signature: 'drive'
|
|
|
|
description: 'drive'
|
|
|
|
parameter: 'drive'
|
|
|
|
alias: 'd'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
permission: 'user'
|
|
|
|
root: true
|
|
|
|
action: (params, options, done) ->
|
2015-12-07 14:32:24 +00:00
|
|
|
Promise = require('bluebird')
|
|
|
|
_ = require('lodash')
|
2018-10-19 14:38:50 +00:00
|
|
|
config = require('balena-config-json')
|
2015-12-07 14:32:24 +00:00
|
|
|
visuals = require('resin-cli-visuals')
|
2017-03-27 09:14:55 +00:00
|
|
|
umountAsync = Promise.promisify(require('umount').umount)
|
2015-12-07 14:32:24 +00:00
|
|
|
|
2015-11-10 18:27:01 +00:00
|
|
|
Promise.try ->
|
|
|
|
return options.drive or visuals.drive('Select the device drive')
|
2017-03-27 09:14:55 +00:00
|
|
|
.tap(umountAsync)
|
2015-11-10 18:27:01 +00:00
|
|
|
.then (drive) ->
|
2015-11-11 14:04:46 +00:00
|
|
|
config.read(drive, options.type).then (configJSON) ->
|
2015-11-10 18:27:01 +00:00
|
|
|
console.info("Setting #{params.key} to #{params.value}")
|
2015-11-11 14:04:46 +00:00
|
|
|
_.set(configJSON, params.key, params.value)
|
|
|
|
return configJSON
|
2015-11-10 18:27:01 +00:00
|
|
|
.tap ->
|
2017-03-27 09:14:55 +00:00
|
|
|
return umountAsync(drive)
|
2015-11-11 14:04:46 +00:00
|
|
|
.then (configJSON) ->
|
|
|
|
return config.write(drive, options.type, configJSON)
|
2015-11-10 18:27:01 +00:00
|
|
|
.tap ->
|
|
|
|
console.info('Done')
|
2015-11-10 16:53:34 +00:00
|
|
|
.nodeify(done)
|
2015-11-11 14:38:45 +00:00
|
|
|
|
2016-03-17 20:07:19 +00:00
|
|
|
exports.inject =
|
|
|
|
signature: 'config inject <file>'
|
|
|
|
description: 'inject a device configuration file'
|
|
|
|
help: '''
|
2018-01-26 14:43:00 +00:00
|
|
|
Use this command to inject a config.json file to the mounted filesystem
|
2018-10-19 14:38:50 +00:00
|
|
|
(e.g. SD card or mounted balenaOS image) of a provisioned device"
|
2016-03-17 20:07:19 +00:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
$ balena config inject my/config.json --type raspberry-pi
|
|
|
|
$ balena config inject my/config.json --type raspberry-pi --drive /dev/disk2
|
2016-03-17 20:07:19 +00:00
|
|
|
'''
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'type'
|
2018-10-19 14:38:50 +00:00
|
|
|
description: 'device type (Check available types with `balena devices supported`)'
|
2016-03-17 20:07:19 +00:00
|
|
|
parameter: 'type'
|
|
|
|
alias: 't'
|
|
|
|
required: 'You have to specify a device type'
|
|
|
|
}
|
|
|
|
{
|
|
|
|
signature: 'drive'
|
|
|
|
description: 'drive'
|
|
|
|
parameter: 'drive'
|
|
|
|
alias: 'd'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
permission: 'user'
|
|
|
|
root: true
|
|
|
|
action: (params, options, done) ->
|
|
|
|
Promise = require('bluebird')
|
2018-10-19 14:38:50 +00:00
|
|
|
config = require('balena-config-json')
|
2016-03-17 20:07:19 +00:00
|
|
|
visuals = require('resin-cli-visuals')
|
2017-03-27 09:14:55 +00:00
|
|
|
umountAsync = Promise.promisify(require('umount').umount)
|
|
|
|
readFileAsync = Promise.promisify(require('fs').readFile)
|
2016-03-17 20:07:19 +00:00
|
|
|
|
|
|
|
Promise.try ->
|
|
|
|
return options.drive or visuals.drive('Select the device drive')
|
2017-03-27 09:14:55 +00:00
|
|
|
.tap(umountAsync)
|
2016-03-17 20:07:19 +00:00
|
|
|
.then (drive) ->
|
2017-03-27 09:14:55 +00:00
|
|
|
readFileAsync(params.file, 'utf8').then(JSON.parse).then (configJSON) ->
|
2016-03-17 20:07:19 +00:00
|
|
|
return config.write(drive, options.type, configJSON)
|
|
|
|
.tap ->
|
|
|
|
console.info('Done')
|
|
|
|
.nodeify(done)
|
|
|
|
|
2015-11-11 14:38:45 +00:00
|
|
|
exports.reconfigure =
|
|
|
|
signature: 'config reconfigure'
|
|
|
|
description: 'reconfigure a provisioned device'
|
|
|
|
help: '''
|
|
|
|
Use this command to reconfigure a provisioned device
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
$ balena config reconfigure --type raspberry-pi
|
|
|
|
$ balena config reconfigure --type raspberry-pi --advanced
|
|
|
|
$ balena config reconfigure --type raspberry-pi --drive /dev/disk2
|
2015-11-11 14:38:45 +00:00
|
|
|
'''
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
signature: 'type'
|
2018-10-19 14:38:50 +00:00
|
|
|
description: 'device type (Check available types with `balena devices supported`)'
|
2015-11-11 14:38:45 +00:00
|
|
|
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
|
|
|
|
action: (params, options, done) ->
|
2015-12-07 14:32:24 +00:00
|
|
|
Promise = require('bluebird')
|
2018-10-19 14:38:50 +00:00
|
|
|
config = require('balena-config-json')
|
2015-12-07 14:32:24 +00:00
|
|
|
visuals = require('resin-cli-visuals')
|
2018-05-22 15:12:51 +00:00
|
|
|
{ runCommand } = require('../utils/helpers')
|
2017-03-27 09:14:55 +00:00
|
|
|
umountAsync = Promise.promisify(require('umount').umount)
|
2015-12-07 14:32:24 +00:00
|
|
|
|
2015-11-11 14:38:45 +00:00
|
|
|
Promise.try ->
|
|
|
|
return options.drive or visuals.drive('Select the device drive')
|
2017-03-27 09:14:55 +00:00
|
|
|
.tap(umountAsync)
|
2015-11-11 14:38:45 +00:00
|
|
|
.then (drive) ->
|
|
|
|
config.read(drive, options.type).get('uuid')
|
|
|
|
.tap ->
|
2017-03-27 09:14:55 +00:00
|
|
|
umountAsync(drive)
|
2015-11-11 14:38:45 +00:00
|
|
|
.then (uuid) ->
|
2018-01-16 16:54:46 +00:00
|
|
|
configureCommand = "os configure #{drive} --device #{uuid}"
|
2015-11-11 14:38:45 +00:00
|
|
|
if options.advanced
|
|
|
|
configureCommand += ' --advanced'
|
2018-05-22 15:12:51 +00:00
|
|
|
return runCommand(configureCommand)
|
2015-11-11 14:38:45 +00:00
|
|
|
.then ->
|
|
|
|
console.info('Done')
|
|
|
|
.nodeify(done)
|
2016-02-27 02:37:15 +00:00
|
|
|
|
|
|
|
exports.generate =
|
2016-03-21 19:42:54 +00:00
|
|
|
signature: 'config generate'
|
2016-02-27 02:37:15 +00:00
|
|
|
description: 'generate a config.json file'
|
|
|
|
help: '''
|
2017-11-14 13:14:53 +00:00
|
|
|
Use this command to generate a config.json for a device or application.
|
|
|
|
|
2018-10-15 17:03:21 +00:00
|
|
|
Calling this command with the exact version number of the targeted image is required.
|
2018-07-11 18:56:13 +00:00
|
|
|
|
2017-11-14 13:14:53 +00:00
|
|
|
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.
|
2016-02-27 02:37:15 +00:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2018-11-05 08:18:18 +00:00
|
|
|
$ balena config generate --device 7cf02a6
|
|
|
|
$ balena config generate --device 7cf02a6 --generate-device-api-key
|
|
|
|
$ balena config generate --device 7cf02a6 --device-api-key <existingDeviceKey>
|
|
|
|
$ balena config generate --device 7cf02a6 --output config.json
|
|
|
|
$ balena config generate --app MyApp
|
|
|
|
$ balena config generate --app MyApp --output config.json
|
|
|
|
$ balena config generate --app MyApp \
|
2018-07-11 18:56:13 +00:00
|
|
|
--network wifi --wifiSsid mySsid --wifiKey abcdefgh --appUpdatePollInterval 1
|
2016-02-27 02:37:15 +00:00
|
|
|
'''
|
|
|
|
options: [
|
2018-11-05 08:18:18 +00:00
|
|
|
commandOptions.optionalOsVersion
|
2016-03-21 19:42:54 +00:00
|
|
|
commandOptions.optionalApplication
|
|
|
|
commandOptions.optionalDevice
|
2017-11-16 18:09:20 +00:00
|
|
|
commandOptions.optionalDeviceApiKey
|
2018-07-10 17:45:48 +00:00
|
|
|
{
|
|
|
|
signature: 'generate-device-api-key'
|
|
|
|
description: 'generate a fresh device key for the device'
|
|
|
|
boolean: true
|
|
|
|
}
|
2016-02-27 02:37:15 +00:00
|
|
|
{
|
|
|
|
signature: 'output'
|
|
|
|
description: 'output'
|
|
|
|
parameter: 'output'
|
|
|
|
alias: 'o'
|
|
|
|
}
|
2017-11-14 13:14:53 +00:00
|
|
|
# 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'
|
|
|
|
}
|
2016-02-27 02:37:15 +00:00
|
|
|
]
|
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2018-02-01 15:48:01 +00:00
|
|
|
normalizeUuidProp(options, 'device')
|
2016-02-27 02:37:15 +00:00
|
|
|
Promise = require('bluebird')
|
2017-03-27 09:14:55 +00:00
|
|
|
writeFileAsync = Promise.promisify(require('fs').writeFile)
|
2018-10-19 14:38:50 +00:00
|
|
|
balena = require('balena-sdk').fromSharedOptions()
|
2016-02-27 02:37:15 +00:00
|
|
|
form = require('resin-cli-form')
|
|
|
|
prettyjson = require('prettyjson')
|
2018-04-16 11:59:11 +00:00
|
|
|
|
2017-10-13 18:23:19 +00:00
|
|
|
{ generateDeviceConfig, generateApplicationConfig } = require('../utils/config')
|
2018-04-17 13:17:48 +00:00
|
|
|
{ exitWithExpectedError } = require('../utils/patterns')
|
2016-02-27 02:37:15 +00:00
|
|
|
|
2016-03-21 19:42:54 +00:00
|
|
|
if not options.device? and not options.application?
|
2018-04-17 13:17:48 +00:00
|
|
|
exitWithExpectedError '''
|
2016-03-21 19:42:54 +00:00
|
|
|
You have to pass either a device or an application.
|
|
|
|
|
|
|
|
See the help page for examples:
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
$ balena help config generate
|
2016-03-21 19:42:54 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
Promise.try ->
|
|
|
|
if options.device?
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.device.get(options.device)
|
|
|
|
return balena.models.application.get(options.application)
|
2016-03-21 19:42:54 +00:00
|
|
|
.then (resource) ->
|
2018-10-19 14:38:50 +00:00
|
|
|
balena.models.device.getManifestBySlug(resource.device_type)
|
2017-10-11 17:52:03 +00:00
|
|
|
.get('options')
|
2017-11-14 13:14:53 +00:00
|
|
|
.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)
|
2017-10-11 17:52:03 +00:00
|
|
|
.then (answers) ->
|
2018-07-11 18:56:13 +00:00
|
|
|
answers.version = options.version
|
|
|
|
|
2017-10-11 17:52:03 +00:00
|
|
|
if resource.uuid?
|
2018-07-10 17:45:48 +00:00
|
|
|
generateDeviceConfig(resource, options.deviceApiKey || options['generate-device-api-key'], answers)
|
2017-10-11 17:52:03 +00:00
|
|
|
else
|
|
|
|
generateApplicationConfig(resource, answers)
|
2016-02-27 02:37:15 +00:00
|
|
|
.then (config) ->
|
|
|
|
if options.output?
|
2017-03-27 09:14:55 +00:00
|
|
|
return writeFileAsync(options.output, JSON.stringify(config))
|
2016-02-27 02:37:15 +00:00
|
|
|
|
|
|
|
console.log(prettyjson.render(config))
|
|
|
|
.nodeify(done)
|