From 7c3e1fd271b20cab8f164b73ee8baedc5fafb31d Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 16 Jan 2016 13:35:09 +0200 Subject: [PATCH] Specify remote endpoint while seeding config Consider remote endpoint options. Assume config file is the last argument. --- readme.md | 16 +++++++++++-- utils/config_seeder.js | 54 ++++++++++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/readme.md b/readme.md index 6b9f87f..2573aae 100644 --- a/readme.md +++ b/readme.md @@ -15,7 +15,7 @@ git2consul takes one or many git repositories and mirrors them into [Consul](htt #### Requirements / Caveats * git2consul does most of its Git work by shelling out to git. Git must be installed and on your path. -* git2consul does the rest of its work by calling Consul's REST API. A Consul agent must be running on localhost. +* git2consul does the rest of its work by calling Consul's REST API. * git2consul requires write access to the KV store of its Consul agent. * git2consul has only been tested on Unix. @@ -42,18 +42,30 @@ The most minimalistic viable git2consul configuration mirrors a single git repo } ``` -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: +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: ``` node utils/config_seeder.js /tmp/git2consul.json ``` +or for remote Consul endpoint: + +``` +node utils/config_seeder.js --endpoint remote.consul.host --port 80 /tmp/git2consul.json +``` + Start git2consul: ``` node . ``` +or for remote Consul endpoint: + +``` +node . --endpoint remote.consul.host --port 80 +``` + 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: ``` diff --git a/utils/config_seeder.js b/utils/config_seeder.js index 448f00b..33acab3 100644 --- a/utils/config_seeder.js +++ b/utils/config_seeder.js @@ -1,6 +1,32 @@ var fs = require('fs'); -var consul = require('consul')(); +/** + * 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) { + 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'); @@ -26,21 +52,19 @@ exports.setConfig = function(path, value, cb) { consul.kv.set(params, cb); }; -if (process.argv.length === 3) { - var config_file = process.argv[2]; - - console.log('Adding %s as consul config', config_file); +var config_file = process.argv[process.argv.length-1]; - var config = fs.readFileSync(config_file, {'encoding':'utf8'}); +console.log('Adding %s as consul config', config_file); - try { - JSON.parse(config); - } catch(e) { - console.error('config_file is not valid JSON'); - process.exit(1); - } +var config = fs.readFileSync(config_file, {'encoding':'utf8'}); - exports.setConfig(config, function(err) { - if (err) return console.error("Failed to write config: %s", err); - }); +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); +});