From fbba77dc1f6f11e230c78c49b3875188ca36be90 Mon Sep 17 00:00:00 2001 From: mag37 Date: Fri, 22 Nov 2024 11:00:45 +0100 Subject: [PATCH 1/8] jq downloading functions Added distribution checker for packagemanager installs. Added generic static binary downloader. --- dockcheck.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/dockcheck.sh b/dockcheck.sh index 31fdf6d..40a7edf 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -173,6 +173,53 @@ SearchName="$1" # Create array of excludes IFS=',' read -r -a Excludes <<< "$Exclude" ; unset IFS +# Static binary downloader for dependencies +binary_downloader() { + BinaryName="$1" + BinaryUrl="$2" + case "$(uname --machine)" in + x86_64|amd64) architecture="amd64" ;; + arm64|aarch64) architecture="arm64";; + *) echo "Architecture not supported, exiting." ; exit 1;; + esac + GetUrl="${BinaryUrl/TEMP/"$architecture"}" + if [[ $(command -v curl) ]]; then curl -L $GetUrl > "$ScriptWorkDir/$BinaryName" ; + elif [[ $(command -v wget) ]]; then wget $GetUrl -O "$ScriptWorkDir/$BinaryName" ; + else printf "%s\n" "curl/wget not available - get $BinaryName manually from the repo link, exiting."; exit 1; + fi + [[ -f "$ScriptWorkDir/$BinaryName" ]] && chmod +x "$ScriptWorkDir/$BinaryName" +} + +distro_checker() { + if [[ -f /etc/arch-release ]] ; then PkgInstaller="pacman -S" + elif [[ -f /etc/redhat-release ]] ; then PkgInstaller="dnf install" + elif [[ -f /etc/SuSE-release ]] ; then PkgInstaller="zypper install" + elif [[ -f /etc/debian_version ]] ; then PkgInstaller="apt-get install" + else PkgInstaller="ERROR" + fi +} + +# Dependency check for jq in PATH or directory +if [[ $(command -v jq) ]]; then jqbin="jq" ; +elif [[ -f "$ScriptWorkDir/jq" ]]; then jqbin="$ScriptWorkDir/jq" ; +else + printf "%s\n" "Required dependency 'jq' missing, do you want to install it?" + read -r -p "y: With packagemanager (sudo). / s: Download static binary. y/s/[n] " GetJq + GetJq=${GetJq:-no} # set default to no if nothing is given + if [[ "$GetJq" =~ [yYsS] ]] ; then + [[ "$GetJq" =~ [yY] ]] && distro_checker + if [[ -n "$PkgInstaller" && "$PkgInstaller" != "ERROR" ]] ; then + (sudo $PkgInstaller jq) # is this the best way? Rewrite so that if it fails it goes to static + else + binary_downloader "jq" "https://github.com/jqlang/jq/releases/latest/download/jq-linux-TEMP" + [[ -f "$ScriptWorkDir/jq" ]] && jqbin="$ScriptWorkDir/jq" + fi + else printf "%s\n" "Dependency missing, quitting." ; exit 1 ; + fi +fi +# Final check if binary is correct +$jqbin --version &> /dev/null || { printf "%s\n" "jq is not working - try to remove it and re-download it, exiting."; exit 1; } + # Check if required binary exists in PATH or directory if [[ $(command -v regctl) ]]; then regbin="regctl" ; elif [[ -f "$ScriptWorkDir/regctl" ]]; then regbin="$ScriptWorkDir/regctl" ; From d98d052af77a963fe4a54cb9d71651b9dfb408b1 Mon Sep 17 00:00:00 2001 From: mag37 Date: Fri, 22 Nov 2024 11:05:16 +0100 Subject: [PATCH 2/8] rewrote regctl-download to use new functions --- dockcheck.sh | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/dockcheck.sh b/dockcheck.sh index 40a7edf..af45552 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -220,27 +220,15 @@ fi # Final check if binary is correct $jqbin --version &> /dev/null || { printf "%s\n" "jq is not working - try to remove it and re-download it, exiting."; exit 1; } -# Check if required binary exists in PATH or directory +# Dependency check for regctl in PATH or directory if [[ $(command -v regctl) ]]; then regbin="regctl" ; elif [[ -f "$ScriptWorkDir/regctl" ]]; then regbin="$ScriptWorkDir/regctl" ; else - read -r -p "Required dependency 'regctl' missing, do you want it downloaded? y/[n] " GetDep - if [[ "$GetDep" =~ [yY] ]] ; then - # Check architecture - case "$(uname --machine)" in - x86_64|amd64) architecture="amd64" ;; - arm64|aarch64) architecture="arm64";; - *) echo "Architecture not supported, exiting." ; exit 1;; - esac - RegUrl="https://github.com/regclient/regclient/releases/latest/download/regctl-linux-$architecture" - if [[ $(command -v curl) ]]; then curl -L $RegUrl > "$ScriptWorkDir/regctl" ; chmod +x "$ScriptWorkDir/regctl" ; regbin="$ScriptWorkDir/regctl" ; - elif [[ $(command -v wget) ]]; then wget $RegUrl -O "$ScriptWorkDir/regctl" ; chmod +x "$ScriptWorkDir/regctl" ; regbin="$ScriptWorkDir/regctl" ; - else - printf "%s\n" "curl/wget not available - get regctl manually from the repo link, quitting." - fi - else - printf "%s\n" "Dependency missing, quitting." - exit 1 + read -r -p "Required dependency 'regctl' missing, do you want it downloaded? y/[n] " GetRegctl + if [[ "$GetRegctl" =~ [yY] ]] ; then + binary_downloader "regctl" "https://github.com/regclient/regclient/releases/latest/download/regctl-linux-TEMP" + [[ -f "$ScriptWorkDir/regctl" ]] && jqbin="$ScriptWorkDir/regctl" + else printf "%s\n" "Dependency missing, quitting." ; exit 1 ; fi fi # Final check if binary is correct From bbe26a0ac289d7c37e4c2a37e4c6474f949dc0ad Mon Sep 17 00:00:00 2001 From: mag37 Date: Fri, 22 Nov 2024 13:08:24 +0100 Subject: [PATCH 3/8] changes to jq install logic Will fall back to static binary if pkgmanager install fails or no distribution matches. --- dockcheck.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dockcheck.sh b/dockcheck.sh index af45552..2ccf79e 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -195,7 +195,7 @@ distro_checker() { elif [[ -f /etc/redhat-release ]] ; then PkgInstaller="dnf install" elif [[ -f /etc/SuSE-release ]] ; then PkgInstaller="zypper install" elif [[ -f /etc/debian_version ]] ; then PkgInstaller="apt-get install" - else PkgInstaller="ERROR" + else PkgInstaller="ERROR" ; printf "%s\n" "No distribution could be determined, falling back to static binary." fi } @@ -209,8 +209,10 @@ else if [[ "$GetJq" =~ [yYsS] ]] ; then [[ "$GetJq" =~ [yY] ]] && distro_checker if [[ -n "$PkgInstaller" && "$PkgInstaller" != "ERROR" ]] ; then - (sudo $PkgInstaller jq) # is this the best way? Rewrite so that if it fails it goes to static - else + (sudo $PkgInstaller jq) ; PkgExitcode="$?" + [[ "$PkgExitcode" != 0 ]] && printf "%s\n" "Packagemanager install failed, falling back to static binary." + fi + if [[ "$GetJq" =~ [nN] || "$PkgInstaller" == "ERROR" || "$PkgExitcode" != 0 ]] ; then binary_downloader "jq" "https://github.com/jqlang/jq/releases/latest/download/jq-linux-TEMP" [[ -f "$ScriptWorkDir/jq" ]] && jqbin="$ScriptWorkDir/jq" fi From 8309b80dc26855d60661385014ee7bc99528b4b0 Mon Sep 17 00:00:00 2001 From: mag37 Date: Fri, 22 Nov 2024 13:45:45 +0100 Subject: [PATCH 4/8] cleaned old jq check + fixed some naming --- dockcheck.sh | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/dockcheck.sh b/dockcheck.sh index 2ccf79e..d9fa436 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -229,7 +229,7 @@ else read -r -p "Required dependency 'regctl' missing, do you want it downloaded? y/[n] " GetRegctl if [[ "$GetRegctl" =~ [yY] ]] ; then binary_downloader "regctl" "https://github.com/regclient/regclient/releases/latest/download/regctl-linux-TEMP" - [[ -f "$ScriptWorkDir/regctl" ]] && jqbin="$ScriptWorkDir/regctl" + [[ -f "$ScriptWorkDir/regctl" ]] && regbin="$ScriptWorkDir/regctl" else printf "%s\n" "Dependency missing, quitting." ; exit 1 ; fi fi @@ -247,12 +247,6 @@ else exit 1 fi -# Check for jq binary -if [[ ! $(command -v jq) ]] ; then - printf "%s\n" "No jq binary, please install jq and try again, exiting." - exit 1 -fi - # Numbered List function options() { num=1 @@ -352,17 +346,17 @@ if [ -n "$GotUpdates" ] ; then # Extract labels and metadata ContLabels=$(docker inspect "$i" --format '{{json .Config.Labels}}') ContImage=$(docker inspect "$i" --format='{{.Config.Image}}') - ContPath=$(jq -r '."com.docker.compose.project.working_dir"' <<< "$ContLabels") + ContPath=$(jqbin -r '."com.docker.compose.project.working_dir"' <<< "$ContLabels") [ "$ContPath" == "null" ] && ContPath="" - ContConfigFile=$(jq -r '."com.docker.compose.project.config_files"' <<< "$ContLabels") + ContConfigFile=$(jqbin -r '."com.docker.compose.project.config_files"' <<< "$ContLabels") [ "$ContConfigFile" == "null" ] && ContConfigFile="" - ContName=$(jq -r '."com.docker.compose.service"' <<< "$ContLabels") + ContName=$(jqbin -r '."com.docker.compose.service"' <<< "$ContLabels") [ "$ContName" == "null" ] && ContName="" - ContEnv=$(jq -r '."com.docker.compose.project.environment_file"' <<< "$ContLabels") + ContEnv=$(jqbin -r '."com.docker.compose.project.environment_file"' <<< "$ContLabels") [ "$ContEnv" == "null" ] && ContEnv="" - ContUpdateLabel=$(jq -r '."mag37.dockcheck.update"' <<< "$ContLabels") + ContUpdateLabel=$(jqbin -r '."mag37.dockcheck.update"' <<< "$ContLabels") [ "$ContUpdateLabel" == "null" ] && ContUpdateLabel="" - ContRestartStack=$(jq -r '."mag37.dockcheck.restart-stack"' <<< "$ContLabels") + ContRestartStack=$(jqbin -r '."mag37.dockcheck.restart-stack"' <<< "$ContLabels") [ "$ContRestartStack" == "null" ] && ContRestartStack="" # Checking if compose-values are empty - hence started with docker run From a28b9e555f8c884230b260858270b3e95dcc1959 Mon Sep 17 00:00:00 2001 From: mag37 Date: Fri, 22 Nov 2024 14:32:30 +0100 Subject: [PATCH 5/8] Colorized error messages --- dockcheck.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dockcheck.sh b/dockcheck.sh index d9fa436..e11939e 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -180,7 +180,7 @@ binary_downloader() { case "$(uname --machine)" in x86_64|amd64) architecture="amd64" ;; arm64|aarch64) architecture="arm64";; - *) echo "Architecture not supported, exiting." ; exit 1;; + *) printf "\n%bArchitecture not supported, exiting.%b\n" "$c_red" "$c_reset" ; exit 1;; esac GetUrl="${BinaryUrl/TEMP/"$architecture"}" if [[ $(command -v curl) ]]; then curl -L $GetUrl > "$ScriptWorkDir/$BinaryName" ; @@ -195,7 +195,7 @@ distro_checker() { elif [[ -f /etc/redhat-release ]] ; then PkgInstaller="dnf install" elif [[ -f /etc/SuSE-release ]] ; then PkgInstaller="zypper install" elif [[ -f /etc/debian_version ]] ; then PkgInstaller="apt-get install" - else PkgInstaller="ERROR" ; printf "%s\n" "No distribution could be determined, falling back to static binary." + else PkgInstaller="ERROR" ; printf "\n%bNo distribution could be determined%b, falling back to static binary.\n" "$c_yellow" "$c_reset" fi } @@ -210,13 +210,13 @@ else [[ "$GetJq" =~ [yY] ]] && distro_checker if [[ -n "$PkgInstaller" && "$PkgInstaller" != "ERROR" ]] ; then (sudo $PkgInstaller jq) ; PkgExitcode="$?" - [[ "$PkgExitcode" != 0 ]] && printf "%s\n" "Packagemanager install failed, falling back to static binary." + [[ "$PkgExitcode" != 0 ]] && printf "\n%bPackagemanager install failed%b, falling back to static binary.\n" "$c_yellow" "$c_reset" fi if [[ "$GetJq" =~ [nN] || "$PkgInstaller" == "ERROR" || "$PkgExitcode" != 0 ]] ; then binary_downloader "jq" "https://github.com/jqlang/jq/releases/latest/download/jq-linux-TEMP" [[ -f "$ScriptWorkDir/jq" ]] && jqbin="$ScriptWorkDir/jq" fi - else printf "%s\n" "Dependency missing, quitting." ; exit 1 ; + else printf "\n%bDependency missing, exiting.%b\n" "$c_red" "$c_reset" ; exit 1 ; fi fi # Final check if binary is correct @@ -230,7 +230,7 @@ else if [[ "$GetRegctl" =~ [yY] ]] ; then binary_downloader "regctl" "https://github.com/regclient/regclient/releases/latest/download/regctl-linux-TEMP" [[ -f "$ScriptWorkDir/regctl" ]] && regbin="$ScriptWorkDir/regctl" - else printf "%s\n" "Dependency missing, quitting." ; exit 1 ; + else printf "\n%bDependency missing, exiting.%b\n" "$c_red" "$c_reset" ; exit 1 ; fi fi # Final check if binary is correct From 1a6826e2ace73b8090ed0b95fe6a8a170e7f4625 Mon Sep 17 00:00:00 2001 From: mag37 Date: Fri, 22 Nov 2024 15:08:15 +0100 Subject: [PATCH 6/8] versionbump + jq info --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9d4caa0..6bbb5a0 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ ___ ## :bell: Changelog +- **v0.5.1**: Rewrite of dependency downloads, now jq can be auto-downloaded with package manager or static binary. - **v0.5.1**: DEPENDENCY WARNING: now requires **jq**. + Upstreaming changes from [sudo-kraken/podcheck](https://github.com/sudo-kraken/podcheck) - **v0.5.0**: Rewritten notify logic - all templates are adjusted and should be migrated! - Copy the custom settings from your current template to the new version of the same template. @@ -27,8 +28,6 @@ ___ - Verbosity changed of `regctl`. - **v0.4.9**: Added a function to enrich the notify-message with release note URLs. See [Release notes addon](https://github.com/mag37/dockcheck#date-release-notes-addon-to-notifications) - **v0.4.8**: Rewrote prune logic to not prompt with options `-a|-y` or `-n`. Auto prune with `-p`. -- **v0.4.7**: Notification Template changes to gotify(new!), DSM(improved), SMTP(deprecation alternative). -- **v0.4.6**: Compatibility changes to timeout, due to busybox. ___ @@ -57,7 +56,6 @@ Options:" -v Prints current version. ``` - ### Basic example: ``` $ ./dockcheck.sh @@ -82,6 +80,8 @@ ___ ## :nut_and_bolt: Dependencies - Running docker (duh) and compose, either standalone or plugin. (see [Podman fork](https://github.com/sudo-kraken/podcheck) - Bash shell or compatible shell of at least v4.3 +- [jq](https://github.com/jqlang/jq) + - User will be prompted to install with package manager or download static binary. - [regclient/regctl](https://github.com/regclient/regclient) (Licensed under [Apache-2.0 License](http://www.apache.org/licenses/LICENSE-2.0)) - User will be prompted to download `regctl` if not in `PATH` or `PWD`. - regctl requires `amd64/arm64` - see [workaround](#roller_coaster-workaround-for-non-amd64--arm64) if other architecture is used. From 704387a7fe544ed93daadd3fa9298f9a9c544c1d Mon Sep 17 00:00:00 2001 From: mag37 Date: Fri, 22 Nov 2024 15:08:15 +0100 Subject: [PATCH 7/8] versionbump + jq info --- README.md | 6 +++--- dockcheck.sh | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9d4caa0..b4cdc45 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ ___ ## :bell: Changelog +- **v0.5.2**: Rewrite of dependency downloads, now jq can be auto-downloaded with package manager or static binary. - **v0.5.1**: DEPENDENCY WARNING: now requires **jq**. + Upstreaming changes from [sudo-kraken/podcheck](https://github.com/sudo-kraken/podcheck) - **v0.5.0**: Rewritten notify logic - all templates are adjusted and should be migrated! - Copy the custom settings from your current template to the new version of the same template. @@ -27,8 +28,6 @@ ___ - Verbosity changed of `regctl`. - **v0.4.9**: Added a function to enrich the notify-message with release note URLs. See [Release notes addon](https://github.com/mag37/dockcheck#date-release-notes-addon-to-notifications) - **v0.4.8**: Rewrote prune logic to not prompt with options `-a|-y` or `-n`. Auto prune with `-p`. -- **v0.4.7**: Notification Template changes to gotify(new!), DSM(improved), SMTP(deprecation alternative). -- **v0.4.6**: Compatibility changes to timeout, due to busybox. ___ @@ -57,7 +56,6 @@ Options:" -v Prints current version. ``` - ### Basic example: ``` $ ./dockcheck.sh @@ -82,6 +80,8 @@ ___ ## :nut_and_bolt: Dependencies - Running docker (duh) and compose, either standalone or plugin. (see [Podman fork](https://github.com/sudo-kraken/podcheck) - Bash shell or compatible shell of at least v4.3 +- [jq](https://github.com/jqlang/jq) + - User will be prompted to install with package manager or download static binary. - [regclient/regctl](https://github.com/regclient/regclient) (Licensed under [Apache-2.0 License](http://www.apache.org/licenses/LICENSE-2.0)) - User will be prompted to download `regctl` if not in `PATH` or `PWD`. - regctl requires `amd64/arm64` - see [workaround](#roller_coaster-workaround-for-non-amd64--arm64) if other architecture is used. diff --git a/dockcheck.sh b/dockcheck.sh index e11939e..22a5e86 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -VERSION="v0.5.1" +VERSION="v0.5.2" # ChangeNotes: DEPENDENCY WARNING: now requires jq. And upstreaming changes from sudo-kraken/podcheck Github="https://github.com/mag37/dockcheck" RawUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/dockcheck.sh" From ae66a6f0fd134fe27f22e744b028d48f849d6524 Mon Sep 17 00:00:00 2001 From: mag37 Date: Sat, 23 Nov 2024 12:40:58 +0100 Subject: [PATCH 8/8] ChangeNote + finalizing fixes --- dockcheck.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dockcheck.sh b/dockcheck.sh index 22a5e86..4c423bf 100755 --- a/dockcheck.sh +++ b/dockcheck.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash VERSION="v0.5.2" -# ChangeNotes: DEPENDENCY WARNING: now requires jq. And upstreaming changes from sudo-kraken/podcheck +# ChangeNotes: DEPENDENCY REMINDER: now requires jq. UPDATE: Rewrite of dependency installer. Github="https://github.com/mag37/dockcheck" RawUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/dockcheck.sh" @@ -210,7 +210,7 @@ else [[ "$GetJq" =~ [yY] ]] && distro_checker if [[ -n "$PkgInstaller" && "$PkgInstaller" != "ERROR" ]] ; then (sudo $PkgInstaller jq) ; PkgExitcode="$?" - [[ "$PkgExitcode" != 0 ]] && printf "\n%bPackagemanager install failed%b, falling back to static binary.\n" "$c_yellow" "$c_reset" + [[ "$PkgExitcode" == 0 ]] && jqbin="jq" || printf "\n%bPackagemanager install failed%b, falling back to static binary.\n" "$c_yellow" "$c_reset" fi if [[ "$GetJq" =~ [nN] || "$PkgInstaller" == "ERROR" || "$PkgExitcode" != 0 ]] ; then binary_downloader "jq" "https://github.com/jqlang/jq/releases/latest/download/jq-linux-TEMP"