-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
·190 lines (166 loc) · 6.16 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
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
#!/bin/bash
# #####################################
# Disk Space
# boot2docker destroy
# boot2docker init
# docker stop $(docker ps -a -q)
# docker rm $(docker ps -a -q)
# docker rmi -f $(docker images | grep "<none>" | awk "{print \$3}")
# #####################################
# Docker versions 1.7
# Installer: http://docs.docker.com/mac/step_one/
# Client & Server needs to be the same:
# boot2docker stop
# boot2docker download
# boot2docker up
# Note: doing this will remove existing containers
# Virtual Box v5
# https://www.virtualbox.org/wiki/Downloads
# Issue with x509 certs
# boot2docker ssh 'sudo /etc/init.d/docker restart'
# --tlsverify=false should never be a recommended workaround
# Get parameters (http://stackoverflow.com/questions/1922490)
for i in "$@"
do
case $i in
--force-rebuild)
paramForceRebuild=true
shift # past argument with no value
;;
*)
# unknown option
echo "Unknown parameter '$i'"
exit 1
;;
esac
done
# Check for docker or boot2docker
if [ ! -z `which boot2docker` ]
then
# Init docker environment on Mac and Windows
$(boot2docker shellinit)
else
if [ -z `which docker` ]
then
echo "Neither docker nor boot2docker found"
echo "Please check your PATH"
exit 127
fi
fi
echo ""
echo "Setting default params..."
echo ""
MONOGO_VERSION=2.6.0
MONOGO_PORT=27017
BUILD_VERSION=0.1.0
BUILD_PORT=9000
echo ""
echo "Stopping and removing existing containers..."
echo ""
containers=( build-DB-01 build-DB-02 build-arbiter build-data-mongo build-node)
for c in ${containers[@]}; do
docker stop ${c} > /dev/null 2>&1
docker rm -f ${c} > /dev/null 2>&1
done
echo ""
echo "Building VM's from Dockerfiles..."
echo "- comment out these lines if you want to use different image tags"
echo ""
# Build docker data volumes - Single Responsibility Principle (SRP)
# Don't rebuild if base images already exist
images=( "base" "mongo" "mongo-data" "node" )
for image in "${images[@]}"
do
imageExists=`docker images | grep longieirl/$image`
if [ -z "$imageExists" ] || [ ! -z "$paramForceRebuild" ]
then
echo "Building image longieirl/$image..."
docker build -t longieirl/$image $image/
else
echo "Skipping buildnig longieirl/$image because it already exists"
fi
done
echo ""
echo "Build data container for mongo i.e. logs/journal/data..."
echo "- this data store holds all the logs and data for ALL the MongoDB instances"
echo ""
docker run -itd --name build-data-mongo longieirl/mongo-data
echo ""
echo "Build replica set with 3 mongodb nodes..."
echo ""
# With three members, majority required to vote is 2, fault tolerance is 1.
# Note: later we add arbiter which only casts votes
# Refer: http://docs.mongodb.org/manual/core/replica-set-architecture-four-members/
docker run -itd -p $MONOGO_PORT:$MONOGO_PORT --name build-DB-01 --volumes-from build-data-mongo --detach --publish-all longieirl/mongo:$MONOGO_VERSION mongod --config /conf/mongo.conf --dbpath /data/mongo-01 --logpath /log/mongoReplica-01.log
docker run -itd --name build-DB-02 --volumes-from build-data-mongo --detach --publish-all longieirl/mongo:$MONOGO_VERSION mongod --config /conf/mongo.conf --dbpath /data/mongo-02 --logpath /log/mongoReplica-02.log
# Adding arbiter as the majority of the members must be accissible for an election to take place
docker run -itd --name build-arbiter -p 30000:30000 --volumes-from build-data-mongo --detach --publish-all longieirl/mongo:$MONOGO_VERSION mongod --dbpath /data/arb --config /conf/mongo-arb.conf --logpath /log/arbiter.log
echo ""
echo "Getting IP addresses of Mongo instances..."
echo ""
MONGODB1=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' build-DB-01)
MONGODB2=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' build-DB-02)
ARBITER=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' build-arbiter)
echo $MONGODB1 + ' (build-DB-01)'
echo $MONGODB2 + ' (build-DB-02)'
echo $ARBITER + ' (build-arbiter)'
echo ""
echo "Configure replica set on primary MongoDB instance..."
echo ""
read -r -d '' MONGOCONFIG <<- EOM
printjson(rs.initiate({ _id : 'localDev', members : [ {_id : 0, host : '$MONGODB1:27017'}, {_id : 1, host : '$MONGODB2:27017'} ] }));
EOM
docker exec build-DB-01 mongo $MONGODB1:27017 --quiet --eval "$MONGOCONFIG"
echo ""
echo "Waiting for everything to come online...."
echo ""
sleep 20
echo ""
echo "Adding arbiter to primary..."
echo ""
docker exec build-DB-01 mongo $MONGODB1:27017 --quiet --eval "printjson(rs.addArb('$ARBITER:30000'))"
read -r -d '' ECHOCONFIG <<- EOM
printjson( rs.status() );
EOM
echo ""
echo "Update BUILD configuration file with MongoDB primary node..."
echo ""
python updateConfig.py 'BUILD/BUILD/server/config.json' $MONGODB1
echo ""
echo "Run and execute BUILD application..."
echo ""
# Adding -e here to make python available as env variable
docker run --rm -v $PWD/BUILD/BUILD:/app -e PYTHON=/usr/bin/python -e GYP_MSVS_VERSION=2012 longieirl/node npm install
docker run --rm -v $PWD/BUILD/BUILD:/app longieirl/node node server/initSchema.js
docker run --rm -v $PWD/BUILD/BUILD:/app longieirl/node node server/setDefaultAccess.js
docker run -itd -v $PWD/BUILD/BUILD:/app -p $BUILD_PORT:$BUILD_PORT --link build-DB-01:build-DB-01 --name build-node longieirl/node grunt serve
# This step can take up to 2mins, it builds the CSS, sprites etc...
sleep 200
echo ""
echo "Enabling BUILD and MongoDB ports..."
echo ""
# Not needed when on linux
if [ -z `which VBoxManage` ]
then
VBoxManage controlvm boot2docker-vm natpf1 mongodb-script,tcp,,27017,,27017
VBoxManage controlvm boot2docker-vm natpf1 build-script,tcp,,$BUILD_PORT,,$BUILD_PORT
else
echo "Skipped because no 'VBoxManage' found (not needed on linux)"
fi
echo ""
echo "#####################################"
echo "Monitor BUILD"
echo "$ docker logs build-node"
echo ""
echo ""
echo "#####################################"
echo "Connect to MongoDB replica set:"
echo "$ mongo $(boot2docker ip)":$MONGO_PORT
echo ""
echo "#####################################"
echo "Access database logs for all mongodb nodes:"
echo "$ docker exec -it build-data-mongo bash"
echo "$ tail -f /log/mongodb/mongoReplica-01.log"
echo "$ tail -f /log/mongodb/mongoReplica-02.log"
echo "$ tail -f /log/mongodb/arbiter.log"
echo ""