#! /bin/bash
# hpc-testing
# Copyright (C) 2025 SUSE LLC
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#########################
#
# Common test functions for phase 0
#
#########################

setup_requirements()
{
    local host=$1
    echo "Setting up needed packages on $host"

    tp $host "zypper install --no-confirm $(get_package_list $host)"

}

firewall_down()
{
    # Take down the firewall as some tests requires more than SSH port to be open (openMPI, NFSoRDMA, etc.)
    local host=$1
    tp $host 'systemctl stop SuSEfirewall2 || true'
    tp $host 'systemctl stop firewalld || true'
}

set_properties()
{
    local host=$1
    PACKAGE_LIST=$(get_package_list $host)

    while read -r line; do
	package=$(echo "$line" | awk '{print $1}')
	version=$(echo "$line" | awk '{print $2}')
	juLogSetProperty "$host.pkg.$package" "$version"
    done < <(tpq $host "rpm -q --qf '%{NAME} %{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\\n' $PACKAGE_LIST")

    kernel_version=$(tpq $host "uname -a")
    juLogSetProperty "$host.kernel" "$kernel_version"

}

get_common_package_list()
{
    local host=$1
    PACKAGE_LIST="infiniband-diags rdma-ndd libibverbs-utils srp_daemon fabtests "
    PACKAGE_LIST+=" rdma-core mvapich2 mpitests-mvapich2 openucx-tools"
    PACKAGE_LIST+=" ibutils psmisc nfs-kernel-server"
    PACKAGE_LIST+=" nvme-cli nvmetcli bc iputils perftest"

    case $(get_suse_version $host) in
	42.3|12.3|12.4|12.5)
	    PACKAGE_LIST+=" openmpi"
	    PACKAGE_LIST+=" mpitests-openmpi targetcli-fb dapl-utils "
	    ;;
	15|15.1)
	    PACKAGE_LIST+=" python3-targetcli-fb dapl-utils "
	    PACKAGE_LIST+=" openmpi2 mpich"
	    PACKAGE_LIST+=" mpitests-openmpi2 mpitests-mpich"
	    ;;
	15.2|15.3)
	    PACKAGE_LIST+=" python3-targetcli-fb dapl-utils "
	    PACKAGE_LIST+=" openmpi2 mpich openmpi3"
	    PACKAGE_LIST+=" mpitests-openmpi2 mpitests-mpich"
	    PACKAGE_LIST+=" mpitests-openmpi3"
	    ;;
	15.4|15.5)
	    PACKAGE_LIST+=" python3-targetcli-fb dapl-utils "
	    PACKAGE_LIST+=" openmpi2 mpich openmpi3 openmpi4"
	    PACKAGE_LIST+=" mpitests-openmpi2 mpitests-mpich"
	    PACKAGE_LIST+=" mpitests-openmpi3 mpitests-openmpi4"
	    ;;
	15.6|15.7)
	    PACKAGE_LIST+=" python3-targetcli-fb dapl-utils "
	    PACKAGE_LIST+=" openmpi4 mpich"
	    PACKAGE_LIST+=" mpitests-openmpi4 mpitests-mpich"
	    ;;
        16.0)
	    PACKAGE_LIST+=" targetcli-fb "
	    PACKAGE_LIST+=" openmpi4 openmpi5 mpich"
	    PACKAGE_LIST+=" mpitests-openmpi4 mpitests-openmpi5 mpitests-mpich"
	    ;;
        16.1)
	    PACKAGE_LIST+=" targetcli-fb "
	    PACKAGE_LIST+=" openmpi4 openmpi5 mpich"
	    PACKAGE_LIST+=" mpitests-openmpi4 mpitests-openmpi5 mpitests-mpich"
	    PACKAGE_LIST+=" mpitests-mvapich4-ucx mpitests-mvapich4-ofi"
	    ;;
	2022*|2023*|2024*)
 	    PACKAGE_LIST+=" python3-targetcli-fb dapl-utils "
	    PACKAGE_LIST+=" openmpi2 mpich openmpi3 openmpi4"
	    PACKAGE_LIST+=" mpitests-openmpi2 mpitests-mpich"
	    PACKAGE_LIST+=" mpitests-openmpi3 mpitests-openmpi4"
            ;;
	2025*)
	    PACKAGE_LIST+=" python3-targetcli-fb dapl-utils "
	    PACKAGE_LIST+=" mpich openmpi4 openmpi5"
	    PACKAGE_LIST+=" mpitests-mpich mpitests-openmpi4"
	    PACKAGE_LIST+=" mpitests-openmpi5"
	    ;;
    esac
    echo $PACKAGE_LIST " "
}

KMOD_LIST="ib_ipoib rdma_rxe siw mlx5_ib mlx5_fwctl mlx5_core mlx4_ib bnxt_re qedr"
do_remove_kmods()
{
    local mod
    for mod in $KMOD_LIST; do
        rmmod $mod 2>/dev/null || true
    done
    true
}
remove_kmods()
{
    local host=$1

    tp_fun $host do_remove_kmods
}

do_load_kmods(){
    local mod
    for mod in $KMOD_LIST; do
        if modprobe -nq $mod; then
            modprobe $mod || return 1
        fi
    done
}
load_kmods(){
    local host=$1

    tp_fun $host do_load_kmods
}

reload_kmods(){
    local host=$1

    remove_kmods $host
    load_kmods $host
}
