Merge pull request #50 from mag37/printf_rework

Added option -m, monochrome mode + cleaning.
This commit is contained in:
mag37 2023-12-23 20:54:45 +01:00 committed by GitHub
commit 80a81f59d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 25 deletions

View file

@ -16,6 +16,7 @@
### :bell: Recent changes ### :bell: Recent changes
- **v0.3.1**: Addded option `-m` , monochrome mode - no printf color codes.
- **v0.3.0**: Added option `-d N`, age (days) new images have to be before being pulled and updated. - **v0.3.0**: Added option `-d N`, age (days) new images have to be before being pulled and updated.
- **v0.2.6**: regctl check / download logic changed. Now using the scripts directory as primary location. - **v0.2.6**: regctl check / download logic changed. Now using the scripts directory as primary location.
- **v0.2.5**: Added a new option `-s` to include stopped containers in the check for updates. - **v0.2.5**: Added a new option `-s` to include stopped containers in the check for updates.

View file

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
VERSION="v0.3.0" VERSION="v0.3.1"
### ChangeNotes: Added feature (-d N) to only update to new images that are N+ days old. ### ChangeNotes: Added feature (-m) Monochrome Mode, no printf color codes.
Github="https://github.com/mag37/dockcheck" Github="https://github.com/mag37/dockcheck"
RawUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/dockcheck.sh" RawUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/dockcheck.sh"
@ -20,24 +20,35 @@ Help() {
echo "Example: dockcheck.sh -y -d 10 -e nextcloud,heimdall" echo "Example: dockcheck.sh -y -d 10 -e nextcloud,heimdall"
echo echo
echo "Options:" echo "Options:"
echo "-h Print this Help."
echo "-a|y Automatic updates, without interaction." echo "-a|y Automatic updates, without interaction."
echo "-n No updates, only checking availability."
echo "-e X Exclude containers, separated by comma."
echo "-d N Only update to new images that are N+ days old. Lists too recent with +prefix and age. 2xSlower." echo "-d N Only update to new images that are N+ days old. Lists too recent with +prefix and age. 2xSlower."
echo "-e X Exclude containers, separated by comma."
echo "-h Print this Help."
echo "-m Monochrome mode, no printf color codes."
echo "-n No updates, only checking availability."
echo "-p Auto-Prune dangling images after update." echo "-p Auto-Prune dangling images after update."
echo "-r Allow updating images for docker run, wont update the container" echo "-r Allow updating images for docker run, wont update the container"
echo "-s Include stopped containers in the check. (Logic: docker ps -a)" echo "-s Include stopped containers in the check. (Logic: docker ps -a)"
} }
### Colors:
c_red="\033[0;31m"
c_green="\033[0;32m"
c_yellow="\033[0;33m"
c_blue="\033[0;34m"
c_teal="\033[0;36m"
c_reset="\033[0m"
Stopped="" Stopped=""
while getopts "aynprhse:d:" options; do while getopts "aynprhsme:d:" options; do
case "${options}" in case "${options}" in
a|y) UpdYes="yes" ;; a|y) AutoUp="yes" ;;
n) UpdYes="no" ;; n) AutoUp="no" ;;
r) DrUp="yes" ;; r) DRunUp="yes" ;;
p) PruneQ="yes" ;; p) AutoPrune="yes" ;;
e) Exclude=${OPTARG} ;; e) Exclude=${OPTARG} ;;
m) declare c_{red,green,yellow,blue,teal,reset}="" ;;
s) Stopped="-a" ;; s) Stopped="-a" ;;
d) DaysOld=${OPTARG} d) DaysOld=${OPTARG}
if ! [[ $DaysOld =~ ^[0-9]+$ ]] ; then { printf "Days -d argument given (%s) is not a number.\n" "${DaysOld}" ; exit 2 ; } ; fi ;; if ! [[ $DaysOld =~ ^[0-9]+$ ]] ; then { printf "Days -d argument given (%s) is not a number.\n" "${DaysOld}" ; exit 2 ; } ; fi ;;
@ -117,7 +128,7 @@ datecheck() {
### Version check & initiate self update ### Version check & initiate self update
[[ "$VERSION" != "$LatestRelease" ]] && { printf "New version available! Local: %s - Latest: %s \n Change Notes: %s \n" "$VERSION" "$LatestRelease" "$LatestChanges" ; [[ -z "$UpdYes" ]] && self_update_select ; } [[ "$VERSION" != "$LatestRelease" ]] && { printf "New version available! Local: %s - Latest: %s \n Change Notes: %s \n" "$VERSION" "$LatestRelease" "$LatestChanges" ; [[ -z "$AutoUp" ]] && self_update_select ; }
### Set $1 to a variable for name filtering later. ### Set $1 to a variable for name filtering later.
SearchName="$1" SearchName="$1"
@ -172,7 +183,7 @@ done
### Listing typed exclusions: ### Listing typed exclusions:
if [[ -n ${Excludes[*]} ]] ; then if [[ -n ${Excludes[*]} ]] ; then
printf "\n\033[0;34mExcluding these names:\033[0m\n" printf "\n%bExcluding these names:%b\n" $c_blue $c_reset
printf "%s\n" "${Excludes[@]}" printf "%s\n" "${Excludes[@]}"
printf "\n" printf "\n"
fi fi
@ -211,27 +222,27 @@ UpdCount="${#GotUpdates[@]}"
### List what containers got updates or not ### List what containers got updates or not
if [[ -n ${NoUpdates[*]} ]] ; then if [[ -n ${NoUpdates[*]} ]] ; then
printf "\n\033[0;32mContainers on latest version:\033[0m\n" printf "\n%bContainers on latest version:%b\n" "$c_green" "$c_reset"
printf "%s\n" "${NoUpdates[@]}" printf "%s\n" "${NoUpdates[@]}"
fi fi
if [[ -n ${GotErrors[*]} ]] ; then if [[ -n ${GotErrors[*]} ]] ; then
printf "\n\033[0;31mContainers with errors, wont get updated:\033[0m\n" printf "\n%bContainers with errors, wont get updated:%b\n" "$c_red" "$c_reset"
printf "%s\n" "${GotErrors[@]}" printf "%s\n" "${GotErrors[@]}"
fi fi
if [[ -n ${GotUpdates[*]} ]] ; then if [[ -n ${GotUpdates[*]} ]] ; then
printf "\n\033[0;33mContainers with updates available:\033[0m\n" printf "\n%bContainers with updates available:%b\n" "$c_yellow" "$c_reset"
[[ -z "$UpdYes" ]] && options || printf "%s\n" "${GotUpdates[@]}" [[ -z "$AutoUp" ]] && options || printf "%s\n" "${GotUpdates[@]}"
fi fi
### Optionally get updates if there's any ### Optionally get updates if there's any
if [ -n "$GotUpdates" ] ; then if [ -n "$GotUpdates" ] ; then
if [ -z "$UpdYes" ] ; then if [ -z "$AutoUp" ] ; then
printf "\n\033[0;36mChoose what containers to update.\033[0m\n" printf "\n%bChoose what containers to update.%b\n" "$c_teal" "$c_reset"
choosecontainers choosecontainers
else else
SelectedUpdates=( "${GotUpdates[@]}" ) SelectedUpdates=( "${GotUpdates[@]}" )
fi fi
if [ "$UpdYes" == "${UpdYes#[Nn]}" ] ; then if [ "$AutoUp" == "${AutoUp#[Nn]}" ] ; then
NumberofUpdates="${#SelectedUpdates[@]}" NumberofUpdates="${#SelectedUpdates[@]}"
CurrentQue=0 CurrentQue=0
for i in "${SelectedUpdates[@]}" for i in "${SelectedUpdates[@]}"
@ -245,11 +256,11 @@ if [ -n "$GotUpdates" ] ; then
ContImage=$(docker inspect "$i" --format='{{.Config.Image}}') ContImage=$(docker inspect "$i" --format='{{.Config.Image}}')
### Checking if compose-values are empty - hence started with docker run: ### Checking if compose-values are empty - hence started with docker run:
if [ -z "$ContPath" ] ; then if [ -z "$ContPath" ] ; then
if [ "$DrUp" == "yes" ] ; then if [ "$DRunUp" == "yes" ] ; 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"
else else
printf "\n\033[33;1m%s\033[0m has no compose labels, probably started with docker run - \033[33;1mskipping\033[0m\n\n" "$i" 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"
fi fi
continue continue
fi fi
@ -261,7 +272,7 @@ if [ -n "$GotUpdates" ] ; then
fi fi
### cd to the compose-file directory to account for people who use relative volumes, eg - ${PWD}/data:data ### cd to the compose-file directory to account for people who use relative volumes, eg - ${PWD}/data:data
cd "$ContPath" || { echo "Path error - skipping $i" ; continue ; } cd "$ContPath" || { echo "Path error - skipping $i" ; continue ; }
printf "\n\033[0;36mNow updating (%s/%s): \033[0;34m%s\033[0m\n" "$CurrentQue" "$NumberofUpdates" "$i" printf "\n%bNow updating (%s/%s): %b%s%b\n" "$c_teal" "$CurrentQue" "$NumberofUpdates" "$c_blue" "$i" "$c_reset"
docker pull "$ContImage" docker pull "$ContImage"
### Reformat for multi-compose: ### Reformat for multi-compose:
IFS=',' read -r -a Confs <<< "$ComposeFile" ; unset IFS IFS=',' read -r -a Confs <<< "$ComposeFile" ; unset IFS
@ -274,9 +285,9 @@ if [ -n "$GotUpdates" ] ; then
$DockerBin ${CompleteConfs[@]} up -d "$ContName" # unquoted array to allow split - rework? $DockerBin ${CompleteConfs[@]} up -d "$ContName" # unquoted array to allow split - rework?
fi fi
done done
printf "\033[0;32mAll done!\033[0m\n" printf "\n%bAll done!%b\n" "$c_green" "$c_reset"
[[ -z "$PruneQ" ]] && read -r -p "Would you like to prune dangling images? y/[n]: " PruneQ [[ -z "$AutoPrune" ]] && read -r -p "Would you like to prune dangling images? y/[n]: " AutoPrune
[[ "$PruneQ" =~ [yY] ]] && docker image prune -f [[ "$AutoPrune" =~ [yY] ]] && docker image prune -f
else else
printf "\nNo updates installed, exiting.\n" printf "\nNo updates installed, exiting.\n"
fi fi