Code coverage report for lib/breaker.js

Statements: 100% (39 / 39)      Branches: 100% (16 / 16)      Functions: 100% (12 / 12)      Lines: 100% (39 / 39)      Ignored: none     

All files » lib/ » breaker.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 1031                       1 19               1 1                 1 1 1 1                 1 3 3   5 5 1   5 6 6             6 6         3     1 8       1 25 25 37 64 14       25     8 6 25 25 13 13       2     8     1  
var _ = require('lodash'),
  async = require('async'),
  bag = require('bagofcli'),
  fsx = require('fs.extra'),
  p = require('path'),
  util = require('util');
 
/**
 * class Breaker
 *
 * @param {Array} labels: an array of filter labels, used to determine which hosts to include
 */
function Breaker(opts) {
  this.opts = opts || {};
}
 
/**
 * Create a sample .breaker.json hosts file in current directory.
 *
 * @param {Function} cb: standard cb(err, result) callback
 */
Breaker.prototype.init = function (cb) {
  fsx.copy(p.join(__dirname, '../examples/.breaker.json'), '.breaker.json', cb);
};
 
/**
 * Format hosts info into specific type.
 *
 * @param {String} type: Format type
 * @param {Function} cb: standard cb(err, result) callback
 */
Breaker.prototype.format = function (type, cb) {
  var formatter = require('./formatters/' + type);
  console.log(formatter.format(this._config()));
  cb();
};
 
/**
 * Execute shell command via SSH
 *
 * @param {String} command: Shell command to execute
 * @param {Function} cb: standard cb(err, result) callback
 */
Breaker.prototype.ssh = function (command, cb) {
  var tasks = [];
  this._config().forEach(function (item) {
    
    console.log('+ %s', item.host);
    if (!item.ssh) {
      item.ssh = [{}];
    }
    item.ssh.forEach(function (sshItem) {
      tasks.push(function (cb) {
        var sshCommand = util.format('ssh %s %s%s%s %s',
          (sshItem.key) ? '-i ' + sshItem.key : '',
          (sshItem.user) ? sshItem.user + '@' : '',
          item.host,
          ((sshItem.port) ? ':' + sshItem.port : ''),
          '\'' + command + '\''
          );
        console.log('> ' + sshCommand);
        bag.exec(sshCommand, true, cb);
      });
    });
  });
 
  async.series(tasks, cb);
};
 
Breaker.prototype._config = function () {
  var conf = JSON.parse(bag.lookupFile('.breaker.json')),
    filtered = [],
    self = this;
 
  function _match(filters, labels) {
    var match = [];
    labels.forEach(function (label) {
      filters.forEach(function (filter) {
        if (label.match(new RegExp(filter))) {
          match.push(label);
        }
      });
    });
    return match;
  }
 
  if (this.opts.labels) {
    conf.forEach(function (item) {
      var match = _match(self.opts.labels, item.labels);
      if (match.length >= 1) {
        item.labels = match;
        filtered.push(item);
      }
    });
  } else {
    filtered = conf;
  }
 
  return filtered;
};
 
module.exports = Breaker;