#!/bin/bash
#
# test_maintenance_cluster_turn_hana  - with resource in maintenace: exchange primary/secondary
#
# - set mst-resource in maintenance mode
# - takeover on secondary with suspend primary (gets new primary)
# - register on primary in online mode (gets new secondary)
# - wait till cluster is idle
# - refresh mst-resource
# - wait till cluster is idle
# - get mst-resource out of maintenance (normal operation)
#
# expected outcome: cluster running, SAP HANA instanes running, SR roles exchanged
src=${BASH_SOURCE[0]}
full_path=$(readlink -f  "$src")
dir_path=$(dirname "$full_path")
source "$dir_path/test_properties"
source .test_properties

# shellcheck disable=SC2029
ssh "${node01}" "crm resource cleanup  $mstResource"

currPrimary="$(ssh "${node01}" "SAPHanaSR-showAttr --format=tester" | awk -F'/' '/score="150"/ { print $2 }' )"
currSecondary="$(ssh "${node01}" "SAPHanaSR-showAttr --format=tester" | awk -F'/' '/score="100"/ { print $2 }' )"

echo "p=$currPrimary, s=$currSecondary"

sitePrimary=$(ssh "${node01}" "SAPHanaSR-showAttr --format=tester" | awk -F'=' '$0 ~ node".site" { print $2 }' node="$currPrimary")
sitePrimary="${sitePrimary//\"/}"

echo "p=$currPrimary ($sitePrimary), s=$currSecondary"


echo "==== SUSE Resource Maintenance Begin ===="
# shellcheck disable=SC2029
ssh "${node02}" "crm resource maintenance $mstResource on"

ssh "$currPrimary" 'su - '"$sidadm"' -c "hdbsql -U '"$userkey"' \"ALTER SYSTEM RECLAIM LOG\""'
ssh "$currPrimary" 'su - '"$sidadm"' -c "find /usr/sap/'"$SID"'/HDB'"$instNr"'/backup/log -type f -mtime +2 -exec rm {} \;"'
sleep 55

# shellcheck disable=SC2029
ssh "${node02}" "crm resource maintenance $mstResource off"

echo "==== SUSE Resource Maintenance End ===="
