Blame SOURCES/bz2024140-SAPInstance-1-add-systemd-support.patch

b681a8
From 2d50c76c366df4e6021c2992e085bbc48fa112b0 Mon Sep 17 00:00:00 2001
b681a8
From: fmherschel <fh-github@herschel-mainz.org>
b681a8
Date: Fri, 25 Jun 2021 16:16:51 +0200
b681a8
Subject: [PATCH] SAPInstance: add systemd compatability (#1662)
b681a8
b681a8
* SAPInstance: for systemd integrated sapstartsrv instances use systemd to check/start the service
b681a8
---
b681a8
 heartbeat/SAPInstance | 146 ++++++++++++++++++++++++++----------------
b681a8
 1 file changed, 91 insertions(+), 55 deletions(-)
b681a8
b681a8
diff --git a/heartbeat/SAPInstance b/heartbeat/SAPInstance
b681a8
index 8c404d4376..15b24024e0 100755
b681a8
--- a/heartbeat/SAPInstance
b681a8
+++ b/heartbeat/SAPInstance
b681a8
@@ -369,6 +369,7 @@ sapinstance_init() {
b681a8
   InstanceName=`echo "$myInstanceName" | cut -d_ -f2`
b681a8
   InstanceNr=`echo "$InstanceName" | sed 's/.*\([0-9][0-9]\)$/\1/'`
b681a8
   SAPVIRHOST=`echo "$myInstanceName" | cut -d_ -f3`
b681a8
+  SYSTEMCTL="systemctl"
b681a8
 
b681a8
   # optional OCF parameters, we try to guess which directories are correct
b681a8
   if  [ -z "$OCF_RESKEY_DIR_EXECUTABLE" ]
b681a8
@@ -446,6 +447,25 @@ sapinstance_init() {
b681a8
   return $OCF_SUCCESS
b681a8
 }
b681a8
 
b681a8
+#
b681a8
+# check_systemd_integration : Check, if SAP instance is controlled by systemd unit file SAP<SID>_<InstanceNr>.service
b681a8
+#                             rc == 0 : sap instance is controlled by the unit file (file at least exists)
b681a8
+#                             rc == 1 : sap instance is NOT controlled by the unit file (file does not exist)
b681a8
+#
b681a8
+check_systemd_integration() {
b681a8
+    local systemd_unit_name="SAP${SID}_${InstanceNr}"
b681a8
+    local rc=1
b681a8
+
b681a8
+    if [ -x "$SYSTEMCTL" ]; then
b681a8
+        if $SYSTEMCTL list-unit-files | \
b681a8
+            awk '$1 == service { found=1 } END { if (! found) {exit 1}}' service="${systemd_unit_name}.service"; then
b681a8
+            rc=0
b681a8
+        else
b681a8
+            rc=1
b681a8
+        fi
b681a8
+    fi
b681a8
+    return "$rc"
b681a8
+}
b681a8
 
b681a8
 #
b681a8
 # check_sapstartsrv : Before using sapcontrol we make sure that the sapstartsrv is running for the correct instance.
b681a8
@@ -458,76 +478,92 @@ check_sapstartsrv() {
b681a8
   local chkrc=$OCF_SUCCESS
b681a8
   local output=""
b681a8
 
b681a8
-  if [ ! -S /tmp/.sapstream5${InstanceNr}13 ]; then
b681a8
-    ocf_log warn "sapstartsrv is not running for instance $SID-$InstanceName (no UDS), it will be started now"
b681a8
-    restart=1
b681a8
-  else
b681a8
-    output=`$SAPCONTROL -nr $InstanceNr -function ParameterValue INSTANCE_NAME -format script`
b681a8
-    if [ $? -eq 0 ]
b681a8
-    then
b681a8
-      runninginst=`echo "$output" | grep '^0 : ' | cut -d' ' -f3`
b681a8
-      if [ "$runninginst" != "$InstanceName" ]
b681a8
-      then
b681a8
-        ocf_log warn "sapstartsrv is running for instance $runninginst, that service will be killed"
b681a8
+  # check for sapstartsrv/systemd integration
b681a8
+
b681a8
+  if check_systemd_integration; then
b681a8
+    # do it the systemd way
b681a8
+    local systemd_unit_name="SAP${SID}_${InstanceNr}"
b681a8
+
b681a8
+    if $SYSTEMCTL  status "$systemd_unit_name" 1>/dev/null 2>/dev/null; then
b681a8
+       ocf_log info "systemd service $systemd_unit_name is active"
b681a8
+    else
b681a8
+       ocf_log warn "systemd service $systemd_unit_name is not active, it will be started using systemd"
b681a8
+       $SYSTEMCTL start "$systemd_unit_name" 1>/dev/null 2>/dev/null
b681a8
+       # use start, because restart does also stop sap instance
b681a8
+    fi
b681a8
+
b681a8
+    return 0
b681a8
+  else # otherwise continue with old code...
b681a8
+      if [ ! -S /tmp/.sapstream5${InstanceNr}13 ]; then
b681a8
+        ocf_log warn "sapstartsrv is not running for instance $SID-$InstanceName (no UDS), it will be started now"
b681a8
         restart=1
b681a8
       else
b681a8
-        output=`$SAPCONTROL -nr $InstanceNr -function AccessCheck Start`
b681a8
-        if [ $? -ne 0 ]; then
b681a8
-          ocf_log warn "FAILED : sapcontrol -nr $InstanceNr -function AccessCheck Start (`ls -ld1 /tmp/.sapstream5${InstanceNr}13`)"
b681a8
-          ocf_log warn "sapstartsrv will be restarted to try to solve this situation, otherwise please check sapstsartsrv setup (SAP Note 927637)"
b681a8
+        output=`$SAPCONTROL -nr $InstanceNr -function ParameterValue INSTANCE_NAME -format script`
b681a8
+        if [ $? -eq 0 ]
b681a8
+        then
b681a8
+          runninginst=`echo "$output" | grep '^0 : ' | cut -d' ' -f3`
b681a8
+          if [ "$runninginst" != "$InstanceName" ]
b681a8
+          then
b681a8
+            ocf_log warn "sapstartsrv is running for instance $runninginst, that service will be killed"
b681a8
+            restart=1
b681a8
+          else
b681a8
+            output=`$SAPCONTROL -nr $InstanceNr -function AccessCheck Start`
b681a8
+            if [ $? -ne 0 ]; then
b681a8
+              ocf_log warn "FAILED : sapcontrol -nr $InstanceNr -function AccessCheck Start (`ls -ld1 /tmp/.sapstream5${InstanceNr}13`)"
b681a8
+              ocf_log warn "sapstartsrv will be restarted to try to solve this situation, otherwise please check sapstsartsrv setup (SAP Note 927637)"
b681a8
+              restart=1
b681a8
+            fi
b681a8
+          fi
b681a8
+        else
b681a8
+          ocf_log warn "sapstartsrv is not running for instance $SID-$InstanceName, it will be started now"
b681a8
           restart=1
b681a8
         fi
b681a8
       fi
b681a8
-    else
b681a8
-      ocf_log warn "sapstartsrv is not running for instance $SID-$InstanceName, it will be started now"
b681a8
-      restart=1
b681a8
-    fi
b681a8
-  fi
b681a8
 
b681a8
-  if [ -z "$runninginst" ]; then runninginst=$InstanceName; fi
b681a8
+      if [ -z "$runninginst" ]; then runninginst=$InstanceName; fi
b681a8
 
b681a8
-  if [ $restart -eq 1 ]
b681a8
-  then
b681a8
+      if [ $restart -eq 1 ]
b681a8
+      then
b681a8
+        if [ -d /usr/sap/$SID/SYS/profile/ ]
b681a8
+        then
b681a8
+          DIR_PROFILE="/usr/sap/$SID/SYS/profile"
b681a8
+        else
b681a8
+          abnormal_end "Expected /usr/sap/$SID/SYS/profile/ to be a directory, please set DIR_PROFILE parameter!"
b681a8
+        fi
b681a8
 
b681a8
-    if [ -d /usr/sap/$SID/SYS/profile/ ]
b681a8
-    then
b681a8
-      DIR_PROFILE="/usr/sap/$SID/SYS/profile"
b681a8
-    else
b681a8
-      abnormal_end "Expected /usr/sap/$SID/SYS/profile/ to be a directory, please set DIR_PROFILE parameter!"
b681a8
-    fi
b681a8
+        [ ! -r $SAPSTARTPROFILE ] && abnormal_end "Expected $SAPSTARTPROFILE to be the instance START profile, please set START_PROFILE parameter!"
b681a8
 
b681a8
-    [ ! -r $SAPSTARTPROFILE ] && abnormal_end "Expected $SAPSTARTPROFILE to be the instance START profile, please set START_PROFILE parameter!"
b681a8
+        pkill -9 -f "sapstartsrv.*$runninginst"
b681a8
 
b681a8
-    pkill -9 -f "sapstartsrv.*$runninginst"
b681a8
+        # removing the unix domain socket files as they might have wrong permissions
b681a8
+        # or ownership - they will be recreated by sapstartsrv during next start
b681a8
+        rm -f /tmp/.sapstream5${InstanceNr}13
b681a8
+        rm -f /tmp/.sapstream5${InstanceNr}14
b681a8
 
b681a8
-    # removing the unix domain socket files as they might have wrong permissions
b681a8
-    # or ownership - they will be recreated by sapstartsrv during next start
b681a8
-    rm -f /tmp/.sapstream5${InstanceNr}13
b681a8
-    rm -f /tmp/.sapstream5${InstanceNr}14
b681a8
+        $SAPSTARTSRV pf=$SAPSTARTPROFILE -D -u $sidadm
b681a8
 
b681a8
-    $SAPSTARTSRV pf=$SAPSTARTPROFILE -D -u $sidadm
b681a8
+        # now make sure the daemon has been started and is able to respond
b681a8
+        local srvrc=1
b681a8
+        while [ $srvrc -eq 1 -a `pgrep -f "sapstartsrv.*$runninginst" | wc -l` -gt 0 ]
b681a8
+        do
b681a8
+          sleep 1
b681a8
+          $SAPCONTROL -nr $InstanceNr -function GetProcessList > /dev/null 2>&1
b681a8
+          srvrc=$?
b681a8
+        done
b681a8
 
b681a8
-    # now make sure the daemon has been started and is able to respond
b681a8
-    local srvrc=1
b681a8
-    while [ $srvrc -eq 1 -a `pgrep -f "sapstartsrv.*$runninginst" | wc -l` -gt 0 ]
b681a8
-    do
b681a8
-      sleep 1
b681a8
-      $SAPCONTROL -nr $InstanceNr -function GetProcessList > /dev/null 2>&1
b681a8
-      srvrc=$?
b681a8
-    done
b681a8
+        if [ $srvrc -ne 1 ]
b681a8
+        then
b681a8
+          ocf_log info "sapstartsrv for instance $SID-$InstanceName was restarted !"
b681a8
+          chkrc=$OCF_SUCCESS
b681a8
+        else
b681a8
+          ocf_log error "sapstartsrv for instance $SID-$InstanceName could not be started!"
b681a8
+          chkrc=$OCF_ERR_GENERIC
b681a8
+          ocf_is_probe && chkrc=$OCF_NOT_RUNNING
b681a8
+        fi
b681a8
+      fi
b681a8
 
b681a8
-    if [ $srvrc -ne 1 ]
b681a8
-    then
b681a8
-      ocf_log info "sapstartsrv for instance $SID-$InstanceName was restarted !"
b681a8
-      chkrc=$OCF_SUCCESS
b681a8
-    else
b681a8
-      ocf_log error "sapstartsrv for instance $SID-$InstanceName could not be started!"
b681a8
-      chkrc=$OCF_ERR_GENERIC
b681a8
-      ocf_is_probe && chkrc=$OCF_NOT_RUNNING
b681a8
-    fi
b681a8
+      return $chkrc
b681a8
   fi
b681a8
-
b681a8
-  return $chkrc
b681a8
 }
b681a8
 
b681a8