#!/bin/sh
#
# ServHA install script for Linux
# By WANG LONG MicroColor
#
platformstr="x86_64"
mirrormethod=0 # 0 use op 1 build
installblockmir=1
installpathmir=1
installdrbdmir=0

#!/bin/bash

# 检查root权限
if [ "$EUID" -ne 0 ]; then
    echo "遗憾: ServHA安装脚本需要以root权限运行。"
    exit 1
fi

echo "感谢您选用ServHA Mirror镜像双机热备软件！"
echo "本脚本将自动安装 ServHA Mirror程序及其相关组件,"

if [ ! -d /etc/rpm ]; then
	if [ -x /bin/dpkg ]; then
		./uins.sh
		exit 0;
	fi
	if [ -x /usr/bin/dpkg ]; then
		./uins.sh
		exit 0;
	fi
fi

lsb_release -a 2>/dev/null | grep Ubuntu > /dev/null
if [ $? -eq 0 ]; then
	./uins.sh
	exit 0;
fi



Clearup(){
	rm -f /tmp/tmp_rpmret.txt > /dev/null
}

basepath=$(cd `dirname $0`; pwd)
cd $basepath

issuse=0
ismirinstall=1
if [ -f /etc/SuSE-release ]; then
	issuse=1
fi

lsmod | grep drbd > /dev/null
if [ $? != 0 ]; then
	ismirinstall=0
fi
uname -r | grep -E "el7" > /dev/null
if [ $? == 0 ]; then
	chkcfgret=`chkconfig drbd`
	if [ -z $chkcfgret ]; then
		ismirinstall=0
	fi
else
	if [ ! -x "/etc/init.d/drbd" ]; then
		ismirinstall=0
	fi
fi


echo "[输入数字，以选择要安装的镜像组件 Choose the mirroring component to install]"
select usersel in "全部安装 All" "仅卷镜像 Only volume mirroring comonent" "仅目录镜像 Only directory mirroring component"; do
    case $usersel in
        	"全部安装 All" ) break;;
        	"仅卷镜像 Only volume mirroring comonent" ) installpathmir=0; break;;
        	"仅目录镜像 Only directory mirroring component" ) installblockmir=0; break;;
    esac
done

if [ $installblockmir -eq 1 ]; then

	#echo "您选择了安装卷镜像，是否额外安装drbd镜像组件：卷镜像支持在原有分区上直接建立镜像同步; drbd镜像组件将带来额外的功能：在原有分区之上，建立一个虚拟块设备进行镜像同步。"
	#echo "输入y表示安装drbd镜像组件。"
	#read -n 1 -p "是否安装drbd镜像组件（y/n）？" usersel
	#if [ "$usersel" = "y" ]; then
	#	echo -e "\n您选择安装drbd镜像组件。"
	#	installdrbdmir=1
	#else
	#	echo -e "\n您选择不要安装drbd镜像组件。"
	#	installdrbdmir=0
	#fi

	installdrbdmir=1
fi


echo "正在检查您的系统环境..."
MOD_DIR="/lib/modules/$(uname -r)"
# 检查模块是否存在
if [ "$(uname -r | cut -d'.' -f1)" -ge 5 ] || \
   find "$MOD_DIR" -name "drbd.ko*" -print -quit | grep -q .
then
	echo "OK."
else
	if [ $installdrbdmir -eq 1 ]; then

		ischeckok=2
		while [ $ischeckok != 0 ]
		do 
			uname -r | grep -E "el7|el6" > /dev/null
			if [ $? == 0 -a $ismirinstall == 0 -o $issuse==1 ]; then
				echo "请确保系统已经安装了 gcc,kernel-devel,flex。您可以在linux的安装光盘中找到相关的安装包。"
				mirrormethod=1
				rpm -qa > /tmp/tmp_rpmret.txt
				#checkgcc
				ischeckok=0
				cat /tmp/tmp_rpmret.txt | grep ^make- > /dev/null
				if [ $? -eq 1 ] ; then
					echo -e "\033[31m检测到您没有安装make，请先安装make。 \033[0m"
					ischeckok=2
				fi
				cat /tmp/tmp_rpmret.txt | grep ^gcc- > /dev/null
				if [ $? -eq 1 ] ; then
					echo -e "\033[31m检测到您没有安装gcc，请先安装gcc。 \033[0m"
					ischeckok=2
				fi
				if [ $issuse == 1 ]; then
					uname -r | grep "default" > /dev/null
					if [ $? == 0 ]; then
						cat /tmp/tmp_rpmret.txt | grep ^kernel-default-devel- > /dev/null
						if [ $? -eq 1 ] ; then
							echo -e "\033[31m检测到您没有安装kernel-default-devel，请先安装kernel-default-devel。 \033[0m"
							ischeckok=2
						fi
					else
						cat /tmp/tmp_rpmret.txt | grep ^kernel-xen-devel- > /dev/null
						if [ $? -eq 1 ] ; then
							echo -e "\033[31m检测到您没有安装kernel-xen-devel，请先安装kernel-xen-devel。 \033[0m"
							ischeckok=2
						fi
					fi
				else
					cat /tmp/tmp_rpmret.txt | grep ^kernel-devel- > /dev/null
					if [ $? -eq 1 ] ; then
						echo -e "\033[31m检测到您没有安装kernel-devel，请先安装kernel-devel。 \033[0m"
						ischeckok=2
					fi
				fi
				cat /tmp/tmp_rpmret.txt | grep ^flex- > /dev/null
				if [ $? -eq 1 ] ; then
					echo -e "\033[31m检测到您没有安装flex，请先安装flex。 \033[0m"
					ischeckok=2
				fi

				if [ $ischeckok != 0 ] ; then
					echo -e "\033[31m安装前检查没有通过。\033[0m"
					Clearup
					read -n 1 -p "是否要执行insenv.sh来自动安装相关依赖组件（y/n）？" usersel
					if [ "$usersel" = "n" ]; then
						echo -e "\n您选择不自动安装依赖组件，安装程序无法继续，退出操作。"
						exit 3
					fi
					./insenv.sh
					if [ $? != 0 ] ; then
						echo -e "\033[31m自动安装依赖失败，退出操作。 \033[0m"
						exit 4
					fi
					echo -e "依赖安装脚本已完成，正在重新检测..."
				fi
			fi
		done
	fi
fi
Clearup
#check for selinux
if [ $issuse != 1 ]; then
	hasenforce=0
	getenforce | grep Enforcing > /dev/null
	if [ $? == 0 ]; then
		hasenforce=1
	fi
	cat /etc/selinux/config | grep ^SELINUX=disabled > /dev/null
	if [ $? != 0 ]; then
		hasenforce=1
	fi

	if [ $hasenforce = 1 ]; then
		echo -e "检测到您的系统开启了SELINUX，需要禁用SELINUX后安装才能继续"
		read -n 1 -p "是否需要禁用（y/n）？" usersel
		if [ "$usersel" = "n" ]; then
			echo -e "\n您选择不禁用SELINUX，安装程序无法继续，退出操作。"
			Clearup
			exit 3
		fi
		setenforce 0 > /dev/null
		sed -i "s/SELINUX=.*/SELINUX=disabled/" /etc/selinux/config > /dev/null
		if [ $? != 0 ]; then
			echo -e "\n禁用SELINUX失败，安装程序无法继续，退出操作。"
			exit 2
		fi
		echo -e "\n已成功禁用SELINUX。"
	fi
fi

basepath=$(cd `dirname $0`; pwd)
cd $basepath

echo "正安装ServHA.."
rpm -ivh Packages/ServHA-Mirror-*.$platformstr.rpm


if [ $installdrbdmir -eq 1 ]; then
	if [ $ismirinstall == 0 ]; then
		uname -r | grep el7 > /dev/null
		if [ $? != 0 ]; then
			uname -r | grep el6 > /dev/null
			if [ $? == 0 -o $issuse==1 ]; then
				echo "begin install drbd"
				/usr/share/servha/drbd/build.sh
				sed -i 's/allow_unsupported_modules 0/allow_unsupported_modules 1/' /etc/modprobe.d/unsupported-modules 2>/dev/null
			else
				/usr/share/servha/op install_drbd
			fi
		else
			/usr/share/servha/drbd/build.sh
		fi
		modprobe drbd
		lsmod | grep drbd > /dev/null
		if [ $? != 0 ]; then
			echo -e "\033[31m无法为当前内核版本自动构建卷镜像同步模块drbd，请将如下内核版本发给技术支持人员： \033[0m"
			echo -e "\033[31mCould not build volume mirroring component drbd, please send the following kernel version string to technical support personnel: \033[0m"
			echo "$(uname -r)"
		fi
	fi
fi


if [ $installpathmir -eq 1 ]; then
	/usr/share/servha/insr0
fi

if [ $installblockmir -eq 1 ]; then
	/usr/share/servha/insmir
fi

systemctl start servmird.service
systemctl start servhad.service


isblockmirready=0
isdrbdready=0
ispathmirready=0

MOD_DIR="/lib/modules/$(uname -r)"
if find "$MOD_DIR" -name "drbd.ko*" -print -quit | grep -q .
 then
	isdrbdready=1
fi
MOD_DIR="/lib/modules/$(uname -r)/updates"
if find "$MOD_DIR" -name "mcmirflt.ko*" -print -quit | grep -q .
 then
	isblockmirready=1
fi
if find "$MOD_DIR" -name "mcnsr0.ko*" -print -quit | grep -q .
 then
	ispathmirready=1
fi

echo -e "以下是镜像功能相关内核模块的安装结果："
if [ $isblockmirready -eq 1 ]; then
	echo -e "  -- 卷镜像驱动模块：\t\033[32m成功安装\033[0m"
else
	echo -e "  -- 卷镜像驱动模块：\t\033[31m未安装\033[0m"
fi

if [ $isdrbdready -eq 1 ]; then
	echo -e "  -- drbd镜像驱动模块：\t\033[32m成功安装\033[0m"
else
	echo -e "  -- drbd镜像驱动模块：\t\033[31m未安装\033[0m"
fi
if [ $ispathmirready -eq 1 ]; then
	echo -e "  -- 目录镜像驱动模块：\t\033[32m成功安装\033[0m"
else
	echo -e "  -- 目录镜像驱动模块：\t\033[31m未安装\033[0m"
fi

if [ $isblockmirready -eq 0 -o $ispathmirready -eq 0  ]; then
	echo -e "存在失败项，将影响对应镜像功能的使用。您可以联系技术支持人员，提供您的内核版本信息：$(uname -r)获取支持。"
fi

if [ $isdrbdready -eq 0 ]; then
	echo -e "如需安装旧版drbd镜像功能支持模块，请手工运行/usr/share/servha/drbd/build.sh"
fi

command -v curl | grep curl > /dev/null
if [ $? -eq 1 ] ; then
	echo -e "## [ 警告 ] curl命令缺失，请手动安装该依赖包."
fi
command -v fuser | grep fuser > /dev/null
if [ $? -eq 1 ] ; then
	echo -e "## [ 警告 ] fuser命令缺失，请手动安装该依赖包."
fi
#command -v arping | grep arping > /dev/null
#if [ $? -eq 1 ] ; then
#	echo -e "## [ 警告 ] arping命令缺失，请手动安装该依赖包."
#fi


echo -e "==============================================="
echo -e "  输入 'servha' 命令启动配置监控端开始配置热备系统。"
echo -e "  ServHA Mirror安装脚本运行完成，请继续后续配置！"
echo -e "==============================================="


Clearup
exit 0
