This repository was archived by the owner on Aug 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathregistrationServer.js
More file actions
110 lines (101 loc) · 3.73 KB
/
Copy pathregistrationServer.js
File metadata and controls
110 lines (101 loc) · 3.73 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
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
var restify = require('restify');
var fs = require('fs');
var exec = require('child_process').exec;
var NginxConfFile = require('nginx-conf').NginxConfFile;
var server = restify.createServer();
var isWin = /^win/.test(process.platform);
var fileLocation = '/etc/nginx/nginx.conf';
if (process.env.NGINX_CONF) {
fileLocation = process.env.NGINX_CONF;
}
function respondToRegistration(req, res, next) {
NginxConfFile.create(fileLocation, function(err, conf) {
if (err) {
console.log(err);
return;
}
var addedItem = null;
if (!conf.nginx.http.upstream) {
conf.nginx.http._add("upstream", "apiserver");
}
if (Array.isArray(conf.nginx.http.upstream.server)) {
var exists = mapAndIndexUpstreamValues(conf.nginx.http.upstream.server, req.params.address) > -1;
console.log("Request to add " + req.params.address);
if (!exists) {
addedItem = req.params.address;
}
} else if (conf.nginx.http.upstream.server) {
var trimmedAddress = conf.nginx.http.upstream.server.toString().trim();
if (trimmedAddress != "server " + req.params.address + ";") {
addedItem = req.params.address;
}
} else {
addedItem = req.params.address;
}
if (addedItem) {
console.log("Current upstream is " + conf.nginx.http.upstream);
conf.nginx.http.upstream._add('server', addedItem);
res.json(200, "{'RegistrationStatus':'Success'}");
console.log("upstream is now " + conf.nginx.http.upstream);
conf.flush();
reloadConfig();
} else {
res.json(203, "{'RegistrationStatus':'Exists'}");
}
});
return next();
}
function respondToDeregistration(req, res, next) {
NginxConfFile.create(fileLocation, function(err, conf) {
if (err) {
console.log(err);
return;
}
var removeAt = -1;
var itemToRemove = null;
if (Array.isArray(conf.nginx.http.upstream.server)) {
removeAt = mapAndIndexUpstreamValues(conf.nginx.http.upstream.server, req.params.address);
if (removeAt > -1) {
itemToRemove = conf.nginx.http.upstream.server[removeAt];
conf.nginx.http.upstream._remove('server', removeAt);
}
} else if (conf.nginx.http.upstream.server) {
trimmedAddress = conf.nginx.http.upstream.server.toString().trim();
if (trimmedAddress === "server " + req.params.address + ";") {
itemToRemove = trimmedAddress;
conf.nginx.http._remove('upstream');
}
}
if (itemToRemove) {
res.json(200, "{'DeregistrationStatus':'Success'}");
conf.flush();
reloadConfig();
} else {
res.json(404, "{'DeregistrationStatus':'Not Found'}");
}
});
return next();
}
function mapAndIndexUpstreamValues(serverConf, address) {
var mapped = serverConf.map(function(x) {
return x.toString().trim();
});
var searchVal = "server " + address + ";";
return mapped.indexOf(searchVal);
}
function reloadConfig() {
var cmd = 'docker restart gateway';
//Ideally docker exec gateway nginx -s reload
//or nginx -c /etc/nginx/nginx.conf
exec(cmd, function(error, stdout, stderr) {
if (error) {
console.error("exec error: " + error);
return;
}
console.log("stdout: " + stdout);
console.log("stderr: " + stderr);
});
}
server.get('/register/:address', respondToRegistration);
server.get('/deregister/:address', respondToDeregistration);
server.listen(8086);