#!/bin/sh
#######################################################
#
# automate-020-install-clsw
# to be called on the landscape 'master' 
# installing pattern ha_sles
# installing current RA rpms
#
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2016-2018 SUSE Linux GmbH, Nuremberg, Germany
#
# Preequisites:
# - VMs installed with SLES
#
# Syntax:
# automate-020-install-clsw [--group=<group>] [--node=<system>] ...
# Next Automation Script
#
#
#######################################################
#
set -u

logFile="/var/log/automate-020-install-clsw.$$"

#
# QUIET MODE
#

exec 3>&1
if true; then
    exec 1>$logFile
    exec 2>$logFile
fi

echo "$(date) == Logs are written to $logFile" >&3
source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"

GROUP="all"
node="all"

while [ $# -gt 0 ]; do
    case "$1" in
        --group=* ) GROUP=${1#--group=}
                   ;;
        --node=* ) node=${1#--node=}
                   ;;
    esac
    shift
done

function do_cluster()
{
    local VM="$1"
    echo "$(date) == Update repos on ${aSystem}" >&3
    ssh $VM zypper --non-interactive ref
    echo "$(date) == Install updates on ${aSystem}" >&3
    ssh $VM zypper --non-interactive up
    echo "$(date) == Install cluster software on ${aSystem}" >&3
    ssh $VM zypper --non-interactive install -t pattern ha_sles
    echo "$(date) == Install SAPHanaSR on ${aSystem}" >&3
    ssh $VM zypper --non-interactive install SAPHanaSR
}

function filterSystem()
{
    local system="$@"
    local systemKV
    for systemKV in ${system}; do
        sKey="${systemKV%=*}"; sKey=${sKey^^}
        sValue=${systemKV#*=}
        local SYSTEM_$sKey=$sValue
    done
    # BAUSTELLE
    if [ "$GROUP" = "all" -a "$node" = "all" ]; then
        echo "$SYSTEM_SYSTEM"
    elif [ "$GROUP" = "$SYSTEM_OWNER" -a $node = "all" ]; then
        echo "$SYSTEM_SYSTEM"
    elif [ "$GROUP" = "all" -a "$node" = "$SYSTEM_SYSTEM" ]; then
        echo "$SYSTEM_SYSTEM"
    elif [ "$GROUP" = "$SYSTEM_OWNER" -a "$node" = "$SYSTEM_SYSTEM" ]; then
        echo "$SYSTEM_SYSTEM"
    fi
}

for system in "${LandscapeSystems[@]}"
do
    allSystems="$allSystems $(filterSystem $system)"
done

for aSystem in ${allSystems}; do
    echo "$(date) == Starting actions on ${aSystem}" >&3
    do_cluster "${aSystem}"
done
