Created
June 20, 2014 18:50
-
-
Save adamdaigian/ed6b83ab14d6444deead to your computer and use it in GitHub Desktop.
MongoDB Cluster Start Script
This file contains 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
{% if variables.primary.address %} | |
#!/bin/bash | |
elasticbox set mongo.username {{ variables.primary.variables.mongo.username }} | |
elasticbox set mongo.password {{ variables.primary.variables.mongo.password }} | |
HOST_ARG='--host {{ variables.REPLICA_SET }}/{{ variables.primary.address.public }} admin' | |
AUTH_ARGS='-u {{ variables.primary.variables.mongo.username }} -p {{ variables.primary.variables.mongo.password }}' | |
HOST_GREP='"name" : "{{ address.public }}:{{ variables.mongo.mongodb }}"' | |
ADD_COMMAND="printjson(rs.add('{{ address.public }}:{{ variables.mongo.mongodb }}'))" | |
[[ -z $(mongo $AUTH_ARGS $HOST_ARG --eval "printjson(rs.status())" | grep "$HOST_GREP") ]] && mongo $AUTH_ARGS $HOST_ARG --eval "$ADD_COMMAND" | |
exit 0 | |
{% else %} | |
#!/bin/bash | |
CONFIG='{_id:"{{ variables.REPLICA_SET }}",version:1,members:[{_id:0,host:"{{ address.public }}:{{ variables.mongo.mongodb }}"}]}' | |
if [[ ! -z $(mongo --eval "printjson(rs.status())" | grep "EMPTYCONFIG") ]]; then | |
mongo --eval "printjson(rs.initiate($CONFIG))" | |
while true | |
do | |
[[ ! -z $(mongo --eval "printjson(rs.status())" | grep '"stateStr" : "PRIMARY"') ]] && break; | |
sleep 1 | |
done | |
fi | |
USER='{user:"{{ variables.mongo.username }}",pwd:"{{ variables.mongo.password }}",roles:[{role:"root",db:"admin"},{role:"restore",db:"admin"}]}' | |
mongo admin --eval "printjson(db.getUsers())" > /dev/null | |
[[ $? -ne 252 ]] && mongo admin --eval "printjson(db.createUser($USER))" | |
exit 0 | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment