mecano
Version:
Common functions for system deployment.
66 lines (61 loc) • 3.46 kB
JavaScript
// Generated by CoffeeScript 1.9.1
var fs, path, string;
module.exports = function(options, callback) {
var cwd, destination, detach, do_destination, do_write;
if (!options.destination) {
throw Error("Required option 'destination'");
}
destination = options.destination;
if (options.name == null) {
options.name = path.basename(options.destination);
}
cwd = options.cwd;
if (options.cwd === true) {
cwd = 'cd $( dirname "${BASH_SOURCE}" )';
}
if (typeof cwd === 'string') {
cwd = "cd \"" + cwd + "\"";
}
detach = options.detach;
if (detach === true) {
detach = ' &';
}
do_destination = function() {
return fs.stat(options.ssh, destination, function(err, stat) {
if (err) {
return callback(err);
}
if (stat.isFile()) {
return do_write();
}
destination = path.join(destination, "" + (string.underscore(options.name)));
return do_write();
});
};
do_write = function() {
var err_file, out_file, pid_file;
pid_file = options.pid_file;
if (pid_file == null) {
pid_file = "/var/run/" + (path.basename(destination)) + ".pid";
}
out_file = options.pid_file;
if (out_file == null) {
out_file = "/var/log/" + (path.basename(destination)) + "/" + (path.basename(destination)) + ".out";
}
err_file = options.pid_file;
if (err_file == null) {
err_file = "/var/log/" + (path.basename(destination)) + "/" + (path.basename(destination)) + ".err";
}
return mecano.write({
content: "#!/bin/bash\n\n" + cwd + "\n\nname='" + options.name + "'\ncmd='" + options.cmd + "'\npid_file=\"" + pid_file + "\"\nout_file='" + out_file + "'\nerr_file='" + err_file + "'\n\nfunction start {\n if running; then\n echo \"$name already started\"\n else\n $cmd </dev/null >$out_file 2>$err_file " + detach + "\n echo $! > $pid_file\n echo \"$name started [`cat $pid_file`]\"\n fi\n}\n\nfunction stop {\n if running; then\n kill $(<$pid_file) 2>/dev/null\n echo \"$name stopped\"\n else\n echo \"$name already stopped\"\n fi\n}\n\nfunction kill {\n if running; then\n kill -9 $(<$pid_file) 2>/dev/null\n echo \"$name stopped\"\n else\n echo \"$name already stopped\"\n fi\n}\n\nfunction status {\n if running; then\n pid=`cat $pid_file`\n echo \"$name started [$pid]\"\n return 0\n else\n echo \"$name stopped\"\n return 3\n fi\n}\n\nfunction running {\n if [ -f $pid_file ]; then\n pid=`cat $pid_file`\n if kill -0 >/dev/null 2>&1 $pid; then\n return 0\n fi\n rm -f $pid_file\n fi\n return 1\n}\n\nfunction help {\n echo \"\"\n echo \"$(basename \"${BASH_SOURCE}\") [help|start|status|stop]\"\n echo \"\"\n echo \"Manage the $name service.\"\n echo \"\"\n echo \"help Print this message\"\n echo \"start Start the server\"\n echo \"status Report the server status\"\n echo \"stop Stop the server\"\n echo \"kill Force the server to stop\"\n echo \"\"\n}\n\nRETVAL=0\ncase \"$1\" in\n start)\n start\n ;;\n stop)\n stop\n ;;\n restart)\n start\n stop\n ;;\n status)\n status\n RETVAL=$?\n ;;\n *)\n help\n ;;\nesac\nexit $RETVAL",
destination: options.destination,
ssh: options.ssh,
log: options.log,
eof: true
}, callback);
};
return do_destination();
};
path = require('path');
fs = require('ssh2-fs');
string = require('./misc/string');