forked from breser/git2consul
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_seeder.js
More file actions
70 lines (56 loc) · 1.91 KB
/
config_seeder.js
File metadata and controls
70 lines (56 loc) · 1.91 KB
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
var fs = require('fs');
/**
* First, check if there is a command line override for the consul endpoint.
* If so, use it to seed the config.
*/
var endpoint = "127.0.0.1";
var port = 8500;
var secure = false;
for (var i=2; i<process.argv.length; ++i) {
if(process.argv[i] === '-s' || process.argv[i] === '--secure') secure = true;
if(process.argv[i] === '-e' || process.argv[i] === '--endpoint') {
if(i+1 >= process.argv.length) {
logger.error("No endpoint provided with --endpoint option");
process.exit(3);
}
endpoint = process.argv[i+1];
}
if(process.argv[i] === '-p' || process.argv[i] === '--port') {
if(i+1 >= process.argv.length) {
logger.error("No port provided with --port option");
process.exit(3);
}
port = process.argv[i+1];
}
}
var consul = require('consul')({'host': endpoint, 'port': port, 'secure': secure});
var _ = require('underscore');
/**
* This utility adds keys from the top level of a .json config file to the /git2consul/ path of
* Consul. We use this to seed Consul's KV with the bootstrapping information used by git2consul.
*/
exports.setConfig = function(path, value, cb) {
if (!cb) {
cb = value;
value = path;
path = 'git2consul/config';
}
console.log('Adding config %s : %s', path, value);
var params = {'key': path, 'value': value};
if (process.env.TOKEN) {
params = _.extend(params, {'token': process.env.TOKEN})
}
consul.kv.set(params, cb);
};
var config_file = process.argv[process.argv.length-1];
console.log('Adding %s as consul config', config_file);
var config = fs.readFileSync(config_file, {'encoding':'utf8'});
try {
JSON.parse(config);
} catch(e) {
console.error('config_file is not valid JSON');
process.exit(1);
}
exports.setConfig(config, function(err) {
if (err) return console.error("Failed to write config: %s", err);
});