#!/bin/sh

. /opt/webservice/install/deployment.properties

# DO NOT REMOVE THIS LINE otherwise the API to swupdated will be broken
# __SWUPDATED_API_VERSION_1__
#OSFOURK-27603 Display warning in Assistant Dashboard in case Kubernetes is enabled on VM without enough RAM Memory
is_swupdated_call=0
[ "x$1" = "xswupdated" ] && is_swupdated_call=1
#health check needs to be done only if Kubernetes is enabled
[ $is_swupdated_call -eq 1 ] && {
    [ "x`/opt/webservice/scripts/gethaipsetupentry.sh system.has4Kube`" = "x1" ] || {
        #NOT enabled => ignore checking
        echo 4KUBE_CHECK_IGNORE && exit 0
    }
}

deployment=`/opt/webservice/scripts/gethaipsetupentry.sh system.multiNodeSystem`
rc=$?
if [ "$rc" != "0" ]; then
 [ $is_swupdated_call -eq 1 ] && echo 4KUBE_CHECK_IGNORE && exit 0
 echo "System is not a valid OpenScape 4000 installation"
 exit -1
fi 

case $deployment in
   $DEP_LOCAL_SEP_SRV | $DEP_LOCAL_SEP_SRV_SG | $DEP_DSCXLV2_DUPLEX | $DEP_DSCXLV2_DUPLEX_SG | $DEP_DSCXLV2_LOCAL_SEP_DUPLEX | $DEP_DSCXLV2_DUPLEX_REPLACEMENT )
          #deployment is Duplex => not feasible
          [ $is_swupdated_call -eq 1 ] && echo 4KUBE_CHECK_IGNORE && exit 0
          echo "Integrated Kubernetes can not be enabled on Duplex Deployment"
          exit 0
          ;;
   *)     
          true
          ;;
esac

sysism=$(grep system.isManager /var/opt/ha/conf/haipsetup.xml)
[[ "x$sysism" =~ "1" ]] && {
    [ $is_swupdated_call -eq 1 ] && echo 4KUBE_CHECK_IGNORE && exit 0
    echo "Integrated Kubernetes can not be enabled on Manager"
    exit 0
}

#if we reached this point then it means we are NOT duplex and also NOT Manager so the only thing left to check is the HW Type
/usr/sbin/system-is-virtual ; r1=$?
/usr/sbin/system-is-ecoserver2 ; r2=$?
/usr/sbin/system-is-branch2 ; r3=$?

if [ $(($r1 * $r2 * $r3)) -eq 0 ] ; then
    #system is one of VM or Eco2 or Branch2 => 4kube is feasible depending on disk size
    true
else
    [ $is_swupdated_call -eq 1 ] && echo 4KUBE_CHECK_IGNORE && exit 0
    echo "Integrated Kubernetes can not be enabled on this HW Type. Use Eco2/Branch2 or VM."
    exit 0
fi

#OSFOURK-27517 Allow Integrated Kubernetes in VMs only if enough resources are configured
/usr/sbin/system-is-virtual && {
    #do some resource checking on VMs before allowing Kubernetes to be enabled
    if [ "x`/opt/webservice/scripts/gethaipsetupentry.sh system.multiNodeSystem`" = "x21" ] ; then
#TH decision on 14.06 - skip checking on StdSG as 4GB should be enough
        minramsize=3400000
        showneedinGB=4
#        #StdSG -> we need minimum 6GB or RAM representing the usual 4GB + extra 2GB to be used by Kubernetes
#        minramsize=5400000
#        showneedinGB=6
    else
        #other deployments -> we need minimum 10GB or RAM representing the usual 8GB + extra 2GB to be used by Kubernetes
        minramsize=9400000
        showneedinGB=10
    fi
    currentramsize=$(grep MemTotal /proc/meminfo | awk '{print $2}')
    [ $minramsize -lt $currentramsize ] || {
        #allow Kubernetes to start even if there is not enough RAM memory
        if [ "x$2" = "x__called_from_4kube_prepare_storage__" ] ; then
            echo "WARNING : Not enough RAM to allow Integrated Kubernetes to be enabled. Current RAM size : $currentramsize KB  Need : $showneedinGB GB"
            echo "WARNING : Will allow Kubernetes to start even if not enough RAM"
            logger $0 "WARNING : Not enough RAM to allow Integrated Kubernetes to be enabled. Current RAM size : $currentramsize KB  Need : $showneedinGB GB"
            logger $0 "WARNING : Will allow Kubernetes to start even if not enough RAM"
        else
            echo "Not enough RAM to allow Integrated Kubernetes to be enabled. Current RAM size : $currentramsize KB  Need : $showneedinGB GB"
            exit 0
        fi
    }
}

#from this point further all other checks are NOT interessting for the Assistant Dachboard, therefore report success if we have reached this point
[ $is_swupdated_call -eq 1 ] && echo 4KUBE_CHECK_OK && exit 0

#if we reached this point then HW Type is OK -> need to check disk space
mindiskSize_B_inGB=125
mindiskSize_A_inGB=300
/usr/sbin/system-is-virtual || {
  [ -e /opt/4kube-big-disk ] || mindiskSize_A_inGB=230
}
disksizeinGB_sda=0
disksizeinGB_sdb=0
if [ $r1 -eq 0 ] ; then
    #for VMs check for extra disk space like this - if SG then 80+50 or 240+50 if aready biggen than 240 then 240+50
    #but first rescan the disks in order to get the latest disk size, but only if called with some special argument otherwise we block the execution for too long
    [ "x$1" = "x_rescan_virtual_disk_" ] && (
        cd /sys/class/scsi_device/
        for adev in $(ls -1 .) ; do echo 1 > $adev/device/rescan ; done
        #allow some short time for the changes to become visible in the OS
        sleep 2
    )
    # in V10R1 or above we have 2 layouts to consider
    #  - layout B -  78 GB - used only for SG
    #  - layout A - 263 GB - used for all deployments
    disksizeinGB=$((`blockdev --getsize64 /dev/sda` / 1024000000))
    #check for layout B or A
    if [ -e /var/opt/soco2/hd_layout_A ] ; then
        if [ $disksizeinGB -lt $mindiskSize_A_inGB ] ; then
            echo "Integrated Kubernetes can not be enabled due to insufficient disk size. Need at least $mindiskSize_A_inGB GB of virtual disk. You can increase the disk size and press Rescan to refresh."
            exit 0
        fi
    else
        if [ $disksizeinGB -lt $mindiskSize_B_inGB ] ; then
            echo "Integrated Kubernetes can not be enabled due to insufficient disk size. Need at least $mindiskSize_B_inGB GB of virtual disk. You can increase the disk size and press Rescan to refresh."
            exit 0
        fi
    fi
    disksizeinGB_sda=$disksizeinGB
else
    #for Eco/Branch -> must be bigger than 300 ...
    #TODO - improve here how the disks are detected
    disksizeinGB=0
    [ -e /dev/sda ] && disksizeinGB_sda=$((`blockdev --getsize64 /dev/sda` / 1024000000))
    [ -e /dev/sdb ] && disksizeinGB_sdb=$((`blockdev --getsize64 /dev/sdb` / 1024000000))
    disksizeinGB=$disksizeinGB_sda
    [ $disksizeinGB_sdb -gt $disksizeinGB_sda ] && disksizeinGB=$disksizeinGB_sdb
    if [ $disksizeinGB -lt $mindiskSize_A_inGB ] ; then
        echo "Integrated Kubernetes can not be enabled due to insufficient disk size. Need at least $mindiskSize_A_inGB GB of SSD. Use Raid Menu for adding a bigger SSD on the fly."
        exit 0
    fi    
    #now check for inconsistencies = 2 disks of diferent sizes
    [ $disksizeinGB_sda -eq 0 ] || {
        if [ $disksizeinGB_sda -lt $mindiskSize_A_inGB ] ; then
            #[OSFOURK-26582]->Begin
            echo "Integrated Kubernetes can not be enabled, because the disk is too small. Please replace the small disk (`smartctl -a /dev/sda | grep -m 1 ^Device | awk '{print $NF}'`) first."
            exit 0
        fi    
    }
    [ $disksizeinGB_sdb -eq 0 ] || {
        if [ $disksizeinGB_sdb -lt $mindiskSize_A_inGB ] ; then
            echo "Integrated Kubernetes can not be enabled, because the disk is too small. Please replace the small disk (`smartctl -a /dev/sdb | grep -m 1 ^Device | awk '{print $NF}'`) first."
            #[OSFOURK-26582]->End
            exit 0
        fi    
    }
fi

#temporary protection due to not-implemented functionality - refuse to run if OS4K internal LAN is not the default
grep internal-lan.listen-ip-address /var/opt/ha/conf/haipsetup.xml | grep -q 192.168.187. || {
    echo "We are very sorry but Integrated Kubernetes can not be enabled in this version if OS4K Internal LAN network segment is not set to default (192.168.187.0/24)."
    exit 0
}

[ -e /opt/4kube-big-disk ] && {
#temporary protection - refuse to run if more than one disk
#TODO - improve and use softraid for 2 disks
nrofdisks=0
for adisk in sda sdb sdc sdd sde sdf ; do 
    [ -e /dev/$adisk ] && nrofdisks=$(($nrofdisks + 1))
done
[ $nrofdisks -eq 1 ] || {
    echo "We are very sorry but in this version only one disk can be used => please remove extra disks (also unplug USB Sticks)."
    exit 0
}
}

#save the detected disk(s) so that this script can also be used from other scripts for the detection/actions/operations, i.e. not only the UI
# but only if called with a file argument where to store the disks
[ "x$1" = "x" ] || {
    # !!! protection - file must exist!! - so the caller must previously have created the file
    #hint/trick - if the 4kube is not feasible (due to too less disk space/wrong HW) then the file will remain unchanged
    [ -e $1 ] && {
        [ $disksizeinGB_sda -eq 0 ] || echo /dev/sda >> $1
        [ $disksizeinGB_sdb -eq 0 ] || echo /dev/sdb >> $1
    }
}

#all OK => return the magic text for the UI to know we are good to go
echo 4KUBE_FEASIBLE
exit 1
