Skip to content

Commit

Permalink
provide some basic information during flights with the target property
Browse files Browse the repository at this point in the history
  • Loading branch information
pstadler committed Feb 27, 2014
1 parent e0d082e commit 5e71d1b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 20 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ two types of flights:

#### Local flights

Commands in local flights are executed on the **local host**.
Commands in local flights are executed on the **localhost**.

```javascript
plan.local(function(transport) {
transport.hostname(); // prints the hostname of the local host
transport.hostname(); // prints the hostname of localhost
});
```

Expand Down Expand Up @@ -197,7 +197,7 @@ fly production --username=admin
### flightplan.local(fn) → this

Calling this method registers a local flight. Local flights are
executed on your local host. When `fn` gets called a `Transport` object
executed on your localhost. When `fn` gets called a `Transport` object
is passed with the first argument.

```javascript
Expand Down Expand Up @@ -271,6 +271,21 @@ We call the Transport object `transport` in the following section to avoid
confusion. However, do yourself a favor and use `local` for local, and
`remote` for remote flights.

#### Accessing flight-specific information

Flightplan provides information during flights with the `target` properties:

```javascript
plan.remote(function(transport) { // applies to local flights as well
// Flightplan specific information
console.log(plan.target.destination); // 'production'
console.log(plan.target.hosts); // [{ host: 'www1.pstadler.sh', port: 22 }, ...]

// Flight specific information
console.log(transport.target); // { host: 'www1.pstadler.sh', port: 22 }
});
```

### transport.exec(command[, options]) → code: int, stdout: String, stderr: String

To execute a command you have the choice between using `exec()` or one
Expand Down Expand Up @@ -399,6 +414,14 @@ if(input.indexOf('yes') === -1) {

// prompt for password (with UNIX-style hidden input)
var password = transport.prompt('Enter your password:', { hidden: true });

// prompt when deploying to a specific destination
if(plan.target.destination === 'production') {
var input = transport.prompt('Ready for deploying to production? [yes]');
if(input.indexOf('yes') === -1) {
transport.abort('user canceled flight');
}
}
```

### transport.log(message)
Expand Down
19 changes: 13 additions & 6 deletions lib/flightplan.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ var Fiber = require('fibers')
*
* #### Local flights
*
* Commands in local flights are executed on the **local host**.
* Commands in local flights are executed on the **localhost**.
*
* ```javascript
* plan.local(function(transport) {
* transport.hostname(); // prints the hostname of the local host
* transport.hostname(); // prints the hostname of localhost
* });
* ```
*
Expand Down Expand Up @@ -62,6 +62,10 @@ var Fiber = require('fibers')
function Flightplan() {
this._briefing = null;
this.flights = [];
this.target = {
destination: null,
hosts: []
};
this.status = {
aborted: false,
executionTime: 0
Expand Down Expand Up @@ -148,7 +152,7 @@ Flightplan.prototype = {

/**
* Calling this method registers a local flight. Local flights are
* executed on your local host. When `fn` gets called a `Transport` object
* executed on your localhost. When `fn` gets called a `Transport` object
* is passed with the first argument.
*
* ```javascript
Expand Down Expand Up @@ -249,23 +253,26 @@ Flightplan.prototype = {
},

start: function(destination, options) {
this.target.destination = destination;

if(this.briefing()) {
this.briefing().applyOptions(options);
} else {
this.briefing(options);
}
this.logger.debug('Briefing done');
if(this.requiresDestination() && !this.briefing().hasDestination(destination)) {
if(this.requiresDestination() && !this.briefing().hasDestination(this.target.destination)) {
this.logger.error((destination || '<empty>').warn, 'is not a valid destination');
process.exit(1);
}
this.target.hosts = this.briefing().getHostsForDestination(this.target.destination);

if(this.isAborted()) {
this.logger.error('Flightplan aborted'.error);
process.exit(1);
}
this.logger.info('Executing flightplan with'.info, String(this.flights.length).magenta
, 'planned flight(s) to'.info, (destination || 'localhost').warn);
, 'planned flight(s) to'.info, (this.target.destination || 'localhost').warn);
this.logger.space();

Fiber(function() {
Expand All @@ -278,7 +285,7 @@ Flightplan.prototype = {
this.logger.info('Flight'.info, this.logger.format('%s/%s', i+1, len).magenta, 'launched...'.info);
this.logger.space();

flight.start(destination);
flight.start(this.target.destination);

var status = flight.getStatus()
, flightNumber = this.logger.format('%s/%s', i+1, len).magenta
Expand Down
4 changes: 2 additions & 2 deletions lib/transport/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var util = require('util')

function ShellTransport(flight) {
ShellTransport.super_.call(this, flight);
this.host = 'local';
this.logger = this.logger.cloneWithPrefix(this.host);
this.target.host = 'localhost';
this.logger = this.logger.cloneWithPrefix(this.target.host);
}

util.inherits(ShellTransport, Transport);
Expand Down
12 changes: 6 additions & 6 deletions lib/transport/ssh.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ var util = require('util')
, Connection = require('ssh2')
, Transport = require('./transport');

function SSHTransport(flight, config) {
function SSHTransport(flight, target) {
SSHTransport.super_.call(this, flight);
this.config = config;
this.host = this.config.host;
this.logger = this.logger.cloneWithPrefix(this.host);
this.target = target;
this.logger = this.logger.cloneWithPrefix(this.target.host);

this.connection = new Connection();

var _fiber = Fiber.current;
Expand All @@ -20,7 +20,7 @@ function SSHTransport(flight, config) {
// TODO
});

this.connection.connect(this.config);
this.connection.connect(this.target);

return Fiber.yield();
}
Expand Down Expand Up @@ -69,7 +69,7 @@ SSHTransport.prototype.__exec = function(cmd, args, options) {
} else {
this.logger.error(this.logger.format('failed').error, 'with exit code:', ret.code);
fiber.throwInto(new Error(this.logger.format('`%s` failed on %s'
, cmd.white, this.config.host.warn)));
, cmd.white, this.target.host.warn)));
}
fiber.run(ret);
}.bind(this));
Expand Down
30 changes: 28 additions & 2 deletions lib/transport/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,34 @@ var util = require('util')
* confusion. However, do yourself a favor and use `local` for local, and
* `remote` for remote flights.
*
* #### Accessing flight-specific information
*
* Flightplan provides information during flights with the `target` properties:
*
* ```javascript
* plan.remote(function(transport) { // applies to local flights as well
* // Flightplan specific information
* console.log(plan.target.destination); // 'production'
* console.log(plan.target.hosts); // [{ host: 'www1.pstadler.sh', port: 22 }, ...]
*
* // Flight specific information
* console.log(transport.target); // { host: 'www1.pstadler.sh', port: 22 }
* });
* ```
*
* @class Transport
* @return transport
*/
function Transport(flight) {
this.flight = flight;
this.target = {};
this.logger = flight.logger;

this.options = {
silent: false,
failsafe: false
};
this.flight = flight;
this.logger = flight.logger;


commands.forEach(function(cmd) {
this[cmd] = function(args, opts) {
Expand Down Expand Up @@ -197,6 +215,14 @@ Transport.prototype = {
*
* // prompt for password (with UNIX-style hidden input)
* var password = transport.prompt('Enter your password:', { hidden: true });
*
* // prompt when deploying to a specific destination
* if(plan.target.destination === 'production') {
* var input = transport.prompt('Ready for deploying to production? [yes]');
* if(input.indexOf('yes') === -1) {
* transport.abort('user canceled flight');
* }
* }
* ```
*
* @method prompt(message[, options])
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.1.7",
"version": "0.1.8",
"author": "Patrick Stadler <patrick.stadler@gmail.com>",
"keywords": [
"deploy",
Expand Down

0 comments on commit 5e71d1b

Please sign in to comment.