#!/bin/sh
#
#
#	Peter's Failover IP. Allows the reassign AWS secondary IP.
#
# Copyright (c) 2004 SUSE LINUX AG, Lars Marowsky-Brée
#                    All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

# Uses the following variables:
#  OCF_RESKEY_ip
#  OCF_RESKEY_region
#

#######################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs

#######################################################################

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="FVIP" version="0.9">
<version>1.0</version>

<longdesc lang="en">
To be described.
</longdesc>
<shortdesc lang="en">Peters failover IP agent</shortdesc>

<parameters>
<parameter name="state" unique="1">
<longdesc lang="en">
Location to store the resource state in.
</longdesc>
<shortdesc lang="en">State file</shortdesc>
<content type="string" default="${HA_VARRUN}/awsfip-{OCF_RESOURCE_INSTANCE}.state" />
</parameter>

<parameter name="ip" unique="1" required="1">
<longdesc lang="en">
The actual failover IP address.
</longdesc>
<shortdesc lang="en">The actual failover IP address.</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="region" unique="1" required="1">
<longdesc lang="en">
passes params to commands.
</longdesc>
<shortdesc lang="en">passes params to commands.</shortdesc>
<content type="string" default="" />
</parameter>

</parameters>

<actions>
<action name="start"        timeout="60s" />
<action name="stop"         timeout="10s" />
<action name="monitor"      timeout="30s" interval="60s" depth="0" start-delay="0s" />
<action name="reload"       timeout="600s" />
<action name="meta-data"    timeout="5s" />
<action name="validate-all" timeout="30s" />
</actions>
</resource-agent>
END
}

#######################################################################

Instance_ID=`/usr/bin/curl --silent http://169.254.169.254/latest/meta-data/instance-id`
ENI_ID=`/usr/local/bin/aws ec2 describe-instances --instance-id $Instance_ID --region $OCF_RESKEY_region | grep NetworkInterfaceId | cut -d '"' -f 4`
AWS=/usr/local/bin/aws
MONITOR=`/usr/local/bin/aws ec2 describe-instances --instance-id $Instance_ID --region $OCF_RESKEY_region | grep $OCF_RESKEY_ip | wc -l`
MONALL=`/usr/local/bin/aws ec2 describe-instances --region $OCF_RESKEY_region | grep $OCF_RESKEY_ip | wc -l`

#######################################################################

awsfip_usage() {
	cat <<END
usage: $0 {start|stop|monitor|migrate_to|migrate_from|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

awsfip_start() {
    $AWS ec2 assign-private-ip-addresses --network-interface-id $ENI_ID --private-ip-addresses $OCF_RESKEY_ip --allow-reassignment --region $OCF_RESKEY_region; /etc/init.d/networking restart
    return $OCF_SUCCESS
}

awsfip_stop() {
    return $OCF_SUCCESS
}

awsfip_monitor() {
	
	if [ $MONITOR == 1 ]; then
		return $OCF_SUCCESS;
        elif [ $MONALL == 1 ]; then
                return $OCF_NOT_RUNNING;
	else
		return $OCF_ERR_GENERIC ;
	fi
	
}

awsfip_validate() {
    
    # Is the state directory writable? 
    state_dir=`dirname "$OCF_RESKEY_state"`
    touch "$state_dir/$$"
    if [ $? != 0 ]; then
        return $OCF_ERR_ARGS
    fi
    rm "$state_dir/$$"


    return $OCF_SUCCESS
}

: ${OCF_RESKEY_CRM_meta_interval=0}
: ${OCF_RESKEY_CRM_meta_globally_unique:="true"}

case $__OCF_ACTION in
meta-data)	meta_data
		exit $OCF_SUCCESS
		;;
start)		awsfip_start;;
stop)		awsfip_stop;;
monitor)	awsfip_monitor;;
reload)		ocf_log err "Reloading..."
	        awsfip_start
		;;
validate-all)	awsfip_validate;;
usage|help)	awsfip_usage
		exit $OCF_SUCCESS
		;;
*)		awsfip_usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc