Move server-related Aphlict options to a configuration file

Summary: Ref T10697. This isn't everything but starts generalizing options and moving us toward a cluster-ready state of affairs.

Test Plan: Started server in various configurations, hit most (all?) of the error cases with bad configs, sent test notifications.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T10697

Differential Revision: https://secure.phabricator.com/D15701
This commit is contained in:
epriestley
2016-04-13 11:03:42 -07:00
parent e32ce529d7
commit c84dee522b
8 changed files with 273 additions and 107 deletions

View File

@@ -7,15 +7,10 @@ var util = require('util');
var fs = require('fs');
function parse_command_line_arguments(argv) {
var config = {
'client-port': 22280,
'admin-port': 22281,
'client-host': '0.0.0.0',
'admin-host': '127.0.0.1',
var args = {
log: '/var/log/aphlict.log',
'ssl-key': null,
'ssl-cert': null,
test: false
test: false,
config: null
};
for (var ii = 2; ii < argv.length; ii++) {
@@ -24,16 +19,18 @@ function parse_command_line_arguments(argv) {
if (!matches) {
throw new Error('Unknown argument "' + arg + '"!');
}
if (!(matches[1] in config)) {
if (!(matches[1] in args)) {
throw new Error('Unknown argument "' + matches[1] + '"!');
}
config[matches[1]] = matches[2];
args[matches[1]] = matches[2];
}
config['client-port'] = parseInt(config['client-port'], 10);
config['admin-port'] = parseInt(config['admin-port'], 10);
return args;
}
return config;
function parse_config(args) {
var data = fs.readFileSync(args.config);
return JSON.parse(data);
}
require('./lib/AphlictLog');
@@ -41,7 +38,8 @@ require('./lib/AphlictLog');
var debug = new JX.AphlictLog()
.addConsole(console);
var config = parse_command_line_arguments(process.argv);
var args = parse_command_line_arguments(process.argv);
var config = parse_config(args);
function set_exit_code(code) {
process.on('exit', function() {
@@ -51,7 +49,7 @@ function set_exit_code(code) {
process.on('uncaughtException', function(err) {
var context = null;
if (err.code == 'EACCES' && err.path == config.log) {
if (err.code == 'EACCES' && err.path == args.log) {
context = util.format(
'Unable to open logfile ("%s"). Check that permissions are set ' +
'correctly.',
@@ -71,8 +69,8 @@ process.on('uncaughtException', function(err) {
});
// Add the logfile so we'll fail if we can't write to it.
if (config.log) {
debug.addLog(config.log);
if (args.log) {
debug.addLog(args.log);
}
try {
@@ -90,51 +88,37 @@ try {
require('./lib/AphlictAdminServer');
require('./lib/AphlictClientServer');
var ssl_config = {
enabled: (config['ssl-key'] || config['ssl-cert'])
};
// Load the SSL certificates (if any were provided) now, so that runs with
// `--test` will see any errors.
if (ssl_config.enabled) {
ssl_config.key = fs.readFileSync(config['ssl-key']);
ssl_config.cert = fs.readFileSync(config['ssl-cert']);
} else {
ssl_config.key = null;
ssl_config.cert = null;
}
var ii;
var servers = [];
for (ii = 0; ii < config.servers.length; ii++) {
var spec = config.servers[ii];
servers.push({
type: 'client',
port: config['client-port'],
listen: config['client-host'],
'ssl.key': ssl_config.key,
'ssl.certificate': ssl_config.cert
});
spec.listen = spec.listen || '0.0.0.0';
servers.push({
type: 'admin',
port: config['admin-port'],
listen: config['admin-host'],
'ssl.key': null,
'ssl.cert': null
});
if (spec['ssl.key']) {
spec['ssl.key'] = fs.readFileSync(spec['ssl.key']);
}
if (spec['ssl.cert']){
spec['ssl.cert'] = fs.readFileSync(spec['ssl.cert']);
}
servers.push(spec);
}
// If we're just doing a configuration test, exit here before starting any
// servers.
if (config.test) {
if (args.test) {
debug.log('Configuration test OK.');
set_exit_code(0);
return;
}
debug.log('Starting servers (service PID %d).', process.pid);
var aphlict_servers = [];
var aphlict_clients = [];
var aphlict_admins = [];
var ii;
for (ii = 0; ii < servers.length; ii++) {
var server = servers[ii];
var is_client = (server.type == 'client');
@@ -161,6 +145,12 @@ for (ii = 0; ii < servers.length; ii++) {
aphlict_server.setLogger(debug);
aphlict_server.listen(server.port, server.listen);
debug.log(
'Started %s server (Port %d, %s).',
server.type,
server.port,
server['ssl.key'] ? 'With SSL' : 'No SSL');
aphlict_servers.push(aphlict_server);
if (is_client) {
@@ -174,5 +164,3 @@ for (ii = 0; ii < aphlict_admins.length; ii++) {
var admin_server = aphlict_admins[ii];
admin_server.setClientServers(aphlict_clients);
}
debug.log('Started Server (PID %d)', process.pid);