#!/bin/bash
# This script install Voltaire InfiniBand Stack and/or configure a cluster of hosts.
# It is assumed that all the hosts are accessible through the Ethernet and connected
# to the same IB fabric.
#
# Written by Nig Gal <nirg@voltaire.com>
#
BASE=$1
START=$2
END=$3
INSTALLFILEPATH=$4
usage="\
Usage: ibhost-cluster-setup Base-Name Start End [Installfile] "

if [[ -z $1 || -z $2 || -z $3 ]] ; then
	echo "${usage}"
	#exit 1
	START=1
	END=100
	NETWORK="192.168.60.0"
	IP="192.168.60.1"
	NM="255.255.255.0"
	BR="192.168.60.255"
	INSTALLFILEPATH=""
	CHECK=""
	clear
	echo "====== Voltaire InfiniBand Cluster Installation ======"
	echo
	echo
	echo "This utility help you to configure large IB clusters."
	echo "The utility is assuming all cluster computers are using the same name "
	echo "with different suffix for example server A is called camel1 server B is called camel2 etc'"
	echo
	echo "The automated configuration can work in two ways:"
	echo "1. Configure the IPoIB interface using IP given by the user (up to 254 nodes)."
	echo "2. Configure the IPoIB interface based on the eth0 IP address and the network given by the user"
	echo "3. Configure the IPoIB interface using DHCP. Requires DHCP server in IB fabric"
	echo	
	echo	
	echo "Before using this utility make sure:"
	echo "1. Host name resolving should work properly ( /etc/hosts , NIS , DNS )"
	echo "2. RSH should be enable on all machines."
	echo "3. Root access for RSH should be enable to all machines."
	echo "4. ipcalc is installed on this node."
	echo
	echo
	while [ -z "$CHECK" ] ; do
		echo -n "Enter the machine base name: "
		read ans ; if [ ! -z "$ans" ]; then BASE="$ans" ; fi
		echo -n "Start enumeration from: [${START}] "
		read ans ; if [ ! -z "$ans" ]; then START="$ans" ; fi
		echo -n "End enumeration at: [${END}] "
		read ans ; if [ ! -z "$ans" ]; then END="$ans" ; fi
		echo -n "Enter RPM file location [Press ENTER for configuration of existing installation]: "
		read ans 
			if [[ -f "$ans" ]] ; then 
				INSTALLFILEPATH="$ans" 
			elif [[ -z "$ans" ]] ; then 
				INSTALLFILEPATH="CONFIGURING ONLY" 
			else
				INSTALLFILEPATH=""
				echo "File not found" 
			fi
		if [[ -z "$BASE" || -z "$START" || -z "$END" || -z "$INSTALLFILEPATH" ]] ; then
			echo "Missing some parameters"
			CHECK=""
		else
			CHECK="ok"
		fi
	done
	CHECK=""
	echo -n "Would you like to configure the IPoIB interface based on the eth0 address? (yes / No): "
	read A 
	if [[ "$A" = y* ]]; then
		while [ -z "$CHECK" ] ; do
        		echo -n "Enter the network address: [${NETWORK}] "
			read ans ; if [ ! -z "$ans" ]; then NETWORK="$ans" ; fi
			NETWORK=`echo $NETWORK | perl -ne 'chomp;print "$1.$2.$3.0" if /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/&&$1<256&&$2<256&&$3<256&&$4<256;print "" '`
			CHECK=$NETWORK
			if [ -z "$CHECK" ] ; then echo "Incorrect network IP address , please type again" ; fi
		done
		# Mark that the installation is network based installation
		METHOD=1
	else
		CHECK=""
		echo -n "Would you like to configure the IPoIB using DHCP ? (yes / No): "
		read A 
		if [[ "$A" = y* ]]; then
			METHOD=2
		else
			while [ -z "$CHECK" ] ; do
				echo -n "Please enter IP address for ipoib-ud0 [${IP}]: "
				read ans
				if [ ! -z "$ans" ]; then IP="$ans" ; fi
				PARTIP=`echo $IP | perl -ne 'chomp;print "$1.$2.$3." if /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/&&$1<256&&$2<256&&$3<256&&$4<256;print "" '`
				CHECK=$PARTIP
				if [ -z "$CHECK" ] ; then echo "Incorrect IP address , please type again" ; fi
			done
			CHECK=""
			if [ -e /bin/ipcalc ]; then
			       NM=$(/bin/ipcalc -m $IP | awk -F= '/NETMASK/{print $2}')
			fi
			while [ -z "$CHECK" ] ; do
				echo -n "Please enter network mask for ipoib-ud0 [${NM}]: "
				read ans
				if [ ! -z "$ans" ]; then NM="$ans" ; fi
				NM=`echo $NM | perl -ne 'chomp;print "$1.$2.$3.$4" if /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/&&$1<256&&$2<256&&$3<256&&$4<256;print "" '`
        	                CHECK=$NM
                	        if [ -z "$CHECK" ] ; then echo "Incorrect netmask address , please type again" ; fi
			done
			CHECK=""
			if [ -e /bin/ipcalc ]; then
				BR=$(/bin/ipcalc -b $IP $NM | awk -F= '/BROADCAST/{print $2}')
			fi
			while [ -z "$CHECK" ] ; do
				echo -n "Please enter broadcast address for ipoib-ud0 [${BR}]: "
				read ans
				if [ ! -z "$ans" ]; then BR="$ans" ; fi
				BR=`echo $BR | perl -ne 'chomp;print "$1.$2.$3.$4" if /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/&&$1<256&&$2<256&&$3<256&&$4<256;print "" '`
                	        CHECK=$BR
                        	if [ -z "$CHECK" ] ; then echo "Incorrect broadcast address , please type again" ; fi
	                done

			if (( $END - $START > 254 )) ; then
				echo
				echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
				echo "Can't install more then 254 nodes"
				echo "Installing only 254 nodes"
				echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
				END="254"
			fi
			# Mark that the installation is based on user IP
        		METHOD=0
		fi
	fi
	echo
	echo
	echo "============== Summary =============="
	if [[ "XX${INSTALLFILEPATH}XX" = "XXCONFIGURING ONLYXX" ]] ; then
		echo "Configuring existing installation"
	else
		echo "Installing $INSTALLFILEPATH "
	fi
	echo "Nodes: ${BASE}${START} - ${BASE}${END} "
	
	if [[ "X$METHOD" = "X1" ]] ; then
		echo "Using eth0 IP address with network $NETWORK "
	elif [[ "X$METHOD" = "X2" ]] ; then
		echo "Using DHCP"
	else
		echo "Using IP address ${PARTIP}${START} - ${PARTIP}${END} netmask $NM broadcast $BR "
	fi

	echo -n "Is this configuration correct? (yes / No): "
	read A ; if [ -z "$A" ]; then A="No" ; fi
	if [[ "$A" != y* ]]; then
		exit 1
	fi
fi

# Check which package was installed - for removal
PACKAGE=$(rpm -qa | grep ibhost | perl -ne 'print "$1\n"if/ibhost-(...)-/i')

if [[ "XX${INSTALLFILEPATH}XX" != "XXCONFIGURING ONLYXX" ]] ; then
	# Check the install file name - remove path
	INSTALLFILE=$(ls $INSTALLFILEPATH | perl -ne 'print "$1\n"if/(ibhost-\S*)/i')
	# Copy install file to current directory
	rm -f $INSTALLFILE > /dev/null
	cp $INSTALLFILEPATH . > /dev/null
fi

# This part will install the file given (if given), and then will configure the node with the ip in the script
for (( i = $START ; i <= $END ; i = $i + 1 )) ; do
	if [[ ! -z $INSTALLFILE ]] ; then
		rcp $INSTALLFILE ${BASE}${i}:/tmp
		rsh ${BASE}${i} ". /etc/profile; /usr/voltaire/scripts/ibhost.init stop"
		rsh ${BASE}${i} ". /etc/profile; rpm -e ibhost-${PACKAGE}"
		rsh ${BASE}${i} ". /etc/profile; /bin/rm -rf /usr/mellanox /usr/voltaire /usr/mst"
		echo "Installing $INSTALLFILE on ${BASE}${i}"
		rsh ${BASE}${i} ". /etc/profile; rpm -ivh /tmp/${INSTALLFILE}"
	fi
	echo "Configuring $INSTALLFILE on ${BASE}${i}"
	if [[ "X$METHOD" = "X1" ]] ; then
		rsh ${BASE}${i} ". /etc/profile; /usr/voltaire/scripts/ib-setup --config --network $NETWORK"
	elif [[ "X$METHOD" = "X2" ]] ; then
		rsh ${BASE}${i} ". /etc/profile; /usr/voltaire/scripts/ib-setup --config --dhcp"
	else
		rsh ${BASE}${i} ". /etc/profile; /usr/voltaire/scripts/ib-setup --config --ip ${PARTIP}${i} --netmask ${NM} --broadcast ${BR}"
	fi
done

exit

