Services & hosts ordered by name
gitea/docker-discover/master This commit looks good
Details
gitea/docker-discover/master This commit looks good
Details
This commit is contained in:
parent
79d657e689
commit
d46fbf556f
43
index.js
43
index.js
|
@ -31,6 +31,23 @@ async function updateProxy() {
|
|||
};
|
||||
}
|
||||
|
||||
function sortFn(a, b) {
|
||||
return a.name > b.name ? 1 : a.name < b.name ? -1 : 0;
|
||||
}
|
||||
|
||||
let nodes = [];
|
||||
for (let node of info.nodes) {
|
||||
let address = node.ManagerStatus
|
||||
? node.ManagerStatus.Addr.split(':')[0]
|
||||
: node.Status.Addr;
|
||||
|
||||
nodes.push({
|
||||
name: node.Description.Hostname,
|
||||
address
|
||||
});
|
||||
}
|
||||
nodes = nodes.sort(sortFn);
|
||||
|
||||
let services = [];
|
||||
for (let serviceInfo of info.services) {
|
||||
let ports = serviceInfo.Endpoint.Ports;
|
||||
|
@ -40,29 +57,17 @@ async function updateProxy() {
|
|||
let match = name.match(/^(.+)_main$/);
|
||||
if (match) name = match[1];
|
||||
|
||||
let service = {
|
||||
services.push({
|
||||
name,
|
||||
port: ports[0].PublishedPort,
|
||||
nodes: []
|
||||
};
|
||||
services.push(service);
|
||||
|
||||
for (let node of info.nodes) {
|
||||
let address = node.ManagerStatus
|
||||
? node.ManagerStatus.Addr.split(':')[0]
|
||||
: node.Status.Addr;
|
||||
|
||||
service.nodes.push({
|
||||
name: node.Description.Hostname,
|
||||
endpoint: `${address}:${service.port}`
|
||||
});
|
||||
}
|
||||
port: ports[0].PublishedPort
|
||||
});
|
||||
}
|
||||
services = services.sort(sortFn);
|
||||
|
||||
// Cheking settings hash
|
||||
|
||||
let infoHash = shajs('sha256')
|
||||
.update(JSON.stringify(services))
|
||||
.update(JSON.stringify({services, nodes}))
|
||||
.digest('hex');
|
||||
console.log('Settings hash:', infoHash);
|
||||
|
||||
|
@ -77,7 +82,7 @@ async function updateProxy() {
|
|||
// Creating configuration file
|
||||
|
||||
let tmpConf = `${tmpDir}/config.cfg`;
|
||||
let configString = template({info, services});
|
||||
let configString = template({services, nodes, info});
|
||||
fs.writeFileSync(tmpConf, configString);
|
||||
|
||||
if (conf.debug) {
|
||||
|
@ -128,7 +133,7 @@ async function updateProxy() {
|
|||
|
||||
await updateProxy();
|
||||
|
||||
console.log('Listenig for events.')
|
||||
console.log('Listening for events.')
|
||||
docker.getEvents({}, (err, stream) => {
|
||||
if (err || !stream) {
|
||||
console.error('Failed to monitor docker host', err);
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
{{#each services}}
|
||||
backend {{name}}
|
||||
{{#each nodes}}
|
||||
server {{name}}({{endpoint}}) {{endpoint}} check
|
||||
{{#each ../nodes}}
|
||||
server {{name}}:{{../port}} {{address}}:{{../port}} check
|
||||
{{/each}}
|
||||
{{/each}}
|
||||
|
|
Reference in New Issue