Last active
October 20, 2016 21:01
-
-
Save timusg/8251999 to your computer and use it in GitHub Desktop.
keepalived notification script for switch elastic ip, helpful to setup highly available haproxy in ec2/VPC
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 | |
export EC2_HOME=/usr/local/share/ec2_tools/ | |
export JAVA_HOME=/usr/lib/jvm/java | |
export PATH=$PATH:$EC2_HOME/bin | |
INSTANCEID=`curl -s http://169.254.169.254/latest/meta-data/instance-id` | |
case "$1" in | |
MASTER) | |
logger triggering notify master event | |
ADDRESS=`ec2-describe-addresses --aws-secret-key '<%= @secret_key%>' --aws-access-key '<%= @access_key%>' <%= @eip%>` | |
ASSOCIATED_INSTANCEID=`echo $ADDRESS | awk '{print $3}'` | |
ASSOCIATION_ID=`echo $ADDRESS | awk '{print $6}'` | |
ALLOCATION_ID=`echo $ADDRESS | awk '{print $5}'` | |
if [ ! "$INSTANCEID" == "$ASSOCIATED_INSTANCEID" ]; then | |
ec2-disassociate-address --aws-secret-key '<%= @secret_key%>' --aws-access-key '<%= @access_key%>' -a $ASSOCIATION_ID 2>&1 | |
logger -p local0.warn associating EIP to instance: $INSTANCEID | |
ec2-associate-address --aws-secret-key '<%= @secret_key%>' --aws-access-key '<%= @access_key%>' -i $INSTANCEID -a $ALLOCATION_ID 2>&1 | |
fi | |
;; | |
FAULT) | |
logger restarting keepalived | |
service keepalived restart | |
;; | |
*) | |
echo $"Usage: $0 {MASTER|FAULT}" | |
exit 2 | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment