-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
300 lines (258 loc) · 9.53 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
'use strict';
const fs = require('fs').promises;
const nunjucks = require('nunjucks');
module.exports = exports;
module.exports.__cmd = require('./cmd');
/**
* @param whaler
*/
async function exports (whaler) {
if (!whaler.version) {
throw new Error('unsupported version of `whaler` installed, require `whaler@>=0.7`');
}
const { default: storage } = await whaler.fetch('storage');
const createHaproxyStorage = () => storage.create('haproxy');
whaler.on('haproxy:domains', async ctx => {
const haproxyStorage = createHaproxyStorage();
const data = await haproxyStorage.all();
const response = [];
for (let domain in data) {
if (!ctx.options['app'] || ctx.options['app'] == data[domain]['app']) {
response.push([data[domain]['app'], domain, data[domain]['regex'] || false]);
}
}
ctx.result = response;
});
whaler.on('haproxy:domains:publish', async ctx => {
const { default: storage } = await whaler.fetch('apps');
const app = await storage.get(ctx.options['app']);
const haproxyStorage = createHaproxyStorage();
await haproxyStorage.insert(ctx.options['domain'], {
app: ctx.options['app'],
regex: ctx.options['regex'] || false
});
});
whaler.on('haproxy:domains:unpublish', async ctx => {
try {
const haproxyStorage = createHaproxyStorage();
const data = await haproxyStorage.get(ctx.options['domain']);
await haproxyStorage.remove(ctx.options['domain']);
ctx.result = data['app'];
} catch (e) {
throw new Error(util.format('Domain `%s` not found.', ctx.options['domain']));
}
});
whaler.after('haproxy:domains:publish', async ctx => {
await touchHaproxy();
});
whaler.after('haproxy:domains:unpublish', async ctx => {
await touchHaproxy();
});
whaler.after('start', async ctx => {
await touchHaproxy();
});
whaler.after('stop', async ctx => {
await touchHaproxy();
});
whaler.after('remove', async ctx => {
let appName = ctx.options['ref'];
let serviceName = null;
const parts = ctx.options['ref'].split('.');
if (2 == parts.length) {
appName = parts[1];
serviceName = parts[0];
}
if (!serviceName && ctx.options['purge']) {
const haproxyStorage = createHaproxyStorage();
const data = await haproxyStorage.all();
for (let domain in data) {
if (appName == data[domain]['app']) {
await haproxyStorage.remove(domain);
}
}
}
await touchHaproxy();
});
// PRIVATE
const touchHaproxy = async () => {
const { default: docker } = await whaler.fetch('docker');
const { default: storage } = await whaler.fetch('apps');
const apps = await storage.all();
const domains = {};
const haproxyStorage = createHaproxyStorage();
const data = await haproxyStorage.all();
for (let domain in data) {
if (!domains[data[domain]['app']]) {
domains[data[domain]['app']] = [];
}
domains[data[domain]['app']].push({
name: domain,
regex: data[domain]['regex'] || false
});
}
const useDNS = 'OFF' != process.env.WHALER_HAPROXY_PLUGIN_DNS;
const opts = {
apps: [],
ssl_apps: [],
defaults: {
timeout: {
connect: process.env.WHALER_HAPROXY_PLUGIN_DEFAULTS_TIMEOUT_CONNECT || '5s',
client: process.env.WHALER_HAPROXY_PLUGIN_DEFAULTS_TIMEOUT_CLIENT || '50s',
server: process.env.WHALER_HAPROXY_PLUGIN_DEFAULTS_TIMEOUT_SERVER || '50s'
}
},
dns: useDNS && {
tcp: {
addr: process.env.WHALER_HAPROXY_PLUGIN_DNS_TCP_ADDR || '127.0.0.11',
port: process.env.WHALER_HAPROXY_PLUGIN_DNS_TCP_PORT || '53'
}
}
};
let whalerNetwork;
if (useDNS) {
whalerNetwork = docker.getNetwork('whaler_nw');
try {
await whalerNetwork.inspect();
} catch (e) {
whalerNetwork = undefined;
}
}
for (let appName in apps) {
const app = apps[appName];
const services = app.config['data']['services'];
for (let name in services) {
const config = services[name];
if (config['web'] || config['ssl']) {
let ip;
if (whalerNetwork) {
ip = name + '.' + appName;
} else {
const container = docker.getContainer(name + '.' + appName);
try {
const info = await container.inspect();
if (info['State']['Running']) {
ip = info['NetworkSettings']['IPAddress'];
}
} catch (e) {}
}
if (ip) {
if (config['web'] || null) {
opts['apps'].push(createConfig(appName, ip, config['web'], domains[appName] || [], 'web'));
}
if (config['ssl'] || null) {
opts['ssl_apps'].push(createConfig(appName, ip, config['ssl'], domains[appName] || [], 'ssl'));
}
}
}
}
}
let created = false;
let started = false;
const haproxyVersion = process.env.WHALER_HAPROXY_PLUGIN_IMAGE_TAG || '2.2';
let container = docker.getContainer('whaler_haproxy');
try {
const info = await container.inspect();
created = true;
if (info['State']['Running']) {
started = true;
}
if (created && 'haproxy:' + haproxyVersion !== info['Config']['Image']) {
await container.remove({
v: true,
force: true
});
}
} catch (e) {}
nunjucks.configure(__dirname + '/templates', { autoescape: false });
const res = nunjucks.render('haproxy.cfg', opts);
const pluginDir = '/var/lib/whaler/plugins/haproxy';
const cfgFile = pluginDir + '/cfg';
if (created && started) {
try {
const prevRes = await fs.readFile(cfgFile, 'utf8');
if (prevRes == res) {
return;
}
} catch (e) {}
}
await fs.mkdir(pluginDir, { recursive: true });
await fs.writeFile(cfgFile, res);
if (!created) {
try {
await docker.followPull('haproxy:' + haproxyVersion);
} catch(e) {}
const createOpts = {
'name': 'whaler_haproxy',
'Image': 'haproxy:' + haproxyVersion,
'ExposedPorts': {
'80/tcp': {},
'443/tcp': {}
},
'HostConfig': {
'Binds': [
cfgFile + ':/usr/local/etc/haproxy/haproxy.cfg'
],
'PortBindings': {
'80/tcp': [
{
'HostIp': '',
'HostPort': process.env.WHALER_HAPROXY_PLUGIN_WEB_PORT || '80'
}
],
'443/tcp': [
{
'HostIp': '',
'HostPort': process.env.WHALER_HAPROXY_PLUGIN_SSL_PORT || '443'
}
]
},
'RestartPolicy': {
'Name': 'always'
}
}
};
container = await docker.createContainer(createOpts);
}
if (whalerNetwork) {
try {
await whalerNetwork.connect({
'Container': container.id
});
} catch(e) {}
}
if (started) {
//await container.restart();
await container.kill({ signal: 'HUP' });
whaler.info('Haproxy restarted.');
} else {
await container.start();
whaler.info('Haproxy started.');
}
}
}
// PRIVATE
function createConfig (appName, ip, config, domains, type) {
const domain = process.env.WHALER_HAPROXY_PLUGIN_DOMAIN || 'whaler.lh';
const port = config['port'] || ('object' == typeof config ? null : config);
const name = appName + '.' + domain + '_' + ip + '_' + (port || type);
let defaults = config['defaults'] || null;
if (defaults) {
defaults = defaults.replace(/(?:\r\n|\r|\n)/g, '\n ').trim();
}
return {
name: name,
domains: [
{
name: appName + '.' + domain,
regex: false
}
].concat(domains || []),
send_proxy: (config['send-proxy'] || false),
backend: {
defaults: defaults,
name: 'backend_' + name,
port: port,
ip: ip
}
};
}