From d704c101976f266ab54ada3ef3c3252d9cdb2aa0 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 29 Sep 2015 14:52:34 -0400 Subject: [PATCH] Implement os initialize command This command initialized an operating system image with a disk device. --- build/actions/device.js | 50 +++------------------------------------ build/actions/os.js | 38 ++++++++++++++++++++++++++++- build/app.js | 2 ++ doc/cli.markdown | 9 +++++++ lib/actions/device.coffee | 41 ++------------------------------ lib/actions/os.coffee | 35 +++++++++++++++++++++++++++ lib/app.coffee | 1 + 7 files changed, 89 insertions(+), 87 deletions(-) diff --git a/build/actions/device.js b/build/actions/device.js index 0f2c11ac..8fc7b747 100644 --- a/build/actions/device.js +++ b/build/actions/device.js @@ -1,5 +1,5 @@ (function() { - var Promise, _, capitano, commandOptions, events, form, fs, helpers, init, patterns, resin, rimraf, stepHandler, tmp, umount, vcs, visuals; + var Promise, _, capitano, commandOptions, events, form, fs, patterns, resin, rimraf, tmp, vcs, visuals; Promise = require('bluebird'); @@ -17,18 +17,12 @@ events = require('resin-cli-events'); - init = require('resin-device-init'); - fs = Promise.promisifyAll(require('fs')); rimraf = Promise.promisify(require('rimraf')); - umount = Promise.promisifyAll(require('umount')); - patterns = require('../utils/patterns'); - helpers = require('../utils/helpers'); - tmp = Promise.promisifyAll(require('tmp')); tmp.setGracefulCleanup(); @@ -135,24 +129,6 @@ } }; - stepHandler = function(step) { - var bar; - step.on('stdout', _.bind(process.stdout.write, process.stdout)); - step.on('stderr', _.bind(process.stderr.write, process.stderr)); - step.on('state', function(state) { - if (state.operation.command === 'burn') { - return; - } - return console.log(helpers.stateToString(state)); - }); - bar = new visuals.Progress('Writing Device OS'); - step.on('burn', _.bind(bar.update, bar)); - return new Promise(function(resolve, reject) { - step.on('error', reject); - return step.on('end', resolve); - }); - }; - exports.init = { signature: 'device init', description: 'initialise a device with resin os', @@ -171,28 +147,8 @@ return capitano.runAsync("os download --output " + temporalPath); }).then(function(temporalPath) { 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'); - return resin.models.device.getManifestBySlug(application.device_type).then(function(manifest) { - var ref; - return form.run((ref = manifest.initialization) != null ? ref.options : void 0); - }).tap(function(answers) { - var message; - if (answers.drive != null) { - message = "This will erase " + answers.drive + ". Are you sure?"; - return patterns.confirm(options.yes, message)["return"](answers.drive).then(umount.umountAsync); - } - }).then(function(answers) { - return init.initialize(temporalPath, device.uuid, answers).then(stepHandler)["return"](answers); - }).tap(function(answers) { - if (answers.drive == null) { - return; - } - return umount.umountAsync(answers.drive).tap(function() { - return console.log("You can safely remove " + answers.drive + " now"); - }); - }); + return capitano.runAsync("os configure " + temporalPath + " " + device.uuid).then(function() { + return capitano.runAsync("os initialize " + temporalPath + " " + device.uuid); }); }).then(function(device) { console.log('Done'); diff --git a/build/actions/os.js b/build/actions/os.js index fcd2c689..d5a01803 100644 --- a/build/actions/os.js +++ b/build/actions/os.js @@ -1,10 +1,14 @@ (function() { - var _, form, fs, helpers, init, manager, resin, stepHandler, visuals; + var Promise, _, form, fs, helpers, init, manager, patterns, resin, stepHandler, umount, visuals; fs = require('fs'); _ = require('lodash'); + Promise = require('bluebird'); + + umount = Promise.promisifyAll(require('umount')); + resin = require('resin-sdk'); manager = require('resin-image-manager'); @@ -17,6 +21,8 @@ helpers = require('../utils/helpers'); + patterns = require('../utils/patterns'); + exports.download = { signature: 'os download ', description: 'download an unconfigured os image', @@ -83,4 +89,34 @@ } }; + exports.initialize = { + signature: 'os initialize ', + description: 'initialize an os image', + help: 'Use this command to initialize a previously configured operating system image.\n\nExamples:\n\n $ resin os initialize ../path/rpi.img 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9', + permission: 'user', + action: function(params, options, done) { + console.info('Initializing device'); + return resin.models.device.get(params.uuid).then(resin.models.device.getManifestBySlug).then(function(manifest) { + var ref; + return (ref = manifest.initialization) != null ? ref.options : void 0; + }).then(form.run).tap(function(answers) { + var message; + if (answers.drive == null) { + return; + } + message = "This will erase " + answers.drive + ". Are you sure?"; + return patterns.confirm(options.yes, message)["return"](answers.drive).then(umount.umountAsync); + }).tap(function(answers) { + return init.initialize(params.image, params.uuid, answers).then(stepHandler); + }).then(function(answers) { + if (answers.drive == null) { + return; + } + return umount.umountAsync(answers.drive).tap(function() { + return console.info("You can safely remove " + answers.drive + " now"); + }); + }).nodeify(done); + } + }; + }).call(this); diff --git a/build/app.js b/build/app.js index eab6c418..b7c90e39 100644 --- a/build/app.js +++ b/build/app.js @@ -96,6 +96,8 @@ capitano.command(actions.os.configure); + capitano.command(actions.os.initialize); + capitano.command(actions.logs); update.notify(); diff --git a/doc/cli.markdown b/doc/cli.markdown index 8304bb7e..1eaa673d 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -74,6 +74,7 @@ Now you have access to all the commands referenced below. - [os download <type>](#os-download-60-type-62-) - [os configure <image> <uuid>](#os-configure-60-image-62-60-uuid-62-) + - [os initialize <image> <uuid>](#os-initialize-60-image-62-60-uuid-62-) - Wizard @@ -578,6 +579,14 @@ Examples: $ resin os configure ../path/rpi.img 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 +## os initialize <image> <uuid> + +Use this command to initialize a previously configured operating system image. + +Examples: + + $ resin os initialize ../path/rpi.img 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 + # Wizard ## quickstart [name] diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index ea56d4d0..e78b7738 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -6,12 +6,9 @@ visuals = require('resin-cli-visuals') vcs = require('resin-vcs') form = require('resin-cli-form') events = require('resin-cli-events') -init = require('resin-device-init') fs = Promise.promisifyAll(require('fs')) rimraf = Promise.promisify(require('rimraf')) -umount = Promise.promisifyAll(require('umount')) patterns = require('../utils/patterns') -helpers = require('../utils/helpers') tmp = Promise.promisifyAll(require('tmp')) tmp.setGracefulCleanup() @@ -174,23 +171,6 @@ exports.rename = events.send('device.rename', device: params.uuid) .nodeify(done) -stepHandler = (step) -> - - step.on('stdout', _.bind(process.stdout.write, process.stdout)) - step.on('stderr', _.bind(process.stderr.write, process.stderr)) - - step.on 'state', (state) -> - return if state.operation.command is 'burn' - console.log(helpers.stateToString(state)) - - bar = new visuals.Progress('Writing Device OS') - - step.on('burn', _.bind(bar.update, bar)) - - return new Promise (resolve, reject) -> - step.on('error', reject) - step.on('end', resolve) - exports.init = signature: 'device init' description: 'initialise a device with resin os' @@ -223,25 +203,8 @@ exports.init = 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") + capitano.runAsync("os configure #{temporalPath} #{device.uuid}").then -> + capitano.runAsync("os initialize #{temporalPath} #{device.uuid}") .then (device) -> console.log('Done') return device.uuid diff --git a/lib/actions/os.coffee b/lib/actions/os.coffee index 78d2d441..aa508380 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -1,11 +1,14 @@ fs = require('fs') _ = require('lodash') +Promise = require('bluebird') +umount = Promise.promisifyAll(require('umount')) resin = require('resin-sdk') manager = require('resin-image-manager') visuals = require('resin-cli-visuals') form = require('resin-cli-form') init = require('resin-device-init') helpers = require('../utils/helpers') +patterns = require('../utils/patterns') exports.download = signature: 'os download ' @@ -83,3 +86,35 @@ exports.configure = .then (answers) -> init.configure(params.image, params.uuid, answers).then(stepHandler) .nodeify(done) + +exports.initialize = + signature: 'os initialize ' + description: 'initialize an os image' + help: ''' + Use this command to initialize a previously configured operating system image. + + Examples: + + $ resin os initialize ../path/rpi.img 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 + ''' + permission: 'user' + action: (params, options, done) -> + console.info('Initializing device') + resin.models.device.get(params.uuid) + .then(resin.models.device.getManifestBySlug) + .then (manifest) -> + return manifest.initialization?.options + .then(form.run) + .tap (answers) -> + return if not answers.drive? + message = "This will erase #{answers.drive}. Are you sure?" + patterns.confirm(options.yes, message) + .return(answers.drive) + .then(umount.umountAsync) + .tap (answers) -> + return init.initialize(params.image, params.uuid, answers).then(stepHandler) + .then (answers) -> + return if not answers.drive? + umount.umountAsync(answers.drive).tap -> + console.info("You can safely remove #{answers.drive} now") + .nodeify(done) diff --git a/lib/app.coffee b/lib/app.coffee index 80535e21..3882b9a2 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -68,6 +68,7 @@ capitano.command(actions.env.remove) # ---------- OS Module ---------- capitano.command(actions.os.download) capitano.command(actions.os.configure) +capitano.command(actions.os.initialize) # ---------- Logs Module ---------- capitano.command(actions.logs)