the-shepherd
Version:
Control a herd of wild processes.
143 lines (133 loc) • 4.88 kB
JavaScript
// Generated by CoffeeScript 2.5.1
(function() {
var $, Fs, Groups, Nginx, configFile, echo, exists, expandPath, int, nginxTemplate, saveConfig, trueFalse, verbose, warn;
({$, echo, warn, verbose} = require('../common'));
Fs = require('fs');
Nginx = require('../daemon/nginx');
({Groups} = require('../daemon/groups'));
({int, trueFalse} = require('../util/format'));
({saveConfig} = require('../util/config'));
({configFile, exists, nginxTemplate, expandPath} = require("../files"));
Object.assign(module.exports, {
options: [["--enable", "Generate a config file and automatically reload nginx when state changes."], ["--disable", "Don't generate files or reload nginx."], ["--file <file>", "Auto-generate an nginx file here using '.shep/nginx.template'."], ["--reload-cmd <cmd>", "What command to run in order to cause nginx to reload."], ["--reload-now", "Read nginx.template, write a fresh config file, and issue the reload cmd to nginx."], ["--group <name>", "Optional. If given, the following options apply:"], [" --port <n>", "Default 80 (or 443). 'server { listen <n>; }'", int], [" --name <hostname>", "Default is the group name. 'server { server_name <name>; }'"], [" --ssl_cert <path>", "Optional. If given, port defaults to 443."], [" --ssl_key <path>", "Optional. If given, port defaults to 443."], ["--list", "Show the current nginx configuration."]],
toMessage: function(cmd) {
return {
c: 'nginx',
f: cmd.file,
r: cmd['reload-cmd'],
k: cmd.keepalive,
d: trueFalse(cmd.disable),
e: trueFalse(cmd.enable),
l: cmd.list,
g: cmd.group,
p: cmd.port,
n: cmd.name,
sc: cmd.ssl_cert,
sk: cmd.ssl_key,
rn: trueFalse(cmd['reload-now'])
};
},
onMessage: function(msg, client, cb) {
var acted, bytes, group, ref, ref1, reply;
reply = function(m, ret) {
if (ret) {
if (client != null) {
client.write($.TNET.stringify(m));
}
if (typeof saveConfig === "function") {
saveConfig();
}
Nginx.reloadNginx();
}
if (typeof cb === "function") {
cb(!ret && m || null, ret);
}
return ret;
};
if (msg.rn) {
Nginx.sync(function(err, acted) {
if (acted) {
return reply("wrote nginx config", true);
} else {
return reply(`did not write config (${err != null ? err : 'none'})`, false);
}
});
return;
}
if (msg.g != null) {
acted = false;
if (!Groups.has(msg.g)) {
return reply("No such group.", false);
}
group = Groups.get(msg.g);
if (msg.p != null) {
acted = true;
group.public_port = int(msg.p);
}
if (msg.n != null) {
acted = true;
group.public_name = msg.n;
}
if (msg.sc != null) {
acted = true;
if (!exists(msg.sc)) {
warn(`ssl_cert file '${msg.sc}' does not exist.`, false);
}
group.ssl_cert = msg.sc;
}
if (msg.sk != null) {
acted = true;
if (!exists(msg.sk)) {
warn(`ssl_key file '${msg.sk}' does not exist.`, false);
}
group.ssl_key = msg.sk;
}
if (acted) {
return reply("Group updated.", true);
} else {
return reply("No valid options specified.", false);
}
}
if (msg.l != null) {
return reply(Nginx.toConfig(), false);
}
acted = 0;
if ((ref = msg.f) != null ? ref.length : void 0) {
++acted && Nginx.setFile(msg.f);
}
if ((ref1 = msg.r) != null ? ref1.length : void 0) {
++acted && Nginx.setReload(msg.r);
}
if (msg.e) {
++acted && Nginx.setDisabled(false);
if (!exists(nginxTemplate)) {
bytes = Nginx.defaults.templateText;
Fs.writeFile(expandPath(nginxTemplate), bytes, (err, written) => {
if (err) {
return warn(err);
} else if (written !== bytes.length) {
return warn(`Only wrote ${written} out of ${bytes.length} to ${nginxTemplate}`);
} else {
return echo(`Wrote default content to ${nginxTemplate}`);
}
});
}
}
if (msg.d) {
++acted && Nginx.setDisabled(true);
}
if (acted) {
if (typeof saveConfig === "function") {
saveConfig();
}
return reply("nginx configuration updated.", true);
} else {
return reply("nginx - no actions requested.", false);
}
},
onResponse: function(resp, socket) {
console.log(resp);
return socket.end();
}
});
}).call(this);