-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
199 lines (163 loc) · 4.83 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 2019 Joyent, Inc.
*/
var assert = require('assert-plus');
var bunyan = require('bunyan');
var clone = require('clone');
var fs = require('fs');
var jsprim = require('jsprim');
var getopt = require('posix-getopt');
var VError = require('verror');
var extend = require('xtend');
var app = require('./lib');
var MIN_PORT = 1;
var MAX_PORT = 65535;
var DEFAULTS = {
file: process.cwd() + '/etc/config.json',
port: 2020,
monitorPort: 3020,
statusPort: 4020,
bindip: '0.0.0.0'
};
var NAME = 'buckets-mdplacement';
var LOG_SERIALIZERS = {
err: bunyan.stdSerializers.err
};
var LOG = bunyan.createLogger({
name: NAME,
level: (process.env.LOG_LEVEL || 'info'),
stream: process.stderr,
serializers: LOG_SERIALIZERS
});
var LOG_LEVEL_OVERRIDE = false;
function setupLogger(config) {
var cfg_b = config.bunyan;
assert.object(cfg_b, 'config.bunyan');
assert.optionalString(cfg_b.level, 'config.bunyan.level');
if (cfg_b.level && !LOG_LEVEL_OVERRIDE) {
if (bunyan.resolveLevel(cfg_b.level))
LOG.level(cfg_b.level);
}
/*
* If the configured logging level is at or below DEBUG then enable
* source logging.
*/
if (LOG.level() <= bunyan.DEBUG) {
LOG = LOG.child({src: true});
}
}
function parsePort(str) {
var port = jsprim.parseInteger(str);
if (port instanceof Error) {
LOG.fatal({ port: str }, 'Invalid port - failed to parse');
throw new VError(port, 'Invalid port %j', str);
}
if (port < MIN_PORT || port > MAX_PORT) {
LOG.fatal({ port: str }, 'Invalid port - out of range');
throw new VError(port, 'Invalid port %j, should be in range %d-%d',
str, MIN_PORT, MAX_PORT);
}
return port;
}
function parseOptions() {
var option;
var opts = {};
var parser = new getopt.BasicParser('cvf:r:p:k:s:', process.argv);
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'c':
opts.cover = true;
break;
case 'f':
opts.file = option.optarg;
break;
case 'r':
opts.ringLocation = option.optarg;
break;
case 'p':
opts.port = parsePort(option.optarg);
break;
case 'k':
opts.monitorPort = parsePort(option.optarg);
break;
case 's':
opts.statusPort = parsePort(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:
process.exit(1);
break;
}
}
if (!opts.file) {
LOG.fatal({ opts: opts }, 'No config file specified.');
process.exit(1);
}
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');
process.exit(1);
}
cfg.ringLocation = options.ringLocation;
return (extend({}, clone(DEFAULTS), cfg, options));
}
function run(options) {
assert.object(options);
var opts = clone(options);
opts.log = LOG;
opts.name = NAME;
app.createServer(opts, function (err, res) {
if (err) {
LOG.fatal(err, 'startup failed');
process.exit(1);
}
assert.object(res, 'res');
assert.object(res.dataDirector, 'res.dataDirector');
app.createStatusServer({
log: LOG.child({ component: 'statusServer' }),
dataDirector: res.dataDirector,
port: opts.statusPort
}, function (err2) {
if (err2) {
LOG.fatal(err2, 'status server startup failed');
process.exit(1);
}
});
});
}
(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);
});
}
})();