From 03d7520de21a800d9acd3c8475e55bd3d91e0570 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Thu, 17 Mar 2016 16:07:19 -0400 Subject: [PATCH] Implement config inject command This command allows to user to inject a whole `config.json` file to a provisioned device. --- build/actions/config.js | 39 +++++++++++++++++++++++++++++++++ build/app.js | 2 ++ doc/cli.markdown | 20 +++++++++++++++++ lib/actions/config.coffee | 45 +++++++++++++++++++++++++++++++++++++++ lib/app.coffee | 1 + 5 files changed, 107 insertions(+) diff --git a/build/actions/config.js b/build/actions/config.js index 96be160b..98b31d30 100644 --- a/build/actions/config.js +++ b/build/actions/config.js @@ -98,6 +98,45 @@ limitations under the License. } }; + exports.inject = { + signature: 'config inject ', + description: 'inject a device configuration file', + help: 'Use this command to inject a config.json file to a provisioned device\n\nExamples:\n\n $ resin config inject my/config.json --type raspberry-pi\n $ resin config inject my/config.json --type raspberry-pi --drive /dev/disk2', + options: [ + { + signature: 'type', + description: 'device type', + parameter: 'type', + alias: 't', + required: 'You have to specify a device type' + }, { + signature: 'drive', + description: 'drive', + parameter: 'drive', + alias: 'd' + } + ], + permission: 'user', + root: true, + action: function(params, options, done) { + var Promise, config, fs, umount, visuals; + Promise = require('bluebird'); + config = require('resin-config-json'); + visuals = require('resin-cli-visuals'); + umount = Promise.promisifyAll(require('umount')); + fs = Promise.promisifyAll(require('fs')); + return Promise["try"](function() { + return options.drive || visuals.drive('Select the device drive'); + }).tap(umount.umountAsync).then(function(drive) { + return fs.readFileAsync(params.file, 'utf8').then(JSON.parse).then(function(configJSON) { + return config.write(drive, options.type, configJSON); + }); + }).tap(function() { + return console.info('Done'); + }).nodeify(done); + } + }; + exports.reconfigure = { signature: 'config reconfigure', description: 'reconfigure a provisioned device', diff --git a/build/app.js b/build/app.js index 5b38e80a..77bc0980 100644 --- a/build/app.js +++ b/build/app.js @@ -123,6 +123,8 @@ limitations under the License. capitano.command(actions.config.write); + capitano.command(actions.config.inject); + capitano.command(actions.config.reconfigure); capitano.command(actions.config.generate); diff --git a/doc/cli.markdown b/doc/cli.markdown index 3c1db2ad..f2e405a2 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -81,6 +81,7 @@ Now you have access to all the commands referenced below. - [config read](#config-read) - [config write <key> <value>](#config-write-60-key-62-60-value-62-) + - [config inject <file>](#config-inject-60-file-62-) - [config reconfigure](#config-reconfigure) - [config generate <uuid>](#config-generate-60-uuid-62-) @@ -685,6 +686,25 @@ device type drive +## config inject <file> + +Use this command to inject a config.json file to a provisioned device + +Examples: + + $ resin config inject my/config.json --type raspberry-pi + $ resin config inject my/config.json --type raspberry-pi --drive /dev/disk2 + +### Options + +#### --type, -t <type> + +device type + +#### --drive, -d <drive> + +drive + ## config reconfigure Use this command to reconfigure a provisioned device diff --git a/lib/actions/config.coffee b/lib/actions/config.coffee index c41e219e..c87690ef 100644 --- a/lib/actions/config.coffee +++ b/lib/actions/config.coffee @@ -110,6 +110,51 @@ exports.write = console.info('Done') .nodeify(done) +exports.inject = + signature: 'config inject ' + description: 'inject a device configuration file' + help: ''' + Use this command to inject a config.json file to a provisioned device + + Examples: + + $ resin config inject my/config.json --type raspberry-pi + $ resin config inject my/config.json --type raspberry-pi --drive /dev/disk2 + ''' + options: [ + { + signature: 'type' + description: 'device type' + parameter: 'type' + alias: 't' + required: 'You have to specify a device type' + } + { + signature: 'drive' + description: 'drive' + parameter: 'drive' + alias: 'd' + } + ] + permission: 'user' + root: true + action: (params, options, done) -> + Promise = require('bluebird') + config = require('resin-config-json') + visuals = require('resin-cli-visuals') + umount = Promise.promisifyAll(require('umount')) + fs = Promise.promisifyAll(require('fs')) + + Promise.try -> + return options.drive or visuals.drive('Select the device drive') + .tap(umount.umountAsync) + .then (drive) -> + fs.readFileAsync(params.file, 'utf8').then(JSON.parse).then (configJSON) -> + return config.write(drive, options.type, configJSON) + .tap -> + console.info('Done') + .nodeify(done) + exports.reconfigure = signature: 'config reconfigure' description: 'reconfigure a provisioned device' diff --git a/lib/app.coffee b/lib/app.coffee index 9866b664..86fc9f12 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -97,6 +97,7 @@ capitano.command(actions.os.initialize) # ---------- Config Module ---------- capitano.command(actions.config.read) capitano.command(actions.config.write) +capitano.command(actions.config.inject) capitano.command(actions.config.reconfigure) capitano.command(actions.config.generate)