forked from TritonDataCenter/moray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
199 lines (157 loc) · 4.74 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 2014, Joyent, Inc.
*/
var fs = require('fs');
var os = require('os');
var assert = require('assert-plus');
var bsyslog = require('bunyan-syslog');
var bunyan = require('bunyan');
var clone = require('clone');
var extend = require('xtend');
var getopt = require('posix-getopt');
var panic = require('panic');
var VError = require('verror').VError;
var app = require('./lib');
///--- Globals
var DEFAULTS = {
file: process.cwd() + '/etc/config.json',
port: 2020,
bindip: '0.0.0.0'
};
var NAME = 'moray';
var LOG_SERIALIZERS = {
err: bunyan.stdSerializers.err,
pg: function (client) {
return (client ? client._moray_id : undefined);
}
};
//We'll replace this with the syslog later, if applicable
var LOG = bunyan.createLogger({
name: NAME,
level: (process.env.LOG_LEVEL || 'info'),
stream: process.stderr,
serializers: LOG_SERIALIZERS
});
var LOG_LEVEL_OVERRIDE = false;
///--- Internal Functions
function setupLogger(config) {
var cfg_b = config.bunyan;
assert.object(cfg_b, 'config.bunyan');
assert.optionalString(cfg_b.level, 'config.bunyan.level');
assert.optionalObject(cfg_b.syslog, 'config.bunyan.syslog');
var level = LOG.level();
if (cfg_b.syslog && !LOG_LEVEL_OVERRIDE) {
assert.string(cfg_b.syslog.facility,
'config.bunyan.syslog.facility');
assert.string(cfg_b.syslog.type, 'config.bunyan.syslog.type');
var facility = bsyslog.facility[cfg_b.syslog.facility];
LOG = bunyan.createLogger({
name: NAME,
serializers: LOG_SERIALIZERS,
streams: [ {
level: level,
type: 'raw',
stream: bsyslog.createBunyanStream({
name: NAME,
facility: facility,
host: cfg_b.syslog.host,
port: cfg_b.syslog.port,
type: cfg_b.syslog.type
})
} ]
});
}
if (cfg_b.level && !LOG_LEVEL_OVERRIDE) {
if (bunyan.resolveLevel(cfg_b.level))
LOG.level(cfg_b.level);
}
}
function parseOptions() {
var option;
var opts = {};
var parser = new getopt.BasicParser('cvf:p:', process.argv);
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'c':
opts.cover = true;
break;
case 'f':
opts.file = option.optarg;
break;
case 'p':
opts.port = parseInt(option.optarg, 10);
if (isNaN(opts.port)) {
LOG.fatal({
port: option.optarg
}, 'Invalid port.');
throw new Error('Invalid port: ' +
option.optarg);
}
break;
case 'v':
// Allows us to set -vvv -> this little hackery
// just ensures that we're never < TRACE
LOG_LEVEL_OVERRIDE = true;
LOG.level(Math.max(bunyan.TRACE, (LOG.level() - 10)));
if (LOG.level() <= bunyan.DEBUG)
LOG = LOG.child({src: true});
break;
default:
throw new Error('Invalid option: ' + option.option);
}
}
if (!opts.file) {
LOG.fatal({ opts: opts }, 'No config file specified.');
throw new Error('No config file specified');
}
return (opts);
}
function readConfig(options) {
assert.object(options);
var cfg;
try {
cfg = JSON.parse(fs.readFileSync(options.file, 'utf8'));
} catch (e) {
LOG.fatal({
err: e,
file: options.file
}, 'Unable to read/parse configuration file');
throw new VError(e,
'Unable to parse configuration file %s',
options.file);
}
return (extend({}, clone(DEFAULTS), cfg, options));
}
function run(options) {
assert.object(options);
var opts = clone(options);
opts.log = LOG;
opts.name = NAME;
var server = app.createServer(opts);
server.listen();
}
///--- Mainline
(function main() {
var options = parseOptions();
var config = readConfig(options);
LOG.debug({
config: config,
options: options
}, 'main: options and config parsed');
setupLogger(config);
run(config);
if (options.cover) {
process.on('SIGUSR2', function () {
process.exit(0);
});
}
panic.enablePanicOnCrash({
'skipDump': true,
'abortOnPanic': true
});
})();