-
Notifications
You must be signed in to change notification settings - Fork 164
/
config_reader.js
58 lines (47 loc) · 1.43 KB
/
config_reader.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
var consul = require('consul')({'host': global.endpoint, 'port': global.port, 'secure':global.secure});
var _ = require('underscore');
var logger = require('./logging.js');
/* istanbul ignore next */
/**
* Grab the meta config to bootstrap git2consul.
*/
exports.read = function(params, cb) {
if (!cb) {
cb = params;
params = {'key': 'git2consul/config'};
}
if (process.env.TOKEN) {
params = _.extend(params, {'token': process.env.TOKEN})
}
consul.kv.get(params, function(err, item) {
if (err) return cb(err);
try {
var config = JSON.parse(item.Value);
} catch(e) {
return cb('Config value is not valid JSON: ' + require('util').inspect(item));
}
cb(null, config, item);
});
};
/* istanbul ignore next */
/**
* Wait for the consul config KV to change
*/
exports.wait = function(cb) {
exports.read(function(err, config, item) {
if (err) return cb(err);
var modify_index = item.ModifyIndex;
logger.info("Current config index is %s, waiting for changes.", modify_index);
var wait_for_change = function() {
exports.read({'key': 'git2consul/config', index: item.ModifyIndex}, function(err, config, item) {
if (err) return cb(err);
if (modify_index !== item.ModifyIndex) {
logger.warn("Config changed.");
return cb(null);
}
process.nextTick(wait_for_change);
});
};
process.nextTick(wait_for_change);
});
};