double-z
1/4/2014 - 5:05 AM

keepalived notification script for switch elastic ip, helpful to setup highly available haproxy in ec2/VPC

keepalived notification script for switch elastic ip, helpful to setup highly available haproxy in ec2/VPC

#!/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