-
Notifications
You must be signed in to change notification settings - Fork 35
/
wait-db.sh
executable file
·50 lines (41 loc) · 1.62 KB
/
wait-db.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
#!/bin/bash
# This script waits for MongoDB to be available before running the API service
# Connection check is done using the MONGODB_URL env variable provided
# Standard mongo URL: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
# Make sure MONGODB_URL is provided
if [[ -z "$MONGODB_URL" ]];then
echo "MONGODB_URL environment variable needs to be setup"
exit 1
fi
# Variables
LOGIN=
PASSWORD=
DATABASE=
HOSTS=
# Get credentials
function parseURI {
# Check presence of @ char
if [[ -n "$(echo $1 | grep "@")" ]];then
echo "credential present"
LOGIN=$(echo $1 | cut -d'/' -f3 | cut -d'@' -f1 | cut -d':' -f1)
PASSWORD=$(echo $1 | cut -d'/' -f3 | cut -d'@' -f1 | cut -d':' -f2)
HOSTS=$(echo $1 | cut -d'/' -f3 | cut -d'@' -f2)
else
HOSTS=$(echo $1 | cut -d'/' -f3)
fi
DATABASE=$(echo $1 | cut -d'/' -f4 | cut -d'?' -f1)
}
# Extract elements from MONGODB_URL environment variable
parseURI $MONGODB_URL
# Check connection
while true; do
if [[ -n "$LOGIN" ]];then
echo mongo -u $LOGIN -p $PASSWORD --host $HOSTS $DATABASE --eval 'db.serverStatus()'
mongo -u $LOGIN -p $PASSWORD --host $HOSTS $DATABASE --eval 'db.serverStatus()' 1>/dev/null 2>/dev/null
else
echo mongo --host $HOSTS $DATABASE --eval 'db.serverStatus()'
mongo --host $HOSTS $DATABASE --eval 'db.serverStatus()' 1>/dev/null 2>/dev/null
fi
if [ $? -eq 0 ]; then echo "DB available [$MONGODB_URL] => running API service"; exit 0; fi
echo "Seems the database is not up yet, will retry to connect in 2 seconds" && sleep 2
done