Add rebuild feature in BETA

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

View file

@ -13,6 +13,9 @@ 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"
REBUILD_LOG_FILE="/var/tmp/$( basename $0 ).log"
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 +59,10 @@ 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})
--rebuild-log Specify rebuild log file path (default: ${REBUILD_LOG_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 +94,17 @@ do
((idx++)) ((idx++))
DOCKERCOMPOSE_FILE=${!idx} DOCKERCOMPOSE_FILE=${!idx}
;; ;;
-b|--build|--rebuild)
REBUILD=1
;;
--rebuild-lock)
((idx++))
REBUILD_LOCK_FILE="${!idx}"
;;
--rebuild-log)
((idx++))
REBUILD_LOG_FILE="${!idx}"
;;
-x) -x)
((idx++)) ((idx++))
EXCLUDED_CONTAINERS+=( ${!idx} ) EXCLUDED_CONTAINERS+=( ${!idx} )
@ -142,6 +160,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 +257,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 +282,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 +291,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 +315,51 @@ 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 [ -z "$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
REBUILD_PID=$(cat $REBUILD_LOCK_FILE)
echo
if [ -d "/proc/$REBUILD_PID" ]
then
echo "Rebuild already triggered on $( stat --format %y "$REBUILD_LOCK_FILE" ) (PID: $REBUILD_PID)"
echo "You could follow it in its log file: ${REBUILD_LOG_FILE}"
else
echo "Container images already rebuilt (see ${REBUILD_LOG_FILE} for details)."
echo "You could recreate and restart them using the following command:"
echo
echo " $COMPOSE_BIN -f $DOCKERCOMPOSE_FILE up -d --no-deps ${UPGRADABLE_CONTAINERS[@]}"
fi
echo
else
echo
echo "Trigger rebuild of following container(s): ${UPGRADABLE_CONTAINERS[@]}"
echo "You could follow the rebuild process in its log file: ${REBUILD_LOG_FILE}"
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[@]}"
echo
$COMPOSE_BIN -f $DOCKERCOMPOSE_FILE build --no-cache ${UPGRADABLE_CONTAINERS[@]} > $REBUILD_LOG_FILE 2>&1 &
echo $! > "$REBUILD_LOCK_FILE"
fi
fi
# Display details, starting by errors # Display details, starting by errors
for container in ${!ERRORS[@]} for container in ${!ERRORS[@]}
do do