Created
November 23, 2014 08:42
-
-
Save asbjornenge/95a83ea3ffdf083336a2 to your computer and use it in GitHub Desktop.
Docker Rainbow-dns Polulator
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
#!/usr/bin/env node | |
var request = require('request') | |
var Docker = require('dockerode') | |
var swarm = Docker({host: 'http://docker.host', port: 4243}) | |
var dnshost = 'http://dns.host:8080' | |
var interval = 2000 | |
var loop = function () { | |
getContainers(function (currContainer) { | |
updateDns(currContainer) | |
}) | |
} | |
var getContainers = function (callback) { | |
var currContainers = {} | |
swarm.listContainers(function (err, containers) { | |
if (!containers) { console.error("ERROR: Unable to query containers"); return } | |
var calls = 0, evalEnd = function () { calls++; if (calls == containers.length) callback(currContainers) } | |
containers.forEach(function (container, index) { | |
var c = swarm.getContainer(container.Id) | |
c.inspect(function (err, data) { | |
currContainers[container.Id] = data | |
evalEnd(index) | |
}) | |
}) | |
}) | |
} | |
var updateDns = function (containers, callback) { | |
Object.keys(containers).forEach(function (id) { | |
var group = containers[id].Config.Image | |
if (group.indexOf('/') > 0) group = group.split('/')[1] | |
var name = containers[id].Name + '.' +group; | |
name = name.replace(':','.') | |
request({ | |
url : dnshost+name, | |
method : 'PUT', | |
json : true, | |
body : { A : [{address:containers[id].NetworkSettings.IPAddress}] }, | |
timeout : interval / 2 | |
}, function (err, res) { | |
if (err) { console.error('ERROR: Unable to update DNS', err.code); return } | |
if (res.statusCode != 200) console.log(res.statusCode) | |
}) | |
}) | |
} | |
loop() | |
setInterval(loop, interval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment