-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (78 loc) · 2.84 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var DockerEvents = require('docker-events'),
Docker = require('dockerode'),
exec = require('child_process').exec,
util = require('util'),
config = require('./config');
var docker = config.dockerOpts ? new Docker(config.dockerOpts) : new Docker();
var domain = (function() {
var d = '';
config.domain.split('.').forEach(function(part) {
d = part + '/' + d;
});
return '/skydns/' + d;
})();
console.log('Domain is ' + domain);
(function () {
console.log('Update etcd for running docker container');
docker.listContainers(function (err, containers) {
containers.forEach(function (containerInfo) {
docker.getContainer(containerInfo.Id).inspect(function(err, data) {
var name = /.*\/(.*)$/.exec(data.Name)[1],
ip = data.NetworkSettings.IPAddress,
ipString = ip ? util.format('\'{"host":"%s"}\'', ip) : '',
cmd = util.format('etcdctl %s %s %s',
'set',
domain + name,
ipString);
console.log('The command to be processed: ' + cmd);
exec(cmd, function(err, stdout, stderr) {
if (err) {
console.log(stderr); // TODO check if it is necessary to tog full error
}
});
});
});
})
}());
var emitter = new DockerEvents({
docker: docker
});
var updateSkydns = function(id, isSet) {
try{
docker.getContainer(id)
.inspect(function(err, data) {
console.log(id);
if(err){
console.log(err);
}else{
var name = /.*\/(.*)$/.exec(data.Name)[1],
ip = data.NetworkSettings.IPAddress,
ipString = ip ? util.format('\'{"host":"%s"}\'', ip) : '',
cmd = util.format('etcdctl %s %s %s',
isSet ? 'set' : 'rm',
domain + name,
ipString);
if (isSet && ! ip) {
console.log('No IP is set to %s. Key adding will be cancelled.', name);
return;
}
console.log('The command to be processed: ' + cmd);
exec(cmd, function(err, stdout, stderr) {
if (err) {
console.log(stderr); // TODO check if it is necessary to tog full error
}
});
}
});
}
catch (e){
console.log(e);
}
}
emitter.on("start", function(message) {
updateSkydns(message.id, true);
});
emitter.on("die", function(message) {
updateSkydns(message.id, false);
});
emitter.start();