Command execution and log function

This commit is contained in:
Vorezal 2026-02-08 12:04:17 +00:00
parent 81d5ef65f6
commit bae266a58d
3 changed files with 222 additions and 69 deletions

2
.gitignore vendored
View file

@ -14,3 +14,5 @@ compose.yaml
compose.yml compose.yml
docker-compose.yaml docker-compose.yaml
docker-compose.yml docker-compose.yml
# ignore log directory
/log/

View file

@ -13,20 +13,6 @@ ScriptArgs=( "$@" )
ScriptPath="$(readlink -f "$0")" ScriptPath="$(readlink -f "$0")"
ScriptWorkDir="$(dirname "$ScriptPath")" ScriptWorkDir="$(dirname "$ScriptPath")"
# Source helper functions
source_if_exists_or_fail() {
if [[ -s "$1" ]]; then
source "$1"
[[ "${DisplaySourcedFiles:-false}" == true ]] && echo " * sourced config: ${1}"
return 0
else
return 1
fi
}
# User customizable defaults
source_if_exists_or_fail "${HOME}/.config/dockcheck.config" || source_if_exists_or_fail "${ScriptWorkDir}/dockcheck.config"
# Help Function # Help Function
Help() { Help() {
echo "Syntax: dockcheck.sh [OPTION] [comma separated names to include]" echo "Syntax: dockcheck.sh [OPTION] [comma separated names to include]"
@ -49,6 +35,7 @@ Help() {
echo "-M Prints custom releasenote urls as markdown (requires template support)." echo "-M Prints custom releasenote urls as markdown (requires template support)."
echo "-n No updates; only checking availability without interaction." echo "-n No updates; only checking availability without interaction."
echo "-p Auto-prune dangling images after update." echo "-p Auto-prune dangling images after update."
echo "-q Quiet mode. Minmal text output. Does not effect file based logging, if enabled."
echo "-r Allow checking/updating images created by 'docker run', containers need to be recreated manually." echo "-r Allow checking/updating images created by 'docker run', containers need to be recreated manually."
echo "-R Skip container recreation after pulling images." echo "-R Skip container recreation after pulling images."
echo "-s Include stopped containers in the check. (Logic: docker ps -a)." echo "-s Include stopped containers in the check. (Logic: docker ps -a)."
@ -60,12 +47,51 @@ Help() {
echo "Project source: $Github" echo "Project source: $Github"
} }
# Print current backups function while getopts "ayb:BfFhiIlmMnpqrsuvc:e:d:t:x:R" options; do
print_backups() { case "${options}" in
printf "\n%b---%b Currently backed up images %b---%b\n\n" "$c_teal" "$c_blue" "$c_teal" "$c_reset" a|y) AutoMode=true ;;
docker images | sed -ne '/^REPOSITORY/p' -ne '/^dockcheck/p' b) BackupForDays="${OPTARG}" ;;
B) PrintBackups=true ;;
c) CollectorTextFileDirectory="${OPTARG}" ;;
d) DaysOld=${OPTARG} ;;
e) Exclude=${OPTARG} ;;
f) ForceRestartStacks=true ;;
F) OnlySpecific=true ;;
i) Notify=true ;;
I) PrintReleaseURL=true ;;
l) OnlyLabel=true ;;
m) MonoMode=true ;;
M) PrintMarkdownURL=true ;;
n) DontUpdate=true; AutoMode=true;;
p) AutoPrune=true ;;
q) Quiet=true ;;
R) SkipRecreate=true ;;
r) DRunUp=true ;;
s) Stopped="-a" ;;
t) Timeout="${OPTARG}" ;;
u) AutoSelfUpdate=true ;;
v) printf "%s\n" "$VERSION"; exit 0 ;;
x) MaxAsync=${OPTARG} ;;
h|*) Help; exit 2 ;;
esac
done
shift "$((OPTIND-1))"
# Source helper function
source_if_exists_or_fail() {
if [[ -s "$1" ]]; then
source "$1"
# DisplaySourcedFiles used for debugging purposes only
[[ "${DisplaySourcedFiles:-false}" == true ]] && echo " * sourced config: ${1}"
return 0
else
return 1
fi
} }
# User customizable defaults
source_if_exists_or_fail "${HOME}/.config/dockcheck.config" || source_if_exists_or_fail "${ScriptWorkDir}/dockcheck.config"
# Initialise variables # Initialise variables
Timeout=${Timeout:-10} Timeout=${Timeout:-10}
MaxAsync=${MaxAsync:-1} MaxAsync=${MaxAsync:-1}
@ -89,13 +115,17 @@ BackupForDays=${BackupForDays:-}
OnlySpecific=${OnlySpecific:-false} OnlySpecific=${OnlySpecific:-false}
SpecificContainer=${SpecificContainer:-""} SpecificContainer=${SpecificContainer:-""}
SkipRecreate=${SkipRecreate:-false} SkipRecreate=${SkipRecreate:-false}
LogToFile=${LogToFile:-false}
LogDaysToKeep=${LogDaysToKeep:-14}
LOG_LEVEL=${LOG_LEVEL:-INFO}
Quiet=${Quiet:-false}
Excludes=() Excludes=()
GotUpdates=() GotUpdates=()
NoUpdates=() NoUpdates=()
GotErrors=() GotErrors=()
SelectedUpdates=() SelectedUpdates=()
Actions=()
CurlArgs="--retry ${CurlRetryCount:=3} --retry-delay ${CurlRetryDelay:=1} --connect-timeout ${CurlConnectTimeout:=5} -sf" CurlArgs="--retry ${CurlRetryCount:=3} --retry-delay ${CurlRetryDelay:=1} --connect-timeout ${CurlConnectTimeout:=5} -sf"
LatestOutput=""
regbin="" regbin=""
jqbin="" jqbin=""
@ -111,34 +141,132 @@ c_reset="\033[0m"
RunTimestamp=$(date +'%Y-%m-%d_%H%M') RunTimestamp=$(date +'%Y-%m-%d_%H%M')
RunEpoch=$(date +'%s') RunEpoch=$(date +'%s')
while getopts "ayb:BfFhiIlmMnprsuvc:e:d:t:x:R" options; do # Duplicate stdout file descriptor for command output purposes
case "${options}" in exec 5>&1
a|y) AutoMode=true ;;
b) BackupForDays="${OPTARG}" ;; # Exec helper function
B) print_backups; exit 0 ;; # Executes the command and arguments passed while both outputting and capturing output to the LatestOutput variable while maintaining return status code
c) CollectorTextFileDirectory="${OPTARG}" ;; exec_command() {
d) DaysOld=${OPTARG} ;; LatestOutput=""
e) Exclude=${OPTARG} ;; if [[ "${Quiet}" == "false" ]]; then
f) ForceRestartStacks=true ;; LatestOutput=$("$@" | tee >(cat - >&5))
F) OnlySpecific=true ;; else
i) Notify=true ;; LatestOutput=$("$@")
I) PrintReleaseURL=true ;; fi
l) OnlyLabel=true ;;
m) MonoMode=true ;; return $?
M) PrintMarkdownURL=true ;; }
n) DontUpdate=true; AutoMode=true;;
p) AutoPrune=true ;; # File Log Path
R) SkipRecreate=true ;; LogFile="${ScriptWorkDir}/log/${RunTimestamp}.log"
r) DRunUp=true ;; [[ "${LogToFile}" == "true" ]] && mkdir -p $(dirname "${LogFile}")
s) Stopped="-a" ;;
t) Timeout="${OPTARG}" ;; # Assign log level to number (based on OpenTelemetry log severity values)
u) AutoSelfUpdate=true ;; # Acceptable values are recognized strings or an integer between 0 and 24
v) printf "%s\n" "$VERSION"; exit 0 ;; log_severity() {
x) MaxAsync=${OPTARG} ;; local loglevel="$1"
h|*) Help; exit 2 ;; local levelnum=0
case "${loglevel}" in
TRACE)
levelnum=1
;;
DEBUG)
levelnum=5
;;
INFO)
levelnum=9
;;
WARN)
levelnum=13
;;
ERROR)
levelnum=17
;;
FATAL)
levelnum=21
;;
*)
if [[ "${loglevel}" =~ ^[0-9]+$ ]]; then
if [[ $loglevel -ge 0 ]] && [[ $loglevel -le 24 ]]; then
levelnum=${loglevel}
else
levelnum=0
fi
else
levelnum=0
fi
;;
esac esac
done
shift "$((OPTIND-1))" echo ${levelnum}
}
LOG_LEVEL_NUM=$(log_severity $LOG_LEVEL)
# Logger helper functions
# Log and print out. For script managed output.
log_print() {
local logvar="$1"
local loglevel="$2"
local format="$3"
shift 3
levelnum=$(log_severity ${loglevel})
local buffer=""
if [[ $levelnum -ge $LOG_LEVEL_NUM ]]; then
# Don't print if in quiet mode
[[ "$Quiet" == "false" ]] && printf "${format}\n" "$@"
# Also log to buffer and file if configured
log "$logvar" "$loglevel" "$format" "$@"
fi
}
# Log only. For use with command executions that already send output or when no stdout is desired.
log() {
local logvar="$1"
local loglevel="$2"
local format="$3"
shift 3
levelnum=$(log_severity ${loglevel})
local buffer=""
if [[ $levelnum -ge $LOG_LEVEL_NUM ]]; then
printf -v buffer "${format}" "$@" # store in buffer
# optionally output to file if enabled
# Replace all newlines with literal \n and remove all color codes
[[ "${LogToFile}" == "true" ]] && { printf "[%(%Y-%m-%d %H:%M:%S)T] [%-5s] ${format}" -1 "$loglevel" "$@" | awk '{printf "%s\\n", $0}' | sed -r 's/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g'; echo; } >> "${LogFile}" # write to file
declare -n out_array="${logvar}"
out_array+=("${buffer}")
fi
}
log_cleanup() {
find "$(dirname "${LogFile}")" -type f -name '*.log' -mtime +${LogDaysToKeep} -exec rm {} \;
}
print_buffer() {
declare -n buffer="$1"
local BufToString
if [[ "${#buffer[@]}" -gt 0 ]]; then
BufToString=$( printf '%s' "${buffer[@]}" )
echo "${BufToString}"
fi
}
# Print current backups function
print_backups() {
printf "\n%b---%b Currently backed up images %b---%b\n\n" "$c_teal" "$c_blue" "$c_teal" "$c_reset"
docker images --format table | sed -ne '/^REPOSITORY/p' -ne '/^dockcheck/p'
}
[[ "${PrintBackups:-false}" == "true" ]] && { exec_command print_backups; log backupbuf INFO "$LatestOutput"; exit 0; }
# Set $1 to a variable for name filtering later, rewriting if multiple # Set $1 to a variable for name filtering later, rewriting if multiple
SearchName="${1:-}" SearchName="${1:-}"
@ -160,7 +288,6 @@ fi
if [[ "$DontUpdate" == true ]]; then AutoMode=true; fi if [[ "$DontUpdate" == true ]]; then AutoMode=true; fi
if [[ "$MonoMode" == true ]]; then declare c_{red,green,yellow,blue,teal,reset}=""; fi if [[ "$MonoMode" == true ]]; then declare c_{red,green,yellow,blue,teal,reset}=""; fi
if [[ "$Notify" == true ]]; then if [[ "$Notify" == true ]]; then
source "${ScriptWorkDir}/notify_templates/notify_v2.sh"
source_if_exists_or_fail "${ScriptWorkDir}/notify.sh" || source_if_exists_or_fail "${ScriptWorkDir}/notify_templates/notify_v2.sh" || Notify=false source_if_exists_or_fail "${ScriptWorkDir}/notify.sh" || source_if_exists_or_fail "${ScriptWorkDir}/notify_templates/notify_v2.sh" || Notify=false
fi fi
if [[ -n "$Exclude" ]]; then if [[ -n "$Exclude" ]]; then
@ -408,7 +535,7 @@ list_options() {
# Version check & initiate self update # Version check & initiate self update
if [[ "$LatestSnippet" != "undefined" ]]; then if [[ "$LatestSnippet" != "undefined" ]]; then
if [[ "$VERSION" != "$LatestRelease" ]]; then if [[ "$VERSION" != "$LatestRelease" ]]; then
printf "New version available! %b%s%b ⇒ %b%s%b \n Change Notes: %s \n" "$c_yellow" "$VERSION" "$c_reset" "$c_green" "$LatestRelease" "$c_reset" "$LatestChanges" log_print mainbuf INFO "New version available! %b%s%b ⇒ %b%s%b \n Change Notes: %s \n" "$c_yellow" "$VERSION" "$c_reset" "$c_green" "$LatestRelease" "$c_reset" "$LatestChanges"
if [[ "$AutoMode" == false ]]; then if [[ "$AutoMode" == false ]]; then
read -r -p "Would you like to update? y/[n]: " SelfUpdate read -r -p "Would you like to update? y/[n]: " SelfUpdate
[[ "$SelfUpdate" =~ [yY] ]] && self_update [[ "$SelfUpdate" =~ [yY] ]] && self_update
@ -518,7 +645,7 @@ fi
# Asynchronously check the image-hash of every running container VS the registry # Asynchronously check the image-hash of every running container VS the registry
while read -r line; do while read -r line; do
((RegCheckQue+=1)) ((RegCheckQue+=1))
if [[ "$MonoMode" == false ]]; then progress_bar "$RegCheckQue" "$ContCount"; fi if [[ "$MonoMode" == false ]]; then exec_command progress_bar "$RegCheckQue" "$ContCount"; fi
Got=${line%% *} # Extracts the first word (NoUpdates, GotUpdates, GotErrors) Got=${line%% *} # Extracts the first word (NoUpdates, GotUpdates, GotErrors)
item=${line#* } item=${line#* }
@ -612,23 +739,23 @@ if [[ -n "${GotUpdates:-}" ]]; then
if [[ "$DRunUp" == true ]]; then if [[ "$DRunUp" == true ]]; then
docker pull "$ContImage" docker pull "$ContImage"
printf "%s\n" "$i got a new image downloaded, rebuild manually with preferred 'docker run'-parameters" printf "%s\n" "$i got a new image downloaded, rebuild manually with preferred 'docker run'-parameters"
Actions+=("Pull $ContImage;Success;Manual rebuild with 'docker run'-parameters required") log actionbuf INFO "Pull $ContImage;Success;Manual rebuild with 'docker run'-parameters required"
else else
printf "\n%b%s%b has no compose labels, probably started with docker run - %bskipping%b\n\n" "$c_yellow" "$i" "$c_reset" "$c_yellow" "$c_reset" printf "\n%b%s%b has no compose labels, probably started with docker run - %bskipping%b\n\n" "$c_yellow" "$i" "$c_reset" "$c_yellow" "$c_reset"
Actions+=("Pull $ContImage;Skipped;Started with docker run") log actionbuf INFO "Pull $ContImage;Skipped;Started with docker run"
fi fi
continue continue
fi fi
if docker pull "$ContImage"; then if exec_command docker pull "$ContImage"; then
# Removal of the <none>-tag image left behind from backup # Removal of the <none>-tag image left behind from backup
if [[ ! -z "${ContRepoDigests:-}" ]] && [[ -n "${BackupForDays:-}" ]]; then docker rmi "$ContRepoDigests"; fi if [[ ! -z "${ContRepoDigests:-}" ]] && [[ -n "${BackupForDays:-}" ]]; then docker rmi "$ContRepoDigests"; fi
SuccessfulUpdates+=("$i") SuccessfulUpdates+=("$i")
Actions+=("Pull $ContImage;Success") log actionbuf INFO "Pull $ContImage;Success"
else else
printf "\n%bError pulling update for %S. Skipping. %b\n" "$c_red" "$i" "$c_reset" printf "\n%bError pulling update for %S. Skipping. %b\n" "$c_red" "$i" "$c_reset"
FailedUpdates+=("$i") FailedUpdates+=("$i")
Actions+=("Pull $ContImage;Error") log actionbuf ERROR "Pull $ContImage;Error"
fi fi
done done
@ -663,18 +790,17 @@ if [[ -n "${GotUpdates:-}" ]]; then
if [[ "$ContStateRunning" == "true" ]]; then if [[ "$ContStateRunning" == "true" ]]; then
printf "\n%bNow recreating (%s/%s): %b%s%b\n" "$c_teal" "$CurrentQue" "$NumberofUpdates" "$c_blue" "$i" "$c_reset" printf "\n%bNow recreating (%s/%s): %b%s%b\n" "$c_teal" "$CurrentQue" "$NumberofUpdates" "$c_blue" "$i" "$c_reset"
{ [[ "${RestartedStacks[@]}" == *"$ContPath"* ]] && { [[ $OnlySpecific != true ]] || [[ $ContOnlySpecific != true ]] } } && printf "%bStack already restarted. Skipping.%b\n" "$c_yellow" "$c_reset"
else else
printf "\n%bSkipping recreation of %b%s%b as it's not running.%b\n" "$c_yellow" "$c_blue" "$i" "$c_yellow" "$c_reset" printf "\n%bSkipping recreation of %b%s%b as it's not running.%b\n" "$c_yellow" "$c_blue" "$i" "$c_yellow" "$c_reset"
Actions+=("Recreate $i;Skipped;Not Running") log actionbuf INFO "Recreate $i;Skipped;Not Running"
continue continue
fi fi
# Checking if compose-values are empty - hence started with docker run # Checking if compose-values are empty - hence started with docker run
[[ -z "$ContPath" ]] && { echo "Not a compose container, skipping."; Actions+=("Recreate $i;Skipped;Not compose") ; continue; } [[ -z "$ContPath" ]] && { echo "Not a compose container, skipping."; log actionbuf INFO "Recreate $i;Skipped;Not compose" ; continue; }
# cd to the compose-file directory to account for people who use relative volumes # cd to the compose-file directory to account for people who use relative volumes
cd "$ContPath" || { printf "\n%bPath error - skipping%b %s" "$c_red" "$c_reset" "$i"; Actions+=("Recreate $i;Skipped;Path error") ; continue; } cd "$ContPath" || { printf "\n%bPath error - skipping%b %s" "$c_red" "$c_reset" "$i"; log actionbuf INFO "Recreate $i;Skipped;Path error" ; continue; }
# Reformatting path + multi compose # Reformatting path + multi compose
if [[ $ContConfigFile == '/'* ]]; then if [[ $ContConfigFile == '/'* ]]; then
CompleteConfs=$(for conf in ${ContConfigFile//,/ }; do printf -- "-f %s " "$conf"; done) CompleteConfs=$(for conf in ${ContConfigFile//,/ }; do printf -- "-f %s " "$conf"; done)
@ -689,13 +815,34 @@ if [[ -n "${GotUpdates:-}" ]]; then
# Check if the whole stack should be restarted # Check if the whole stack should be restarted
if [[ "$ContRestartStack" == true ]] || [[ "$ForceRestartStacks" == true ]]; then if [[ "$ContRestartStack" == true ]] || [[ "$ForceRestartStacks" == true ]]; then
[[ "${RestartedStacks[@]}" != *"$ContPath"* ]] && { ${DockerBin} ${CompleteConfs} stop; ${DockerBin} ${CompleteConfs} ${ContEnvs} up -d && Actions+=("Recreate $i;Success;Full stack restart") || { printf "\n%bFailed to recreate $i, skipping.%b\n" "$c_red" "$c_reset" ; Actions+=("Recreate $i;Error;Failed to start stack") ; } } # Restart if compose path has not already been restarted
RestartedStacks+=("$ContPath") if [[ "${RestartedStacks[@]}" != *"$ContPath"* ]]; then # Restart if stack has not already been restarted
if ${DockerBin} ${CompleteConfs} stop; ${DockerBin} ${CompleteConfs} ${ContEnvs} up -d; then
log actionbuf INFO "Recreate $i;Success;Full stack restart"
RestartedStacks+=("$ContPath")
else
printf "\n%bFailed to recreate $i, skipping.%b\n" "$c_red" "$c_reset"
log actionbuf INFO "Recreate $i;Error;Failed to start stack"
fi
else
printf "%bStack already restarted. Skipping.%b\n" "$c_yellow" "$c_reset"
fi
else else
{ [[ "${RestartedStacks[@]}" != *"$ContPath"* ]] || [[ -n "${SpecificContainer:-}" ]] } && { ${DockerBin} ${CompleteConfs} ${ContEnvs} up -d ${SpecificContainer} && Actions+=("Recreate $i;Success;${SpecificContainer:-Stack}") || { printf "\n%bFailed to recreate $i, skipping.%b\n" "$c_red" "$c_reset" ; Actions+=("Recreate $i;Error;Failed to start ${SpecificContainer:-Stack}") ; } } # Restart if compose path has not already been restarted or specific container(s) are configured to be restarted individually
if [[ -z "${SpecificContainer:-}" ]]; then if [[ "${RestartedStacks[@]}" != *"$ContPath"* ]] || [[ -n "${SpecificContainer:-}" ]]; then
RestartedStacks+=("$ContPath") if ${DockerBin} ${CompleteConfs} ${ContEnvs} up -d ${SpecificContainer}; then
fi log actionbuf INFO "Recreate $i;Success;${SpecificContainer:-Stack}"
# Consider stack restarted only if specific container is not set
if [[ -z "${SpecificContainer:-}" ]]; then
RestartedStacks+=("$ContPath")
fi
else
printf "\n%bFailed to recreate $i, skipping.%b\n" "$c_red" "$c_reset"
log actionbuf INFO "Recreate $i;Error;Failed to start ${SpecificContainer:-Stack}"
fi
else
printf "%bStack already restarted. Skipping.%b\n" "$c_yellow" "$c_reset"
fi
fi fi
done done
fi fi
@ -704,7 +851,7 @@ if [[ -n "${GotUpdates:-}" ]]; then
# Trigger pruning only when backup-function is not used # Trigger pruning only when backup-function is not used
if [[ -z "${BackupForDays:-}" ]]; then if [[ -z "${BackupForDays:-}" ]]; then
if [[ "$AutoPrune" == false ]] && [[ "$AutoMode" == false ]]; then printf "\n"; read -rep "Would you like to prune all dangling images? y/[n]: " AutoPrune; fi if [[ "$AutoPrune" == false ]] && [[ "$AutoMode" == false ]]; then printf "\n"; read -rep "Would you like to prune all dangling images? y/[n]: " AutoPrune; fi
if [[ "$AutoPrune" == true ]] || [[ "$AutoPrune" =~ [yY] ]]; then printf "\nAuto pruning.."; docker image prune -f && Actions+=("Prune images;Success"); fi if [[ "$AutoPrune" == true ]] || [[ "$AutoPrune" =~ [yY] ]]; then printf "\nAuto pruning.."; docker image prune -f && log actionbuf INFO "Prune images;Success"; fi
fi fi
else else
@ -718,6 +865,10 @@ fi
[[ -n "${BackupForDays:-}" ]] && remove_backups [[ -n "${BackupForDays:-}" ]] && remove_backups
# Send final summary notification if enabled # Send final summary notification if enabled
[[ "${Notify}" == true ]] && [[ "${ENABLE_SUMMARY_NOTIFICATION:-}" == true ]] && [[ "${#Actions[@]} -gt 0" ]] && { exec_if_exists_or_fail send_summary_notification || printf "Could not source summary notification function.\n"; } [[ "${Notify}" == true ]] && [[ "${ENABLE_SUMMARY_NOTIFICATION:-}" == true ]] && [[ -n "${actionbuf:-}" ]] && [[ "${#actionbuf[@]} -gt 0" ]] && { exec_if_exists_or_fail send_summary_notification || printf "Could not source summary notification function.\n"; }
if [[ -d $(dirname "${LogFile}") ]]; then
log_cleanup
fi
exit 0 exit 0

View file

@ -317,7 +317,7 @@ send_summary_notification() {
MessageTitle="$FromHost - Action summary" MessageTitle="$FromHost - Action summary"
UpdToString=$( printf '%s\\n' "${Actions[@]}" ) UpdToString=$( printf '%s\\n' "${actionbuf[@]}" )
UpdToString="${UpdToString%, }" UpdToString="${UpdToString%, }"
UpdToString=${UpdToString%\\n} UpdToString=${UpdToString%\\n}