#! /bin/bash

#使用须知
#1、先将操作系统的安装镜像和gpfs的软件包放在/root/目录下
#2、在/etc/hosts中设置好主机名和ip地址。
#
#

os_version=$(cat /etc/redhat-release)
width=$(tput cols)
hostnumber=$(cat /etc/hosts | grep -v 127.0.0.1| grep -v ::1| wc -l)
if [ $hostnumber = 0 ];then
 echo -e "Please configure the /etc/hosts file  \033[31mFail\033[0m."
	exit 1 
else	
	echo -e " $hostnumber hosts detected  \033[32mPass\033[0m." 
fi
for (( i=1;i<=hostnumber;i++ ))
	do tmpip=$(cat /etc/hosts | awk -F " " {'print $1'} | grep -v 127.0.0.1| grep -v ::1  | sed -n "$i"p)
	ip a | grep $tmpip
	if [ $? = 0 ];then ip=$tmpip
	fi
	done
hostname=$(cat /etc/hosts | grep $ip | awk -F "$ip" '{print $2}')
hostnamectl --static set-hostname $hostname


function check_os_version(){
	echo -e "Detecting operating system version."
#	echo ""
	if  [ "$os_version" = 'Red Hat Enterprise Linux release 9.2 (Plow)' ]; then
		echo -e "The operating system version is $os_version   \033[32mPass\033[0m."
	else
		echo -e "The operating system version is $os_version   \033[31mFail\033[0m."
		exit 1
	fi
	
}

function selinx_firewalld(){
	echo "Changing selinux and firewall"
	setenforce 0
	sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
	systemctl stop firewalld 
	systemctl disable firewalld
	echo "Checking selinux and firewall"
	if [ `getenforce` = Eenforcing	]; then
	       echo -e "Selinux status is `getenforce` \033[31mFail\033[0m."
	       exit 1 
        else	
		echo -e "Selinux status is `getenforce` \033[32mPass\033[0m."
		
	fi
	
	echo "Selinux configuration file checking."
	selinux_config=$(cat /etc/selinux/config | grep SELINUX=disabled | wc -l)
	if [ $selinux_config = 2  ];then
		echo -e "Selinux configuration file \033[32mPass\033[0m."
	else
		echo -e "Selinux configuration file \033[31mFail\033[0m."
		exit 1 
	fi

	systemctl status firewalld | grep "inactive"
	if [ $? = 0 ]; then
		echo -e "Firewalld status is inactive \033[32mPass\033[0m."
	else
		echo -e "Firewalld status is active \033[31mFail\033[0m."
		exit 1
	fi	

	systemctl list-unit-files --type=service | grep enabled| grep firewalld| grep "disabled        enabled"
	if [ $? = 0 ]; then
		echo -e "Firewalld startup status is disable  \033[32mPass\033[0m."
	else
		echo -e "Firewalld startup status is enable  \033[31mFail\033[0m."
		exit 1
	fi

}
function check_installation_resources(){
	echo "Verifying installation resources。"
#检查系统镜像是否存在。
	if [[ -e /root/rhel-9.2-x86_64-dvd.iso ]]; then
		echo "Verifying operating system installation image."
	else
		echo "/root/rhel-9.2-x86_64-dvd.iso does not exist, please check."
		echo "Please place the installation image of rhel9.2 in the /root/ directory"
		exit 1 
	fi
#校验系统镜像。
	os_installation_md5sum=$(md5sum /root/rhel-9.2-x86_64-dvd.iso)
	echo "$os_installation_md5sum"
	if [ "$os_installation_md5sum" = "90cf58ff7a8f6ef8cb20b8ff091e84b7  /root/rhel-9.2-x86_64-dvd.iso" ];then
		echo -e "/root/rhel-9.2-x86_64-dvd.iso   \033[32mPass\033[0m."
	else
		echo -e "/root/rhel-9.2-x86_64-dvd.iso   \033[31mFail\033[0m."
		exit 1
	fi
#检查gpfs安装包是否存在。
        if [[ -e /root/Storage_Scale_Erasure_Code-5.1.8.0-x86_64-Linux-install ]]; then
                echo "Verifying GPFS installation image."
        else
                echo "/root/Storage_Scale_Erasure_Code-5.1.8.0-x86_64-Linux-install does not exist, please check."
		echo "Please place the installation image of gpfs 5.1.8.0 in the /root/ directory"
		exit 1 
	fi
#校验gpfs安装包
	gpfs_installation_md5sum=$(md5sum /root/Storage_Scale_Erasure_Code-5.1.8.0-x86_64-Linux-install)
	echo $gpfs_installation_md5sum
	if [ "$gpfs_installation_md5sum" = "8f627c54749ae530afca68b05d02af77  /root/Storage_Scale_Erasure_Code-5.1.8.0-x86_64-Linux-install" ];then
                echo -e "/root/Storage_Scale_Erasure_Code-5.1.8.0-x86_64-Linux-install   \033[32mPass\033[0m."
        else
                echo -e "/root/Storage_Scale_Erasure_Code-5.1.8.0-x86_64-Linux-install   \033[31mFail\033[0m."
                exit 1
        fi
}

function repo_build(){
	echo "Building a local repo"
	mount -o loop /root/rhel-9.2-x86_64-dvd.iso /mnt/
	echo "[centos-app]
name=os-app
baseurl=file:///mnt/AppStream
gpgcheck=0
enable=1

[centos-BaseOS]
name=os-BaseOS
baseurl=file:///mnt/BaseOS
gpgcheck=0
enable=1" > /etc/yum.repos.d/gpfs.repo
	echo "installing nginx."
	yum install nginx -y 
	sed -i 's/listen       80;/listen       82;/g' /etc/nginx/nginx.conf
	systemctl start nginx
	systemctl enable nginx
	echo "Copy the system software repository to nginx."
	cp -r /mnt/ /usr/share/nginx/html/
	umount /mnt
	mv /usr/share/nginx/html/mnt /usr/share/nginx/html/rhel92 
	chmod +x /root/Storage_Scale_Erasure_Code-5.1.8.0-x86_64-Linux-install
	/root/Storage_Scale_Erasure_Code-5.1.8.0-x86_64-Linux-install  --silent
	cp -r /usr/lpp/mmfs/5.1.8.0/ /usr/share/nginx/html/
	echo "[os-base]
name=os-base
baseurl=http://$ip:82/rhel92/BaseOS/
gpgcheck=0
enabled=1 

[os-app]
name=os-app
baseurl=http://$ip:82/rhel92/AppStream/
gpgcheck=0
enabled=1


[gpfs-base]
name=gpfs-base
baseurl=http://$ip:82/5.1.8.0/gpfs_rpms/
gpgcheck=0
enabled=1

[gpfs-ganesha]
name=gpfs-ganesha
baseurl=http://$ip:82/5.1.8.0/ganesha_rpms/rhel9/
gpgcheck=0
enabled=1

[gpfs-zimon]
name=gpfs-zimon
baseurl=http://$ip:82/5.1.8.0/zimon_rpms/rhel9/
gpgcheck=0
enabled=1" > /etc/yum.repos.d/gpfs.repo
	
}

function modifying_software_configuration(){
	echo "Modifying the chrony configuration"
	yum install -y chrony
	ip_sub1=$(echo $ip | awk -F "." '{print $1}' )
	ip_sub2=$(echo $ip | awk -F "." '{print $2}' )
	echo "allow $ip_sub1.$ip_sub2.0.0/16" >> /etc/chrony.conf
	echo "local stratum 10" >> /etc/chrony.conf
	systemctl restart chronyd
	systemctl enable chronyd
	chronyc sources -v
	echo "Modifying the ssh configuration"
	sed -i 's/#   StrictHostKeyChecking ask/    StrictHostKeyChecking no/g' /etc/ssh/ssh_config
	echo "Verify the ssh configuration"
	cat /etc/ssh/ssh_config | grep "StrictHostKeyChecking no"
	if [ $? = 0  ];then
		echo -e "Verify the ssh configuration \033[32mPass\033[0m."
	else
		echo -e "Verify the ssh configuration \033[31mFail\033[0m."
		exit 1 
	fi
}

function software_install(){
	echo "Installing gpfs."
	yum install -y gpfs.adv gpfs.afm.cos gpfs.base gpfs.compression gpfs.crypto gpfs.docs gpfs.gnr gpfs.gnr.base gpfs.gnr.support-scaleout gpfs.gpl gpfs.gskit gpfs.java gpfs.license.ec gpfs.msg.en_US gpfs.gss.pmsensors gpfs.gui gpfs.nfs*
	echo "Verify gpfs installation"
	number_gpfs=$(rpm -aq | grep gpfs|wc -l)
	if [ $number_gpfs = 20  ];then
		echo -e "Verify gpfs installation  \033[32mPass\033[0m."
	else	
		echo -e "Installation failed, please check \033[31mFail\033[0m."
		exit 1
	fi
	/usr/bin/yum -y install kernel-devel make cpp gcc gcc-c++ kernel-headers numactl 
	/usr/lpp/mmfs/bin/mmbuildgpl

}

function generate_sshkey(){
	if [ `ls -al /root/.ssh/ | grep id_rsa|wc -l` -eq 0 ];then
		ssh-keygen -t rsa -N ''<<EOF
/root/.ssh/id_rsa
yes


EOF
else
	echo -e "检测出/root/.ssh/中已存在id_rsa文件"
	exit 1
	
fi
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
if [ $? -eq 0 ]
then
	echo -e "生成密钥成功"
else
	echo -e "Error"
fi
}

function configure_other_hosts(){
	yum install sshpass -y
	other_host_number=$(($hostnumber-1))
	echo "需配置的主机$other_host_number"
for  ((hostid=1;hostid<=$other_host_number;hostid++))
do
	curr_number=$(($hostid+1))
	echo -e "Total \033[32m$hostnumber\033[0m, $curr_number/$hostnumber"
	curr_host_ip=$(cat /etc/hosts | grep -v 127.0.0.1 | grep -v ::1 |awk -F " " '{print $1}' | grep -v $ip | sed -n "$hostid"p)
	sshpass -p '1111111' scp -r /root/.ssh root@$curr_host_ip:/root/
	ssh $curr_host_ip ip a
	echo -e "\033[32m$curr_host_ip\033[0m hostname"
	curr_hostname=$(cat /etc/hosts | grep $curr_host_ip | awk -F "$curr_host_ip" '{print $2}')
	ssh $curr_host_ip hostnamectl --static set-hostname $curr_hostname
#Detect the current host operating system
	echo -e "\033[32m$curr_host_ip\033[0m Detecting operating system version."
	curr_os_version=$(ssh $curr_host_ip cat /etc/redhat-release)
	if  [ "$curr_os_version" = 'Red Hat Enterprise Linux release 9.2 (Plow)' ]; then
		echo -e "\033[32m$curr_host_ip\033[0m The operating system version is $os_version   \033[32mPass\033[0m."
	else
		echo -e "\033[32m$curr_host_ip\033[0m The operating system version is $os_version   \033[31mFail\033[0m."
		exit 1
	fi
#SELinux and Firwalld
	echo -e "\033[32m$curr_host_ip\033[0m  Changing selinux and firewall"
	ssh $curr_host_ip setenforce 0
	ssh $curr_host_ip sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
	ssh $curr_host_ip systemctl stop firewalld 
	ssh $curr_host_ip systemctl disable firewalld
	echo -e  "\033[32m$curr_host_ip\033[0m  Checking selinux and firewall"
	if [ `ssh $curr_host_ip getenforce` = Eenforcing	]; then
	       echo -e "\033[32m$curr_host_ip\033[0m Selinux status is `getenforce` \033[31mFail\033[0m."
	       exit 1
        else
		echo -e "\033[32m$curr_host_ip\033[0m Selinux status is `getenforce` \033[32mPass\033[0m."
	fi

	echo -e  "\033[32m$curr_host_ip\033[0m Selinux configuration file checking."
	curr_selinux_config=$(ssh $curr_host_ip cat /etc/selinux/config | grep SELINUX=disabled | wc -l)
	if [ $curr_selinux_config = 2  ];then
		echo -e "\033[32m$curr_host_ip\033[0m Selinux configuration file \033[32mPass\033[0m."
	else
		echo -e "\033[32m$curr_host_ip\033[0m Selinux configuration file \033[31mFail\033[0m."
		exit 1 
	fi


	ssh $curr_host_ip systemctl status firewalld | grep "inactive"
	if [ $? = 0 ]; then
		echo -e "\033[32m$curr_host_ip\033[0m Firewalld status is inactive \033[32mPass\033[0m."
	else
		echo -e "\033[32m$curr_host_ip\033[0m Firewalld status is active \033[31mFail\033[0m."
		exit 1
	fi	

	ssh $curr_host_ip systemctl list-unit-files --type=service | grep enabled| grep firewalld| grep "disabled        enabled"
	if [ $? = 0 ]; then
		echo -e "\033[32m$curr_host_ip\033[0m Firewalld startup status is disable  \033[32mPass\033[0m."
	else
		echo -e "\033[32m$curr_host_ip\033[0m Firewalld startup status is enable  \033[31mFail\033[0m."
		exit 1
	fi

	echo -e "\033[32m$curr_host_ip\033[0m Yum configuration"
	scp /etc/yum.repos.d/gpfs.repo $curr_host_ip:/etc/yum.repos.d/
	echo -e "\033[32m$curr_host_ip\033[0m Installing gpfs."
	ssh $curr_host_ip yum install -y gpfs.adv gpfs.afm.cos gpfs.base gpfs.compression gpfs.crypto gpfs.docs gpfs.gnr gpfs.gnr.base gpfs.gnr.support-scaleout gpfs.gpl gpfs.gskit gpfs.java gpfs.license.ec gpfs.msg.en_US gpfs.gss.pmsensors gpfs.nfs*
	echo -e "\033[32m$curr_host_ip\033[0m  Verify gpfs installation"
	curr_number_gpfs=$(ssh $curr_host_ip rpm -aq | grep gpfs|wc -l)
	if [ $curr_number_gpfs = 18  ];then
		echo -e "\033[32m$curr_host_ip\033[0m Verify gpfs installation  \033[32mPass\033[0m."
	else	
		echo -e "\033[32m$curr_host_ip\033[0m Installation failed, please check \033[31mFail\033[0m."
		exit 1
	fi
	ssh $curr_host_ip yum -y install kernel-devel make cpp gcc gcc-c++ kernel-headers numactl 
	ssh $curr_host_ip /usr/lpp/mmfs/bin/mmbuildgpl

	echo -e "\033[32m$curr_host_ip\033[0m chrony.conf"
	scp $curr_host_ip:/etc/chrony.conf /tmp/chrony.conf
	echo "server $ip iburst" >> /tmp/chrony.conf
	scp /tmp/chrony.conf $curr_host_ip:/etc/
	ssh $curr_host_ip systemctl restart chronyd
	ssh $curr_host_ip chronyc sources -v
	ssh $curr_host_ip systemctl enable chronyd.service

	echo -e "\033[32m$curr_host_ip\033[0m ssh_config"
	scp /etc/ssh/ssh_config root@$curr_host_ip:/etc/ssh/
	echo -e "\033[32m$curr_host_ip\033[0m Verify the ssh configuration"
        ssh $curr_host_ip cat /etc/ssh/ssh_config | grep "StrictHostKeyChecking no"
        if [ $? = 0  ];then
                echo -e "\033[32m$curr_host_ip\033[0m Verify the ssh configuration \033[32mPass\033[0m."
        else
                echo -e "\033[32m$curr_host_ip\033[0m Verify the ssh configuration \033[31mFail\033[0m."
		exit 1
        fi

done
}


function install_gpfs(){
	check_os_version
	selinx_firewalld
	check_installation_resources
	repo_build
	software_install
	modifying_software_configuration
	generate_sshkey
	configure_other_hosts
	echo "installing $ip"
}


# Check if GPFS is already installed,exit.
if [[ -e /var/lib/mmfs/ ]]; then
        echo "GPFS has been installed。"
	exit 1
else
        install_gpfs
fi
