#!/bin/bash
#
#*****************************************************
#File Name    : create_mqcluster.sh
#Author       : guokai
#Mail         : xxx@163.com
#Create Time  : 2022-05-08 22:40:02
#Description  : The test script
#*****************************************************
PROMPT="================================
     *** 注意事项 ***
   本脚本只需在主节点执行！
1,主节点切换到root用户，并且先打开从节点root用户ssh权限
# sed  -i '/^PermitRootLogin/c PermitRootLogin yes' /etc/ssh/sshd_config #
# systemctl restart sshd #
2,请检查脚本中的密码和ssh端口
3,请确认服务的安装路径
4,如果输入错误，请退出后重新执行
================================
开始创建集群，请输入从节点的ip："
read -p "$PROMPT" SLAVE_IP
read -p "请选择是否启动延时消息插件：y/n ?" PLUGINS 
MASTER_IP=`ifconfig eth0 | awk '/inet /{print $2}'`
ROOT_PASSWORD=cqt@2020
SSH_PORT=56022
#PASSWORD是admin用户的密码
PASSWORD=cqt!010@Rabbitmq

color () {
	RES_COL=60
	MOVE_TO_COL="echo -en \\033[${RES_COL}G"
	SETCOLOR_SUCCESS="echo -en \\033[1;32m"
	SETCOLOR_FAILURE="echo -en \\033[1;31m"
	SETCOLOR_WARNING="echo -en \\033[1;33m"
	SETCOLOR_NORMAL="echo -en \E[0m"
	echo -n "$2" && $MOVE_TO_COL
	echo -n "["
	if [ $1 = "success" -o $1 = "0" ] ;then ${SETCOLOR_SUCCESS}
		echo -n $"  OK  "
	elif [ $1 = "failure" -o $1 = "1" ] ;then ${SETCOLOR_FAILURE}
		echo -n $"FAILED"
	else 
		${SETCOLOR_WARNING}
		echo -n $"WARNING"
	fi
	${SETCOLOR_NORMAL}
	echo -n "]"
	echo
}

cluster () {

	color 0 "ansible，并配置集群"
	#yum install -y epel-release && yum install -y ansible sshpass && rpm -ql ansible | grep '/usr/share/ansible' || { color 1 "安装失败，检查yum源" ; exit ; }

cat >> /etc/ansible/hosts <<EOF
[mq]
$MASTER_IP ansible_ssh_port=$SSH_PORT
$SLAVE_IP ansible_ssh_port=$SSH_PORT
EOF
	ssh-keygen -f /root/.ssh/id_rsa -N ""
	sshpass -p $ROOT_PASSWORD ssh-copy-id -p $SSH_PORT  -o StrictHostKeyChecking=no root@$SLAVE_IP
	MASTER_HOSTNAME=`hostname` 
	SLAVE_HOSTNAME=`ansible $SLAVE_IP -m shell -a "hostname" | tail -1`
	
	if [ $PLUGINS == y ];then 
		CLUSTER_TYPE=' '
		DELAYED='enable'
		else
		CLUSTER_TYPE='--ram'
		DELAYED='disable'
	fi
cat > /root/mq_cluster.sh <<EOF
    rabbitmq-plugins enable rabbitmq_management
	rabbitmq-plugins $DELAYED rabbitmq_delayed_message_exchange
    rabbitmqctl add_user admin $PASSWORD
    rabbitmqctl set_user_tags admin administrator
	rabbitmqctl delete_user guest
	echo "$MASTER_IP $MASTER_HOSTNAME" >> /etc/hosts
	echo "$SLAVE_IP $SLAVE_HOSTNAME" >> /etc/hosts   
EOF
	chmod 755 /root/mq_cluster.sh
	/root/mq_cluster.sh
	ansible $SLAVE_IP -m script -a "/root/mq_cluster.sh"
	ansible $SLAVE_IP -m shell -a "rabbitmqctl stop"
	ansible $SLAVE_IP -m copy -a "src=/var/lib/rabbitmq/.erlang.cookie backup=yes dest=/var/lib/rabbitmq/"
	ansible $SLAVE_IP -m shell -a "rabbitmq-server -detached"
	ansible $SLAVE_IP -m shell -a "rabbitmqctl stop_app"
	ansible $SLAVE_IP -m shell -a "rabbitmqctl reset"
	ansible $SLAVE_IP -m shell -a "rabbitmqctl join_cluster rabbit@$MASTER_HOSTNAME $CLUSTER_TYPE"
	ansible $SLAVE_IP -m shell -a "rabbitmqctl start_app"
	ansible $SLAVE_IP -m shell -a "rabbitmqctl cluster_status" && color 0 "mq 集群搭建成功"
	ansible $SLAVE_IP -m shell -a "rabbitmqctl list_users"
	rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
	rabbitmqctl list_policies && color 0 "mq 管理用户创建成功，登录地址为http://$MASTER_IP:15672"
	
}

cluster
