summaryrefslogtreecommitdiff
path: root/hooks/replica-set-relation-changed
blob: d32293f137e4dba67f12abb98dc03d4b48669abf (plain)
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 75 76 77 78 79 80 81 82 83 84 85 86 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