-
Notifications
You must be signed in to change notification settings - Fork 1
/
consul.js
47 lines (40 loc) · 1.21 KB
/
consul.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
'use strict'
var request = require('request')
var SWARM_PREFIX = 'swarm/docker/swarm/nodes/'
function Consul () {
this.CONSUL_HOST = process.env.CONSUL_HOST
if (!this.CONSUL_HOST) {
throw new Error('CONSUL_HOST must be defined')
}
}
Consul.prototype._makeRequest = function (url, cb) {
request.get(url, {}, function (err, res, body) {
if (err) { return cb(err) }
try {
body = JSON.parse(body)
} catch (parseErr) {
return cb(new Error('Parse Consul response error', { originalErr: parseErr }))
}
if (!Array.isArray(body)) {
return cb(new Error('Invalid Consul response'))
}
body = body.map(function (v) {
v.Value = v.Value ? (new Buffer(v.Value, 'base64')).toString('utf-8') : ''
return v
})
cb(null, body)
})
}
Consul.prototype._getRecursiveKV = function (prefix, cb) {
this._makeRequest('http://' + this.CONSUL_HOST + '/v1/kv/' + prefix + '?recurse=true', cb)
}
Consul.prototype.getSwarmNodes = function (cb) {
this._getRecursiveKV(SWARM_PREFIX, function (err, hosts) {
if (err) { return cb(err) }
hosts = hosts.map(function (p) {
return p.Key.substr(SWARM_PREFIX.length)
})
cb(null, hosts)
})
}
module.exports = Consul