#!/bin/bash
##################################################
# HA-Lizard noSAN Re-Installer 1.1
# used to reinstall HA-Lizard+iSCSI-HA+All Deps
# after a dom0 upgrade has occured
##################################################
#################################################################################################
#
# HA-Lizard - Open Source High Availability Framework for Xen Cloud Platform and XenServer
#
# Copyright 2019 Salvatore Costantino
# ha@pulsesupply.com
#
# This file is part of HA-Lizard.
#
#    HA-Lizard 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.
#
#    HA-Lizard 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 HA-Lizard.  If not, see <http://www.gnu.org/licenses/>.
#
##################################################################################################

clear
echo -e "\n\n\n"
echo "-----------------------------------------------------------------------"
echo "| This script will re-install all packages and dependencies required  |"
echo "| to restore an HA-Lizard pool's shared storage after a major dom0    |"
echo "| upgrade has been performed, which requires that HA and shared       |"
echo "| storage functionality be re-installed. Since HA-Lizard and iSCSI-HA |"
echo "| can store all thier configuration parameters in the XAPI database,  |"
echo "| running this script will entirely re-install all required packages  |"
echo "| and restore all configuration data                                  |"
echo "|                                                                     |"
echo "| IMPORTANT: iSCSI-HA configuration is expected to be saved to the    |"
echo "| pool database prior to upgrading a host. This can be done using the |"
echo "| CLI tool <iscsi-cfg backup>. If your version of the cli tool does   |"
echo "| not support the backup function, first upgrade iSCSI-HA in order to |"
echo "| backup critical configuration data before upgrading dom0            |"
echo "-----------------------------------------------------------------------"
echo "Continue? [y|n]"
while :
do
	read CONTINUE
	if [ "$CONTINUE" != "y" ]
	then
		continue
	else
		break
	fi
done

INSTALL_LOG=/tmp/ha-lizard-deps-install.log
#####################################
# Check for connection to internet
#####################################
HALIZARD_MIRROR_URL='http://halizard.org/release'
wget -q --tries=10 --timeout=20 --spider $HALIZARD_MIRROR_URL
if [ $? -ne 0 ]
then
	echo "No internet access detected. An internet connection"
	echo "is required to download the required components"
	exit 1
fi

#######################################
# Create repos required for installing
# DRBD init, DRBD userland tools, tgt
#######################################
echo '#######################
## Base CentOS7 Repo ##
#######################
[ha-lizard-base]
name=CentOS-$releasever - ha-lizard
mirrorlist=http://mirrorlist.centos.org/?release=7&arch=$basearch&repo=os&infra=$infra
baseurl=http://mirror.centos.org/centos/7/os/$basearch/
enabled=0
gpgcheck=0

#######################
## epel CentOS7 Repo ##
#######################
[ha-lizard-epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=0
gpgcheck=0

#########################
## elrepo CentOS7 Repo ##
#########################
### Name: ELRepo.org Community Enterprise Linux Repository for el7
### URL: http://elrepo.org/

[ha-lizard-elrepo]
name=ELRepo.org Community Enterprise Linux Repository - el7
baseurl=http://elrepo.org/linux/elrepo/el7/$basearch/
        http://mirrors.coreix.net/elrepo/elrepo/el7/$basearch/
        http://jur-linux.org/download/elrepo/elrepo/el7/$basearch/
        http://repos.lax-noc.com/elrepo/elrepo/el7/$basearch/
        http://mirror.ventraip.net.au/elrepo/elrepo/el7/$basearch/
mirrorlist=http://mirrors.elrepo.org/mirrors-elrepo.el7
enabled=0
gpgcheck=0
protect=0' > /etc/yum.repos.d/ha-lizard.repo

######################################
# Install DRBD
# Set chkconfig to off
######################################
echo "Installing DRBD packages"
yum install drbd84-utils --enablerepo=ha-lizard-elrepo -y >> $INSTALL_LOG
yum install drbd84-utils-sysvinit --enablerepo=ha-lizard-elrepo -y >> $INSTALL_LOG
systemctl disable drbd

####################################
# Install iSCSI Target Framework
# and make sure it does not start
# automatically
####################################
echo "Installing iSCSI Target Framework"
yum install scsi-target-utils --enablerepo=ha-lizard-epel,ha-lizard-base -y >> $INSTALL_LOG
systemctl disable tgtd

echo "Optionally install latest release of HA-Lizard and iSCSI-HA [y|n]?"
while :
do
	read INPUT
	if [ "$INPUT" = "n" ]
	then
		break
	elif [ "$INPUT" != "y" ]
	then
		continue
	else
		HALIZARD_MIRROR_URL='http://halizard.org/release'
		####################################
		# Install ha-lizard
		####################################
		HALIZARD_RELEASE=$(curl -4s $HALIZARD_MIRROR_URL/ha-lizard/current_rpm_release)
		if [ -n "$HALIZARD_RELEASE" ]
		then
			rpm -Uvh ${HALIZARD_MIRROR_URL}/ha-lizard/${HALIZARD_RELEASE}
		else
			echo "Error connecting to mirror"
		fi

		####################################
		# Install iscsi-ha
		####################################
		ISCSIHA_RELEASE=$(curl -4s $HALIZARD_MIRROR_URL/iscsi-ha/current_rpm_release)
		if [ -n "$ISCSIHA_RELEASE" ]
		then
			rpm -Uvh ${HALIZARD_MIRROR_URL}/iscsi-ha/${ISCSIHA_RELEASE}
		else
			echo "Error connecting to mirror"
		fi

		break
	fi
done

####################################
# Restore shared storage conf files
####################################
echo "Restore DRBD, LVM, IPTABLES, TGT configuration parameters? [y|n]?"
echo "(previous backup to pool DB is required)"
while :
do
	read INPUT
	if [ "$INPUT" = "n" ]
	then
		break
	elif [ "$INPUT" != "y" ]
	then
		continue
	else
		iscsi-cfg restore	
		break
	fi
done

##################################
# Reboot
##################################
echo "Reboot required. Reboot now? [y|n]"
read REBOOT
if [ "$REBOOT" = "y" ]
then
	echo "Rebooting in 10 seconds. <ctrl+c> to cancel"
	echo
	COUNT=10
	while [ $COUNT -gt 0 ]
	do
		echo -en "Reboot in [$COUNT] seconds    \r"
		sleep 1
		COUNT=$(( $COUNT - 1 ))
	done
	reboot
else
	exit 0
fi
