From 8b485b5ad54a2d9834e3477d6d5fb85329e2224c Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 14 Sep 2016 10:51:50 -0700 Subject: [PATCH] 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 --- build/actions/os.js | 36 +++++++++++++++++++----------------- build/utils/helpers.js | 20 +++++++++++++++++++- lib/actions/os.coffee | 9 ++++----- lib/utils/helpers.coffee | 18 ++++++++++++++++++ package.json | 1 + 5 files changed, 61 insertions(+), 23 deletions(-) diff --git a/build/actions/os.js b/build/actions/os.js index 820e325a..2461814e 100644 --- a/build/actions/os.js +++ b/build/actions/os.js @@ -110,22 +110,24 @@ limitations under the License. init = require('resin-device-init'); helpers = require('../utils/helpers'); 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) { - var advancedGroup, override; - if (!options.advanced) { - advancedGroup = _.findWhere(questions, { - name: 'advanced', - isGroup: true - }); - if (advancedGroup != null) { - override = helpers.getGroupDefaults(advancedGroup); + return resin.models.device.get(params.uuid).then(function(device) { + return helpers.getManifest(params.image, device.device_type).get('options').then(function(questions) { + var advancedGroup, override; + if (!options.advanced) { + advancedGroup = _.findWhere(questions, { + name: 'advanced', + isGroup: true + }); + if (advancedGroup != null) { + override = helpers.getGroupDefaults(advancedGroup); + } } - } - return form.run(questions, { - override: override + return form.run(questions, { + 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); } }; @@ -151,15 +153,15 @@ limitations under the License. ], root: true, action: function(params, options, done) { - var Promise, form, init, patterns, resin, umount; + var Promise, form, helpers, init, patterns, umount; Promise = require('bluebird'); umount = Promise.promisifyAll(require('umount')); - resin = require('resin-sdk'); form = require('resin-cli-form'); init = require('resin-device-init'); patterns = require('../utils/patterns'); + helpers = require('../utils/helpers'); 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; return (ref = manifest.initialization) != null ? ref.options : void 0; }).then(function(questions) { diff --git a/build/utils/helpers.js b/build/utils/helpers.js index 4ed256d6..9521b2d4 100644 --- a/build/utils/helpers.js +++ b/build/utils/helpers.js @@ -16,7 +16,7 @@ limitations under the License. */ (function() { - var Promise, _, capitano, chalk, os, president; + var Promise, _, capitano, chalk, imagefs, os, president, resin, rindle; Promise = require('bluebird'); @@ -28,6 +28,12 @@ limitations under the License. president = Promise.promisifyAll(require('president')); + resin = require('resin-sdk'); + + imagefs = require('resin-image-fs'); + + rindle = require('rindle'); + os = require('os'); chalk = require('chalk'); @@ -63,4 +69,16 @@ limitations under the License. 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); diff --git a/lib/actions/os.coffee b/lib/actions/os.coffee index e2d6ddfb..fb7fe034 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -113,9 +113,8 @@ exports.configure = helpers = require('../utils/helpers') console.info('Configuring operating system image') - resin.models.device.get(params.uuid) - .get('device_type') - .then(resin.models.device.getManifestBySlug) + resin.models.device.get(params.uuid).then (device) -> + helpers.getManifest(params.image, device.device_type) .get('options') .then (questions) -> @@ -163,13 +162,13 @@ exports.initialize = action: (params, options, done) -> Promise = require('bluebird') umount = Promise.promisifyAll(require('umount')) - resin = require('resin-sdk') form = require('resin-cli-form') init = require('resin-device-init') patterns = require('../utils/patterns') + helpers = require('../utils/helpers') console.info('Initializing device') - resin.models.device.getManifestBySlug(options.type) + helpers.getManifest(params.image, options.type) .then (manifest) -> return manifest.initialization?.options .then (questions) -> diff --git a/lib/utils/helpers.coffee b/lib/utils/helpers.coffee index 315875d0..9c8cad4c 100644 --- a/lib/utils/helpers.coffee +++ b/lib/utils/helpers.coffee @@ -19,6 +19,9 @@ capitano = Promise.promisifyAll(require('capitano')) _ = require('lodash') _.str = require('underscore.string') president = Promise.promisifyAll(require('president')) +resin = require('resin-sdk') +imagefs = require('resin-image-fs') +rindle = require('rindle') os = require('os') chalk = require('chalk') @@ -52,3 +55,18 @@ exports.sudo = (command, message) -> console.log('Type your computer password to continue') 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) diff --git a/package.json b/package.json index 7e9060fb..402ca00e 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "resin-config-json": "^1.0.0", "resin-device-config": "^3.0.0", "resin-device-init": "^2.1.0", + "resin-image-fs": "^2.1.2", "resin-image-manager": "^4.0.0", "resin-pine": "^1.3.0", "resin-sdk": "^5.3.5",