forked from softfire-eu/public-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-single-mongo.sh
89 lines (70 loc) · 1.89 KB
/
start-single-mongo.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
#!/bin/bash
if [ -z ${PORT+x} ]; then
echo "PORT is unset";
PORT=27018
else
echo "PORT is set to '$PORT'";
fi
if [ -z ${SECURITY+x} ]; then
echo "SECURITY is unset";
SECURITY="false"
else
echo "SECURITY is set to '$SECURITY'";
fi
if [ -z ${SMALLFILES+x} ]; then
echo "SMALLFILES is unset";
SMALLFILES="false"
else
echo "SMALLFILES is set to '$SMALLFILES'";
fi
function kill {
if screen -list | grep "mongodb"; then
screen -ls | grep mongodb | cut -d. -f1 | awk '{print $1}' | xargs kill
fi
}
mkdir /home/ubuntu/data
export LC_ALL=C
if [ $SMALLFILES = 'true' ]
then
screen -d -m -S mongodb mongod --smallfiles --port $PORT --dbpath /home/ubuntu/data
else
screen -d -m -S mongodb mongod --port $PORT --dbpath /home/ubuntu/data
fi
echo "wainting for mongodb to start"
while ! nc -z localhost $PORT; do
sleep 0.5 # wait for 1/10 of the second before check again
done
if [ $SECURITY = 'true' ]
then
echo "enabling security"
if [ -z ${USERNAME_MD+x} ]; then
echo "USERNAME_MD needs to be set if SECURITY is set to true"
exit 99
fi
if [ -z ${PASSWORD+x} ]; then
echo "PASSWORD needs to be set if SECURITY is set to true"
exit 98
fi
echo "use admin" >> addUser.js
echo "db.createUser( { user: '`echo $USERNAME_MD`', pwd: '`echo $PASSWORD`', roles: [ 'userAdminAnyDatabase' ] } )" >> addUser.js
cat addUser.js
mongo --port $PORT < addUser.js
kill
sleep 3
if [ $SMALLFILES = 'true' ]
then
echo "executing screen"
screen -d -m -S mongodb mongod --smallfiles --port $PORT --dbpath /home/ubuntu/data --auth
else
screen -d -m -S mongodb mongod --port $PORT --dbpath /home/ubuntu/data --auth
fi
fi
COUNTER=0
while ! nc -z localhost $PORT; do
echo "it's been $COUNTER seconds and it is not started yet..."
let COUNTER=COUNTER+1
sleep 1 # wait for 1 second before check again
if [ $COUNTER -gt 120 ]; then
exit 2
fi
done