-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreplicator.js
148 lines (108 loc) · 3.29 KB
/
replicator.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
/*
* 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 path = require('path');
var fs = require('fs');
var nopt = require('nopt');
var bunyan = require('bunyan');
var Replicator = require('./lib/ufds-replicator');
var LOG = bunyan.createLogger({
name: 'ufds-replicator',
stream: process.stdout,
serializers: bunyan.stdSerializers
});
var OPTS = {
'file': String,
'ufdsFile': String,
'help': Boolean
};
var SHORT_OPTS = {
'f': ['--file'],
'u': ['--ufdsFile'],
'h': ['--help']
};
function usage(code, message) {
var _opts = '', msg;
Object.keys(SHORT_OPTS).forEach(function (k) {
var longOpt = SHORT_OPTS[k][0].replace('--', ''),
type = OPTS[longOpt].name || 'string';
if (type && type === 'boolean') {
type = '';
}
type = type.toLowerCase();
_opts += ' [--' + longOpt + ' ' + type + ']';
});
msg = (message ? message + '\n' : '') +
'usage: ' + path.basename(process.argv[1]) + _opts;
console.error(msg);
process.exit(code);
}
function errorAndExit(err, message) {
LOG.fatal({err: err}, message);
process.exit(1);
}
function processConfig() {
var _config,
parsed = nopt(OPTS, SHORT_OPTS, process.argv, 2),
file = parsed.file || path.join(__dirname, 'etc/replicator.json'),
ufdsFile = parsed.ufdsFile || path.join(__dirname, 'etc/config.json');
if (parsed.help) {
usage(0);
}
LOG.info({file: file}, 'Processing configuration file');
try {
_config = JSON.parse(fs.readFileSync(file, 'utf8'));
} catch (e) {
console.error('Unable to parse configuration file: ' + e.message);
process.exit(1);
}
try {
var ufdsConfigObj = JSON.parse(fs.readFileSync(ufdsFile, 'utf8'));
if (ufdsConfigObj.moray.version === undefined) {
console.error('Unable to find local ufds version.');
process.exit(1);
}
var localVersion = parseInt(ufdsConfigObj.moray.version, 10);
LOG.info({
version: localVersion
}, 'found local ufds version');
_config.localUfdsVersion = localVersion;
} catch (e) {
console.error('Unable to parse ufds configuration file: ' + e.message);
process.exit(1);
}
LOG.level(_config.logLevel || 'info');
LOG.debug('config processed: %j', _config);
_config.log = LOG;
return _config;
}
function main() {
var config = processConfig();
var rep;
rep = new Replicator(config);
rep.init();
rep.once('started', function () {
LOG.info('Replicator has started!');
});
rep.on('caughtup', function (id, cn) {
LOG.info('Replicator %d has caught up with UFDS at changenumber %s',
id, cn);
});
rep.once('stopped', function () {
LOG.info('Replicator has stopped!');
process.exit(0);
});
process.on('SIGINT', function () {
rep.stop();
});
}
process.on('uncaughtException', function (err) {
console.log('UncaughtException happened.');
LOG.fatal({err: err}, 'uncaughtException');
});
main();