From b23300615b728f622caef6dcc83abffd964ad021 Mon Sep 17 00:00:00 2001 From: Patrick Stadler Date: Sun, 16 Feb 2014 19:46:18 +0100 Subject: [PATCH] failed commands now throw exceptions to abort a flight --- lib/flight.js | 10 +++++----- lib/remote.js | 10 +++++----- lib/transport/shell.js | 2 +- lib/transport/ssh.js | 3 ++- lib/transport/transport.js | 15 +++------------ package.json | 2 +- 6 files changed, 17 insertions(+), 25 deletions(-) diff --git a/lib/flight.js b/lib/flight.js index 21bbe1e..89c26aa 100644 --- a/lib/flight.js +++ b/lib/flight.js @@ -44,16 +44,16 @@ Flight.prototype = { var t = process.hrtime(); var transport = new this.transportClass(this); - this.fn(transport); + try { + this.fn(transport); + } catch(e) { + this.abort(e); + } transport.close(); t = process.hrtime(t); this.status.executionTime = Math.round(t[0]*1e3 + t[1]/1e6); - if(this.isAborted()) { - this.flightplan.abort(); - } - return future.return(); }.bind(this)).run(); diff --git a/lib/remote.js b/lib/remote.js index d1dcc9a..dca2250 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -23,12 +23,12 @@ RemoteFlight.prototype.__start = function() { var t = process.hrtime(); var transport = new flight.transportClass(flight, host); - flight.fn(transport); - transport.close(); - - if(flight.isAborted()) { - this.flightplan.abort(); + try { + flight.fn(transport); + } catch(e) { + this.abort(e); } + transport.close(); t = process.hrtime(t); flight.status.executionTime = Math.round(t[0]*1e3 + t[1]/1e6); diff --git a/lib/transport/shell.js b/lib/transport/shell.js index 3cbc46c..1f9f452 100644 --- a/lib/transport/shell.js +++ b/lib/transport/shell.js @@ -47,7 +47,7 @@ ShellTransport.prototype.__exec = function(cmd, args, options) { this.logger.warn(this.logger.format('failed safely').warn, 'with exit code:', ret.code); } else { this.logger.error(this.logger.format('failed').error, 'with exit code:', ret.code); - this.flight.abort(this.logger.format('`%s` failed on localhost', cmd.white)); + fiber.throwInto(new Error(this.logger.format('`%s` failed on localhost', cmd.white))); } fiber.run(ret); }.bind(this)); diff --git a/lib/transport/ssh.js b/lib/transport/ssh.js index 8f78a37..ab47a3d 100644 --- a/lib/transport/ssh.js +++ b/lib/transport/ssh.js @@ -69,7 +69,8 @@ SSHTransport.prototype.__exec = function(cmd, args, options) { this.logger.warn(this.logger.format('safely failed').warn, 'with exit code:', ret.code); } else { this.logger.error(this.logger.format('failed').error, 'with exit code:', ret.code); - this.flight.abort(this.logger.format('`%s` failed on %s', cmd.white, this.config.host.warn)); + fiber.throwInto(new Error(this.logger.format('`%s` failed on %s' + , cmd.white, this.config.host.warn))); } fiber.run(ret); }.bind(this)); diff --git a/lib/transport/transport.js b/lib/transport/transport.js index a935051..7a3e822 100644 --- a/lib/transport/transport.js +++ b/lib/transport/transport.js @@ -36,7 +36,7 @@ function Transport(flight) { commands.forEach(function(cmd) { this[cmd] = function(args, opts) { opts = this._parseOptions(opts); - return this._execOrSkip(cmd, args, opts); + return this.__exec(cmd, args, opts); }; }, this); } @@ -84,7 +84,7 @@ Transport.prototype = { args = args.split(' '); cmd = args.shift(); opts = this._parseOptions(opts); - return this._execOrSkip(cmd, args.join(' '), opts); + return this.__exec(cmd, args.join(' '), opts); }, /** @@ -112,7 +112,7 @@ Transport.prototype = { var format = "%s -i bash -c '%s'"; args = util.format(format, user, args); opts = this._parseOptions(opts); - return this._execOrSkip('sudo', args, opts); + return this.__exec('sudo', args, opts); }, /** @@ -291,15 +291,6 @@ Transport.prototype = { throw new Error('transfer: transport does not support this method'); }, - _execOrSkip: function(cmd, args, opts) { - if(this.flight.isAborted()) { - return { code: null, stdout: null, stderr: null }; - } - var result = this.__exec(cmd, args, opts); - this.logger.space(); - return result; - }, - _parseOptions: function(opts) { var options = util._extend({}, this.options); // clone return util._extend(options, opts); diff --git a/package.json b/package.json index 88bc5c3..4a0e587 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "flightplan", "description": "Run a sequence of commands against local and remote hosts", - "version": "0.1.3", + "version": "0.1.4", "author": "Patrick Stadler ", "keywords": [ "deploy",