mecano
Version:
Common functions for system deployment.
133 lines (126 loc) • 3.77 kB
JavaScript
// Generated by CoffeeScript 1.7.1
var child, conditions, each, exec, misc;
each = require('each');
exec = require('ssh2-exec');
misc = require('./misc');
conditions = require('./misc/conditions');
child = require('./misc/child');
module.exports = function(goptions, options, callback) {
var finish, isArray, result, _ref;
_ref = misc.args(arguments), goptions = _ref[0], options = _ref[1], callback = _ref[2];
result = child();
finish = function(err, created, stdout, stderr) {
if (callback) {
callback(err, created, stdout, stderr);
}
return result.end(err, created);
};
isArray = Array.isArray(options);
misc.options(options, function(err, options) {
var escape, executed, stderrs, stdouts, stds;
if (err) {
return finish(err);
}
executed = 0;
stdouts = [];
stderrs = [];
escape = function(cmd) {
var char, esccmd, _i, _len;
esccmd = '';
for (_i = 0, _len = cmd.length; _i < _len; _i++) {
char = cmd[_i];
if (char === '$') {
esccmd += '\\';
}
esccmd += char;
}
return esccmd;
};
stds = callback ? callback.length > 2 : false;
return each(options).parallel(goptions.parallel).on('item', function(options, i, next) {
var cmd;
if (typeof options === 'string') {
options = {
cmd: options
};
}
if (options.cmd == null) {
return next(new Error("Missing cmd: " + options.cmd));
}
if (options.code == null) {
options.code = [0];
}
if (!Array.isArray(options.code)) {
options.code = [options.code];
}
if (options.code_skipped == null) {
options.code_skipped = [];
}
if (!Array.isArray(options.code_skipped)) {
options.code_skipped = [options.code_skipped];
}
if (options.trap_on_error) {
options.cmd = "set -e\n" + options.cmd;
}
cmd = function() {
var run, stderr, stdout;
if (typeof options.log === "function") {
options.log("Execute: " + options.cmd);
}
run = exec(options);
stdout = stderr = [];
if (options.stdout) {
run.stdout.pipe(options.stdout, {
end: false
});
}
if (stds) {
run.stdout.on('data', function(data) {
return stdout.push(data);
});
}
if (options.stderr) {
run.stderr.pipe(options.stderr, {
end: false
});
}
if (stds) {
run.stderr.on('data', function(data) {
return stderr.push(data);
});
}
return run.on("exit", function(code) {
return setTimeout(function() {
stdouts.push(stds ? stdout.join('') : void 0);
stderrs.push(stds ? stderr.join('') : void 0);
if (options.stdout) {
run.stdout.unpipe(options.stdout);
}
if (options.stderr) {
run.stderr.unpipe(options.stderr);
}
if (options.code.indexOf(code) === -1 && options.code_skipped.indexOf(code) === -1) {
err = new Error("Invalid exec code " + code);
err.code = code;
return next(err);
}
if (options.code_skipped.indexOf(code) === -1) {
executed++;
}
return next();
}, 1);
});
};
return conditions.all(options, next, cmd);
}).on('both', function(err) {
if (!isArray) {
stdouts = stdouts[0];
}
if (!isArray) {
stderrs = stderrs[0];
}
return finish(err, executed, stdouts, stderrs);
});
});
return result;
};