Skip to content

Commit

Permalink
implement failsafe connections. fix #92
Browse files Browse the repository at this point in the history
  • Loading branch information
pstadler committed Jun 21, 2015
1 parent c0c0f71 commit 627fecc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,26 @@ plan.target('dynamic-hosts', function(done) {
});
```

Usually flightplan will abort when a host is not reachable or authentication
fails. This can be prevented by setting a property `failsafe` to `true` on
any of the host configurations:

```bash
plan.target('production', [
{
host: 'www1.example.com',
username: 'pstadler',
agent: process.env.SSH_AUTH_SOCK
},
{
host: 'www2.example.com',
username: 'pstadler',
agent: process.env.SSH_AUTH_SOCK,
failsafe: true // continue flightplan even if connection to www2 fails
}
]);
```
You can override the `username` value of hosts by calling `fly` with
the `-u|--username` option:
Expand Down Expand Up @@ -534,7 +554,7 @@ if(plan.runtime.target === 'production') {
}
```
### transport.waitFor(fn(done)) → mixed
### transport.waitFor(fn(done)) → {} mixed
Execute a function and return after the callback `done` is called.
This is used for running asynchronous functions in a synchronous way.
Expand Down
17 changes: 14 additions & 3 deletions lib/flight/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ var Fiber = require('fibers')
, extend = require('util-extend')
, SSH = require('../transport/ssh')
, logger = require('../logger')()
, prettyTime = require('pretty-hrtime');
, prettyTime = require('pretty-hrtime')
, errors = require('../errors');

var _connections = [];

exports.run = function(fn, context) {
var connect = function(context) {
var future = new Future();
new Fiber(function() {
logger.info('Connecting to "' + context.remote.host + '"');
_connections.push(new SSH(context));
logger.info("Connecting to '" + context.remote.host + "'");
try {
var connection = new SSH(context);
_connections.push(connection);
} catch(e) {
if(!context.remote.failsafe) {
throw new errors.ConnectionFailedError("Error connecting to '" +
context.remote.host + "': " + e.message);
}
logger.warn("Safely failed connecting to '" + context.remote.host +
"': " + e.message);
}
return future.return();
}).run();
return future;
Expand Down
21 changes: 21 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ function Flightplan() {
* });
* ```
*
* Usually flightplan will abort when a host is not reachable or authentication
* fails. This can be prevented by setting a property `failsafe` to `true` on
* any of the host configurations:
*
* ```bash
* plan.target('production', [
* {
* host: 'www1.example.com',
* username: 'pstadler',
* agent: process.env.SSH_AUTH_SOCK
* },
* {
* host: 'www2.example.com',
* username: 'pstadler',
* agent: process.env.SSH_AUTH_SOCK,
* failsafe: true // continue flightplan even if connection to www2 fails
* }
* ]);
* ```
*
*
* You can override the `username` value of hosts by calling `fly` with
* the `-u|--username` option:
*
Expand Down
6 changes: 3 additions & 3 deletions lib/transport/ssh.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var util = require('util')
, extend = require('util-extend')
, Fiber = require('fibers')
, Connection = require('ssh2')
, Connection = require('ssh2').Client
, byline = require('byline')
, Transport = require('./index')
, errors = require('../errors')
Expand Down Expand Up @@ -48,9 +48,9 @@ function SSH(context) {
});

this._connection.on('error', function(err) {
throw new errors.ConnectionFailedError('Error connecting to ' +
self._context.remote.host + ': ' + err);
return fiber.throwInto(err);
});

this._connection.connect(config);

return Fiber.yield();
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": "Library for streamlining application deployment or systems administration tasks",
"version": "0.6.3",
"version": "0.6.4",
"author": "Patrick Stadler <patrick.stadler@gmail.com>",
"keywords": [
"deploy",
Expand Down

0 comments on commit 627fecc

Please sign in to comment.