Attempt to get device type from the image first partition

New images will ship a `device-type.json` file in the first partition,
which we can use instead of querying the API for certain configuration
and initialisation commands.

If the file is not found, or is malformed, we still fallback to the API.

Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
This commit is contained in:
Juan Cruz Viotti 2016-09-14 10:51:50 -07:00
parent fbccf8a465
commit 8b485b5ad5
5 changed files with 61 additions and 23 deletions

View File

@ -110,22 +110,24 @@ limitations under the License.
init = require('resin-device-init'); init = require('resin-device-init');
helpers = require('../utils/helpers'); helpers = require('../utils/helpers');
console.info('Configuring operating system image'); console.info('Configuring operating system image');
return resin.models.device.get(params.uuid).get('device_type').then(resin.models.device.getManifestBySlug).get('options').then(function(questions) { return resin.models.device.get(params.uuid).then(function(device) {
var advancedGroup, override; return helpers.getManifest(params.image, device.device_type).get('options').then(function(questions) {
if (!options.advanced) { var advancedGroup, override;
advancedGroup = _.findWhere(questions, { if (!options.advanced) {
name: 'advanced', advancedGroup = _.findWhere(questions, {
isGroup: true name: 'advanced',
}); isGroup: true
if (advancedGroup != null) { });
override = helpers.getGroupDefaults(advancedGroup); if (advancedGroup != null) {
override = helpers.getGroupDefaults(advancedGroup);
}
} }
} return form.run(questions, {
return form.run(questions, { override: override
override: override });
}).then(function(answers) {
return init.configure(params.image, params.uuid, answers).then(stepHandler);
}); });
}).then(function(answers) {
return init.configure(params.image, params.uuid, answers).then(stepHandler);
}).nodeify(done); }).nodeify(done);
} }
}; };
@ -151,15 +153,15 @@ limitations under the License.
], ],
root: true, root: true,
action: function(params, options, done) { action: function(params, options, done) {
var Promise, form, init, patterns, resin, umount; var Promise, form, helpers, init, patterns, umount;
Promise = require('bluebird'); Promise = require('bluebird');
umount = Promise.promisifyAll(require('umount')); umount = Promise.promisifyAll(require('umount'));
resin = require('resin-sdk');
form = require('resin-cli-form'); form = require('resin-cli-form');
init = require('resin-device-init'); init = require('resin-device-init');
patterns = require('../utils/patterns'); patterns = require('../utils/patterns');
helpers = require('../utils/helpers');
console.info('Initializing device'); console.info('Initializing device');
return resin.models.device.getManifestBySlug(options.type).then(function(manifest) { return helpers.getManifest(params.image, options.type).then(function(manifest) {
var ref; var ref;
return (ref = manifest.initialization) != null ? ref.options : void 0; return (ref = manifest.initialization) != null ? ref.options : void 0;
}).then(function(questions) { }).then(function(questions) {

View File

@ -16,7 +16,7 @@ limitations under the License.
*/ */
(function() { (function() {
var Promise, _, capitano, chalk, os, president; var Promise, _, capitano, chalk, imagefs, os, president, resin, rindle;
Promise = require('bluebird'); Promise = require('bluebird');
@ -28,6 +28,12 @@ limitations under the License.
president = Promise.promisifyAll(require('president')); president = Promise.promisifyAll(require('president'));
resin = require('resin-sdk');
imagefs = require('resin-image-fs');
rindle = require('rindle');
os = require('os'); os = require('os');
chalk = require('chalk'); chalk = require('chalk');
@ -63,4 +69,16 @@ limitations under the License.
return president.executeAsync(command); return president.executeAsync(command);
}; };
exports.getManifest = function(image, deviceType) {
return imagefs.read({
image: image,
partition: {
primary: 1
},
path: '/device-type.json'
}).then(rindle.extractAsync).then(JSON.parse)["catch"](function() {
return resin.models.device.getManifestBySlug(deviceType);
});
};
}).call(this); }).call(this);

View File

@ -113,9 +113,8 @@ exports.configure =
helpers = require('../utils/helpers') helpers = require('../utils/helpers')
console.info('Configuring operating system image') console.info('Configuring operating system image')
resin.models.device.get(params.uuid) resin.models.device.get(params.uuid).then (device) ->
.get('device_type') helpers.getManifest(params.image, device.device_type)
.then(resin.models.device.getManifestBySlug)
.get('options') .get('options')
.then (questions) -> .then (questions) ->
@ -163,13 +162,13 @@ exports.initialize =
action: (params, options, done) -> action: (params, options, done) ->
Promise = require('bluebird') Promise = require('bluebird')
umount = Promise.promisifyAll(require('umount')) umount = Promise.promisifyAll(require('umount'))
resin = require('resin-sdk')
form = require('resin-cli-form') form = require('resin-cli-form')
init = require('resin-device-init') init = require('resin-device-init')
patterns = require('../utils/patterns') patterns = require('../utils/patterns')
helpers = require('../utils/helpers')
console.info('Initializing device') console.info('Initializing device')
resin.models.device.getManifestBySlug(options.type) helpers.getManifest(params.image, options.type)
.then (manifest) -> .then (manifest) ->
return manifest.initialization?.options return manifest.initialization?.options
.then (questions) -> .then (questions) ->

View File

@ -19,6 +19,9 @@ capitano = Promise.promisifyAll(require('capitano'))
_ = require('lodash') _ = require('lodash')
_.str = require('underscore.string') _.str = require('underscore.string')
president = Promise.promisifyAll(require('president')) president = Promise.promisifyAll(require('president'))
resin = require('resin-sdk')
imagefs = require('resin-image-fs')
rindle = require('rindle')
os = require('os') os = require('os')
chalk = require('chalk') chalk = require('chalk')
@ -52,3 +55,18 @@ exports.sudo = (command, message) ->
console.log('Type your computer password to continue') console.log('Type your computer password to continue')
return president.executeAsync(command) return president.executeAsync(command)
exports.getManifest = (image, deviceType) ->
# Attempt to read manifest from the first
# partition, but fallback to the API if
# we encounter any errors along the way.
imagefs.read
image: image
partition:
primary: 1
path: '/device-type.json'
.then(rindle.extractAsync)
.then(JSON.parse)
.catch ->
resin.models.device.getManifestBySlug(deviceType)

View File

@ -50,6 +50,7 @@
"resin-config-json": "^1.0.0", "resin-config-json": "^1.0.0",
"resin-device-config": "^3.0.0", "resin-device-config": "^3.0.0",
"resin-device-init": "^2.1.0", "resin-device-init": "^2.1.0",
"resin-image-fs": "^2.1.2",
"resin-image-manager": "^4.0.0", "resin-image-manager": "^4.0.0",
"resin-pine": "^1.3.0", "resin-pine": "^1.3.0",
"resin-sdk": "^5.3.5", "resin-sdk": "^5.3.5",