forked from crizstian/mongo-replica-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-replica-set.sh
220 lines (184 loc) · 5.7 KB
/
create-replica-set.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
function howManyServers {
arg=''
c=0
for server in manager1 worker1 worker2
do
cmd='docker-machine ip '$server
arg=$arg' --add-host '${server}':'$($cmd)
done
echo $arg
}
function switchToServer {
env='docker-machine env '$1
echo '···························'
echo '·· swtiching >>>> '$1' server ··'
echo '···························'
eval $($env)
}
function startReplicaSet {
# init replica set in mongodb master
wait_for_databases $2 "$4"
docker exec -i $1 bash -c 'mongo --eval "rs.initiate() && rs.conf()" --port '$p' -u $MONGO_SUPER_ADMIN -p $MONGO_PASS_SUPER --authenticationDatabase="admin"'
}
function createDockerVolume {
cmd=$(docker volume ls -q | grep $1)
if [[ "$cmd" == $1 ]];
then
echo 'volume available'
else
cmd='docker volume create --name '$1
eval $cmd
fi
}
function copyFilesToContainer {
echo '·· copying files to container >>>> '$1' ··'
# copy necessary files
docker cp ./admin.js $1:/data/admin/
docker cp ./replica.js $1:/data/admin/
docker cp ./mongo-keyfile $1:/data/keyfile/
docker cp ./grantRole.js $1:/data/admin
docker cp ./movies.js $1:/data/admin
}
# @params container volume
function configMongoContainer {
echo '·· configuring container >>>> '$1' ··'
# check if volume exists
createDockerVolume $2
# start container
docker run --name $1 -v $2:/data -d mongo --smallfiles
# create the folders necessary for the container
docker exec -i $1 bash -c 'mkdir /data/keyfile /data/admin'
# copy the necessary files to the container
copyFilesToContainer $1
# change folder owner to the current container user
docker exec -i $1 bash -c 'chown -R mongodb:mongodb /data'
}
# @params container volume
function removeAndCreateContainer {
echo '·· removing container >>>> '$1' ··'
# remove container
docker rm -f $1
env='./env'
serv=$(howManyServers)
keyfile='mongo-keyfile'
port='27017:27017'
p='27017'
rs='rs1'
echo '·· recreating container >>>> '$1' ··'
#create container with sercurity and replica configuration
docker run --restart=unless-stopped --name $1 --hostname $1 \
-v $2:/data \
--env-file $env \
$serv \
-p $port \
-d mongo --smallfiles \
--keyFile /data/keyfile/$keyfile \
--replSet $rs \
--storageEngine wiredTiger \
--port $p
}
# @params server container volume
function createMongoDBNode {
# switch to corresponding server
switchToServer $1
echo '·· creating container >>>> '$2' ··'
# start configuration of the container
configMongoContainer $2 $3
sleep 2
#create container with sercurity and replica configuration
removeAndCreateContainer $2 $3
# verify if container is ready
wait_for_databases 'manager1'
echo '·······························'
echo '· CONTAINER '$1' CREATED ··'
echo '·······························'
}
function wait_for {
echo ">>>>>>>>>>> waiting for mongodb"
start_ts=$(date +%s)
while :
do
(echo > /dev/tcp/$1/$2) >/dev/null 2>&1
result=$?
if [[ $result -eq 0 ]]; then
end_ts=$(date +%s)
echo "<<<<< $1:$2 is available after $((end_ts - start_ts)) seconds"
sleep 3
break
fi
sleep 5
done
}
function wait_for_databases {
if [[ ($1 == 'manager1') ]]; then
ip=$(docker-machine ip manager1)
elif [[ $1 == 'worker1' ]]; then
ip=$(docker-machine ip worker1)
elif [[ $1 == 'worker2' ]]; then
ip=$(docker-machine ip worker2)
fi
# make tcp call
echo "IP == $ip PORT == 27017"
wait_for "$ip" 27017
}
function createKeyFile {
openssl rand -base64 741 > $1
chmod 600 $1
}
# @params server primary-mongo-container
function add_replicas {
echo '·· adding replicas >>>> '$1' ··'
switchToServer $1
# add nuppdb replicas
for server in worker1 worker2
do
rs="rs.add('$server:27017')"
add='mongo --eval "'$rs'" -u $MONGO_REPLICA_ADMIN -p $MONGO_PASS_REPLICA --authenticationDatabase="admin"'
sleep 2
wait_for_databases $server
docker exec -i $2 bash -c "$add"
done
}
function init_replica_set {
docker exec -i $1 bash -c 'mongo < /data/admin/replica.js'
sleep 2
docker exec -i $1 bash -c 'mongo < /data/admin/admin.js'
cmd='mongo -u $MONGO_REPLICA_ADMIN -p $MONGO_PASS_REPLICA --eval "rs.status()" --authenticationDatabase "admin"'
sleep 2
docker exec -i mongoNode1 bash -c "$cmd"
}
function init_mongo_primary {
# @params name-of-keyfile
createKeyFile mongo-keyfile
# @params server container volume
createMongoDBNode manager1 mongoNode1 mongo_storage
# @params container
init_replica_set mongoNode1
echo '·······························'
echo '· REPLICA SET READY TO ADD NODES ··'
echo '·······························'
}
function init_mongo_secondaries {
# @Params server container volume
createMongoDBNode worker1 mongoNode2 mongo_storage
createMongoDBNode worker2 mongoNode3 mongo_storage
}
# @params server primary-mongo-container
function check_status {
switchToServer $1
cmd='mongo -u $MONGO_REPLICA_ADMIN -p $MONGO_PASS_REPLICA --eval "rs.status()" --authenticationDatabase "admin"'
docker exec -i $2 bash -c "$cmd"
}
function add_moviesdb_test {
docker exec -i mongoNode1 bash -c 'mongo -u $MONGO_USER_ADMIN -p $MONGO_PASS_ADMIN --authenticationDatabase "admin" < /data/admin/grantRole.js'
sleep 1
docker exec -i mongoNode1 bash -c 'mongo -u $MONGO_USER_ADMIN -p $MONGO_PASS_ADMIN --authenticationDatabase "admin" < /data/admin/movies.js'
}
function main {
init_mongo_primary
init_mongo_secondaries
add_replicas manager1 mongoNode1
check_status manager1 mongoNode1
add_moviesdb_test
}
main