#!/bin/bash

function install_sshpass() {
    log_info "install package sshpass."
    rpm -Uvh "${install_path}"/Install_Packages/anolis-soft/lm_sensors-libs/*.rpm --force
    rpm -Uvh "${install_path}"/Install_Packages/anolis-soft/ipset/*.rpm --force
    rpm -Uvh "${install_path}"/Install_Packages/anolis-soft/sshpass/*.rpm --force
    sleep 5
}

function install_expect() {
    log_info "install package expect."
    rpm -Uvh "${install_path}"/Install_Packages/anolis-soft/expect/*.rpm --force
}

scp_soft() { 
    for _ip in ${IP_LIST[@]}; do
	echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	$SCP -r "${install_path}"/Install_Packages/anolis-soft/* root@${_ip}:/tmp/
	#$SCP "${install_path}"/Install_Packages/anolis-soft/ root@${_ip}:/tmp/
	echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    done
}

function install_jq() {
    log_info "install package jq."
    rpm -Uvh "${install_path}"/Install_Packages/anolis-soft/jq/*.rpm --force
}

function install_iperf3() {
    # install iperf3
    log_info "install package iperf3"
    for _ip in ${IP_LIST[@]}; do
        $SCP -r "${install_path}"/Install_Packages/anolis-soft/iperf3 root@${_ip}:/tmp/
        $SSH root@${_ip} "rpm -ivh /tmp/iperf3/*.rpm --force"
	$SSH root@${_ip} "rpm -ivh /tmp/lm_sensors-libs/*.rpm --force"
	$SSH root@${_ip} "rpm -ivh /tmp/ipset/*.rpm --force"
    done
}

function install_fio() {
    # install fio
    log_info "install package fio"
    for _ip in ${IP_LIST[@]}; do
        $SCP -r "${install_path}"/Install_Packages/anolis-soft/fio root@${_ip}:/tmp/
        $SSH root@${_ip} "rpm -ivh /tmp/fio/*.rpm --force"
    done
}

function install_bc() {
    # install bc
    log_info "install package bc"
    for _ip in ${IP_LIST[@]}; do
        $SCP -r "${install_path}"/Install_Packages/anolis-soft/bc root@${_ip}:/tmp/
        $SSH root@${_ip} "rpm -ivh /tmp/bc/*.rpm --force"
    done
}

function install_mc() {
    # install mc
    log_info "install s3 tool mc"
    \cp "${install_path}"/Install_Packages/mc /usr/local/sbin/
    chmod +x /usr/local/sbin/mc 
}

function install_ipcalc() {
    # install install_ipcalc
    log_info "install ipcalc"
    if ! $(which ipcalc &> /dev/null);then
        rpm -ivh "${install_path}"/Install_Packages/ipcalc*.rpm
    fi
}

function install_nfs_server() {
    # install install_ipcalc
    log_info "install nfs"
    if ! $(which nfs &> /dev/null);then
        dnf -y remove nfs-* rpcbind
        sleep 5
        rpm -ivh "${install_path}"/Install_Packages/anolis-soft/nfs/* --force --nodeps
        #yum -y install nfs-utils rpcbind
        mkdir -p /data/nas/esback
        service nfs status
        service rpcbind status
        cat << EOF > /etc/exports
/data/nas/esback *(rw,sync,no_root_squash)
EOF
        exportfs -rv
        systemctl restart rpcbind.service
        systemctl restart nfs-server
        systemctl enable rpcbind.service
        systemctl enable nfs-server
    fi
}

function install_requirements() {
    install_sshpass
    install_expect

    source "${install_path}"/scripts/ssh_key_config.sh
    sleep 3
    scp_soft
    install_jq

    install_nfs_server
    install_fio
    install_iperf3
    install_bc
    install_expect
    install_mc

    chronyd -q "server $TIMESERVER iburst"
    sleep 3
    #install_ipcalc
    cat << EOF > /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 添加时间服务器 TIMESERVER
#server 0.centos.pool.ntp.org iburst
server $TIMESERVER iburst
rtcsync
makestep 30 3
# 允许指定网段访问此时间服务器，不然只允许本地网络
allow 0.0.0.0/0
# 此服务离线了也可以对外提供服务，提供服务的层级时10
local stratum 10
#masterserver
EOF
    sleep 3
    systemctl restart chronyd.service
    sleep 2
    chronyc makestep
    chronyc sources
    chronyc tracking
}

install_requirements
