forked from lovelysystems/rabbitmq-swarm-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.sh
executable file
·111 lines (102 loc) · 2.98 KB
/
join.sh
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
#
# Script to join rabbitmq cluster.
#
# This script is call in background once the mangement ui of this node is
# running (see Dockerfile).
#
#
# Wait a random amount of seconds (between 1 and 10 seconds) to get in
# parallel started instances a littel bit out of sync.
echo "Wait random duration..."
sleep $[ ( $RANDOM % 10 ) + 10 ]s
echo "Try to join rabbitmq cluster..."
# busybox 'nslookup' required
# Try to determine all hosts of this service (given by env var `SERVICE_NAME`).
# Sometimes wrong hostnames are returned, hence the retry functionality.
for i in `seq 5`
do
if [[ "$i" > "5" ]]
then
echo "Retry count exceeded"
exit 1
fi
retry=false
nodes=`nslookup tasks.$SERVICE_NAME 2>/dev/null | grep -v $(hostname) | grep Address | awk '{print $4}' | cut -d. -f1-3`
for node in $nodes
do
if [[ "$node" != $SERVICE_NAME* ]]
then
retry=true
break
fi
done
if ! $retry; then break; fi
done
# If the service is configured with just one replica this rabbitmq instance is
# running in standalone mode and no further cluster joining arithmetic is
# required. If there are multiple nodes configured stop the app to start
# setting up a cluster.
echo
if [[ ${#nodes} > 0 ]]
then
echo "Found nodes of cluster:"
echo $nodes
rabbitmqctl stop_app
rabbitmqctl reset
else
echo "Found standalone setup."
exit 0
fi
echo
# Join cluster by trying one node after each other. If successfully joined, start the rabbitmq
# app again
while true
do
for node in $nodes
do
# manually force a start by setting the env variable FORCE_START
if [[ -f /tmp/FORCE_START ]]
then
echo "Startup forced manually."
echo
rabbitmqctl start_app
exit 0
fi
# of peer is reachable try to join the cluster of that host
echo "Try to reach $node"
if nc -z "$node" 15672
then
rabbitmqctl join_cluster rabbit@$node
if [[ $? == "0" ]]
then
echo
echo "Start app after joining cluster"
rabbitmqctl start_app
echo
echo "Try to cleanup old nodes of same slot..."
for n in `rabbitmqctl cluster_status | awk '/disc/,/]}]}/' | grep -o "$SERVICE_NAME[^']*"`
do
if [[ $n == "$SERVICE_NAME.$SLOT."* && $n != $HOSTNAME ]]
then
echo
echo "removing node $n from cluster"
rabbitmqctl forget_cluster_node rabbit@$n
fi
done
echo
echo "Successfully joined cluster"
exit 0
fi
elif [[ "$SLOT" == "$MASTER_SLOT" ]]
then
echo "Startup due to claimed master role on slot $MASTER_SLOT."
echo
rabbitmqctl start_app
exit 0
fi
done
sleep 10
done
echo "Failed to join cluster."
exit 1