Associate a device before first boot

This commit is contained in:
Juan Cruz Viotti 2015-06-04 08:55:32 -04:00
parent fdd0e4a966
commit 2bfeb7f42c
3 changed files with 97 additions and 42 deletions

View File

@ -1,5 +1,5 @@
(function() { (function() {
var _, async, capitano, commandOptions, fse, image, inject, manager, path, resin, tmp, vcs, visuals; var _, async, capitano, commandOptions, fse, image, inject, manager, path, pine, registerDevice, resin, tmp, vcs, visuals;
fse = require('fs-extra'); fse = require('fs-extra');
@ -23,6 +23,10 @@
inject = require('resin-config-inject'); inject = require('resin-config-inject');
registerDevice = require('resin-register-device');
pine = require('resin-pine');
tmp = require('tmp'); tmp = require('tmp');
tmp.setGracefulCleanup(); tmp.setGracefulCleanup();
@ -211,29 +215,44 @@
console.info("Checking application: " + options.application); console.info("Checking application: " + options.application);
return resin.models.application.get(options.application, callback); return resin.models.application.get(options.application, callback);
}, function(application, callback) { }, function(application, callback) {
console.info('Getting device manifest for the application'); return async.parallel({
return resin.models.device.getManifestBySlug(application.device_type, callback); manifest: function(callback) {
}, function(manifest, callback) { console.info('Getting device manifest for the application');
var bar, spinner; return resin.models.device.getManifestBySlug(application.device_type, callback);
params.manifest = manifest; },
bar = new visuals.widgets.Progress('Downloading Device OS'); config: function(callback) {
spinner = new visuals.widgets.Spinner('Downloading Device OS (size unknown)'); console.info('Fetching application configuration');
console.info('Fetching application configuration'); return resin.models.application.getConfiguration(options.application, networkOptions, callback);
return resin.models.application.getConfiguration(options.application, networkOptions, function(error, config) { }
}, callback);
}, function(results, callback) {
console.info('Associating the device');
return registerDevice.register(pine, results.config, function(error, device) {
if (error != null) { if (error != null) {
return callback(error); return callback(error);
} }
console.info('Configuring device operating system image'); results.config.deviceId = device.id;
return manager.configure(params.manifest, config, function(error, imagePath, removeCallback) { results.config.uuid = device.uuid;
spinner.stop(); params.uuid = results.config.uuid;
return callback(error, imagePath, removeCallback); return callback(null, results);
}, function(state) { });
if (state != null) { }, function(results, callback) {
return bar.update(state); var bar, spinner;
} else { console.info('Configuring device operating system image');
return spinner.start(); if (process.env.DEBUG) {
} console.log(results.config);
}); }
bar = new visuals.widgets.Progress('Downloading Device OS');
spinner = new visuals.widgets.Spinner('Downloading Device OS (size unknown)');
return manager.configure(results.manifest, results.config, function(error, imagePath, removeCallback) {
spinner.stop();
return callback(error, imagePath, removeCallback);
}, function(state) {
if (state != null) {
return bar.update(state);
} else {
return spinner.start();
}
}); });
}, function(configuredImagePath, removeCallback, callback) { }, function(configuredImagePath, removeCallback, callback) {
var bar; var bar;
@ -252,6 +271,11 @@
}, function(temporalImagePath, removeCallback, callback) { }, function(temporalImagePath, removeCallback, callback) {
console.info('Image written successfully'); console.info('Image written successfully');
return removeCallback(callback); return removeCallback(callback);
}, function(callback) {
return resin.models.device.getByUUID(params.uuid, callback);
}, function(device, callback) {
console.info("Device created: " + device.name);
return callback(null, device.name);
} }
], done); ], done);
} }

View File

@ -9,6 +9,8 @@ vcs = require('resin-vcs')
manager = require('resin-image-manager') manager = require('resin-image-manager')
image = require('resin-image') image = require('resin-image')
inject = require('resin-config-inject') inject = require('resin-config-inject')
registerDevice = require('resin-register-device')
pine = require('resin-pine')
tmp = require('tmp') tmp = require('tmp')
# Cleanup the temporary files even when an uncaught exception occurs # Cleanup the temporary files even when an uncaught exception occurs
@ -284,29 +286,49 @@ exports.init =
resin.models.application.get(options.application, callback) resin.models.application.get(options.application, callback)
(application, callback) -> (application, callback) ->
console.info('Getting device manifest for the application') async.parallel
resin.models.device.getManifestBySlug(application.device_type, callback)
manifest: (callback) ->
console.info('Getting device manifest for the application')
resin.models.device.getManifestBySlug(application.device_type, callback)
config: (callback) ->
console.info('Fetching application configuration')
resin.models.application.getConfiguration(options.application, networkOptions, callback)
, callback
(results, callback) ->
console.info('Associating the device')
registerDevice.register pine, results.config, (error, device) ->
return callback(error) if error?
# Associate a device
results.config.deviceId = device.id
results.config.uuid = device.uuid
params.uuid = results.config.uuid
return callback(null, results)
(results, callback) ->
console.info('Configuring device operating system image')
if process.env.DEBUG
console.log(results.config)
(manifest, callback) ->
params.manifest = manifest
bar = new visuals.widgets.Progress('Downloading Device OS') bar = new visuals.widgets.Progress('Downloading Device OS')
spinner = new visuals.widgets.Spinner('Downloading Device OS (size unknown)') spinner = new visuals.widgets.Spinner('Downloading Device OS (size unknown)')
console.info('Fetching application configuration') manager.configure results.manifest, results.config, (error, imagePath, removeCallback) ->
spinner.stop()
resin.models.application.getConfiguration options.application, networkOptions, (error, config) -> return callback(error, imagePath, removeCallback)
return callback(error) if error? , (state) ->
if state?
console.info('Configuring device operating system image') bar.update(state)
else
manager.configure params.manifest, config, (error, imagePath, removeCallback) -> spinner.start()
spinner.stop()
return callback(error, imagePath, removeCallback)
, (state) ->
if state?
bar.update(state)
else
spinner.start()
(configuredImagePath, removeCallback, callback) -> (configuredImagePath, removeCallback, callback) ->
console.info('Attempting to write operating system image to drive') console.info('Attempting to write operating system image to drive')
@ -324,4 +346,11 @@ exports.init =
console.info('Image written successfully') console.info('Image written successfully')
removeCallback(callback) removeCallback(callback)
(callback) ->
resin.models.device.getByUUID(params.uuid, callback)
(device, callback) ->
console.info("Device created: #{device.name}")
return callback(null, device.name)
], done) ], done)

View File

@ -59,12 +59,14 @@
"nplugm": "^2.2.0", "nplugm": "^2.2.0",
"npm": "^2.6.1", "npm": "^2.6.1",
"open": "0.0.5", "open": "0.0.5",
"resin-image": "^1.1.3",
"resin-settings-client": "^1.1.0",
"resin-cli-visuals": "^0.1.1", "resin-cli-visuals": "^0.1.1",
"resin-config-inject": "^2.0.0", "resin-config-inject": "^2.0.0",
"resin-image": "^1.1.3",
"resin-image-manager": "^1.1.0", "resin-image-manager": "^1.1.0",
"resin-pine": "^1.1.1",
"resin-register-device": "^1.0.1",
"resin-sdk": "^1.7.4", "resin-sdk": "^1.7.4",
"resin-settings-client": "^1.1.0",
"resin-vcs": "^1.2.0", "resin-vcs": "^1.2.0",
"selfupdate": "^1.1.0", "selfupdate": "^1.1.0",
"tmp": "^0.0.25", "tmp": "^0.0.25",