Skip to content

Commit

Permalink
failed commands now throw exceptions to abort a flight
Browse files Browse the repository at this point in the history
  • Loading branch information
pstadler committed Feb 16, 2014
1 parent 3a2ae8f commit b233006
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 25 deletions.
10 changes: 5 additions & 5 deletions lib/flight.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
10 changes: 5 additions & 5 deletions lib/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/transport/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion lib/transport/ssh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
15 changes: 3 additions & 12 deletions lib/transport/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
},

/**
Expand Down Expand Up @@ -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);
},

/**
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"keywords": [
"deploy",
Expand Down

0 comments on commit b233006

Please sign in to comment.