-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbitmq-cluster
executable file
·92 lines (73 loc) · 2.85 KB
/
rabbitmq-cluster
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
set -u
set -e
CLUSTER=${CLUSTER:-''}
MESSAGE_TTL=${MESSAGE_TTL:-''} # milliseconds
FQDN=$(hostname -s).wot.io
ENV_PRODPASSWORD=${PRODPASSWORD:-''}
ENV_WOTPASSWORD=${WOTPASSWORD:-''}
ENV_ADMINPASSWORD=${ADMINPASSWORD:-''}
export RABBITMQNODE_NAME=rabbit@$(hostname -s)
set_message_policy() {
local vhost=${1:?'Usage: set_message_policy <vhost>'}
# Set a message TTL on queues and exchanges if requested.
if [ "${MESSAGE_TTL}" != "" ]; then
rabbitmqctl set_policy -p ${vhost} --apply-to all msg-ttl-${MESSAGE_TTL} '.*' '{"message-ttl":'''${MESSAGE_TTL}'''}'
fi
}
echo "Starting RabbitMQ on ${FQDN}"
echo "Configuring RabbitMQ for LDAP auth"
sed -i 's/%LDAP_SERVER%/'${LDAP_SERVER:-"ldap.wot.io"}'/;
s/%RABBITMQ_PASSWORD%/'${RABBITMQ_PASSWORD}'/' /etc/rabbitmq/rabbitmq.config
echo "Setting RabbitMQ Erlang cookie"
install -o rabbitmq -g rabbitmq -m 0400 /.erlang.cookie /var/lib/rabbitmq/.erlang.cookie
echo "Starting RabbitMQ"
runuser -l rabbitmq '/usr/lib/rabbitmq/bin/rabbitmq-server' &
sleep 10
CONFIGURED_VHOSTS="$(rabbitmqctl list_vhosts | wc -l)"
if [ "$CONFIGURED_VHOSTS" -lt "5" ]; then # 3 actual vhosts (/, wot, management) plus two lines from rabbitmqctl output
if [[ -z "$CLUSTER" ]]; then
echo "Loading passwords"
[ -f /.passwords ] && source /.passwords
# Passwords from the environment override the embedded passwords.
PRODPASSWORD=${ENV_PRODPASSWORD:-${PRODPASSWORD}}
WOTPASSWORD=${ENV_WOTPASSWORD:-${WOTPASSWORD}}
ADMINPASSWORD=${ENV_ADMINPASSWORD:-${ADMINPASSWORD}}
if [ -z "${PRODPASSWORD}" ] || [ -z "${WOTPASSWORD}" ] || [ -z "${ADMINPASSWORD}" ]; then
echo "Local RabbitMQ passwords cannot be set."
exit 1
fi
echo "Running as master"
sleep 10
echo "Configuring master"
rabbitmqctl add_vhost management
rabbitmqctl add_user prod $PRODPASSWORD
rabbitmqctl add_user wot $WOTPASSWORD
rabbitmqctl add_user admin $ADMINPASSWORD
rabbitmqctl set_user_tags prod wot-observer
rabbitmqctl set_user_tags wot management WOT wot-observer
rabbitmqctl set_user_tags admin administrator WOT wot-administrator
rabbitmqctl set_permissions admin '.*' '.*' '.*'
rabbitmqctl set_permissions -p management admin '.*' '.*' '.*'
rabbitmqctl set_permissions -p management wot '.*' '.*' '.*'
set_message_policy /
set_message_policy management
env | grep "^VHOSTS.*" | awk -F "=" '{print $2}' | while read -r line ; do
rabbitmqctl add_vhost $line
rabbitmqctl set_permissions -p $line admin '.*' '.*' '.*'
rabbitmqctl set_permissions -p $line prod '.*' '.*' '.*'
rabbitmqctl set_permissions -p $line wot '.*' '.*' '.*'
set_message_policy $line
done
echo "RabbitMQ master configured... running..."
else
echo "Joining cluster at $CLUSTER"
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster $CLUSTER
rabbitmqctl start_app
fi
else
echo "RabbitMQ already configured. Running."
fi
/bin/bash -l