Skip to content

Commit 7c3e1fd

Browse files
committed
Specify remote endpoint while seeding config
Consider remote endpoint options. Assume config file is the last argument.
1 parent 4cc8775 commit 7c3e1fd

2 files changed

Lines changed: 53 additions & 17 deletions

File tree

readme.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ git2consul takes one or many git repositories and mirrors them into [Consul](htt
1515
#### Requirements / Caveats
1616

1717
* git2consul does most of its Git work by shelling out to git. Git must be installed and on your path.
18-
* git2consul does the rest of its work by calling Consul's REST API. A Consul agent must be running on localhost.
18+
* git2consul does the rest of its work by calling Consul's REST API.
1919
* git2consul requires write access to the KV store of its Consul agent.
2020
* git2consul has only been tested on Unix.
2121

@@ -42,18 +42,30 @@ The most minimalistic viable git2consul configuration mirrors a single git repo
4242
}
4343
```
4444

45-
Put that configuration in a file called `/tmp/git2consul.json`. From the git2consul directory, upload that JSON file into the KV as your git2consul config:
45+
Put that configuration in a file called `/tmp/git2consul.json`. From the git2consul directory, upload that JSON file into the local KV as your git2consul config:
4646

4747
```
4848
node utils/config_seeder.js /tmp/git2consul.json
4949
```
5050

51+
or for remote Consul endpoint:
52+
53+
```
54+
node utils/config_seeder.js --endpoint remote.consul.host --port 80 /tmp/git2consul.json
55+
```
56+
5157
Start git2consul:
5258

5359
```
5460
node .
5561
```
5662

63+
or for remote Consul endpoint:
64+
65+
```
66+
node . --endpoint remote.consul.host --port 80
67+
```
68+
5769
git2consul will now poll the "dev" branch of the "git2consul_data.git" repo once per minute. On first run, it will mirror the 3 files into your Consul K/V with keys:
5870

5971
```

utils/config_seeder.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
var fs = require('fs');
22

3-
var consul = require('consul')();
3+
/**
4+
* First, check if there is a command line override for the consul endpoint.
5+
* If so, use it to seed the config.
6+
*/
7+
8+
var endpoint = "127.0.0.1";
9+
var port = 8500;
10+
var secure = false;
11+
for (var i=2; i<process.argv.length; ++i) {
12+
if(process.argv[i] === '-s' || process.argv[i] === '--secure') secure = true;
13+
if(process.argv[i] === '-e' || process.argv[i] === '--endpoint') {
14+
if(i+1 >= process.argv.length) {
15+
logger.error("No endpoint provided with --endpoint option");
16+
process.exit(3);
17+
}
18+
endpoint = process.argv[i+1];
19+
}
20+
if(process.argv[i] === '-p' || process.argv[i] === '--port') {
21+
if(i+1 >= process.argv.length) {
22+
logger.error("No port provided with --port option");
23+
process.exit(3);
24+
}
25+
port = process.argv[i+1];
26+
}
27+
}
28+
29+
var consul = require('consul')({'host': endpoint, 'port': port, 'secure': secure});
430

531
var _ = require('underscore');
632

@@ -26,21 +52,19 @@ exports.setConfig = function(path, value, cb) {
2652
consul.kv.set(params, cb);
2753
};
2854

29-
if (process.argv.length === 3) {
30-
var config_file = process.argv[2];
31-
32-
console.log('Adding %s as consul config', config_file);
55+
var config_file = process.argv[process.argv.length-1];
3356

34-
var config = fs.readFileSync(config_file, {'encoding':'utf8'});
57+
console.log('Adding %s as consul config', config_file);
3558

36-
try {
37-
JSON.parse(config);
38-
} catch(e) {
39-
console.error('config_file is not valid JSON');
40-
process.exit(1);
41-
}
59+
var config = fs.readFileSync(config_file, {'encoding':'utf8'});
4260

43-
exports.setConfig(config, function(err) {
44-
if (err) return console.error("Failed to write config: %s", err);
45-
});
61+
try {
62+
JSON.parse(config);
63+
} catch(e) {
64+
console.error('config_file is not valid JSON');
65+
process.exit(1);
4666
}
67+
68+
exports.setConfig(config, function(err) {
69+
if (err) return console.error("Failed to write config: %s", err);
70+
});

0 commit comments

Comments
 (0)