From a38424d3adf3496bdd94b841a815a02eb4c520f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Sat, 26 May 2018 22:19:17 +0200 Subject: [PATCH] [Watchdog] Find exact matches for service names, shuffle scaled services, check if ip is in mailcow network --- data/Dockerfiles/watchdog/watchdog.sh | 20 ++++++++++++++++---- docker-compose.yml | 3 ++- 2 files changed, 18 insertions(+), 5 deletions(-) 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: