Allow to run check on a specified Docker compose project

This commit is contained in:
Benjamin Renard 2024-03-11 19:15:06 +01:00
parent c7283ea44e
commit 1cde0c3b86
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
2 changed files with 39 additions and 9 deletions

View file

@ -28,6 +28,7 @@ Usage : check_container_upgrade [-d] [-E /path/to/engine] [container1,...]
-E [path] Force a specific engine (possible values: auto docker podman, default: auto) -E [path] Force a specific engine (possible values: auto docker podman, default: auto)
-x [container] Exclude specified container (could be repeat) -x [container] Exclude specified container (could be repeat)
-M [integer] Max number of container checks to run in parallel (default: 4, 0=no limit) -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 -d Debug mode
-X Enable bash tracing (=set -x) -X Enable bash tracing (=set -x)
-h Show this message -h Show this message

View file

@ -8,6 +8,7 @@
ENGINE="auto" ENGINE="auto"
POSSIBLE_ENGINES=( "auto" "docker" "podman" ) POSSIBLE_ENGINES=( "auto" "docker" "podman" )
DOCKERCOMPOSE_FILE=""
DEBUG=0 DEBUG=0
MAX_PARALLEL_CHECKS=4 MAX_PARALLEL_CHECKS=4
ONLY_CONTAINERS=() 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) -E [path] Force a specific engine (possible values: ${POSSIBLE_ENGINES[@]}, default: $ENGINE)
-x [container] Exclude specified container (could be repeat) -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) -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 -d Debug mode
-X Enable bash tracing (=set -x) -X Enable bash tracing (=set -x)
-h Show this message -h Show this message
@ -74,6 +76,10 @@ do
in_array $ENGINE ${POSSIBLE_ENGINES[@]} || usage "Invalid engine $ENGINE" in_array $ENGINE ${POSSIBLE_ENGINES[@]} || usage "Invalid engine $ENGINE"
fi fi
;; ;;
-f)
((idx++))
DOCKERCOMPOSE_FILE=${!idx}
;;
-x) -x)
((idx++)) ((idx++))
EXCLUDED_CONTAINERS+=( ${!idx} ) EXCLUDED_CONTAINERS+=( ${!idx} )
@ -117,6 +123,12 @@ then
debug "Auto-detected engine: $ENGINE" debug "Auto-detected engine: $ENGINE"
fi 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 EXIT_CODE=0
declare -A CONTAINER_STATUS_FILE declare -A CONTAINER_STATUS_FILE
declare -A CONTAINER_PID declare -A CONTAINER_PID
@ -125,9 +137,26 @@ declare -A ERRORS
CHECKED_CONTAINERS=( ) CHECKED_CONTAINERS=( )
debug "List running 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" 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 # Implement check inside a function to allow running it in parallel
# Parameters : [container] [output file] # Parameters : [container] [output file]
function check_container() { function check_container() {
@ -136,14 +165,14 @@ function check_container() {
STATUS="" STATUS=""
for check_plugin in ${CHECK_PLUGINS[@]} for check_plugin in ${CHECK_PLUGINS[@]}
do 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 ] if [ $? -ne 0 ]
then then
debug "$container - Plugin $check_plugin not found" debug "$container - Plugin $check_plugin not found"
continue continue
fi fi
debug "$container - Plugin $check_plugin found, use it" 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=$? ex=$?
debug "$container - Plugin output: $STATUS" debug "$container - Plugin output: $STATUS"
debug "$container - Plugin exit code: $ex" debug "$container - Plugin exit code: $ex"