######################################################################
#### function: init rabbitmq
####author:danial_chen
####create date:2019-11-07
####modify date:2019-11-07
######################################################################
#/bin/bash
#useage: sh initmq.sh mq_user mq_password ierp_vhost mc_vhost
#eg. sh initmq.sh admin kingdee2019 next mc

## ENV ##
mq_user=$1
mq_password=$2
ierp_vhost=$3
mc_vhost=$4
export mq_user=${mq_user:-admin}
export mq_password=${mq_password:-Cosmic@2020}
export ierp_vhost=${ierp_vhost:-ierp}
export mc_vhost=${mc_vhost:-mc}

## add rabbitmq_user ##
rabbitmqctl add_user $mq_user $mq_password
rabbitmqctl set_user_tags $mq_user administrator

## add rabbitmq_vhost ##
rabbitmqctl add_vhost $ierp_vhost
rabbitmqctl  set_permissions -p $ierp_vhost $mq_user '.*'  '.*'  '.*'
rabbitmqctl set_policy -p  $ierp_vhost  ha-allqueue "^"  '{"ha-mode":"all"}'
rabbitmqctl add_vhost $mc_vhost
rabbitmqctl  set_permissions -p $mc_vhost $mq_user '.*'  '.*'  '.*'
rabbitmqctl set_policy -p  $mc_vhost  ha-allqueue "^"  '{"ha-mode":"all"}'
