#!/bin/bash
#本脚本依赖ansible
Package_path_tmp=/tmp/rabbitmq       #包临时解压目录
Package_scp_tmp=/usr/local/packege   


Package_path=/usr/local/packege/rabbitmq       #包目录
Mq_script_file=${Package_path}/mq.sh      #本脚本绝对路径                    
Ip_Local=`ip addr|grep -w inet|grep -v "127.0.0.1"|awk -F '[ /]+' '{print $3}'`  #本机ip

Cluster_node=( 		
172.16.8.xx
172.16.8.xx
172.16.9.xx
)			#es集群节点

Cluset_host=(
NEW-RabbitMQ1
NEW-RabbitMQ2
NEW-RabbitMQ3
)

PassWord=xxxxx     #集群主机密码,需相同




Erlang_install()
#es 集群安装配置,需要在各节点执行该函数
{
#安装Erlang
Public_Key_file=${Package_path}/erlang_solutions.asc
Erlang_file=${Package_path}/esl-erlang_19.3-1~centos~6_amd64.rpm

[ -f $Public_Key_file ]||exit 1
[ -f $Erlang_file ]||exit 1

rpm --import $Public_Key_file >/dev/null 2>&1
if [ $? -eq 0 ];then
  yum install -y $Erlang_file >/dev/null 2>&1 &
#  [ $? -eq 0 ]||exit 1
  erl +V
  if [ $? -eq 0 ];then
    echo -e "\033[31m Erlang installed successfully!  \033[0m"
  else
    echo -e "\033[31m Erlang installed failed!  \033[0m"
#    exit 1
  fi
fi
}

mq_server_install()
#es 集群安装配置,需要在各节点执行该函数
{
#安装mq_server
Mq_server_file=${Package_path}/rabbitmq-server-3.6.6-1.el6.noarch.rpm

[ -f $Mq_server_file ]||exit 1

rpm -ivh --nodeps $Mq_server_file >/dev/null 2>&1 &
if [ $? -eq 0 ];then
    echo -e "\033[31m Mq_server installed successfully!  \033[0m"
else
    echo -e "\033[31m Mq_server installed failed!  \033[0m"
#    exit 1
fi
}




Packege_install()
#Packege下载函数;将要下载的包定义在Packeges数组;仅支持yum下载
{
Packeges=$1
for packege in ${Packeges[*]}
  do
    yum install -y $packege >/dev/null 2>&1
    if [ $? -eq 0 ];then
      echo -e "\033[31m {$packege} was successfully downloaded! \033[0m"
    else
      echo -e "\033[31m {$packege} download failed! \033[0m"
      exit 1
    fi
  done
}


Scp_tool()
#作者:fxl
#版本:V2019.08.01.02
#依赖:expect
#功能:该函数可免密往多个主机的同一目录同时传输单个或多个文件或目录。
#example:
#Scp_tool "172.16.17.14  172.16.17.15" "/tmp/555.txt /tmp/666.txt" "/mnt" "yunshitest"
{
#需要传输的主机ip
Transfer_ips=$1
#需要传输的文件,绝对路径 ,单个或者多个文件均可
Transfer_file=$2
#需要传输的目的主机目录
Transfer_path=$3
#远程主机密码
Transfer_pwd=$4
for node in ${Transfer_ips[*]}
  do
	expect -c "
			spawn scp -r ${Transfer_file[@]} root@${node}://$Transfer_path
			expect {
					\"Are you sure you want to continue connecting (yes/no)?\" {send \"yes\r\"; exp_continue}
					\"password\" {send \"${Transfer_pwd}\r\"; exp_continue}             
			}
	expect eof" >/dev/null 2>&1

  done
  
}

Remote_exec()
#依赖:expect
#功能:该函数可登录到多台主机上执行命令
#example:
#Remote_exec "172.16.17.14  172.16.17.15"  "touch /tmp/test0801_88.txt" "yunshitest"
{

#远程主机ip
Remote_ips=$1
#需执行的命令
Command_exec=$2
#远程主机密码
Password=$3
for node in ${Remote_ips[*]}
  do
        expect -c "
                        spawn ssh root@${node}
                        expect {
                                        \"yes/no\" {send \"yes\r\"; exp_continue}
                                        \"password:\" {send \"${Password}\r\"; exp_continue}             
                        }
						
						expect \"]*\"
						send \"$Command_exec\r\"
						expect \"]*\"
						send \"exit\r\"
		 
        " >/dev/null 2>&1
  done 
 
}


mq_cluster_install()
{
Packege_install expect 
Remote_exec "${Cluster_node[0]} ${Cluster_node[1]} ${Cluster_node[2]}"   "mkdir -p $Package_path" "$PassWord"
#将相关包复制到指定目录
Scp_tool "${Cluster_node[0]} ${Cluster_node[1]} ${Cluster_node[2]}" "$Package_path_tmp" "$Package_scp_tmp" "$PassWord"

#在各节点下载Erlang_install,mq_server_install
Remote_exec "${Cluster_node[0]} ${Cluster_node[1]} ${Cluster_node[2]}"  "sh $Mq_script_file Erlang_install" "$PassWord"
sleep 30
Remote_exec "${Cluster_node[0]} ${Cluster_node[1]} ${Cluster_node[2]}"  "sh $Mq_script_file mq_server_install" "$PassWord"
sleep 10
Remote_exec "${Cluster_node[0]} ${Cluster_node[1]} ${Cluster_node[2]}"  "echo '${Cluster_node[0]} ${Cluset_host[0]}' >>/etc/hosts;echo '${Cluster_node[1]} ${Cluset_host[1]}' >>/etc/hosts;echo '${Cluster_node[2]} ${Cluset_host[2]}' >>/etc/hosts" "$PassWord"

#2将节点1 erlang.cookie的值复制到其他两个节点中
Scp_tool "${Cluster_node[1]} ${Cluster_node[2]}" "/var/lib/rabbitmq/.erlang.cookie" "/var/lib/rabbitmq/" "$PassWord"
Remote_exec "${Cluster_node[0]} ${Cluster_node[1]} ${Cluster_node[2]}"  "chown rabbitmq.rabbitmq /var/lib/rabbitmq/.erlang.cookie" "$PassWord"


Remote_exec "${Cluster_node[0]} ${Cluster_node[1]} ${Cluster_node[2]}"  "/usr/lib/rabbitmq/bin/rabbitmq-plugins enable rabbitmq_management" "$PassWord"

Remote_exec "${Cluster_node[0]} ${Cluster_node[1]} ${Cluster_node[2]}"  "service rabbitmq-server start" "$PassWord"

Remote_exec "${Cluster_node[1]} ${Cluster_node[2]}"  "rabbitmqctl stop_app" "$PassWord"
Remote_exec "${Cluster_node[1]} ${Cluster_node[2]}"  "rabbitmqctl join_cluster --ram rabbit@${Cluset_host[0]}" "$PassWord"
Remote_exec "${Cluster_node[1]} ${Cluster_node[2]}"  "rabbitmqctl start_app" "$PassWord"

echo "ege: {rabbitmqctl cluster_status}"
}




case "$1" in

Erlang_install)
Erlang_install
result=$?
;;

mq_server_install)
mq_server_install
result=$?

;;

Packege_install)
Packege_install
result=$?

;;

Scp_tool)
Scp_tool
result=$?

;;

Remote_exec)
Remote_exec
result=$?

;;

mq_cluster_install)
mq_cluster_install
result=$?

;;

*)

echo "usage:$0 {Erlang_install|mq_server_install|Packege_install|mq_cluster_install}"
exit 1

esac