-
-
Save dolgarev/29033a730b032eb19f0695d7d1e2d434 to your computer and use it in GitHub Desktop.
ugly hack to initialize replica set for MongoDB docker container, put under /docker-entrypoint-initdb.d/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| : "${FORKED:=}" | |
| if [ -z "${FORKED}" ]; then | |
| echo >&2 'mongod for initdb is going to shutdown' | |
| mongod --pidfilepath /tmp/docker-entrypoint-temp-mongod.pid --shutdown | |
| echo >&2 'replica set will be initialized later' | |
| FORKED=1 "${BASH_SOURCE[0]}" & | |
| unset FORKED | |
| mongodHackedArgs=(:) # bypass mongod --shutdown in docker-entrypoint.sh | |
| return | |
| fi | |
| # FIXME: assume mongod listens on 127.0.0.1:27017 | |
| mongo=( mongo --host 127.0.0.1 --port 27017 --quiet ) | |
| tries=30 | |
| while true; do | |
| sleep 1 | |
| if "${mongo[@]}" --eval 'quit(0)' &> /dev/null; then | |
| # success! | |
| break | |
| fi | |
| (( tries-- )) | |
| if [ "$tries" -le 0 ]; then | |
| echo >&2 | |
| echo >&2 'error: unable to initialize replica set' | |
| echo >&2 | |
| kill -STOP 1 # initdb won't be executed twice, so fail loudly | |
| exit 1 | |
| fi | |
| done | |
| echo 'about to initialize replica set' | |
| # FIXME: hard-coded replica set name & member host | |
| "${mongo[@]}" <<-EOF | |
| rs.initiate({ | |
| _id: "rs0", | |
| version: 1, | |
| members: [ | |
| { _id: 0, host : "mongo" }, | |
| ] | |
| }); | |
| EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment