From d46fbf556f658435f56deeab11e643932bf98d57 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Wed, 29 Jan 2020 17:01:39 +0100 Subject: [PATCH] Services & hosts ordered by name --- index.js | 43 ++++++++++++++++++++++++------------------- rproxy.handlebars | 4 ++-- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index a346410..81a6a2d 100644 --- a/index.js +++ b/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); diff --git a/rproxy.handlebars b/rproxy.handlebars index 4278892..675425f 100644 --- a/rproxy.handlebars +++ b/rproxy.handlebars @@ -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}}