Add rebuild feature in BETA

This commit is contained in:
Benjamin Renard 2024-04-18 12:16:45 +02:00
parent efe3258bd4
commit 4c2ffd7afb
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -13,6 +13,8 @@ DEBUG=0
MAX_PARALLEL_CHECKS=4 MAX_PARALLEL_CHECKS=4
ONLY_CONTAINERS=() ONLY_CONTAINERS=()
EXCLUDED_CONTAINERS=( buildx_buildkit_default ) EXCLUDED_CONTAINERS=( buildx_buildkit_default )
REBUILD=0
REBUILD_LOCK_FILE="/var/tmp/$( basename $0 ).lock"
declare -rA CHECK_PLUGINS=( declare -rA CHECK_PLUGINS=(
["/usr/lib/nagios/plugins/check_apt"]="/usr/lib/nagios/plugins/check_apt -u -U -t 60 -l" ["/usr/lib/nagios/plugins/check_apt"]="/usr/lib/nagios/plugins/check_apt -u -U -t 60 -l"
["/usr/lib/nagios/plugins/check_apk"]="/usr/lib/nagios/plugins/check_apk" ["/usr/lib/nagios/plugins/check_apk"]="/usr/lib/nagios/plugins/check_apk"
@ -56,6 +58,9 @@ Usage : $(basename $0) [-d] [-E /path/to/engine] [container1,...]
-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 -f [docker-compose.yml] To check upgrade on docker compose project, specified the path of the docker-compose.yml file
-b|--build|--rebuild Trigger container build if upgrade detected (only possible if a docker compose file if provided)
/!\\ WARNING /!\\ Beta feature, not really tested yet!
--rebuild-lock Specify rebuild lock file path (default: ${REBUILD_LOCK_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
@ -87,6 +92,13 @@ do
((idx++)) ((idx++))
DOCKERCOMPOSE_FILE=${!idx} DOCKERCOMPOSE_FILE=${!idx}
;; ;;
-b|--build|--rebuild)
REBUILD=1
;;
--rebuild-lock)
((idx++))
REBUILD_LOCK_FILE="${!idx}"
;;
-x) -x)
((idx++)) ((idx++))
EXCLUDED_CONTAINERS+=( ${!idx} ) EXCLUDED_CONTAINERS+=( ${!idx} )
@ -142,6 +154,7 @@ declare -A CONTAINER_PID
declare -A UP_TO_DATE declare -A UP_TO_DATE
declare -A ERRORS declare -A ERRORS
declare -A UNKNOWNS declare -A UNKNOWNS
UPGRADABLE_CONTAINERS=( )
CHECKED_CONTAINERS=( ) CHECKED_CONTAINERS=( )
debug "List running containers..." debug "List running containers..."
@ -238,7 +251,7 @@ do
UP_TO_DATE+=( ["$container"]=$STATUS ) UP_TO_DATE+=( ["$container"]=$STATUS )
else else
ERRORS+=( ["$container"]=$STATUS ) ERRORS+=( ["$container"]=$STATUS )
[ $ex -ge 3 ] && UNKNOWNS+=( "$container" ) [ $ex -ge 3 ] && UNKNOWNS+=( "$container" ) || UPGRADABLE_CONTAINERS+=( "$container" )
fi fi
[ $EXIT_CODE -ge $ex ] && continue [ $EXIT_CODE -ge $ex ] && continue
[ $ex -gt 3 ] && ex=3 [ $ex -gt 3 ] && ex=3
@ -263,6 +276,8 @@ fi
debug "Final exit code: $EXIT_CODE" debug "Final exit code: $EXIT_CODE"
debug "Check containers (${#CHECKED_CONTAINERS[@]}): $( implode ", " "${CHECKED_CONTAINERS[@]}" )" debug "Check containers (${#CHECKED_CONTAINERS[@]}): $( implode ", " "${CHECKED_CONTAINERS[@]}" )"
debug "Up-to-date containers (${#UP_TO_DATE[@]}): $( implode ", " "${!UP_TO_DATE[@]}" )"
debug "Upgradable containers (${#UPGRADABLE_CONTAINERS[@]}): $( implode ", " "${UPGRADABLE_CONTAINERS[@]}" )"
debug "Containers with errors (${#ERRORS[@]}): $( implode ", " "${!ERRORS[@]}" )" debug "Containers with errors (${#ERRORS[@]}): $( implode ", " "${!ERRORS[@]}" )"
debug "Not found containers (${#NOTFOUNDS[@]}): $( implode ", " "${NOTFOUNDS[@]}" )" debug "Not found containers (${#NOTFOUNDS[@]}): $( implode ", " "${NOTFOUNDS[@]}" )"
@ -270,6 +285,7 @@ debug "Not found containers (${#NOTFOUNDS[@]}): $( implode ", " "${NOTFOUNDS[@]}
let CONTAINER_COUNTS=${#CHECKED_CONTAINERS[@]}+${#NOTFOUNDS[@]} let CONTAINER_COUNTS=${#CHECKED_CONTAINERS[@]}+${#NOTFOUNDS[@]}
PERF_DATA=( PERF_DATA=(
"uptodate_containers=${#UP_TO_DATE[@]};;;0;$CONTAINER_COUNTS" "uptodate_containers=${#UP_TO_DATE[@]};;;0;$CONTAINER_COUNTS"
"upgradable_containers=${#UPGRADABLE_CONTAINERS[@]};;;0;$CONTAINER_COUNTS"
"containers_with_errors=${#ERRORS[@]};1;;0;$CONTAINER_COUNTS" "containers_with_errors=${#ERRORS[@]};1;;0;$CONTAINER_COUNTS"
"unknown_state_containers=${#UNKNOWNS[@]};;;0;$CONTAINER_COUNTS" "unknown_state_containers=${#UNKNOWNS[@]};;;0;$CONTAINER_COUNTS"
) )
@ -293,6 +309,38 @@ esac
# Add performance data # Add performance data
echo " |$( implode " " "${PERF_DATA[@]}" )" echo " |$( implode " " "${PERF_DATA[@]}" )"
# Trigger container build (if need, enabled and docker compose file is provided)
if [ $REBUILD -eq 1 ]
then
if [ ${#UPGRADABLE_CONTAINERS[@]} -eq 0 ]
then
debug "No upgradable container to rebuild"
if [ -e "$REBUILD_LOCK_FILE" ]
then
debug "Remove previous rebuild lock file ($REBUILD_LOCK_FILE)"
rm -f "$REBUILD_LOCK_FILE"
fi
elif [ -n "$DOCKERCOMPOSE_FILE" ]
then
echo
echo "WARNING: No docker compose file provided, can't trigger rebuild of following container(s):"
echo "- $( implode "\n- " ${UPGRADABLE_CONTAINERS[@]} )"
elif [ -e "$REBUILD_LOCK_FILE" ]
then
echo
echo "Rebuild already triggered on $( stat --format %y "$REBUILD_LOCK_FILE" )"
echo "Note: if not or to force rebuild, remove the '$REBUILD_LOCK_FILE' file."
else
echo
echo "Trigger rebuild of following container(s): ${UPGRADABLE_CONTAINERS[@]}"
echo "You will able to recreate and restart them using the following command:"
echo
echo " $COMPOSE_BIN -f $DOCKERCOMPOSE_FILE up -d --no-deps ${UPGRADABLE_CONTAINERS[@]}"
$COMPOSE_BIN -f $DOCKERCOMPOSE_FILE build --no-cache ${UPGRADABLE_CONTAINERS[@]} > $REBUILD_LOCK_FILE 2>&1 &
fi
fi
# Display details, starting by errors # Display details, starting by errors
for container in ${!ERRORS[@]} for container in ${!ERRORS[@]}
do do