mirror of
https://github.com/mag37/dockcheck.git
synced 2026-02-25 12:44:17 +01:00
Merge pull request #128 from Thaurin/parallel_check
Add async checking for updates for improved performance
This commit is contained in:
commit
0c6674ac8e
1 changed files with 51 additions and 11 deletions
62
dockcheck.sh
62
dockcheck.sh
|
|
@ -46,6 +46,7 @@ c_blue="\033[0;34m"
|
||||||
c_teal="\033[0;36m"
|
c_teal="\033[0;36m"
|
||||||
c_reset="\033[0m"
|
c_reset="\033[0m"
|
||||||
|
|
||||||
|
MaxAsync=32
|
||||||
Timeout=10
|
Timeout=10
|
||||||
Stopped=""
|
Stopped=""
|
||||||
while getopts "aynpfrhlisvmc:e:d:t:" options; do
|
while getopts "aynpfrhlisvmc:e:d:t:" options; do
|
||||||
|
|
@ -282,31 +283,70 @@ if [[ $t_out ]]; then
|
||||||
else t_out=""
|
else t_out=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check the image-hash of every running container VS the registry
|
check_image() {
|
||||||
for i in $(docker ps $Stopped --filter "name=$SearchName" --format '{{.Names}}') ; do
|
i="$1"
|
||||||
((RegCheckQue+=1))
|
local Excludes=($Excludes_string)
|
||||||
progress_bar "$RegCheckQue" "$ContCount"
|
for e in "${Excludes[@]}" ; do
|
||||||
# Looping every item over the list of excluded names and skipping
|
if [[ "$i" == "$e" ]]; then
|
||||||
for e in "${Excludes[@]}" ; do [[ "$i" == "$e" ]] && continue 2 ; done
|
echo Skip $i
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
local NoUpdates GotUpdates GotErrors
|
||||||
ImageId=$(docker inspect "$i" --format='{{.Image}}')
|
ImageId=$(docker inspect "$i" --format='{{.Image}}')
|
||||||
RepoUrl=$(docker inspect "$i" --format='{{.Config.Image}}')
|
RepoUrl=$(docker inspect "$i" --format='{{.Config.Image}}')
|
||||||
LocalHash=$(docker image inspect "$ImageId" --format '{{.RepoDigests}}')
|
LocalHash=$(docker image inspect "$ImageId" --format '{{.RepoDigests}}')
|
||||||
|
|
||||||
# Checking for errors while setting the variable
|
# Checking for errors while setting the variable
|
||||||
if RegHash=$(${t_out} $regbin -v error image digest --list "$RepoUrl" 2>&1) ; then
|
if RegHash=$(${t_out} $regbin -v error image digest --list "$RepoUrl" 2>&1) ; then
|
||||||
if [[ "$LocalHash" = *"$RegHash"* ]] ; then
|
if [[ "$LocalHash" = *"$RegHash"* ]] ; then
|
||||||
NoUpdates+=("$i")
|
echo NoUpdates "$i"
|
||||||
else
|
else
|
||||||
if [[ -n "$DaysOld" ]] && ! datecheck ; then
|
if [[ -n "$DaysOld" ]] && ! datecheck ; then
|
||||||
NoUpdates+=("+$i ${ImageAge}d")
|
echo NoUpdates "+$i ${ImageAge}d"
|
||||||
else
|
else
|
||||||
GotUpdates+=("$i")
|
echo GotUpdates "$i"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
# Here the RegHash is the result of an error code
|
# Here the RegHash is the result of an error code
|
||||||
GotErrors+=("$i - ${RegHash}")
|
echo GotErrors "$i - ${RegHash}"
|
||||||
fi
|
fi
|
||||||
done
|
}
|
||||||
|
|
||||||
|
# Make required functions and variables available to subprocesses
|
||||||
|
export -f check_image datecheck
|
||||||
|
export Excludes_string="${Excludes[@]}" # Can only export scalar variables
|
||||||
|
export t_out regbin RepoUrl DaysOld
|
||||||
|
|
||||||
|
# Check for POSIX xargs with -P option, fallback without async
|
||||||
|
if (echo "test" | xargs -P 10 >/dev/null 2>&1) ; then
|
||||||
|
XargsAsync="-P $MaxAsync"
|
||||||
|
else
|
||||||
|
XargsAsync=""
|
||||||
|
printf "%bMissing POSIX xargs, consider installing 'findutils' for asynchronous lookups.%b\n" "$c_red" "$c_reset"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Asynchronously check the image-hash of every running container VS the registry
|
||||||
|
while read -r line; do
|
||||||
|
((RegCheckQue+=1))
|
||||||
|
progress_bar "$RegCheckQue" "$ContCount"
|
||||||
|
|
||||||
|
Got=${line%% *} # Extracts the first word (NoUpdates, GotUpdates, GotErrors)
|
||||||
|
item=${line#* }
|
||||||
|
|
||||||
|
case "$Got" in
|
||||||
|
NoUpdates) NoUpdates+=("$item") ;;
|
||||||
|
GotUpdates) GotUpdates+=("$item") ;;
|
||||||
|
GotErrors) GotErrors+=("$item") ;;
|
||||||
|
Skip) ;;
|
||||||
|
*) echo "Error! Unexpected output from subprocess: ${line}" ;;
|
||||||
|
esac
|
||||||
|
done < <( \
|
||||||
|
docker ps $Stopped --filter "name=$SearchName" --format '{{.Names}}' | \
|
||||||
|
xargs ${XargsAsync} -I {} bash -c 'check_image "{}"' \
|
||||||
|
)
|
||||||
|
|
||||||
# Sort arrays alphabetically
|
# Sort arrays alphabetically
|
||||||
IFS=$'\n'
|
IFS=$'\n'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue