From dbf53866cc3de22f8990c4b1b3e888021c304c5f Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Wed, 24 Jan 2024 14:09:27 +0100
Subject: [PATCH] added debug infos

---
 inc_dockerfunctions.sh | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/inc_dockerfunctions.sh b/inc_dockerfunctions.sh
index 22da4dd..86383e8 100644
--- a/inc_dockerfunctions.sh
+++ b/inc_dockerfunctions.sh
@@ -17,6 +17,9 @@
 
 . $(dirname $0)/inc_pluginfunctions
 
+_is_docker_rootless=0
+_is_docker_detected=0
+
 # ----------------------------------------------------------------------
 # FUNCTIONS
 # ----------------------------------------------------------------------
@@ -32,19 +35,33 @@ function _filterJson(){
 # param  string  json data
 # param  string  jq filter
 function _getString(){
-    _filterJson "$1" "$2" | tr -d '"'
+    _filterJson "$1" "$2" | tr -d '"' | sed "s#^null\$##"
 }
 
 # if DOCKER_HOST is not set we try to detect the user running "containerd"
 # create an env var DOCKER_HOST="unix:///run/user/<ID>/docker.sock"
 function _detectDockerenv(){
+    local dockeruid=
     if [ -z "$DOCKER_HOST" ]; then
+        _is_docker_detected=1
         dockeruid=$( ps -ef | grep containerd | grep -Eo "/run/user/([0-9]*)/" | head -1 | cut -f 4 -d '/' )
         test -n "$dockeruid" && export DOCKER_HOST="unix:///run/user/$dockeruid/docker.sock"
         # Don't abort - it is allowed that the variable DOCKER_HOST is missing
     fi
+    if grep "/run/user/[0-9]*" <<< "$DOCKER_HOST" >/dev/null; then
+        _is_docker_rootless=1
+    fi
 }
 
+# show some debug infos to be shown if needed.
+function _debugInfos(){
+    echo
+    echo "DEBUG INFOS:"
+    echo "DOCKER_HOST = $DOCKER_HOST"
+    test "$_is_docker_detected" -eq 0 && echo "It was set in the environment."
+    test "$_is_docker_detected" -eq 1 && echo "It was detected from process list."
+    echo
+}
 # detect error after dicker command. It stops if
 # - no content was fetched
 # - content contains key "ServerErrors"
@@ -57,6 +74,7 @@ function _detectDockererror(){
         ph.exit
     fi
 
+    local sSrvErrror; 
     sSrvErrror=$(  _filterJson "$data" ".ServerErrors" )
     if [ "$sSrvErrror" != "null" ] ; then
         ph.setStatus unknown
-- 
GitLab