From 1cde0c3b86271aabf87118485ac9ad4b3526f45f Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 11 Mar 2024 19:15:06 +0100 Subject: [PATCH] Allow to run check on a specified Docker compose project --- README.md | 13 +++++++------ check_container_upgrade | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f72355f..508c11a 100644 --- a/README.md +++ b/README.md @@ -25,12 +25,13 @@ service nagios-nrpe-server reload ``` Usage : check_container_upgrade [-d] [-E /path/to/engine] [container1,...] - -E [path] Force a specific engine (possible values: auto docker podman, default: auto) - -x [container] Exclude specified container (could be repeat) - -M [integer] Max number of container checks to run in parallel (default: 4, 0=no limit) - -d Debug mode - -X Enable bash tracing (=set -x) - -h Show this message + -E [path] Force a specific engine (possible values: auto docker podman, default: auto) + -x [container] Exclude specified container (could be repeat) + -M [integer] Max number of container checks to run in parallel (default: 4, 0=no limit) + -f [docker-compose.yml] To check upgrade on docker compose project, specified the path of the docker-compose.yml file + -d Debug mode + -X Enable bash tracing (=set -x) + -h Show this message ``` ## Copyright diff --git a/check_container_upgrade b/check_container_upgrade index 595e0b0..1d03415 100755 --- a/check_container_upgrade +++ b/check_container_upgrade @@ -8,6 +8,7 @@ ENGINE="auto" POSSIBLE_ENGINES=( "auto" "docker" "podman" ) +DOCKERCOMPOSE_FILE="" DEBUG=0 MAX_PARALLEL_CHECKS=4 ONLY_CONTAINERS=() @@ -47,6 +48,7 @@ Usage : $(basename $0) [-d] [-E /path/to/engine] [container1,...] -E [path] Force a specific engine (possible values: ${POSSIBLE_ENGINES[@]}, default: $ENGINE) -x [container] Exclude specified container (could be repeat) -M [integer] Max number of container checks to run in parallel (default: $MAX_PARALLEL_CHECKS, 0=no limit) + -f [docker-compose.yml] To check upgrade on docker compose project, specified the path of the docker-compose.yml file -d Debug mode -X Enable bash tracing (=set -x) -h Show this message @@ -74,6 +76,10 @@ do in_array $ENGINE ${POSSIBLE_ENGINES[@]} || usage "Invalid engine $ENGINE" fi ;; + -f) + ((idx++)) + DOCKERCOMPOSE_FILE=${!idx} + ;; -x) ((idx++)) EXCLUDED_CONTAINERS+=( ${!idx} ) @@ -117,6 +123,12 @@ then debug "Auto-detected engine: $ENGINE" fi +if [ -n "$DOCKERCOMPOSE_FILE" -a ! -e "$DOCKERCOMPOSE_FILE" ] +then + echo "UNKNOWN - Docker compose file not found ($DOCKERCOMPOSE_FILE)" + exit 3 +fi + EXIT_CODE=0 declare -A CONTAINER_STATUS_FILE declare -A CONTAINER_PID @@ -125,9 +137,26 @@ declare -A ERRORS CHECKED_CONTAINERS=( ) debug "List running containers..." -RUNNING_CONTAINERS=$($ENGINE ps --format '{{.Names}}' | tr '\n' ' ') +if [ -n "$DOCKERCOMPOSE_FILE" ] +then + RUNNING_CONTAINERS=$($ENGINE compose -f $DOCKERCOMPOSE_FILE ps --format '{{.Service}}' | tr '\n' ' ') +else + RUNNING_CONTAINERS=$($ENGINE ps --format '{{.Names}}' | tr '\n' ' ') +fi debug "Running containers: $RUNNING_CONTAINERS" +function exec_in_container() { + container=$1 + shift; + if [ -n "$DOCKERCOMPOSE_FILE" ] + then + $ENGINE compose -f $DOCKERCOMPOSE_FILE exec $container $@ + return $? + fi + $ENGINE exec $container $@ + return $? +} + # Implement check inside a function to allow running it in parallel # Parameters : [container] [output file] function check_container() { @@ -136,14 +165,14 @@ function check_container() { STATUS="" for check_plugin in ${CHECK_PLUGINS[@]} do - $ENGINE exec $container test -e $check_plugin > /dev/null 2>&1 + exec_in_container $container test -e $check_plugin > /dev/null 2>&1 if [ $? -ne 0 ] then debug "$container - Plugin $check_plugin not found" continue fi debug "$container - Plugin $check_plugin found, use it" - STATUS="$($ENGINE exec $container ${CHECK_PLUGINS[${check_plugin}]} 2>&1)" + STATUS="$(exec_in_container $container ${CHECK_PLUGINS[${check_plugin}]} 2>&1)" ex=$? debug "$container - Plugin output: $STATUS" debug "$container - Plugin exit code: $ex"