#!/bin/bash
######################################################################
####Time:2019/11/19
####Author:baoziqiang
####
####
######################################################################
pd=$PWD/keepalived
role=$1
username=$2
password="`echo ${3}|sed -e 's/&/\\\\\\&/g' -e 's/\//\\\\\\//g'`"
DBport=$4
masterIP=$5
slaveIP=$6
VIP=$7
ROUTE_ID=${VIP##*.}
########################################################################################
#Unzip file
########################################################################################
tar -xvf keepalived.tar.gz
cd $pd
if [ $role == 'master' ];then
	IP=$masterIP
else
	IP=$slaveIP
fi
line=$(expr $(ifconfig|grep "$IP" -n|awk -F: '{print $1}') - 1 )
ENS=`ifconfig|sed -n "$line p"|awk -F: '{print $1}'`
HOSTNAME=`hostname`
tar -zxvf keepalived*.tar.gz
cd $pd/keepalived*
./configure --prefix=/usr/local/keepalived
make && make install

if [ ! -d "/etc/keepalived/" ];then
	mkdir /etc/keepalived/
fi
cd $pd
########################################################################################
#Create start/stop shell
########################################################################################
rm -f $pd/readonly_off.sh
/bin/cp $pd/readonly_off.sh-bak $pd/readonly_off.sh

echo "">$pd/readonly_on.sh
echo '/bin/mysql -uusername -p'password' -hIP --port=DBport -e "set global read_only=1;set global super_read_only=1;" > /dev/null 2>&1'>>$pd/readonly_on.sh
if [ $role == 'master' ];then
   /bin/cp -rf keepalived_master.conf /etc/keepalived/keepalived.conf
   sed -i "s/username/$username/g" readonly_off.sh readonly_on.sh
   sed -i "s/password/$password/g" readonly_off.sh readonly_on.sh
   sed -i "s/IP/$masterIP/g" readonly_off.sh readonly_on.sh
   sed -i "s/VIP/$VIP/g"     readonly_off.sh readonly_on.sh 
   sed -i "s/DBport/$DBport/g" readonly_off.sh readonly_on.sh
   /bin/cp -rf readonly_off.sh readonly_on.sh /etc/keepalived/
else
   /bin/cp -rf keepalived_backup.conf /etc/keepalived/keepalived.conf
   sed -i "s/username/$username/g" readonly_off.sh readonly_on.sh
   sed -i "s/password/$password/g" readonly_off.sh readonly_on.sh
   sed -i "s/IP/$slaveIP/g" readonly_off.sh readonly_on.sh
   sed -i "s/VIP/$VIP/g"     readonly_off.sh readonly_on.sh
   sed -i "s/DBport/$DBport/g" readonly_off.sh readonly_on.sh
   /bin/cp -rf readonly_off.sh readonly_on.sh /etc/keepalived/
fi
/bin/cp -rf mysql_check.sh /etc/keepalived/mysql_check.sh
sed -i "s/DBport/$DBport/g" /etc/keepalived/mysql_check.sh
chmod +x /etc/keepalived/*

sed -i s/eth0/$ENS/g /etc/keepalived/keepalived.conf
sed -i s/hostname/$HOSTNAME/g /etc/keepalived/keepalived.conf
sed -i s/host_ip/$IP/g /etc/keepalived/keepalived.conf
sed -i s/mysql_vip/$VIP/g /etc/keepalived/keepalived.conf
sed -i s/route_id_33/$ROUTE_ID/g /etc/keepalived/keepalived.conf
chmod 644 /etc/keepalived/keepalived.conf

## server options ##
systemctl stop keepalived
if [ $VIP != 'mysql_vip' ];then
	systemctl daemon-reload
	systemctl start keepalived
	systemctl disable keepalived
fi

if [ $role = 'slave' ];then
	systemctl disable keepalived
	systemctl start keepalived
fi
echo "Keepalived install success "
