Created
May 9, 2011 02:33
-
-
Save chetan/961947 to your computer and use it in GitHub Desktop.
init script for grabbing an EC2 elastic IP
This file contains 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 | |
############################################################################### | |
# CONFIG | |
# paths to key & cert files | |
export EC2_PRIVATE_KEY= | |
export EC2_CERT= | |
# needed for moving the IP | |
MY_ID="" # different for each node, ex: i-xxxxxxxx | |
ELASTIC_IP="" # ex: 174.129.253.XXX | |
############################################################################### | |
############################################################################### | |
# INIT | |
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat} | |
if [ -f ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs ]; then | |
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs | |
fi | |
USAGE="usage: $0 {start|stop|status|meta-data}"; | |
############################################################################### | |
meta_data() { | |
cat <<END | |
<?xml version="1.0"?> | |
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd"> | |
<resource-agent name="ElasticIP"> | |
<version>1.0</version> | |
<longdesc lang="en"> | |
This script manages Amazon EC2 Elastic IP addresses. | |
</longdesc> | |
<shortdesc lang="en">Manages Amazon EC2 Elastic IP addresses</shortdesc> | |
<actions> | |
<action name="start" timeout="20s" /> | |
<action name="stop" timeout="20s" /> | |
<action name="status" timeout="20s" /> | |
<action name="meta-data" timeout="5s" /> | |
</actions> | |
</resource-agent> | |
END | |
exit $OCF_SUCCESS | |
} | |
ip_start() { | |
ec2-associate-address "$ELASTIC_IP" -i "$MY_ID" > /dev/null | |
echo $0 started | |
exit $OCF_SUCCESS | |
} | |
ip_stop() { | |
ec2-disassociate-address "$ELASTIC_IP" > /dev/null | |
echo $0 stopped | |
exit $OCF_SUCCESS | |
} | |
ip_status() { | |
ec2-describe-addresses | grep "$ELASTIC_IP" | grep "$MY_ID" > /dev/null | |
# grep will return true if this ip is mapped to this instance | |
[ $? -eq 0 ] && echo $0 OK || echo $0 FAIL | |
exit $OCF_SUCCESS | |
} | |
usage() { | |
echo $USAGE >&2 | |
return $1 | |
} | |
if [ $# -ne 1 ]; then | |
usage $OCF_ERR_ARGS | |
fi | |
case $1 in | |
meta-data) meta_data;; | |
start) ip_start;; | |
stop) ip_stop;; | |
status) ip_status;; | |
# monitor) ip_monitor;; | |
# validate-all) ip_validate_all;; | |
usage) usage $OCF_SUCCESS;; | |
*) usage $OCF_ERR_UNIMPLEMENTED;; | |
esac | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment