NobGit
public nobgit read

NobMail

Based on mailcow: dockerized

Languages

Repository composition by tracked source files.

PHP
PHP 49% JavaScript 35% HTML 9% CSS 4% Shell 2% Python 1% Lua 0% Perl 0% Ruby 0% SCSS 0%
Create file Wiki Documentation
Clone
https://nobgit.com/orgs/nobgit/nobmail.git
ssh://[email protected]:2222/orgs/nobgit/nobmail.git

Commit

[Watchdog] Find exact matches for service names, shuffle scaled services, check if ip is in mailcow network

a38424d3
AndrĂ© <[email protected]> 8 years ago
data/Dockerfiles/watchdog/watchdog.sh | 20 ++++++++++++++++----
 docker-compose.yml                    |  3 ++-
 2 files changed, 18 insertions(+), 5 deletions(-)

Diff

diff --git a/data/Dockerfiles/watchdog/watchdog.sh b/data/Dockerfiles/watchdog/watchdog.sh
index c5a274e9..3522d89a 100755
--- a/data/Dockerfiles/watchdog/watchdog.sh
+++ b/data/Dockerfiles/watchdog/watchdog.sh
@@ -63,14 +63,26 @@ function mail_error() {
 
 get_container_ip() {
   # ${1} is container
-  CONTAINER_ID=
+  CONTAINER_ID=()
   CONTAINER_IP=
   LOOP_C=1
   until [[ ${CONTAINER_IP} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [[ ${LOOP_C} -gt 5 ]]; do
-    sleep 1
-    CONTAINER_ID=$(curl --silent http://dockerapi:8080/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring | contains(\"${1}\")) | .id")
+    sleep 0.5
+    # get long container id for exact match
+    CONTAINER_ID=($(curl --silent http://dockerapi:8080/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" | jq -rc "select( .name | tostring == \"${1}\") | .id"))
+    # returned id can have multiple elements (if scaled), shuffle for random test
+    CONTAINER_ID=($(printf "%s\n" "${CONTAINER_ID[@]}" | shuf))
     if [[ ! -z ${CONTAINER_ID} ]]; then
-      CONTAINER_IP=$(curl --silent http://dockerapi:8080/containers/${CONTAINER_ID}/json | jq -r '.NetworkSettings.Networks[].IPAddress')
+      for matched_container in "${CONTAINER_ID[@]}"; do
+        CONTAINER_IP=$(curl --silent http://dockerapi:8080/containers/${matched_container}/json | jq -r '.NetworkSettings.Networks[].IPAddress')
+        # grep will do nothing if one of these vars is empty
+        [[ -z ${CONTAINER_IP} ]] && continue
+        [[ -z ${IPV4_NETWORK} ]] && continue
+        # only return ips that are part of our network
+        if ! grep -q ${IPV4_NETWORK} <(echo ${CONTAINER_IP}); then
+          CONTAINER_IP=
+        fi
+      done
     fi
     LOOP_C=$((LOOP_C + 1))
   done
diff --git a/docker-compose.yml b/docker-compose.yml
index 260021b1..fff4ffa8 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -344,7 +344,7 @@ services:
         - /lib/modules:/lib/modules:ro
 
     watchdog-mailcow:
-      image: mailcow/watchdog:1.16
+      image: mailcow/watchdog:1.17
       # Debug
       #command: /watchdog.sh
       build: ./data/Dockerfiles/watchdog
@@ -362,6 +362,7 @@ services:
         - USE_WATCHDOG=${USE_WATCHDOG:-n}
         - WATCHDOG_NOTIFY_EMAIL=${WATCHDOG_NOTIFY_EMAIL}
         - MAILCOW_HOSTNAME=${MAILCOW_HOSTNAME}
+        - IPV4_NETWORK=${IPV4_NETWORK:-172.22.1}
       dns:
         - ${IPV4_NETWORK:-172.22.1}.254
       networks: