Skip to content

Commit

Permalink
Make specifying the version during configuration optional
Browse files Browse the repository at this point in the history
`version` used to be optional but it seems we recently had to make it a required parameter. However it really feels redundant when all it’s used for is to determine whether the command should issue a legacy user API key or a provisioning key.

This makes version optional but tries to figure it out by itself by reading os-release from the image's boot partition. This is not foul-proof however, and while it'll work with most recent images it won't work with all and in that case it'll bail out and only then warn the user to specify it via the --version argument.

Change-type: minor
  • Loading branch information
dfunckt committed Nov 16, 2018
1 parent 0273d2e commit feaa8d6
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 184 deletions.
25 changes: 14 additions & 11 deletions doc/cli.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,9 @@ the path to the output JSON file
Use this command to configure a previously downloaded operating system image for
the specific device or for an application generally.

Calling this command with the exact version number of the targeted image is required.
This command will try to automatically determine the operating system version in order
to correctly configure the image. It may fail to do so however, in which case you'll
have to call this command again with the exact version number of the targeted image.

Note that device api keys are only supported on balenaOS 2.0.3+.

Expand All @@ -983,9 +985,10 @@ are passed directly on the command line, but the recommended way is to pass eith

Examples:

$ balena os configure ../path/rpi.img --device 7cf02a6 --version 2.12.7
$ balena os configure ../path/rpi.img --device 7cf02a6 --version 2.12.7 --device-api-key <existingDeviceKey>
$ balena os configure ../path/rpi.img --app MyApp --version 2.12.7
$ balena os configure ../path/rpi.img --device 7cf02a6
$ balena os configure ../path/rpi.img --device 7cf02a6 --device-api-key <existingDeviceKey>
$ balena os configure ../path/rpi.img --app MyApp
$ balena os configure ../path/rpi.img --app MyApp --version 2.12.7

### Options

Expand Down Expand Up @@ -1135,13 +1138,13 @@ that will be asked for the relevant 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 --output config.json
$ balena config generate --app MyApp --version 2.12.7 --network wifi --wifiSsid mySsid --wifiKey abcdefgh --appUpdatePollInterval 1
$ 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 --network wifi --wifiSsid mySsid --wifiKey abcdefgh --appUpdatePollInterval 1

### Options

Expand Down
115 changes: 0 additions & 115 deletions lib/actions/command-options.coffee

This file was deleted.

16 changes: 8 additions & 8 deletions lib/actions/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,17 @@ exports.generate =
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 --output config.json
$ balena config generate --app MyApp --version 2.12.7 \
$ 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 \
--network wifi --wifiSsid mySsid --wifiKey abcdefgh --appUpdatePollInterval 1
'''
options: [
commandOptions.osVersion
commandOptions.optionalOsVersion
commandOptions.optionalApplication
commandOptions.optionalDevice
commandOptions.optionalDeviceApiKey
Expand Down
9 changes: 5 additions & 4 deletions lib/actions/internal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ exports.osInit =
root: true
action: (params, options, done) ->
Promise = require('bluebird')
init = require('resin-device-init')
init = require('balena-device-init')
helpers = require('../utils/helpers')

return Promise.try ->
config = JSON.parse(params.config)
init.initialize(params.image, params.type, config)
configPromise = Promise.try -> JSON.parse(params.config)
manifestPromise = helpers.getManifest(params.image, params.type)
Promise.join configPromise, manifestPromise, (config, manifest) ->
init.initialize(params.image, manifest, config)
.then(helpers.osProgressHandler)
.nodeify(done)

Expand Down
45 changes: 30 additions & 15 deletions lib/actions/os.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ buildConfig = (image, deviceType, advanced = false) ->
form = require('resin-cli-form')
helpers = require('../utils/helpers')

helpers.getManifest(image, deviceType)
Promise.resolve(helpers.getManifest(image, deviceType))
.get('options')
.then (questions) ->
if not advanced
Expand Down Expand Up @@ -203,7 +203,9 @@ exports.configure =
Use this command to configure a previously downloaded operating system image for
the specific device or for an application generally.
Calling this command with the exact version number of the targeted image is required.
This command will try to automatically determine the operating system version in order
to correctly configure the image. It may fail to do so however, in which case you'll
have to call this command again with the exact version number of the targeted image.
Note that device api keys are only supported on balenaOS 2.0.3+.
Expand All @@ -213,17 +215,18 @@ exports.configure =
Examples:
$ balena os configure ../path/rpi.img --device 7cf02a6 --version 2.12.7
$ balena os configure ../path/rpi.img --device 7cf02a6 --version 2.12.7 --device-api-key <existingDeviceKey>
$ balena os configure ../path/rpi.img --app MyApp --version 2.12.7
$ balena os configure ../path/rpi.img --device 7cf02a6
$ balena os configure ../path/rpi.img --device 7cf02a6 --device-api-key <existingDeviceKey>
$ balena os configure ../path/rpi.img --app MyApp
$ balena os configure ../path/rpi.img --app MyApp --version 2.12.7
'''
permission: 'user'
options: [
commandOptions.advancedConfig
commandOptions.optionalApplication
commandOptions.optionalDevice
commandOptions.optionalDeviceApiKey
commandOptions.osVersion
commandOptions.optionalOsVersion
{
signature: 'config'
description: 'path to the config JSON file, see `balena os build-config`'
Expand All @@ -236,7 +239,7 @@ exports.configure =
Promise = require('bluebird')
readFileAsync = Promise.promisify(fs.readFile)
balena = require('balena-sdk').fromSharedOptions()
init = require('resin-device-init')
init = require('balena-device-init')
helpers = require('../utils/helpers')
patterns = require('../utils/patterns')
{ generateDeviceConfig, generateApplicationConfig } = require('../utils/config')
Expand Down Expand Up @@ -265,20 +268,32 @@ exports.configure =

balena.models[configurationResourceType].get(uuid || options.application)
.then (appOrDevice) ->
Promise.try ->
manifestPromise = helpers.getManifest(params.image, appOrDevice.device_type)
answersPromise = Promise.try ->
if options.config
return readFileAsync(options.config, 'utf8')
.then(JSON.parse)
return buildConfig(params.image, appOrDevice.device_type, options.advanced)
.then (answers) ->
Promise.join answersPromise, manifestPromise, (answers, manifest) ->
answers.version = options.version

(if configurationResourceType == 'device'
generateDeviceConfig(appOrDevice, deviceApiKey, answers)
else
generateApplicationConfig(appOrDevice, answers)
).then (config) ->
init.configure(params.image, appOrDevice.device_type, config, answers)
if not answers.version?
answers.version = helpers.getOsVersion(params.image, manifest).tap (version) ->
if not version?
throw new Error(
'Could not read OS version from the image. ' +
'Please specify the version manually with the ' +
'--version argument to this command.'
)

Promise.props(answers).then (answers) ->
(if configurationResourceType == 'device'
generateDeviceConfig(appOrDevice, deviceApiKey, answers)
else
generateApplicationConfig(appOrDevice, answers)
)
.then (config) ->
init.configure(params.image, manifest, config, answers)
.then(helpers.osProgressHandler)
.nodeify(done)

Expand Down
15 changes: 7 additions & 8 deletions lib/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ export function generateApplicationConfig(
options: { version: string },
) {
return generateBaseConfig(application, options).tap(config => {
if (semver.satisfies(options.version, '>=2.7.8')) {
return addProvisioningKey(config, application.id);
} else {
if (semver.satisfies(options.version, '<2.7.8')) {
return addApplicationKey(config, application.id);
}
return addProvisioningKey(config, application.id);
});
}

Expand All @@ -91,13 +90,13 @@ export function generateDeviceConfig(
.get(device.belongs_to__application.__id)
.then(application => {
return generateBaseConfig(application, options).tap(config => {
if (deviceApiKey) {
return addDeviceKey(config, device.uuid, deviceApiKey);
} else if (semver.satisfies(options.version, '>=2.0.3')) {
return addDeviceKey(config, device.uuid, true);
} else {
if (
deviceApiKey == null &&
semver.satisfies(options.version, '<2.0.3')
) {
return addApplicationKey(config, application.id);
}
return addDeviceKey(config, device.uuid, deviceApiKey || true);
});
})
.then(config => {
Expand Down
Loading

0 comments on commit feaa8d6

Please sign in to comment.