-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.js
More file actions
76 lines (72 loc) · 2.25 KB
/
docker.js
File metadata and controls
76 lines (72 loc) · 2.25 KB
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
var dockerode = require('dockerode');
var docker = new dockerode({socketPath: '/var/run/docker.sock'});
var haproxy = require('./HAProxy/haproxy.js');
const image = "dynamic_node";
var haproxyid;
module.exports = {
rescale: function (nodes, callback) {
console.log(nodes);
docker.listContainers(function (err, containers) {
var runningContainers = [];
containers.forEach(function (containerInfo) {
if ((containerInfo.Image == image) && (containerInfo.State == "running")) {
runningContainers.push(containerInfo);
} else if (containerInfo.Image == "haproxy") {
haproxyid = containerInfo.Id;
}
});
// runningContainers = X
if (runningContainers.length == nodes) {
// Do Nothing
callback();
} else if (runningContainers.length < nodes) {
// Initialise More Nodes
var newNodes = nodes - runningContainers.length;
for (var n = 0; n < newNodes; n++) {
(function (n) {
// Initialise New Node
docker.createContainer({Image: image}, function (err, container) {
container.start(function (err, data) {
// Container Started
container.inspect(function (err, data) {
haproxy.addNode(data.Id, data.NetworkSettings.IPAddress, function () {
if (n == rmNodes - 1) {
docker.getContainer(haproxyid).kill({signal: 'HUP'}, function () {
console.log('reloaded');
});
}
});
});
});
});
})(n);
}
} else if (runningContainers.length > nodes) {
// Remove Nodes
var rmNodes = runningContainers.length - nodes;
for (var n = 0; n < rmNodes; n++) {
(function (n) {
// Remove Node
var container = docker.getContainer(runningContainers[n].Id);
container.inspect(function (err, data) {
haproxy.removeNode(runningContainers[n].Id, data.NetworkSettings.IPAddress, function (success) {
if (success == true) {
container.stop(function () {
container.remove(function () {
console.log('removed');
});
});
}
if (n == rmNodes - 1) {
docker.getContainer(haproxyid).kill({signal: 'HUP'}, function () {
console.log('reloaded');
});
}
});
});
})(n);
}
}
});
}
};