From 1a70fc97f7fbec00a00fa91333ea92151aa65961 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Tue, 8 Feb 2022 12:22:49 +0100 Subject: [PATCH] add more logging --- packages/health-check/Dockerfile | 11 ++++++----- packages/health-check/src/checks/middleware.js | 13 +++++++++++-- packages/health-check/src/index.js | 6 ++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/health-check/Dockerfile b/packages/health-check/Dockerfile index 9578757b..1ea6daa8 100644 --- a/packages/health-check/Dockerfile +++ b/packages/health-check/Dockerfile @@ -23,14 +23,15 @@ COPY bin bin EXPOSE 3100 ENV NODE_ENV production -# 1. start dnsmasq in the background with: +# 1. get public server ip and save it in /etc/environment (passed to cron tasks as env variable) +# 2. start dnsmasq in the background with: # - alias PORTAL_DOMAIN with current server ip so it overrides potential load balancer request # - default docker nameserver 127.0.0.11 for any other request -# 2. replace docker nameserver with dnsmasq nameserver in /etc/resolv.conf -# 3. start crond in the background to schedule periodic health checks -# 4. start the health-check api service +# 3. replace docker nameserver with dnsmasq nameserver in /etc/resolv.conf +# 4. start crond in the background to schedule periodic health checks +# 5. start the health-check api service CMD [ "sh", "-c", \ - "serverip=$(node src/whatismyip.js) && \ + "export serverip=$(node src/whatismyip.js) && \ echo \"export serverip=${serverip}\" >> /etc/environment && \ dnsmasq --no-resolv --log-facility=/var/log/dnsmasq.log --address=/$PORTAL_DOMAIN/$serverip --server=127.0.0.11 && \ echo \"$(sed 's/127.0.0.11/127.0.0.1/' /etc/resolv.conf)\" > /etc/resolv.conf && \ diff --git a/packages/health-check/src/checks/middleware.js b/packages/health-check/src/checks/middleware.js index 7f520206..98ad71e1 100644 --- a/packages/health-check/src/checks/middleware.js +++ b/packages/health-check/src/checks/middleware.js @@ -3,11 +3,20 @@ const { ipCheckService, ipRegex } = require("../utils"); const getCurrentAddress = async () => { // use serverip env variable when available (set via Dockerfile) - if (process.env.serverip) return process.env.serverip; + if (process.env.serverip) { + if (ipRegex.test(process.env.serverip)) return process.env.serverip; + + // log error to console for future reference but do not break + console.log(`Environment variable serverip contains invalid ip: "${process.env.serverip}"`); + } try { const { body } = await got(`http://${ipCheckService}`); - if (ipRegex.test(body)) return body; + if (ipRegex.test(body)) { + console.info(`Server public ip: ${body} (source: ${ipCheckService})`); + + return body; + } throw new Error(`${ipCheckService} responded with invalid ip: "${body}"`); } catch (error) { diff --git a/packages/health-check/src/index.js b/packages/health-check/src/index.js index 444c7a9b..7eb270a9 100644 --- a/packages/health-check/src/index.js +++ b/packages/health-check/src/index.js @@ -42,4 +42,10 @@ server.listen(port, host, (error) => { if (error) throw error; console.info(`Server listening at http://${host}:${port} (NODE_ENV: ${process.env.NODE_ENV})`); + + const { ipRegex } = require("./utils"); + + if (ipRegex.test(process.env.serverip)) { + console.info(`Server public ip: ${process.env.serverip}`); + } });