######################################################################
#### function: install rabbitmq_master
####author:danial_chen
####create date:2019-11-07
####modify date:2019-11-07
######################################################################
#/bin/bash
#useage: sh installmq_master.sh

## ENV ##
source /etc/profile
pd=$PWD
rabbitmq_version="rabbitmq-3.8.18"

## install rabbitmq ##
tar -zxf $pd/$rabbitmq_version.tar.gz
chmod -R 755 $pd/$rabbitmq_version
cd $pd/$rabbitmq_version
yum localinstall erlang-23.3.1-1.el7.x86_64.rpm  rabbitmq-server-3.8.18-1.el7.noarch.rpm  tcp_wrappers-7.6-77.el7.x86_64.rpm  socat-1.7.3.2-1.1.el7.x86_64.rpm -y

## parameter of Prevent fissure ##
if [ -s /etc/rabbitmq/rabbitmq.conf ];then
    grep cluster_partition_handling /etc/rabbitmq/rabbitmq.conf >&/dev/null
    if [ $? -eq 0 ];then
        sed -i "/cluster_partition_handling/c cluster_partition_handling = pause_minority" /etc/rabbitmq/rabbitmq.conf
    else
        echo "cluster_partition_handling = pause_minority" >> /etc/rabbitmq/rabbitmq.conf
        fi
else
    echo "cluster_partition_handling = pause_minority" > /etc/rabbitmq/rabbitmq.conf
fi


## start rabbitmq ##
systemctl restart rabbitmq-server
rabbitmq-plugins enable rabbitmq_management

#copy .erlang.cookie to /tmp/
cp -rp /var/lib/rabbitmq/.erlang.cookie /tmp/
chmod -R 755 /tmp/.erlang.cookie
chown rabbitmq. /etc/rabbitmq/rabbitmq.conf

## register systemd ##
systemctl daemon-reload
systemctl enable rabbitmq-server
