From f4c4ba98be1883059e61e1296c461e2ac11d0df4 Mon Sep 17 00:00:00 2001 From: Aaron Williams Date: Sun, 27 Feb 2022 17:27:19 +1000 Subject: [PATCH] Add support for multiple docker hosts Add hosts split by a semi-colon and they will all be fetched --- controllers/apps/docker/useDocker.js | 35 +++++++++++----------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/controllers/apps/docker/useDocker.js b/controllers/apps/docker/useDocker.js index 88ecb3e..db7d632 100644 --- a/controllers/apps/docker/useDocker.js +++ b/controllers/apps/docker/useDocker.js @@ -11,33 +11,26 @@ const useDocker = async (apps) => { dockerHost: host, } = await loadConfig(); - let containers = null; + let containers = [] + const hosts = host.split(';'); - // Get list of containers try { - if (host.includes('localhost')) { - // Use default host - let { data } = await axios.get( + // Get list of containers + containers = (await Promise.all(hosts.reduce((acc, host) => { + const isLocalhost = host.includes('localhost'); + acc.push(axios.get( `http://${host}/containers/json?{"status":["running"]}`, - { + isLocalhost ? { socketPath: '/var/run/docker.sock', - } - ); - - containers = data; - } else { - // Use custom host - let { data } = await axios.get( - `http://${host}/containers/json?{"status":["running"]}` - ); - - containers = data; - } - } catch { - logger.log(`Can't connect to the Docker API on ${host}`, 'ERROR'); + } : {} + )) + return acc; + }, []))).flatMap(({ data }) => data) + } catch (err) { + logger.log(`Can't connect to the Docker API on one of the hosts: ${hosts.join(', ')}`, 'ERROR'); } - if (containers) { + if (containers.length > 0) { apps = await App.findAll({ order: [[orderType, 'ASC']], });