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

set +e
## ENV ##
source /etc/profile
set -e
pd=$PWD

os=${1}
test -z ${os} && echo "操作系统变量为空" && exit 1

rabbitmq_version="3.8.18"
if [[ ${os} == "ubuntu" ]]; then
  rabbitmq_version="3.8.18-1"
  # ubuntu 24.04
  #rabbitmq_version="3.12.1-1ubuntu1"
fi

echo "install rabbitmq version is $rabbitmq_version"
## install rabbitmq ##
if [[ ${os} == "ubuntu" ]]; then
  apt install -y erlang tcpd socat logrotate rabbitmq-server=$rabbitmq_version
  if [ $? -eq 1 ]; then
    exit 1
  fi
else
  # yum --disablerepo='*' --enablerepo=singularity-base-repo install -y openssl openssl-devel 冲突的话
  # 使用 --allowerasing 参数 尝试使用 --allowerasing 来替换有冲突的包：
  # yum --disablerepo='*' --enablerepo=singularity-base-repo install -y openssl openssl-devel --allowerasing
  yum --disablerepo='*' --enablerepo=singularity-base-repo install -y rabbitmq-server-$rabbitmq_version tcp_wrappers
  if [ $? -eq 1 ]; then
    exit 1
  fi
fi

SERVICE_FILE="/usr/lib/systemd/system/rabbitmq-server.service"
declare -A service_conf
service_conf["Restart"]="always"       # 指定服务在退出时总是重新启动。
service_conf["RestartSec"]="5"         # 如果服务启动失败，指定在尝试重新启动之前等待的时间间隔，这里是5秒。
service_conf["StartLimitInterval"]="0" # 指定尝试启动服务的最小时间间隔。在这里设置为0表示没有最小间隔，可以立即尝试重新启动服务。
service_conf["StartLimitBurst"]="5"    # 在StartLimitInterval指定的时间间隔内，允许启动服务的最大次数。这里设置为5次。
for key in "${!service_conf[@]}"; do
  value="${service_conf[${key}]}"
  if [ $(grep "^${key}=" ${SERVICE_FILE} | wc -l) != 0 ]; then
    # 能匹配到key但是匹配不到value 注释掉 加新的参数
    if [ $(grep "^${key}=${value}" ${SERVICE_FILE} | wc -l) == 0 ]; then
      sed -i "/^${key}=/s/^/#/" ${SERVICE_FILE}
      sed -i "$(sed -n '/\[Install\]/=' ${SERVICE_FILE} | awk '{print $1-1}')i ${key}=${value}" ${SERVICE_FILE}
    fi
  else
    sed -i "$(sed -n '/\[Install\]/=' ${SERVICE_FILE} | awk '{print $1-1}')i ${key}=${value}" ${SERVICE_FILE}
  fi
done

if [ ! -d /etc/rabbitmq ]; then mkdir /etc/rabbitmq; fi
## parameter of Prevent fissure ##
rm -rf /etc/rabbitmq/rabbitmq.conf
cat > /etc/rabbitmq/rabbitmq.conf << EOF
cluster_partition_handling = autoheal
net_ticktime = 120
collect_statistics_interval = 10000
EOF

mkdir -p /etc/systemd/system/rabbitmq-server.service.d/
cat > /etc/systemd/system/rabbitmq-server.service.d/limits.conf << EOF
[Service]
LimitNOFILE=65536
EOF

# add group and user if not exist
groupadd -f rabbitmq
exist=$(grep "^rabbitmq:" /etc/passwd | wc -l)
if [[ ${exist} -eq 0 ]]; then
  useradd -g rabbitmq -d /var/lib/rabbitmq -s /sbin/nologin rabbitmq
fi

# start rabbitmq ##
chown -R rabbitmq:rabbitmq /var/lib/rabbitmq
chown -R rabbitmq:rabbitmq /var/log/rabbitmq
chown -R rabbitmq:rabbitmq /etc/rabbitmq
systemctl restart rabbitmq-server
su -s /bin/bash rabbitmq -c '/usr/lib/rabbitmq/bin/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
