-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbinit.sh
executable file
·201 lines (170 loc) · 4.34 KB
/
dbinit.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
#!/bin/bash
##
## A helper script used to setup and takedown a Mongo DB for unit tests.
##
# Make sure to use the correct mongo binaries
PATH=`m path`:$PATH
#
# Output usage
#
function usage () {
cat <<EOT
`basename $0` is a helper script used to setup and takedown a MongoDB
for unit tests.
Usage: `basename $0` [OPTIONS] COMMAND MONGODB_VERSION
Options:
-v increase verbosity
-a starts db with authentication enabled
-h prints help
Supported commands:
init sets up and starts database
check checks to see if database is running
clean stops and tears down database
Mongo DB Binary Versions:
2.6.x
3.0.x
3.2.x
EOT
}
# Check usage
if [ $# -lt 1 ]; then
usage
exit 1
fi
# Handle options
while getopts ":avh" opt; do
case ${opt} in
a)
USE_AUTH=1
;;
v)
VERBOSE=1
;;
h)
usage
exit
;;
\?)
echo "Invalid option: -$OPTARG"
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
#
# Initializes the DB with auth users.
# All connections will use authentication.
#
function initdb () {
checkit=`nc -z localhost 27017`
if [ $? -eq 0 ]; then
echo "mongodb is still running, exiting"
exit 1
fi
if [ -e "/tmp/test-data" ]; then
rm -rf /tmp/test-data
fi
# Switch to correct version of Mongo DB
# Requires dependency mongodb-version-manager
# Be sure to `npm install` beforehand.
m use ${MONGODB_VERSION}
mkdir -p /tmp/test-data
mongod --dbpath /tmp/test-data --bind_ip 127.0.0.1 > /dev/null 2>&1 &
until nc -z localhost 27017; do
echo "Starting mongod..."
sleep 1
done
if [ -n "${USE_AUTH}" ]; then
echo "Creating admin user..."
root_user_cmd='{user:"myadmin", pwd:"pass1234", roles:["root"]}'
mongo admin --eval "db.createUser(${root_user_cmd})" > /dev/null 2>&1
test_user_cmd='{user:"myadmin", pwd:"pass1234", roles:["readWrite"]}'
echo "Creating user for humblejs_test database..."
mongo humblejs_test --eval "db.createUser(${test_user_cmd})" \
> /dev/null 2>&1
echo "Stopping mongod..."
mongo admin --eval 'db.shutdownServer()' > /dev/null 2>&1
sleep 2
mongod --dbpath /tmp/test-data --bind_ip 127.0.0.1 --auth \
> /dev/null 2>&1 &
until nc -z localhost 27017; do
echo "Starting mongod with auth enabled..."
sleep 1
done
fi
echo "Done"
}
#
# Stops DB and cleans up entire database directory.
#
function cleandb () {
# Switch to correct version of Mongo DB
# Requires dependency mongodb-version-manager
# Be sure to `npm install` beforehand.
m use ${MONGODB_VERSION}
checkit=`nc -z localhost 27017`
if [ $? -eq 0 ]; then
echo "Stopping mongod..."
# Double check to see if mongod was started with auth
using_auth=`ps aux | grep mongo | grep auth`
if [ -n "${using_auth}" ]; then
mongo admin -u myadmin -p pass1234 --authenticationDatabase admin \
--eval "db.shutdownServer()" > /dev/null 2>&1
else
mongo admin --eval "db.shutdownServer()" > /dev/null 2>&1
fi
sleep 2
else
echo "mongod is not running, exiting"
exit
fi
if [ -e "/tmp/test-data" ]; then
echo "Cleaning up dbpath..."
rm -rf /tmp/test-data
fi
echo "Done"
}
function checkdb () {
checkit=`nc -z localhost 27017`
if [ $? -eq 0 ]; then
echo "mongodb is running"
else
echo "mongodb is not running"
fi
}
# Handle arguments
CMD=$1
MONGODB_VERSION=$2
if [ -z "${CMD}" ]; then
echo "command is required"
usage
exit 1
fi
# Choose default as 2.6.x if db_version is not given
if [ -z "${MONGODB_VERSION}" ]; then
MONGODB_VERSION="2.6.x"
fi
# Print out settings for debugging
if [ -n "${VERBOSE}" ]; then
cat <<EOT
Performing [${CMD}] for version MongoDB [${MONGODB_VERSION}]
Using auth: ${USE_AUTH:-0}
EOT
fi
# Handle commands
case ${CMD} in
init)
initdb
;;
check)
checkdb
;;
clean)
cleandb
;;
*)
echo "Unrecognized command: ${CMD}"
usage
;;
esac