mecano
Version:
Common functions for system deployment.
291 lines (284 loc) • 7.84 kB
JavaScript
// Generated by CoffeeScript 1.9.1
var each, exec, fs, misc, template;
module.exports = {
"if": function(options, skip, succeed) {
var ok;
if (!Array.isArray(options["if"])) {
options["if"] = [options["if"]];
}
ok = true;
return each(options["if"]).run(function(si, next) {
var err, type;
if (!ok) {
return next();
}
type = typeof si;
if (si === null || type === 'undefined') {
ok = false;
return next();
} else if (type === 'boolean' || type === 'number') {
if (!si) {
ok = false;
}
return next();
} else if (type === 'function') {
if (si.length < 2) {
try {
if (!si(options)) {
ok = false;
}
next();
} catch (_error) {
err = _error;
next(err);
}
}
if (si.length === 2) {
return si(options, function(err, is_ok) {
if (err) {
return next(err);
}
if (!is_ok) {
ok = false;
}
return next();
});
} else {
return next(new Error("Invalid callback"));
}
} else if (type === 'string') {
si = template(si, options);
if (si.length === 0) {
ok = false;
}
return next();
} else {
return next(new Error("Invalid condition type"));
}
}).then(function(err) {
if (err || !ok) {
return skip(err);
} else {
return succeed();
}
});
},
not_if: function(options, skip, succeed) {
var ok;
if (!Array.isArray(options.not_if)) {
options.not_if = [options.not_if];
}
ok = true;
return each(options.not_if).run(function(not_if, next) {
var err, type;
if (!ok) {
return next();
}
type = typeof not_if;
if (not_if === null || type === 'undefined') {
ok = true;
return next();
} else if (type === 'boolean' || type === 'number') {
if (not_if) {
ok = false;
}
return next();
} else if (type === 'function') {
if (not_if.length < 2) {
try {
if (not_if(options)) {
ok = false;
}
next();
} catch (_error) {
err = _error;
next(err);
}
}
if (not_if.length === 2) {
return not_if(options, function(err, is_ok) {
if (err) {
return next(err);
}
if (is_ok) {
ok = false;
}
return next();
});
} else {
return next(new Error("Invalid callback"));
}
} else if (type === 'string') {
not_if = template(not_if, options);
if (not_if.length !== 0) {
ok = false;
}
return next();
} else {
return next(new Error("Invalid condition type"));
}
}).then(function(err) {
if (err || !ok) {
return skip(err);
} else {
return succeed();
}
});
},
if_exec: function(options, skip, succeed) {
return each(options.if_exec).run(function(cmd, next) {
var run;
if (typeof options.log === "function") {
options.log("Mecano `not_if_exec`: " + cmd);
}
options = {
cmd: cmd,
ssh: options.ssh
};
run = exec(options);
if (options.stdout) {
run.stdout.pipe(options.stdout, {
end: false
});
}
if (options.stderr) {
run.stderr.pipe(options.stderr, {
end: false
});
}
return run.on("exit", function(code) {
if (typeof options.log === "function") {
options.log("Mecano `if_exec`: code is \"" + code + "\"");
}
if (code === 0) {
return next();
} else {
return skip();
}
});
}).then(succeed);
},
not_if_exec: function(options, skip, succeed) {
return each(options.not_if_exec).run(function(cmd, next) {
var run;
if (typeof options.log === "function") {
options.log("Mecano `not_if_exec`: " + cmd);
}
options = {
cmd: cmd,
ssh: options.ssh
};
run = exec(options);
if (options.stdout) {
run.stdout.pipe(options.stdout, {
end: false
});
}
if (options.stderr) {
run.stderr.pipe(options.stderr, {
end: false
});
}
return run.on("exit", function(code) {
if (typeof options.log === "function") {
options.log("Mecano `not_if_exec`: code is \"" + code + "\"");
}
if (code === 0) {
return skip();
} else {
return next();
}
});
}).then(succeed);
},
if_exists: function(options, skip, succeed) {
var destination, if_exists, ssh;
ssh = options.ssh, if_exists = options.if_exists, destination = options.destination;
if (typeof not_if_exists === 'boolean' && destination) {
if_exists = if_exists ? [destination] : null;
}
return each(if_exists).run(function(if_exists, next) {
return fs.exists(ssh, if_exists, function(err, exists) {
if (exists) {
return next();
} else {
return skip();
}
});
}).then(succeed);
},
not_if_exists: function(options, skip, succeed) {
var destination, not_if_exists, ssh;
ssh = options.ssh, not_if_exists = options.not_if_exists, destination = options.destination;
if (typeof not_if_exists === 'boolean' && destination) {
not_if_exists = not_if_exists ? [destination] : null;
}
return each(not_if_exists).run(function(not_if_exists, next) {
return fs.exists(ssh, not_if_exists, function(err, exists) {
if (exists) {
return skip();
} else {
return next();
}
});
}).then(succeed);
},
should_exist: function(options, skip, succeed) {
return each(options.should_exist).run(function(should_exist, next) {
return fs.exists(options.ssh, should_exist, function(err, exists) {
if (exists) {
return next();
} else {
return next(Error("File does not exist: " + should_exist));
}
});
}).on('error', function(err) {
return skip(err);
}).on('end', succeed);
},
should_not_exist: function(options, skip, succeed) {
return each(options.should_not_exist).run(function(should_not_exist, next) {
return fs.exists(options.ssh, should_not_exist, function(err, exists) {
if (exists) {
return next(new Error("File does not exist: " + should_not_exist));
} else {
return next();
}
});
}).on('error', function(err) {
return skip(err);
}).on('end', function() {
return succeed();
});
},
all: function(options, failed, succeed) {
var i, keys, next;
if (!((options != null) && (typeof options === 'object' && !Array.isArray(options)))) {
return succeed();
}
keys = Object.keys(options);
i = 0;
next = function() {
var key;
key = keys[i++];
if (key == null) {
return succeed();
}
if (module.exports[key] == null) {
return next();
}
return module.exports[key](options, function(err) {
if (typeof options.log === "function") {
options.log("Mecano `" + key + "`: skipping action");
}
return failed(err);
}, next);
};
return next();
}
};
each = require('each');
misc = require('./index');
exec = require('ssh2-exec');
fs = require('ssh2-fs');
template = require('./template');