diff options
Diffstat (limited to 'hooks/replica-set-relation-changed')
| -rwxr-xr-x | hooks/replica-set-relation-changed | 87 | 
1 files changed, 87 insertions, 0 deletions
| diff --git a/hooks/replica-set-relation-changed b/hooks/replica-set-relation-changed new file mode 100755 index 0000000..d32293f --- /dev/null +++ b/hooks/replica-set-relation-changed @@ -0,0 +1,87 @@ +#!/bin/bash +# This must be renamed to the name of the relation. The goal here is to +# affect any change needed by relationships being formed, modified, or broken +# This script should be idempotent. + +############################################################################################################ +# Set debugging information +############################################################################################################ +set -ux + +############################################################################################################ +# Set some global variables +############################################################################################################ +MY_HOSTNAME=`hostname -f` +MY_REPLSET=`facter replset-name` +MY_INSTALL_TIME=`facter install-time` + +MASTER_HOSTNAME=${MY_HOSTNAME} +MASTER_REPLSET=${MY_REPLSET} +MASTER_INSTALL_TIME=${MY_INSTALL_TIME} + +echo "My hosntmae: ${MY_HOSTNAME}" +echo "My ReplSet: ${MY_REPLSET}" +echo "My install time: ${MY_INSTALL_TIME}" + +############################################################################################################ +# Here we need to find out which is the first node ( we record the install time ). +# The one with the lowest install time is the master. +# Initialize the master node. +# Add the other nodes to the master's replica set. +############################################################################################################ +# Find the master ( lowest install time ) +for MEMBER in `relation-list` +do + HOSTNAME=`relation-get hostname ${MEMBER}` + REPLSET=`relation-get replset ${MEMBER}` + INSTALL_TIME=`relation-get install-time ${MEMBER}` + [ ${INSTALL_TIME} -lt ${MASTER_INSTALL_TIME} ] && MASTER_INSTALL_TIME=${INSTALL_TIME} +done + +echo "Master install-time: ${MASTER_INSTALL_TIME}" + +# We should now have the lowest member of this relationship. Let's get all of the information about it. +for MEMBER in `relation-list` +do + HOSTNAME=`relation-get hostname ${MEMBER}` + REPLSET=`relation-get replset ${MEMBER}` + INSTALL_TIME=`relation-get install-time ${MEMBER}` + if [ ${INSTALL_TIME} -eq ${MASTER_INSTALL_TIME} ]; then + MASTER_HOSTNAME=${HOSTNAME} + MASTER_REPLSET=${REPLSET} + fi +done + +echo "Master Hostname: ${MASTER_HOSTNAME}" +echo "Master ReplSet: ${MASTER_REPLSET}" +echo "Master install time: ${MASTER_INSTALL_TIME}" + +# We should now have all the information about the master node. +# If the node has already been initialized, it will just inform you +# about it with no other consequence. +if [ ${MASTER_INSTALL_TIME} -eq ${MY_INSTALL_TIME} ]; then + mongo --eval "rs.initiate()" +else + mongo --host ${MASTER_HOSTNAME} --eval "rs.initiate()" +fi + +# Now we need to add the rest of nodes to the replica set +for MEMBER in `relation-list` +do + HOSTNAME=`relation-get hostname ${MEMBER}` + REPLSET=`relation-get replset ${MEMBER}` + INSTALL_TIME=`relation-get install-time ${MEMBER}` + if [ ${MASTER_INSTALL_TIME} -ne ${INSTALL_TIME} ]; then + if [ ${INSTALL_TIME} -eq ${MY_INSTALL_TIME} ]; then + mongo --eval "rs.add(\""${HOSTNAME}"\")" + else + mongo --host ${MASTER_HOSTNAME} --eval "rs.add(\""${HOSTNAME}"\")" + fi + fi +done + +echo $ENSEMBLE_REMOTE_UNIT modified its settings +echo Relation settings: +relation-get +echo Relation members: +relation-list | 
