#!/usr/bin/env bash

#Used after configure-host.sh to join the 2 nodes together
#Will sync ssh keys, join DRBD, enable pacemaker...
#mReschke 2012-04-09

this_script_dir="$( cd "$( dirname "$0" )" && pwd )"
source $this_script_dir/functions.sh
source /etc/nwq/cluster/cluster.conf

# Determine which node this is being run from
if [ "$HOSTNAME" == "$host1" ]; then
	is_node1=true
	host=$host1
	ip=$ip1
	port=$port1
elif [ "$HOSTNAME" == "$host2" ]; then
	is_node1=false
	host=$host2
	ip=$ip2
	port=$port2
else
	error "Hostname problems, check /etc/nwq/cluster/cluster.conf"
	exit 1
fi

function main() {
	# Display warnings and introduction text
	intro_text
	clearscreen

    header "Configuring the System"
        line "Syncing SSH Keys"
            configure_ssh
		if $is_node1; then
			line "Sync-Config"
			#sync-config
		fi
    
    echo
	echo "All Done!"
	echo "At this point the nodes should be joined, drbd should be syncing..."
    echo "I did NOT setup pacemaker, see /etc/nwq/cluster/examples for CRM config"
    echo "examples.  Remember to enable corosync on boot from the /etc/default/corosync"
    echo "file.  Have Fun!"
    echo
    echo "Bye!"
   
}

function configure_ssh() {
	clearscreen
	notice "I am copying these ssh keys to the other nodes, just keep entering the root and toor passwords for me"
	if $is_node1; then
		ssh-copy-id toor@$host2
		ssh-copy-id root@$host2
		su -l toor -c "ssh-copy-id toor@$host2"
		su -l toor -c "ssh-copy-id toor@$host2"
	else
		ssh-copy-id toor@$host1
		ssh-copy-id root@$host1
		su -l toor -c "ssh-copy-id toor@$host1"
		su -l toor -c "ssh-copy-id toor@$host1"
	fi
	notice "Done - SSH keys created and exchanged"
	sleep 2
	clearscreen
}


function configure_drbd() {
	clearscreen

	echo "I am about to create DRBD metadata and start syncing"
	echo "You should wait until both nodes are at this point, then continue"
	echo 
	echo -n "Press enter to continue"; read tmp
	
	drbdadm create-md all

	echo "DRBD metadata has been created on this node, make sure its has been created"
	echo "on the other node too, both nodes shold be at this prompt!"
	echo
	echo -n "Press enter to continue"; read tmp

	service drbd start

	if $is_node1; then
		drbdadm -- --overwrite-data-of-peer primary all
		drbdadm primary all
	fi

	echo "At this point DRBD should be syncing all 3 devices"
	echo "You can run: watch cat /proc/drbd"
	echo "On each node to watch the progress.  You do NOT have to wait"
	echo "for the initial sync to finish before you can continue, you may continue"
	echo "at any time"
	echo
	echo "Press enter to continue formatting these devices"; read tmp

	if $is_node1; then
		mkfs.ext3 -m0 /dev/drbd1
		mkfs.ext3 -m0 /dev/drbd2
		mkfs.ext3 -m0 /dev/drbd0
		drbdadm secondary all
		echo
		echo "Ok formatting done, node2 should be waiting for you"
		echo "Go ahead and continue on each node"
		echo
		echo "Press enter to continue"; read tmp
	else
		drbdadm secondary all
		echo "At this piont the primary node should be formatting the drives"
		echo "You should wait here (on this node2) until the formats on node1 are complete"
		echo
		echo "Press enter to continue (once formatting is done on node1)"; read tmp
	fi

	mkdir /mnt/{data,xennode1,xennode2}

	if $is_node1; then
		drbdadm primary xennode1
	else
		drbdadm primary data
		drbdadm primary xennode2
	fi

}



function configure_pacemaker() {
    sed -i 's/no/yes/g' /etc/default/corosync
    
	#service corosync start
	#sleep 5
	#crm configure show

	#echo
	#notice "Pacemaker is ready for you to setup, I will NOT configure it here"
	#echo
	#sleep 3
}



function intro_text() {
	clearscreen
	echo -e "${RED}"
	echo "READ CAREFULLY BEFORE YOU CONTINUE:"
	echo -e "${LIGHTRED}"
	echo "You MUST verify that /etc/nwq/cluster/cluster.conf is setup properly for these nodes!"
	echo -e "${DEFAULT}"

    echo "This will join the two nodes defined in cluster.conf together.  You should run"
    echo "this script on BOTH NODES at the same time but only after they are 100% configured"
    echo "and ready to go (usually from the configure-host.sh script)."
    echo "Make sure all network cables are connected, both the LAN and dedicated DRBD line."
    echo "Make sure you can ping all the IP's on each node."
	echo
	echo "To continue installation please type 'begin'"
	echo
	echo -n "<>>(#)> "; read begin

	if [ "$begin" != "begin" ]; then
		echo
		echo "Fine, bye then :("
		echo
		exit 0
	fi
}


main