From 7e6eb4b9e46a7e6f8b6a721c9affd3cf8081e896 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 29 Sep 2015 14:33:31 -0400 Subject: [PATCH] Implement device register command This command registers a new device with the passed application, returning the new device uuid. --- build/actions/device.js | 20 +++++++-- build/app.js | 2 + doc/cli.markdown | 9 +++++ lib/actions/device.coffee | 85 ++++++++++++++++++++++++--------------- lib/app.coffee | 1 + 5 files changed, 80 insertions(+), 37 deletions(-) diff --git a/build/actions/device.js b/build/actions/device.js index 97eae2a4..0f2c11ac 100644 --- a/build/actions/device.js +++ b/build/actions/device.js @@ -71,6 +71,21 @@ } }; + exports.register = { + signature: 'device register ', + description: 'register a device', + help: 'Use this command to register a device to an application.\n\nExamples:\n\n $ resin device register MyApp', + permission: 'user', + action: function(params, options, done) { + return resin.models.application.get(params.application).then(function(application) { + var uuid; + uuid = resin.models.device.generateUUID(); + console.info("Registering to " + application.app_name + ": " + uuid); + return resin.models.device.register(application.app_name, uuid); + }).get('uuid').nodeify(done); + } + }; + exports.remove = { signature: 'device rm ', description: 'remove a device', @@ -155,10 +170,7 @@ return tmp.tmpNameAsync().then(function(temporalPath) { return capitano.runAsync("os download --output " + temporalPath); }).then(function(temporalPath) { - var uuid; - uuid = resin.models.device.generateUUID(); - console.log("Registering to " + application.app_name + ": " + uuid); - return resin.models.device.register(application.app_name, uuid).tap(function(device) { + return capitano.runAsync("device register " + application.app_name).then(resin.models.device.get).tap(function(device) { console.log('Configuring operating system'); return capitano.runAsync("os configure " + temporalPath + " " + uuid).then(function() { console.log('Initializing device'); diff --git a/build/app.js b/build/app.js index 5670e39d..eab6c418 100644 --- a/build/app.js +++ b/build/app.js @@ -72,6 +72,8 @@ capitano.command(actions.device.identify); + capitano.command(actions.device.register); + capitano.command(actions.notes.set); capitano.command(actions.keys.list); diff --git a/doc/cli.markdown b/doc/cli.markdown index 50c39ced..8304bb7e 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -34,6 +34,7 @@ Now you have access to all the commands referenced below. - [devices](#devices) - [device <uuid>](#device-60-uuid-62-) + - [device register <application>](#device-register-60-application-62-) - [device rm <uuid>](#device-rm-60-uuid-62-) - [device identify <uuid>](#device-identify-60-uuid-62-) - [device rename <uuid> [newName]](#device-rename-60-uuid-62-newname-) @@ -242,6 +243,14 @@ Examples: $ resin device 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 +## device register <application> + +Use this command to register a device to an application. + +Examples: + + $ resin device register MyApp + ## device rm <uuid> Use this command to remove a device from resin.io. diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index 43cd5ae4..ea56d4d0 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -89,6 +89,25 @@ exports.info = events.send('device.open', device: device.uuid) .nodeify(done) +exports.register = + signature: 'device register ' + description: 'register a device' + help: ''' + Use this command to register a device to an application. + + Examples: + + $ resin device register MyApp + ''' + permission: 'user' + action: (params, options, done) -> + resin.models.application.get(params.application).then (application) -> + uuid = resin.models.device.generateUUID() + console.info("Registering to #{application.app_name}: #{uuid}") + return resin.models.device.register(application.app_name, uuid) + .get('uuid') + .nodeify(done) + exports.remove = signature: 'device rm ' description: 'remove a device' @@ -201,41 +220,41 @@ exports.init = tmp.tmpNameAsync().then (temporalPath) -> return capitano.runAsync("os download --output #{temporalPath}") .then (temporalPath) -> - uuid = resin.models.device.generateUUID() - console.log("Registering to #{application.app_name}: #{uuid}") - resin.models.device.register(application.app_name, uuid).tap (device) -> - console.log('Configuring operating system') - capitano.runAsync("os configure #{temporalPath} #{uuid}").then -> - console.log('Initializing device') - resin.models.device.getManifestBySlug(application.device_type).then (manifest) -> - return form.run(manifest.initialization?.options) - .tap (answers) -> - if answers.drive? - message = "This will erase #{answers.drive}. Are you sure?" - patterns.confirm(options.yes, message) - .return(answers.drive) - .then(umount.umountAsync) - .then (answers) -> - init.initialize(temporalPath, device.uuid, answers) - .then(stepHandler) - .return(answers) - .tap (answers) -> - return if not answers.drive? - umount.umountAsync(answers.drive).tap -> - console.log("You can safely remove #{answers.drive} now") - .then (device) -> - console.log('Done') - return device.uuid + capitano.runAsync("device register #{application.app_name}") + .then(resin.models.device.get) + .tap (device) -> + console.log('Configuring operating system') + capitano.runAsync("os configure #{temporalPath} #{uuid}").then -> + console.log('Initializing device') + resin.models.device.getManifestBySlug(application.device_type).then (manifest) -> + return form.run(manifest.initialization?.options) + .tap (answers) -> + if answers.drive? + message = "This will erase #{answers.drive}. Are you sure?" + patterns.confirm(options.yes, message) + .return(answers.drive) + .then(umount.umountAsync) + .then (answers) -> + init.initialize(temporalPath, device.uuid, answers) + .then(stepHandler) + .return(answers) + .tap (answers) -> + return if not answers.drive? + umount.umountAsync(answers.drive).tap -> + console.log("You can safely remove #{answers.drive} now") + .then (device) -> + console.log('Done') + return device.uuid - .finally -> - fs.statAsync(temporalPath).then (stat) -> - return rimraf(temporalPath) if stat.isDirectory() - return fs.unlinkAsync(temporalPath) - .catch (error) -> + .finally -> + fs.statAsync(temporalPath).then (stat) -> + return rimraf(temporalPath) if stat.isDirectory() + return fs.unlinkAsync(temporalPath) + .catch (error) -> - # Ignore errors if temporary file does not exist - return if error.code is 'ENOENT' + # Ignore errors if temporary file does not exist + return if error.code is 'ENOENT' - throw error + throw error .nodeify(done) diff --git a/lib/app.coffee b/lib/app.coffee index 1077c911..80535e21 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -48,6 +48,7 @@ capitano.command(actions.device.init) capitano.command(actions.device.info) capitano.command(actions.device.remove) capitano.command(actions.device.identify) +capitano.command(actions.device.register) # ---------- Notes Module ---------- capitano.command(actions.notes.set)