#!/bin/bash

function install_rpmtools() {
    log_info "install package sshpass."
    rpm -Uvh "${install_path}"/Install_Packages/rpm-tools/*
}

function install_baserpm() {
    log_info "install package expect."
    rpm -Uvh "${install_path}"/Install_Packages/expect/*
    #rpm -Uvh "${install_path}"/Install_Packages/libnetsnmp/*
    rpm -Uvh "${install_path}"/Install_Packages/base-rpm/*
}

function install_fio() {
    # install fio
    log_info "install package fio"
    for _ip in ${IP_LIST[@]}; do
        $SCP "${install_path}"/Install_Packages/base-rpm root@${_ip}:/tmp/
	$SCP "${install_path}"/Install_Packages/rpm-tools root@${_ip}:/tmp/
	$SCP "${install_path}"/Install_Packages/haproxy root@${_ip}:/tmp/
	$SCP "${install_path}"/Install_Packages/keepalived root@${_ip}:/tmp/
	$SCP "${install_path}"/Install_Packages/chrony.tar.gz root@${_ip}:/tmp/
	$SCP "${install_path}"/Install_Packages/nfs root@${_ip}:/tmp/
        $SSH root@${_ip} "rpm -ivh /tmp/base-rpm/fio-3.2-1.el7.x86_64.rpm &> /dev/null"
    done
}

function install_ipvs() {
    # install fio
    log_info "install package ipvs"
    for _ip in ${IP_LIST[@]}; do
        $SCP "${install_path}"/Install_Packages/ipvs root@${_ip}:/tmp/
        $SSH root@${_ip} "rpm -ivh /tmp/ipvs/* &> /dev/null"
    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_nfs_server() {
    # install install_ipcalc
    log_info "install nfs"
    if ! $(which nfs &> /dev/null);then
        #chmod 777 "${install_path}"/Install_Packages/nfs-utils-1.3.0-0.68.el7.x86_64.rpm
        #chmod 777 "${install_path}"/Install_Packages/rpcbind-0.2.0-49.el7.src.rpm
        #rpm -ivh "${install_path}"/Install_Packages/nfs-utils-1.3.0-0.68.el7.x86_64.rpm --force --nodeps
        #rpm -ivh "${install_path}"/Install_Packages/rpcbind-0.2.0-49.el7.src.rpm --force --nodeps
        #yum -y remove nfs-utils rpcbind
        sleep 3
        rpm -ivh "${install_path}"/Install_Packages/nfs/* --force --nodeps
        #yum -y install nfs-utils rpcbind
        mkdir -p /data/nas/{esback,data,weboffice}
        service nfs status
        service rpcbind status
        cat << EOF > /etc/exports
/data/nas/esback *(rw,sync,no_root_squash)
/data/nas/data *(rw,sync,no_root_squash)
/data/nas/weboffice *(rw,sync,no_root_squash)
EOF
        exportfs -rv
        systemctl restart rpcbind.service
        systemctl restart nfs.service
	systemctl restart nfs-server
	systemctl restart nfs-utils.service
	systemctl enable nfs-server
    fi
}

function install_requirements() {
    install_rpmtools
    install_baserpm
    source "${install_path}"/scripts/ssh_key_config.sh
    if [ "$TYPE" == "allinone" ]; then
        rpm -ivh "${install_path}"/Install_Packages/base-rpm/fio-3.2-1.el7.x86_64.rpm
    fi
    if [ "$TYPE" == "cluster" ]; then
        install_nfs_server
        install_fio
	install_ipvs
    fi
    install_mc
    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 ntp1.aliyun.com 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
