#!/bin/bash
#
#*****************************************************
#File Name    : install_rabbitmq.sh
#Author       : guokai
#Mail         : xxx@163.com
#Create Time  : 2022-04-21 13:57:52
#Description  : The test script
#*****************************************************
LOCAL_HOSTNAME=`hostname`
LOCAL_IP=`ifconfig eth0 | awk '/inet /{print $2}'`
NEIBU_URL=http://58.220.50.162:7777/download/
ERLANG_RPM=erlang-25.3.2.8-1.el9.x86_64.rpm
RABBITMQ_SERVER_RPM=rabbitmq-server-3.11.26-1.el8.noarch.rpm
CPUS=`lscpu | awk '/^CPU\(s\)/{print $2}'`

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
}


wget_rpm () {

	 wget ${NEIBU_URL}${ERLANG_RPM}
	 [ $? -ne 0 ] && { color 1 "erlang安装包下载异常" ;exit; }
	 wget ${NEIBU_URL}${RABBITMQ_SERVER_RPM}
	 [ $? -ne 0 ] && { color 1 "rabbitmq安装包下载异常" ;exit; }
	 wget http://58.220.50.162:7777/download/rabbitmq_delayed_message_exchange-3.11.1.ez
	 [ $? -ne 0 ] && { color 1 "延时消息插件下载异常" ;exit; }
	 color 0 "开始安装ansible"
	 dnf  install -y ansible-core sshpass


}

check_install_erlang () {
	if [ $(rpm -qa | grep -c erlang) -gt 1 ]; then
    	color 1 "erlang 已安装,请卸载后再安装"
		exit
	else
    	color 0 "开始安装Erlang 25.3.2.8"
		dnf install ${ERLANG_RPM} -y 
		[ $? -ne 0 ] && { color 1 "请mv走repo文件后重新执行" ;exit; }
	fi
}

check_install_rabbitmq () {
	if [ $(rpm -qa | grep -c rabbitmq) -gt 1 ]; then
    	color 1 "rabbitmq 已安装,请卸载后再安装"
		exit
	else
    	color 0 "开始安装RabbitMQ 3.11.26"
		dnf install ${RABBITMQ_SERVER_RPM} -y 
		[ $? -ne 0 ] && { color 1 "请mv走repo文件后重新执行" ;exit; }
	fi
}


install_rabbitmq () {
	color 0 "开始安装rabbitmq"
	echo "$LOCAL_IP $LOCAL_HOSTNAME" >> /etc/hosts
	mv ./rabbitmq_delayed_message_exchange-3.11.1.ez /usr/lib/rabbitmq/lib/rabbitmq_server-3.11.26/plugins/
	rabbitmq-plugins enable rabbitmq_management
	rabbitmq-server -detached
	MQ_PID=`ps -ef | grep "rabbit" | grep -v "grep" | wc -l`
	if [ ${MQ_PID} -eq 0 ]; then
		{ color 1 "rabbitmq 启动失败,请检查脚本!"; exit; }
	else
		color 0 "rabbitmq 启动成功,请继续配置集群! 提前切换用户 su - root "

	fi
}

wget_rpm
check_install_erlang
check_install_rabbitmq
install_rabbitmq



#! /bin/sh
#. $HOME/.bash_profile
#. /etc/profile
#. ~/.bash_profile
#
#APP_NAME=rabbitmq
#
#A=`ps -ef|grep $APP_NAME |grep -v sh |grep -v grep|grep -v gcc|grep -v '^vi'|grep -v '^tail'| wc -l`
#if [ $A == 0 ];then
#	sleep 1
#	echo "$APP_NAME not run!"
#	nohup /home/rabbitmq/sbin/rabbitmq-server --detached &
#	echo "$APP_NAME start ok"
#else
#	echo "$APP_NAME are running,do nothing!"
#fi
#
#
#* * * * * /home/shellJob/rabbitmq_check.sh
#* * * * * sleep 10; /home/shellJob/rabbitmq_check.sh
#* * * * * sleep 20; /home/shellJob/rabbitmq_check.sh
#* * * * * sleep 30; /home/shellJob/rabbitmq_check.sh
#* * * * * sleep 40; /home/shellJob/rabbitmq_check.sh
#* * * * * sleep 50; /home/shellJob/rabbitmq_check.sh