-
Notifications
You must be signed in to change notification settings - Fork 155
/
start_mongo.bash
executable file
·88 lines (70 loc) · 2.58 KB
/
start_mongo.bash
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
#!/usr/bin/env bash
needs_initiate=1
# Non-replicaset mongod used for some tests
mkdir -p tmp/db0
mongod --fork --dbpath tmp/db0 --logpath tmp/db0/log --port 27017 --bind_ip 127.0.0.1 &>/dev/null
# start the mongod servers of the replica set
for i in $(seq 1 3); do
mkdir -p tmp/db$i
pid="$(cat tmp/db$i/mongod.lock 2>/dev/null)"
if [ $? -ne 0 -a -z "$pid" ]; then
# >&2 echo "starting mongo server $i"
mongod --fork --dbpath tmp/db$i --logpath tmp/db$i/log --port 2700$i --bind_ip 127.0.0.1 \
--replSet mongodb_test &>/dev/null
if [ $? -ne 0 ]; then
. ./stop_mongo.bash
# >&2 echo "failed to start mongo servers..."
exit 1
fi
# >&2 echo "mongo server $i started"
else
needs_initiate=0
fi
done
sleep 1
# initiate the replica set
# >&2 echo "needs initiate: $needs_initiate"
if [[ $needs_initiate -eq 1 ]]; then
# >&2 echo "initiating"
host1='"127.0.0.1:27001"'
host2='"127.0.0.1:27002"'
host3='"127.0.0.1:27003"'
members="[{_id: 0, host: $host1}, {_id: 1, host: $host2}, {_id: 2, host: $host3}]"
mongo --quiet --port 27001 --eval \
"JSON.stringify(rs.initiate({_id: \"mongodb_test\", members: $members}))" >/dev/null
if [ $? -ne 0 ]; then
# >&2 echo "failed to configure replica set"
exit 1
fi
# >&2 echo "initiated"
fi
repl_set_url='mongodb_test/127.0.0.1:27001,127.0.0.1:27002,127.0.0.1:27003'
# wait for replica set election
# >&2 echo "replica set election"
mongo --port 27001 --quiet --eval 'typeof db.createUser === "function"' >tmp/over24
if [ $? -ne 0 ]; then
# >&2 echo "failed while waiting for replica set election"
exit 1
else
# >&2 echo "done"
if [ "$(tail -n1 tmp/over24)" == "true" ]; then
createUser="createUser"
else
createUser="addUser"
if [[ $needs_initiate -eq 1 ]]; then
sleep 60
fi
fi
# >&2 echo "dropping database, adding users"
mongo mongodb_test --host $repl_set_url --quiet --eval 'db.dropDatabase()' &>/dev/null && \
mongo admin_test --host $repl_set_url --quiet --eval 'db.dropDatabase()' &>/dev/null &&
mongo mongodb_test --host $repl_set_url --quiet --eval \
"db.${createUser}({user:'mongodb_user',pwd:'mongodb_user',roles:[]})" &>/dev/null && \
mongo mongodb_test --host $repl_set_url --quiet --eval \
"db.${createUser}({user:'mongodb_user2',pwd:'mongodb_user2',roles:[]})" &>/dev/null && \
mongo admin_test --host $repl_set_url --quiet --eval \
"db.${createUser}({user:'mongodb_admin_user',pwd:'mongodb_admin_user',roles:[ \
{role:'readWrite',db:'mongodb_test'},{role:'read',db:'mongodb_test2'}]})" &>/dev/null
# >&2 echo "done"
fi
exit 0