Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MongoDB Cluster Start Script
{% 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
You can’t perform that action at this time.