-
Notifications
You must be signed in to change notification settings - Fork 23
/
mongo.sh
executable file
·74 lines (65 loc) · 1.47 KB
/
mongo.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
#!/bin/dash
set -eu
usage() {
echo "$0 restart|start|stop"
exit 64
}
failure_hints() {
echo '
Failed to start mongo. If using Docker Desktop:
- Go into Settings -> Features in development
- untick "Use containerd"
- tick "Use Rosetta"'
exit 1
}
docker_run() {
docker run --name router-mongo -dp 27017:27017 mongo:2.6 --replSet rs0 --quiet
}
init_replicaset() {
docker exec router-mongo mongo --quiet --eval 'rs.initiate();' >/dev/null 2>&1
}
healthy() {
docker exec router-mongo mongo --quiet --eval \
'if (rs.status().members[0].health==1) print("healthy");' \
2>&1 | grep healthy >/dev/null
}
# usage: retry_or_fatal description command-to-try
retry_or_fatal() {
n=60
echo -n "Waiting up to $n s for $1"; shift
while [ "$n" -ge 0 ]; do
if "$@"; then
echo " done"
return
fi
sleep 1 && echo -n .
n=$((n-1))
done
echo "gave up"
exit 1
}
stop() {
if ! docker stop router-mongo >/dev/null 2>&1; then
echo "router-mongo not running"
return
fi
echo -n Waiting for router-mongo container to exit.
docker wait router-mongo >/dev/null || true
docker rm -f router-mongo >/dev/null 2>&1 || true
echo " done"
}
start() {
if healthy; then
echo router-mongo already running.
return
fi
stop
docker_run || failure_hints
retry_or_fatal "for successful rs.initiate()" init_replicaset
retry_or_fatal "for healthy rs.status()" healthy
}
case $1 in
start) $1;;
stop) $1;;
*) usage
esac