-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7acd1f
commit da033dc
Showing
7 changed files
with
172 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
{ | ||
"env": { | ||
"node": true | ||
}, | ||
"extends": "leisurelink", | ||
"rules": { | ||
"no-console": 0, | ||
"complexity": 0 | ||
} | ||
"extends": "leisurelink", | ||
"env": { | ||
"node": true, | ||
"es6": true | ||
}, | ||
"rules": { | ||
"no-console": 0, | ||
"complexity": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const consul = require('consul'); | ||
const _ = require('lodash'); | ||
const Promise = require('bluebird'); | ||
const readFileSync = require('fs').readFileSync; | ||
const log = require('./logger'); | ||
|
||
module.exports = (program) => { | ||
const certificates = _.map(program.ca, readFileSync); | ||
var config = { | ||
host: program.host || process.env.CONSUL_HOST || 'consul.service.consul', | ||
port: program.port || process.env.CONSUL_PORT || 8500, | ||
secure: program.secure || process.env.CONSUL_SECURE === 'true', | ||
ca: certificates | ||
}; | ||
log.debug('Config: ', config); | ||
|
||
let client = consul(config); | ||
Promise.promisifyAll(client.kv); | ||
return client; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
const SkinnyLoggins = require('@leisurelink/skinny-loggins'); | ||
const logger = new SkinnyLoggins(); | ||
|
||
logger.transports.console.timestamp = false; | ||
logger.transports.console.showLevel = false; | ||
|
||
module.exports = logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict'; | ||
|
||
const jptr = require('json-ptr'); | ||
const Promise = require('bluebird'); | ||
const readFile = Promise.promisify(require('fs').readFile); | ||
const _ = require('lodash'); | ||
const log = require('./logger'); | ||
|
||
module.exports = (client, files) => { | ||
let workflow = { stats: { put: 0, deleted: 0 }, config: null }; | ||
let firstFragmentId; | ||
const readFragments = (fileName) => { | ||
return readFile(fileName, 'utf8') | ||
.then(JSON.parse) | ||
.then((contents) => { | ||
log.debug(`Read ${fileName}:`); | ||
log.debug(contents); | ||
let keys = _.keys(contents); | ||
if (keys.length !== 1) { | ||
throw new Error('Each configuration file must have a single top-level node identifying the service. "' + fileName + '" has ' + keys.length + ' top-level nodes.'); | ||
} | ||
let pointers = jptr.list(contents); | ||
if (firstFragmentId) { | ||
if (pointers[1].pointer !== firstFragmentId) { | ||
throw new Error('Each file must have the same top-level node. Expected "' + fileName + '" to have top-level node "' + firstFragmentId.substring(1) + '", but it has "' + pointers[1].pointer.substring(1) + '".'); | ||
} | ||
} else { | ||
firstFragmentId = pointers[1].pointer; | ||
} | ||
return pointers; | ||
}); | ||
}; | ||
|
||
const readFiles = () => { | ||
return Promise.map(files, readFragments); | ||
}; | ||
|
||
let flattened, prefix; | ||
const flatten = (contents) => { | ||
flattened = _.flatten(contents); | ||
prefix = flattened[1].pointer.substring(1) + '/'; | ||
return flattened; | ||
}; | ||
|
||
const reduce = (flattened) => { | ||
let reduced = _.reduce(_.filter(flattened, (x) => { | ||
return _.isString(x.value) || _.isFinite(x.value); | ||
}), function (acc, item) { | ||
acc[item.pointer.substring(1)] = item.value; | ||
return acc; | ||
}, {}); | ||
workflow.config = reduced; | ||
return reduced; | ||
}; | ||
|
||
let existing; | ||
const getExistingKeys = () => { | ||
log.debug('Getting all keys for ' + prefix); | ||
return client.kv.keysAsync(prefix) | ||
.catch((err) => { | ||
if (err.message === 'not found') { | ||
return []; | ||
} | ||
throw err; | ||
}) | ||
.then((keys) => { | ||
log.debug('Keys: ', keys); | ||
existing = keys; | ||
}); | ||
}; | ||
|
||
const put = () => { | ||
return Promise.all(_.map(workflow.config, (value, key) => { | ||
workflow.stats.put++; | ||
existing = _.filter(existing, (item) => { | ||
return item !== key; | ||
}); | ||
log.debug(`Setting "${key}" to "${value}"`); | ||
return client.kv.setAsync({ | ||
key: key, | ||
value: '' + value | ||
}); | ||
})); | ||
}; | ||
|
||
const del = () => { | ||
return Promise.map(existing, function(key) { | ||
workflow.stats.deleted++; | ||
log.debug(`Deleting "${key}"`); | ||
return client.kv.delAsync(key); | ||
}); | ||
}; | ||
|
||
workflow.exec = () => { | ||
return readFiles() | ||
.then(flatten) | ||
.then(reduce) | ||
.then(getExistingKeys) | ||
.then(put) | ||
.then(del); | ||
}; | ||
|
||
return workflow; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters