Skip to content

Commit

Permalink
Merge pull request #37 from balena-io-modules/version-from-img
Browse files Browse the repository at this point in the history
Load OS version from boot partition for greater compatibility
  • Loading branch information
dfunckt authored Nov 14, 2018
2 parents f0cf97e + 3f0f840 commit 913f851
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 142 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ Documentation


* [init](#module_init)
* [.configure(image, device, config, [options])](#module_init.configure) ⇒ <code>Promise.&lt;EventEmitter&gt;</code>
* [.initialize(image, deviceType, options)](#module_init.initialize) ⇒ <code>Promise.&lt;EventEmitter&gt;</code>
* [.configure(image, manifest, config, [options])](#module_init.configure) ⇒ <code>Promise.&lt;EventEmitter&gt;</code>
* [.initialize(image, manifest, options)](#module_init.initialize) ⇒ <code>Promise.&lt;EventEmitter&gt;</code>

<a name="module_init.configure"></a>

### init.configure(image, device, config, [options]) ⇒ <code>Promise.&lt;EventEmitter&gt;</code>
This function injects `config.json` and network settings into the device.
### init.configure(image, manifest, config, [options]) ⇒ <code>Promise.&lt;EventEmitter&gt;</code>
This function injects `config.json` and network settings into the image.

**Kind**: static method of <code>[init](#module_init)</code>
**Summary**: Configure an image with an application
Expand All @@ -44,13 +44,13 @@ This function injects `config.json` and network settings into the device.
| Param | Type | Description |
| --- | --- | --- |
| image | <code>String</code> | path to image |
| device | <code>String</code> | type - device type slug |
| manifest | <code>Object</code> | device type manifest |
| config | <code>Object</code> | a fully populated config object |
| [options] | <code>Object</code> | configuration options |

**Example**
```js
init.configure('my/rpi.img', 'raspberrypi', config).then (configuration) ->
init.configure('my/rpi.img', manifest, config).then (configuration) ->

configuration.on('stdout', process.stdout.write)
configuration.on('stderr', process.stderr.write)
Expand All @@ -67,7 +67,7 @@ init.configure('my/rpi.img', 'raspberrypi', config).then (configuration) ->
```
<a name="module_init.initialize"></a>

### init.initialize(image, deviceType, options) ⇒ <code>Promise.&lt;EventEmitter&gt;</code>
### init.initialize(image, manifest, options) ⇒ <code>Promise.&lt;EventEmitter&gt;</code>
**Kind**: static method of <code>[init](#module_init)</code>
**Summary**: Initialize an image
**Returns**: <code>Promise.&lt;EventEmitter&gt;</code> - initialization event emitter
Expand All @@ -76,12 +76,12 @@ init.configure('my/rpi.img', 'raspberrypi', config).then (configuration) ->
| Param | Type | Description |
| --- | --- | --- |
| image | <code>String</code> | path to image |
| deviceType | <code>String</code> | device type slug |
| manifest | <code>Object</code> | device type manifest |
| options | <code>Object</code> | configuration options |

**Example**
```js
init.initialize('my/rpi.img', 'raspberry-pi', network: 'ethernet').then (configuration) ->
init.initialize('my/rpi.img', manifest, network: 'ethernet').then (configuration) ->

configuration.on('stdout', process.stdout.write)
configuration.on('stderr', process.stderr.write)
Expand Down
62 changes: 31 additions & 31 deletions build/init.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 19 additions & 22 deletions build/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 32 additions & 33 deletions lib/init.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,26 @@ resinSemver = require('resin-semver')
utils = require('./utils')
network = require('./network')

exports.getImageManifest = utils.getImageManifest
exports.getImageOsVersion = utils.getImageOsVersion

###*
# @summary Configure an image with an application
# @function
# @public
#
# @description
# This function injects `config.json` and network settings into the device.
# This function injects `config.json` and network settings into the image.
#
# @param {String} image - path to image
# @param {String} device type - device type slug
# @param {Object} manifest - device type manifest
# @param {Object} config - a fully populated config object
# @param {Object} [options] - configuration options
#
# @returns {Promise<EventEmitter>} configuration event emitter
#
# @example
# init.configure('my/rpi.img', 'raspberrypi', config).then (configuration) ->
# init.configure('my/rpi.img', manifest, config).then (configuration) ->
#
# configuration.on('stdout', process.stdout.write)
# configuration.on('stderr', process.stderr.write)
Expand All @@ -56,46 +59,44 @@ network = require('./network')
# configuration.on 'end', ->
# console.log('Configuration finished')
###
exports.configure = (image, deviceType, config, options = {}) ->
utils.getManifestByDeviceType(image, deviceType)
.then (manifest) ->
Promise.try ->
# We only know how to find /etc/os-release on specific types of OS image. In future, we'd like to be able
# to do this for any image, but for now we'll just treat others as unknowable (which means below we'll
# configure the network to work for _either_ OS version.
if manifest.yocto?.image == 'resin-image' and _.includes(['resinos-img', 'resin-sdcard'], manifest.yocto?.fstype)
utils.getImageOsVersion(image)
.then (osVersion) ->
configuration = manifest.configuration
exports.configure = (image, manifest, config, options = {}) ->
Promise.try ->
# We only know how to find /etc/os-release on specific types of OS image. In future, we'd like to be able
# to do this for any image, but for now we'll just treat others as unknowable (which means below we'll
# configure the network to work for _either_ OS version.
if manifest.yocto?.image == 'resin-image' and _.includes(['resinos-img', 'resin-sdcard'], manifest.yocto?.fstype)
utils.getImageOsVersion(image, manifest)
.then (osVersion) ->
configuration = manifest.configuration

majorVersion = resinSemver.major(osVersion)
majorVersion = resinSemver.major(osVersion)

configPathDefinition = utils.convertFilePathDefinition(configuration.config)
utils.writeConfigJSON(image, config, configPathDefinition)
.then ->
# Configure for OS2 if it is OS2, or if we're just not sure
if not majorVersion? || majorVersion == 2
network.configureOS2Network(image, manifest, options)
.then ->
# Configure for OS1 if it is OS1, or if we're just not sure
if not majorVersion? || majorVersion == 1
network.configureOS1Network(image, manifest, options)
.then ->
return operations.execute(image, configuration.operations, options)
configPathDefinition = utils.convertFilePathDefinition(configuration.config)
utils.writeConfigJSON(image, config, configPathDefinition)
.then ->
# Configure for OS2 if it is OS2, or if we're just not sure
if not majorVersion? || majorVersion == 2
network.configureOS2Network(image, manifest, options)
.then ->
# Configure for OS1 if it is OS1, or if we're just not sure
if not majorVersion? || majorVersion == 1
network.configureOS1Network(image, manifest, options)
.then ->
return operations.execute(image, configuration.operations, options)

###*
# @summary Initialize an image
# @function
# @public
#
# @param {String} image - path to image
# @param {String} deviceType - device type slug
# @param {Object} manifest - device type manifest
# @param {Object} options - configuration options
#
# @returns {Promise<EventEmitter>} initialization event emitter
#
# @example
# init.initialize('my/rpi.img', 'raspberry-pi', network: 'ethernet').then (configuration) ->
# init.initialize('my/rpi.img', manifest, network: 'ethernet').then (configuration) ->
#
# configuration.on('stdout', process.stdout.write)
# configuration.on('stderr', process.stderr.write)
Expand All @@ -113,7 +114,5 @@ exports.configure = (image, deviceType, config, options = {}) ->
# configuration.on 'end', ->
# console.log('Configuration finished')
###
exports.initialize = (image, deviceType, options) ->
utils.getManifestByDeviceType(image, deviceType)
.then (manifest) ->
return operations.execute(image, manifest.initialization.operations, options)
exports.initialize = (image, manifest, options) ->
return operations.execute(image, manifest.initialization.operations, options)
Loading

0 comments on commit 913f851

Please sign in to comment.