From 7ca32ceaa9e5970d5f60a1eab7e79b823d367587 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Mon, 23 Jan 2023 11:32:36 +0100
Subject: [PATCH] fix _bStopOnError; some shellfixes

---
 director-cli.sh | 63 +++++++++++++++++++++++++------------------------
 1 file changed, 32 insertions(+), 31 deletions(-)

diff --git a/director-cli.sh b/director-cli.sh
index 696e033..db6a855 100755
--- a/director-cli.sh
+++ b/director-cli.sh
@@ -17,6 +17,7 @@
 # ah = axel.hahn@iml.unibe.ch
 # 2022-02-16  v0.2  ah  add --cfg param
 # 2022-03-04  v0.3  ah  abort on http 5xx error
+# 2023-01-22  v0.4  ah  fix _bStopOnError; some shellfixes
 # ======================================================================
 
 tmpfile=/tmp/outcurl.tmp
@@ -57,8 +58,8 @@ typeset cfg_dryrun=false
 
   function _initVars(){
     if [ -z "$loadfile" ]; then
-      MY_NAME=`hostname -f`
-      MY_IP=`_getIpFrontend | head -1`
+      MY_NAME=$(hostname -f)
+      MY_IP=$(_getIpFrontend | head -1)
       MY_ZONE=
     fi
     IDC_host__cachefile="${dir_data}/${MY_NAME}__host_at-director.txt"
@@ -69,7 +70,7 @@ typeset cfg_dryrun=false
   }
 
   function flushDatadir(){
-    if [ ! -z "${dir_data}" -a -d "${dir_data}" ]; then
+    if [ -n "${dir_data}" -a -d "${dir_data}" ]; then
       _wd "deleting ${dir_data} ..."
       rm -f ${dir_data}/*.txt
     fi
@@ -87,10 +88,10 @@ typeset cfg_dryrun=false
     if [ -z "$loadfile" ]; then
       # --- host infos
 
-      local MY_Platform=`uname -a | cut -f 1 -d ' '`
-      . `dirname $0`/plugins/inc_pluginfunctions || exit 1
-      local MY_OSName=`ph.getOS`
-      local MY_OSMajorVersion=`ph.getOSMajor`
+      local MY_Platform=$(uname -a | cut -f 1 -d ' ')
+      . $(dirname $0)/plugins/inc_pluginfunctions || exit 1
+      local MY_OSName=$(ph.getOS)
+      local MY_OSMajorVersion=$(ph.getOSMajor)
     fi
 
     export CFGSTORAGE="directorhost"
@@ -121,16 +122,16 @@ typeset cfg_dryrun=false
       # ----- set host type
 
       # host in the UNIBE network:
-      echo $MY_IP | grep -E "^(10\.|172\.1[6-9]\.|172.2[0-9]\.|172\.3[01]\.|192.168\.)" >/dev/null
-      if [ $? -eq 0 ]; then
+      if echo "$MY_IP" | grep -E "^(10\.|172\.1[6-9]\.|172.2[0-9]\.|172\.3[01]\.|192.168\.)" >/dev/null
+      then
         $ch --set  imports  '["host passive only"]'
       else
         $ch --set  imports  '["host in network"]'
 
         # port checks initiated by icinga server to monitor client
-        if [ ! -z "${host_vars_tcpport}" -a "${host_vars_tcpport}" != "[]" ]; then
-          host_vars_tcpport=`echo ${host_vars_tcpport} | sed "s# ##g"`
-          $ch --set  vars.tcp_port  ${host_vars_tcpport}
+        if [ -n "${host_vars_tcpport}" -a "${host_vars_tcpport}" != "[]" ]; then
+          host_vars_tcpport=$(echo ${host_vars_tcpport} | sed "s# ##g")
+          $ch --set  vars.tcp_port  "${host_vars_tcpport}"
         fi
       fi
 
@@ -361,7 +362,7 @@ typeset cfg_dryrun=false
 
     # --- get json data of object
     if [ $_bSendData = true ]; then
-      _jsondata=`$ch --json 2>/dev/null`
+      _jsondata=$($ch --json 2>/dev/null)
     fi
     $ch --flush 2>/dev/null
 
@@ -369,7 +370,7 @@ typeset cfg_dryrun=false
     if [ ${_paramDryrun} = false ]; then
       _wd ">>>>> $_paramAction $_paramObj [${_object_name}] >> $_sMethod $_sUrl $_jsondata"
       http.makeRequest "$_sMethod" "$_sUrl" "$_jsondata"
-      if _bStopOnError && http.isServerError >/dev/null; then
+      if $_bStopOnError && http.isServerError >/dev/null; then
         echo "CRITICAL ERROR: Director API request failed with a server error $_sMethod $_sUrl"
         exit 1
       fi
@@ -446,7 +447,7 @@ typeset cfg_dryrun=false
     _h2 "create host"
 
     ObjAction create host
-    if [ -z "`http.isOk`" ]; then
+    if [ -z "$(http.isOk)" ]; then
       echo "ERROR, host was NOT created."
     else
       echo "OK, host was created successfully."
@@ -460,7 +461,7 @@ typeset cfg_dryrun=false
   function hostRead(){
     _h2 "read host"
     ObjAction read host
-    if [ -z "`http.isOk`" ]; then
+    if [ -z "$(http.isOk)" ]; then
       echo "ERROR, host was NOT found."
     fi
   }
@@ -473,8 +474,8 @@ typeset cfg_dryrun=false
     _h2 "update host - set $1 $2"
 
     ObjAction update host
-    if [ -z "`http.isOk`" ]; then
-      case `http.getStatuscode` in
+    if [ -z "$(http.isOk)" ]; then
+      case $(http.getStatuscode) in
         "304")
           echo "OK, no update"
           ;;
@@ -503,7 +504,7 @@ typeset cfg_dryrun=false
   function hostDelete(){
     _h2 "delete host"
     ObjAction delete host
-    case `http.getStatuscode` in
+    case $(http.getStatuscode) in
       "200")
         echo "OK, host was deleted"
         ;;
@@ -535,7 +536,7 @@ typeset cfg_dryrun=false
 
     IDC_command__obj_name="${checkName}"
     IDC_service__obj_name="service-template_for_command_${checkName}"
-    IDC_svcathost__obj_name="`_getName4Svcathost ${checkName}`"
+    IDC_svcathost__obj_name="$(_getName4Svcathost ${checkName})"
     # IDC_service__obj_name="${checkName}"
     # IDC_svcathost__obj_name="${checkName}"
 
@@ -603,7 +604,7 @@ typeset cfg_dryrun=false
     _h2 "${FUNCNAME[0]}() - create all services"
 
     # loop over all services and create
-    for mycheckfile in `getChecks`
+    for mycheckfile in $(getChecks)
     do
       serviceCreateByCfgFile "${mycheckfile}"
       echo
@@ -624,7 +625,7 @@ typeset cfg_dryrun=false
 
     # --- perpare II: create file with local configs and object names for its link
     rm -f $tmpLocal 2>/dev/null
-    for mycheckfile in `getChecks`
+    for mycheckfile in $(getChecks)
     do
       _parseCheckConfig "${mycheckfile}"
       _generateVarsByCheckname "${checkName}"
@@ -640,12 +641,12 @@ typeset cfg_dryrun=false
     # _h3 "Compare"
     cat $tmpRemote | while read remoteLink
     do
-      grep $remoteLink $tmpLocal >/dev/null
-      if [ $? -eq 0 ]; then
+      if grep "$remoteLink" $tmpLocal >/dev/null
+      then
         echo "OK: $remoteLink"
       else
         echo "DELETE: link [$remoteLink] is not used by any local check anymore."
-        checkName=`_getName4Svcathost $remoteLink reverse`
+        checkName=$(_getName4Svcathost $remoteLink reverse)
         _generateVarsByCheckname "${checkName}"
         ObjAction delete svclink
       fi
@@ -667,7 +668,7 @@ typeset cfg_dryrun=false
 
     _wd POST director/config/deploy
     _APIcall POST director/config/deploy
-    if [ -z "`http.isOk`" ]; then
+    if [ -z "$(http.isOk)" ]; then
       echo "ERROR deploy config was not queued."
     else
       echo "OK, deploy was triggered"
@@ -678,7 +679,7 @@ typeset cfg_dryrun=false
 #
 # show a help
 function showHelp(){
-script=`basename $0`
+script=$(basename $0)
 cat <<ENDOFHELP
 
 HELP:
@@ -779,8 +780,8 @@ if [ "$1" = "--cfg" ] && [ -n "$2" ]; then
 else
   . "$( dirname $0 )/inc_getconfig.sh"
 fi
-. `dirname $0`/inc_functions.sh
-. `dirname $0`/inc/rest-api-client.sh
+. $(dirname $0)/inc_functions.sh
+. $(dirname $0)/inc/rest-api-client.sh
 
 
 if [ $# -eq 0 ]; then
@@ -788,8 +789,8 @@ if [ $# -eq 0 ]; then
   exit 0
 fi
 
-cd `dirname $0`
-ls ./`basename $0` >/dev/null || exit 1
+cd $(dirname $0) || exit
+ls ./$(basename $0) >/dev/null || exit 1
 
 _initVars
 
-- 
GitLab