-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.sh
executable file
·79 lines (66 loc) · 1.77 KB
/
config.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
#!/bin/bash
echo "Waiting for mongo server to be available at 27017..."
while ! nc -z 127.0.0.1 27017; do sleep 0.5; done
echo "Mongo OK"
sleep 1
MAX_RETRIES=9999
if [ "$INIT_CONFIG_NODES" != "" ]; then
MAX_RETRIES=5
fi
echo "Verifying if this config node is already part of a config replicaset..."
C=0
while (( "$C" < "$MAX_RETRIES" )); do
mongo --eval "db.isMaster()" | grep $CONFIG_REPLICA_SET
if [ "$?" == "0" ]; then
mongo --eval "db.isMaster()"
echo ">>> THIS NODE IS PART OF A CONFIG REPLICASET"
exit 0
fi
echo "."
if [ "$MAX_RETRIES" != "9999" ]; then
C=($C+1)
fi
sleep 1
done
if [ "$INIT_CONFIG_NODES" == "" ]; then
echo ">>> THIS NODE IS NOT PART OF A CONFIG. ADD IT IN ORDER TO BE ACTIVE"
echo "Tip: On master node, execute rs.add( { host: \"[host]\", priority: 0, votes: 0 } )"
exit 0
fi
echo "Generating configsrv config"
echo ""
rm /init-configserver.js
cat <<EOT >> /init-configserver.js
rs.initiate(
{
EOT
echo "_id: \"$CONFIG_REPLICA_SET\"," >> /init-configserver.js
cat <<EOT >> /init-configserver.js
configsvr: true,
version: 1,
members: [
EOT
IFS=',' read -r -a NODES <<< "$INIT_CONFIG_NODES"
S=""
c=0
for N in "${NODES[@]}"; do
echo ">>> Node $N"
echo "${S}{ _id: $c, host : \"$N:27017\" }" >> /init-configserver.js
S=","
c=$((c+1))
echo " - Waiting for host $N to be available..."
until ping -c1 $N >/dev/null; do sleep 2; done
# echo " - Host available. Waiting port 27017..."
# while ! nc -z $N 27017; do sleep 1; done
echo " - Host $N OK"
done
cat <<EOT >> /init-configserver.js
]
}
)
EOT
echo "/init-configserver.js"
cat /init-configserver.js
echo "CONFIGURING CLUSTER CONFIG SERVER..."
mongo < /init-configserver.js
echo ">>> CONFIG SERVER INITIALIZED SUCCESSFULLY"