-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.js
129 lines (96 loc) · 3.27 KB
/
proxy.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
'use strict';
var _ = require('lodash');
var traverse = require('traverse');
/**
* proxy injector
*
* for each target determine if proxies are enabled. If this is the case inject proxy containers into
* each machine. A machine is defined as a container that holds child docker or process containers.
* This is done pre-compilation to the objects in the topology tree. In addition a proxy definition is
* added to the list of container definitions
*/
module.exports = function() {
/**
* nscale proxy definition, this will execute a container that holds the
*/
var proxyCDef = {
shared$: {
type: 'docker',
configPath: '/etc/haproxy/haproxy.cfg',
hup: 'killall haproxy',
execute: {
args: '--net=host -d',
}
}
};
var injectCDef = function injectCDef(defs) {
defs.push({__proxy: proxyCDef});
};
var isExecutableContainer = function(name, defs) {
var result = false;
_.each(defs, function(defSet) {
_.each(defSet[name], function(ds) {
if (ds.type && (ds.type === 'docker' || ds.type === 'process')) {
result = true;
}
});
});
return result;
};
var saveInjectPathUnique = function(path, injectPaths) {
if (!_.find(injectPaths, function(ip) { return ip === path; })) {
injectPaths.push(path);
}
};
var inject = function(topology, paths) {
_.each(paths, function(path) {
var context = topology;
_.each(path, function(element) {
context = context[element];
});
context.push('__proxy');
});
};
var isNumeric = function(s) {
return !isNaN(parseInt(s));
};
/**
* inject the proxy container on each machine container. A machine container is
* defined as a container that directly holds process or docker containers
*/
var injectContainers = function injectContainers(sys, defs, platform) {
var injectPaths = [];
traverse(sys.topology[platform]).forEach(function() {
var _this = this;
var name = _this.isLeaf ? _this.node : _this.key;
if (isExecutableContainer(name, defs)) {
saveInjectPathUnique(isNumeric(_this.parent.key) ? _this.parent.parent.path : _this.parent.path, injectPaths);
}
});
inject(sys.topology[platform], injectPaths);
};
/**
* determine if proxy containers are required for this target and insert them
*/
var injectProxies = function injectProxies(target, sys, defs, cb) {
injectCDef(defs);
injectContainers(sys, defs, target);
cb();
};
return {
injectProxies: injectProxies
};
};