forked from yisoft/docker-mongo-local-replicaset
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
·82 lines (62 loc) · 2.43 KB
/
setup.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
#!/bin/bash
set -e
REPLICA_SET_NAME=${REPLICA_SET_NAME:=rs0}
USERNAME=${USERNAME:=dev}
PASSWORD=${PASSWORD:=dev}
function waitForMongo {
port=$1
user=$2
pass=$3
n=0
until [ $n -ge 20 ]
do
if [ -z "$user" ]; then
mongo admin --quiet --port $port --eval "db" && break
else
echo "trying: $port $user $pass"
mongo admin --quiet --port $port -u $user -p $pass --eval "db" && break
fi
n=$[$n+1]
sleep 2
done
}
if [ ! "$(ls -A /data/db1)" ]; then
mkdir /data/db1
mkdir /data/db2
mkdir /data/db3
mongod --smallfiles --dbpath /data/db1 &
MONGO_PID=$!
waitForMongo 27017
echo "CREATING USER ACCOUNT"
mongo admin --eval "db.createUser({ user: '$USERNAME', pwd: '$PASSWORD', roles: ['root', 'restore', 'readWriteAnyDatabase', 'dbAdminAnyDatabase'] })"
echo "KILLING MONGO"
kill $MONGO_PID
wait $MONGO_PID
fi
echo "WRITING KEYFILE"
openssl rand -base64 741 > /var/mongo_keyfile
chown mongodb /var/mongo_keyfile
chmod 600 /var/mongo_keyfile
echo "STARTING CLUSTER"
mongod --port 27003 --smallfiles --dbpath /data/db3 --auth --replSet $REPLICA_SET_NAME --keyFile /var/mongo_keyfile &
DB3_PID=$!
mongod --port 27002 --smallfiles --dbpath /data/db2 --auth --replSet $REPLICA_SET_NAME --keyFile /var/mongo_keyfile &
DB2_PID=$!
mongod --port 27001 --smallfiles --dbpath /data/db1 --auth --replSet $REPLICA_SET_NAME --keyFile /var/mongo_keyfile &
DB1_PID=$!
waitForMongo 27001 $USERNAME $PASSWORD
waitForMongo 27002
waitForMongo 27003
echo "CONFIGURING REPLICA SET: $HOSTNAME"
CONFIG="{ _id: '$REPLICA_SET_NAME', members: [{_id: 0, host: '$HOSTNAME:27001', priority: 2 }, { _id: 1, host: '$HOSTNAME:27002' }, { _id: 2, host: '$HOSTNAME:27003' } ]}"
mongo admin --port 27001 -u $USERNAME -p $PASSWORD --eval "db.runCommand({ replSetInitiate: $CONFIG })"
waitForMongo 27002 $USERNAME $PASSWORD
waitForMongo 27003 $USERNAME $PASSWORD
mongo admin --port 27001 -u $USERNAME -p $PASSWORD --eval "db.runCommand({ setParameter: 1, quiet: 1 })"
mongo admin --port 27002 -u $USERNAME -p $PASSWORD --eval "db.runCommand({ setParameter: 1, quiet: 1 })"
mongo admin --port 27003 -u $USERNAME -p $PASSWORD --eval "db.runCommand({ setParameter: 1, quiet: 1 })"
echo "REPLICA SET ONLINE"
trap 'echo "KILLING"; kill $DB1_PID $DB2_PID $DB3_PID; wait $DB1_PID; wait $DB2_PID; wait $DB3_PID' SIGINT SIGTERM EXIT
wait $DB1_PID
wait $DB2_PID
wait $DB3_PID