From 78d11d1b37b1b1b0712111b6b8a65095718b5f4a Mon Sep 17 00:00:00 2001 From: mag37 Date: Wed, 31 Jan 2024 21:33:14 +0100 Subject: [PATCH 1/5] Self update rework Now just y/n and checking if it's the right git directory, else curl/wget. --- dockcheck.sh | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/dockcheck.sh b/dockcheck.sh index aca5a51..fe9e819 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -63,37 +63,35 @@ while getopts "aynpfrhlisme:d:" options; do done shift "$((OPTIND-1))" -self_update_git() { - cd "$ScriptWorkDir" || { printf "Path error, skipping update.\n" ; return ; } - [[ $(builtin type -P git) ]] || { printf "Git not installed, skipping update.\n" ; return ; } - ScriptUpstream=$(git rev-parse --abbrev-ref --symbolic-full-name "@{upstream}") || { printf "Script not in git directory, choose a different method.\n" ; self_update_select ; return ; } - git fetch - [ -n "$(git diff --name-only "$ScriptUpstream" "$ScriptName")" ] && { - printf "%s\n" "Pulling the latest version." - git pull --force - printf "%s\n" "--- starting over with the updated version ---" - cd - || { printf "Path error.\n" ; return ; } - exec "$ScriptPath" "${ScriptArgs[@]}" # run the new script with old arguments - exit 1 # exit the old instance - } - echo "Local is already latest." -} self_update_curl() { cp "$ScriptPath" "$ScriptPath".bak if [[ $(builtin type -P curl) ]]; then curl -L $RawUrl > "$ScriptPath" ; chmod +x "$ScriptPath" - printf "%s\n" "--- starting over with the updated version ---" + printf "\n%s\n" "--- starting over with the updated version ---" + exec "$ScriptPath" "${ScriptArgs[@]}" # run the new script with old arguments + exit 1 # exit the old instance + elif [[ $(builtin type -P wget) ]]; then + wget $RawUrl -O "$ScriptPath" ; chmod +x "$ScriptPath" + printf "\n%s\n" "--- starting over with the updated version ---" exec "$ScriptPath" "${ScriptArgs[@]}" # run the new script with old arguments exit 1 # exit the old instance else - printf "curl not available - download the update manually: %s \n" "$RawUrl" + printf "curl/wget not available - download the update manually: %s \n" "$RawUrl" fi } -self_update_select() { - read -r -p "Choose update procedure (or do it manually) - git/curl/[no]: " SelfUpQ - if [[ "$SelfUpQ" == "git" ]]; then self_update_git ; - elif [[ "$SelfUpQ" == "curl" ]]; then self_update_curl ; - else printf "Download it manually from the repo: %s \n\n" "$Github" + +self_update() { + cd "$ScriptWorkDir" || { printf "Path error, skipping update.\n" ; return ; } + if [[ $(builtin type -P git) ]] && [[ "$(git ls-remote --get-url)" =~ .*"mag37/dockcheck".* ]] ; then + printf "\n%s\n" "Pulling the latest version." + git pull --force || { printf "Git error, manually pull/clone.\n" ; return ; } + printf "\n%s\n" "--- starting over with the updated version ---" + cd - || { printf "Path error.\n" ; return ; } + exec "$ScriptPath" "${ScriptArgs[@]}" # run the new script with old arguments + exit 1 # exit the old instance + else + cd - + self_update_curl fi } @@ -145,7 +143,13 @@ progress_bar() { } ### Version check & initiate self update -[[ "$VERSION" != "$LatestRelease" ]] && { 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" ; [[ -z "$AutoUp" ]] && self_update_select ; } +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" + if [[ -z "$AutoUp" ]] ; then + read -r -p "Would you like to update? y/[n]: " SelfUpdate + [[ "$SelfUpdate" =~ [yY] ]] && self_update + fi +fi ### Set $1 to a variable for name filtering later. SearchName="$1" From 5ed3b90ca4e108414fe5d95bb3118b65850d885b Mon Sep 17 00:00:00 2001 From: mag37 Date: Thu, 1 Feb 2024 20:19:04 +0100 Subject: [PATCH 2/5] syntax cleanup --- dockcheck.sh | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/dockcheck.sh b/dockcheck.sh index fe9e819..f39bfce 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -52,7 +52,7 @@ while getopts "aynpfrhlisme:d:" options; do p) AutoPrune="yes" ;; l) OnlyLabel=true ;; f) ForceRestartStacks=true ;; - i) [ -s $ScriptWorkDir/notify.sh ] && { source $ScriptWorkDir/notify.sh ; Notify="yes" ; } ;; + i) [ -s "$ScriptWorkDir"/notify.sh ] && { source "$ScriptWorkDir"/notify.sh ; Notify="yes" ; } ;; e) Exclude=${OPTARG} ;; m) declare c_{red,green,yellow,blue,teal,reset}="" ;; s) Stopped="-a" ;; @@ -90,7 +90,7 @@ self_update() { exec "$ScriptPath" "${ScriptArgs[@]}" # run the new script with old arguments exit 1 # exit the old instance else - cd - + cd - || { printf "Path error.\n" ; return ; } self_update_curl fi } @@ -123,7 +123,7 @@ choosecontainers() { datecheck() { ImageDate=$($regbin image inspect "$RepoUrl" --format='{{.Created}}' | cut -d" " -f1 ) ImageAge=$(( ( $(date +%s) - $(date -d "$ImageDate" +%s) )/86400 )) - if [ $ImageAge -gt $DaysOld ] ; then + if [ "$ImageAge" -gt "$DaysOld" ] ; then return 0 else return 1 @@ -133,13 +133,13 @@ datecheck() { progress_bar() { QueCurrent="$1" QueTotal="$2" - ((Percent=100*${QueCurrent}/${QueTotal})) - ((Complete=50*${Percent}/100)) # change first number for width (50) - ((Left=50-${Complete})) # change first number for width (50) + ((Percent=100*QueCurrent/QueTotal)) + ((Complete=50*Percent/100)) # change first number for width (50) + ((Left=50-Complete)) # change first number for width (50) BarComplete=$(printf "%${Complete}s" | tr " " "#") BarLeft=$(printf "%${Left}s" | tr " " "-") - [[ $QueTotal == $QueCurrent ]] || printf "\r[%s%s] %s/%s " $BarComplete $BarLeft $QueCurrent $QueTotal - [[ $QueTotal == $QueCurrent ]] && printf "\r[%b%s%b] %s/%s \n" $c_teal $BarComplete $c_reset $QueCurrent $QueTotal + [[ "$QueTotal" == "$QueCurrent" ]] || printf "\r[%s%s] %s/%s " "$BarComplete" "$BarLeft" "$QueCurrent" "$QueTotal" + [[ "$QueTotal" == "$QueCurrent" ]] && printf "\r[%b%s%b] %s/%s \n" "$c_teal" "$BarComplete" "$c_reset" "$QueCurrent" "$QueTotal" } ### Version check & initiate self update @@ -204,7 +204,7 @@ done ### Listing typed exclusions: if [[ -n ${Excludes[*]} ]] ; then - printf "\n%bExcluding these names:%b\n" $c_blue $c_reset + printf "\n%bExcluding these names:%b\n" "$c_blue" "$c_reset" printf "%s\n" "${Excludes[@]}" printf "\n" fi @@ -216,7 +216,7 @@ RegCheckQue=0 ### Check the image-hash of every running container VS the registry for i in $(docker ps $Stopped --filter "name=$SearchName" --format '{{.Names}}') ; do ((RegCheckQue+=1)) - progress_bar $RegCheckQue $DocCount + progress_bar "$RegCheckQue" "$DocCount" ### Looping every item over the list of excluded names and skipping: for e in "${Excludes[@]}" ; do [[ "$i" == "$e" ]] && continue 2 ; done RepoUrl=$(docker inspect "$i" --format='{{.Config.Image}}') @@ -260,7 +260,7 @@ fi if [[ -n ${GotUpdates[*]} ]] ; then printf "\n%bContainers with updates available:%b\n" "$c_yellow" "$c_reset" [[ -z "$AutoUp" ]] && options || printf "%s\n" "${GotUpdates[@]}" - [[ ! -z "$Notify" ]] && { [[ $(type -t send_notification) == function ]] && send_notification "${GotUpdates[@]}" || printf "Could not source notification function.\n" ; } + [[ -n "$Notify" ]] && { [[ $(type -t send_notification) == function ]] && send_notification "${GotUpdates[@]}" || printf "Could not source notification function.\n" ; } fi ### Optionally get updates if there's any @@ -313,7 +313,7 @@ if [ -n "$GotUpdates" ] ; then ### Check if the container got an environment file set, use it if so: if [ -n "$ContEnv" ]; then ### prepare env-files arguments - ContEnvs=$(for env in ${ContEnv//,/ } ; do printf -- "--env-file %s " $env; done) + ContEnvs=$(for env in ${ContEnv//,/ } ; do printf -- "--env-file %s " "$env"; done) ### Check if the whole stack should be restarted if [[ "$ContRestartStack" == true ]] || [[ "$ForceRestartStacks" == true ]] ; then $DockerBin ${CompleteConfs[@]} stop ; $DockerBin ${CompleteConfs[@]} ${ContEnvs} up -d From 4c26768374948637471c65ec8cf604da4c358ddb Mon Sep 17 00:00:00 2001 From: mag37 Date: Fri, 2 Feb 2024 20:52:01 +0100 Subject: [PATCH 3/5] Added -v option, prints version --- dockcheck.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dockcheck.sh b/dockcheck.sh index f39bfce..c2d2688 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -31,8 +31,9 @@ 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 "-r Allow updating images for docker run, wont update the container" - echo "-s Include stopped containers in the check. (Logic: docker ps -a)" + 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 "-v Prints current version." } ### Colors: @@ -44,7 +45,7 @@ c_teal="\033[0;36m" c_reset="\033[0m" Stopped="" -while getopts "aynpfrhlisme:d:" options; do +while getopts "aynpfrhlisvme:d:" options; do case "${options}" in a|y) AutoUp="yes" ;; n) AutoUp="no" ;; @@ -56,6 +57,7 @@ while getopts "aynpfrhlisme:d:" options; do e) Exclude=${OPTARG} ;; m) declare c_{red,green,yellow,blue,teal,reset}="" ;; s) Stopped="-a" ;; + v) printf "%s\n" "$VERSION" ; exit 0 ;; d) DaysOld=${OPTARG} if ! [[ $DaysOld =~ ^[0-9]+$ ]] ; then { printf "Days -d argument given (%s) is not a number.\n" "${DaysOld}" ; exit 2 ; } ; fi ;; h|*) Help ; exit 2 ;; From 292b3e1a32ff24fe6b2c1f4b774a2918d647bf7b Mon Sep 17 00:00:00 2001 From: mag37 Date: Fri, 2 Feb 2024 21:18:19 +0100 Subject: [PATCH 4/5] Version bump + readme --- README.md | 12 ++++++++++++ dockcheck.sh | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4acc102..957f19e 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ ___ ## :bell: Changelog +- **v0.3.4**: Reworked selfupdate (auto git/curl/wget), general syntax cleanup, added -v for version. - **v0.3.8**: Fixed `--env-file` logic to work with multiple env-files. - **v0.3.7**: Added support for [labels](#bookmark-labels), added the `-f` option (force restart stack). - **v0.3.6**: Added pushbullet template. @@ -57,6 +58,7 @@ Options:" -p Auto-Prune dangling images after update. -r Allow updating images for docker run, wont update the container. -s Include stopped containers in the check. (Logic: docker ps -a). +-v Prints current version. ``` @@ -126,6 +128,16 @@ chmod 755 regctl ``` Test it with `./regctl --help` and then either add the file to the same path as *dockcheck.sh* or in your path (eg. `~/.local/bin/regctl`). +## :open_hands: Function to auth with docker hub before running +### Example +Change names, paths, and remove cat+password flag if you rather get prompted: +```sh +function dchk { + cat ~/pwd.txt | docker login --username YourUser --password-stdin + ~/dockcheck.sh "$@" +} +``` + ## :hammer: Known issues - No detailed error feedback (just skip + list what's skipped). - Not respecting `--profile` options when re-creating the container. diff --git a/dockcheck.sh b/dockcheck.sh index c2d2688..723df39 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -VERSION="v0.3.8" -### ChangeNotes: Fixed --env-file logic to work with multiple env-files. +VERSION="v0.4.0" +### ChangeNotes: Reworked selfupdate (auto git/curl/wget), cleanups, -v for version. Github="https://github.com/mag37/dockcheck" RawUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/dockcheck.sh" From f853bfd318c67535870fa2567bfdb89feff02ea7 Mon Sep 17 00:00:00 2001 From: mag37 Date: Fri, 2 Feb 2024 21:21:56 +0100 Subject: [PATCH 5/5] typos --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 957f19e..3f972ab 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ ___ ## :bell: Changelog -- **v0.3.4**: Reworked selfupdate (auto git/curl/wget), general syntax cleanup, added -v for version. +- **v0.4.0**: Reworked selfupdate (auto git/curl/wget), general syntax cleanup, added -v for version. - **v0.3.8**: Fixed `--env-file` logic to work with multiple env-files. - **v0.3.7**: Added support for [labels](#bookmark-labels), added the `-f` option (force restart stack). - **v0.3.6**: Added pushbullet template. @@ -129,8 +129,7 @@ chmod 755 regctl Test it with `./regctl --help` and then either add the file to the same path as *dockcheck.sh* or in your path (eg. `~/.local/bin/regctl`). ## :open_hands: Function to auth with docker hub before running -### Example -Change names, paths, and remove cat+password flag if you rather get prompted: +**Example** - Change names, paths, and remove cat+password flag if you rather get prompted: ```sh function dchk { cat ~/pwd.txt | docker login --username YourUser --password-stdin