-
Notifications
You must be signed in to change notification settings - Fork 29
/
backupserver.js
126 lines (104 loc) · 3.07 KB
/
backupserver.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
/*
* 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.
*/
/*
*
* _.---.._
* _ _.-' \ \ ''-.
* .' '-,_.-' / / / '''.
* ( _ o :
* '._ .-' '-._ \ \- ---]
* '-.___.-') )..-'
* (_/
*
*/
var assert = require('assert-plus');
var bunyan = require('bunyan');
var extend = require('xtend');
var fs = require('fs');
var getopt = require('posix-getopt');
var BackupServer = require('./lib/backupServer');
var BackupSender = require('./lib/backupSender');
/*
* globals
*/
var NAME = 'manatee-backupserver';
var LOG = bunyan.createLogger({
level: (process.env.LOG_LEVEL || 'info'),
name: NAME,
serializers: {
err: bunyan.stdSerializers.err
},
// always turn source to true, manatee isn't in the data path
src: true
});
var LOG_LEVEL_OVERRIDE = false;
/*
* private functions
*/
function parseOptions() {
var option;
var opts = {};
var parser = new getopt.BasicParser('vf:(file)', process.argv);
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'f':
opts.file = 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:
LOG.fatal('Unsupported option: ', option.option);
process.abort();
break;
}
}
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.abort();
}
return (extend({}, cfg, options));
}
/*
* mainline
*/
(function main() {
var _config;
var _options = parseOptions();
LOG.debug({options: _options}, 'command line options parsed');
_config = readConfig(_options);
LOG.debug({config: _config}, 'configuration loaded');
if (_config.logLevel && !LOG_LEVEL_OVERRIDE) {
if (bunyan.resolveLevel(_config.logLevel)) {
LOG.level(_config.logLevel);
}
}
_config.backupServerCfg.log = LOG;
_config.backupSenderCfg.log = LOG;
var backupServer = BackupServer.start(_config.backupServerCfg);
// server and sender share the same queue
_config.backupSenderCfg.queue = backupServer.getQueue();
BackupSender.start(_config.backupSenderCfg);
LOG.info('backupserver started');
})();