Code coverage report for lib/formatters/clusterssh.js

Statements: 100% (24 / 24)      Branches: 100% (4 / 4)      Functions: 100% (7 / 7)      Lines: 100% (24 / 24)      Ignored: none     

All files » lib/formatters/ » clusterssh.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 521               1     6 6 11 11 1   10           6 6 10 10 10   6 7       6   6 10 10 10         6 5     6     1
var _ = require('lodash');
 
/**
 * Format configuration data into a clusterssh config
 * http://www.debianadmin.com/ssh-on-multiple-servers-using-cluster-ssh.html
 *
 * @param {Object} conf: Breaker configuration data
 */
function format(conf) {
 
  // group hosts by label
  var groups = {};
  conf.forEach(function (item) {
    item.labels.forEach(function (label) {
      if (groups[label]) {
        groups[label].push(item.host);
      } else {
        groups[label] = [ item.host ];
      }
    });
  });
 
  // sort groups by label
  var data = [];
  _.keys(groups).forEach(function (label) {
    var group = {};
    group[label] = groups[label];
    data.push(group);
  });
  data.sort(function (o1, o2) {
    return _.keys(o1)[0] > _.keys(o2)[0];
  });
 
  // format sorted groups as lines
  var labels = [],
    lines = [];
  data.forEach(function (item) {
    _.keys(item).forEach(function (label) {
      labels.push(label);
      lines.push(label + ' = ' + item[label].join(' '));
    });
  });
 
  // add header
  if (lines.length >= 1) {
    lines.unshift('clusters = ' + labels.join(' ') + '\n');
  }
 
  return lines.join('\n');
}
 
exports.format = format;