-
Notifications
You must be signed in to change notification settings - Fork 3
/
cluster.sh
55 lines (48 loc) · 1.73 KB
/
cluster.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
#!/usr/bin/env sh
set -m
EJABBERDCTL=$HOME/bin/ejabberdctl
HOSTNAME_S=$(hostname -s) # ejabberd-0
HOSTNAME_F=$(hostname -f) # ejabberd-0.ejabberd.default.svc.cluster.local
HEADLESS_SERVICE="${HOSTNAME_F/$HOSTNAME_S./}" # ejabberd.default.svc.cluster.local
ERLANG_NODE_ARG="ejabberd@$HOSTNAME_F" # [email protected]
ERLANG_NODE=$ERLANG_NODE_ARG
echo $HOME
echo $EJABBERDCTL
echo $HEADLESS_SERVICE
echo $ERLANG_NODE_ARG
IPS=$(nslookup $HEADLESS_SERVICE | tail -n +3 | grep "Address:" | sed -E 's/^Address: (.*)$/\1/')
for IP in ${IPS}
do
echo "looking up hostname for: $IP"
HOSTNAME=$(nslookup $IP | tail -n +3 | grep -E '[^=]*= (.*).'"$HEADLESS_SERVICE"'$' | sed -E 's/[^=]*= (.*).'"$HEADLESS_SERVICE"'$/\1/')
if [[ "$HOSTNAME_S" == "$HOSTNAME" ]] ; then
echo "found own hostname, skipping"
continue
fi
$EJABBERDCTL -n $ERLANG_NODE_ARG start
$EJABBERDCTL -n $ERLANG_NODE_ARG started
echo "trying to connect to node with hostname $HOSTNAME.$HEADLESS_SERVICE"
$EJABBERDCTL -n $ERLANG_NODE_ARG join_cluster "ejabberd@$HOSTNAME.$HEADLESS_SERVICE"
CLUSTERING_RESULT=$?
$EJABBERDCTL -n $ERLANG_NODE_ARG list_cluster
$EJABBERDCTL -n $ERLANG_NODE_ARG stop
$EJABBERDCTL -n $ERLANG_NODE_ARG stopped
if [[ $? -eq 0 ]] ; then
echo "successfully joined";
break
else
echo "failed to join, trying next";
fi
done
#Define cleanup procedure
cleanup() {
echo "Container stopped, performing cleanup..."
$EJABBERDCTL -n $ERLANG_NODE_ARG leave_cluster "$ERLANG_NODE_ARG"
$EJABBERDCTL -n $ERLANG_NODE_ARG stop
}
#Trap SIGTERM
trap 'cleanup' SIGTERM
echo "launching in foreground"
$EJABBERDCTL -n $ERLANG_NODE_ARG foreground &
#Wait
wait $!