-
-
Save azilber/f94c02cab19d74c8d94e56d38404dd4d to your computer and use it in GitHub Desktop.
Revisions
-
jsianes revised this gist
Jul 28, 2014 . 1 changed file with 14 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -45,7 +45,6 @@ valid_ip() { check_ec2metadata() { ec2-metadata >/dev/null 2>&1 if [ $? -eq 0 ] then # Amazon Linux ec2-metadata command found @@ -54,16 +53,26 @@ check_ec2metadata() { else # Checking ec2metadata command ec2metadata >/dev/null 2>&1 if [ $? -eq 0 ] then # ec2metadata command found EC2INSTANCE=`ec2metadata --instance-id` EC2PUBLICIP=`ec2metadata --public-ipv4` else # No ec2metadata/ec2-metadata command found. Trying to obtain using curl requests directly IP_AVAILABLE=`curl --connect-timeout 5 -i http://169.254.169.254/latest/meta-data/public-ipv4 2>/dev/null | grep "200 OK" | wc -l` IS_AVAILABLE=`curl --connect-timeout 5 -i http://169.254.169.254/latest/meta-data/instance-id 2>/dev/null | grep "200 OK" | wc -l` (( INFO=${IP_AVAILABLE}+${IS_AVAILABLE} )) if [ ${INFO} -eq 2 ] then EC2INSTANCE=`curl --connect-timeout 5 http://169.254.169.254/latest/meta-data/instance-id 2>/dev/null` EC2PUBLICIP=`curl --connect-timeout 5 http://169.254.169.254/latest/meta-data/public-ipv4 2>/dev/null` else # ec2metadata/ec2-metadata command not found and unable to obtain metadata info using curl LOG="ERROR: Unable to obtain metadata information using ec2metadata/ec2-metadata command or curl requests"; write_log exit 127 fi fi fi } -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ #!/bin/bash # chkconfig: 2345 99 10 # description: Set Public IP from ElasticIP pool during instance startup # processname: ipassign # Provides: ipassign # Required-Start: -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -150,7 +150,7 @@ stop() { exit 6 } status() { check_ec2metadata valid_ip -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -54,6 +54,7 @@ check_ec2metadata() { else # Checking ec2metadata command ec2metadata >/dev/null 2>&1 if [ $? -eq 0 ] then # ec2metadata command found -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -45,6 +45,7 @@ valid_ip() { check_ec2metadata() { ec2-metadata >/dev/null 2>&1 if [ $? -eq 0 ] then # Amazon Linux ec2-metadata command found -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,7 @@ # Default-Stop: # Short-Description: Set Public IP from ElasticIP pool during instance startup # # # Developed by: Javier Sianes - [email protected] # -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,10 @@ # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: # Short-Description: Set Public IP from ElasticIP pool during instance startup # # Developed by: Javier Sianes - [email protected] # OUTPUT="text" REGION="eu-west-1" -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -105,7 +105,7 @@ start() { if [ "${IP}" = "" ] then # Public IP on Elastic pool unavailable LOG="ERROR: Free Public IP inside Elastic pool in ${REGION} region unavailable"; write_log exit 5 else # Attach Public IP from Elastic pool -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -81,8 +81,8 @@ start() { if [ ${VALIDIP} -eq 0 ] then # No Public IP available. So, AWS CLI unavailable LOG="ERROR: There is no Public IP address attached to ${EC2INSTANCE} instance. Unable to use AWS CLI."; write_log exit 3 fi -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: # Short-Description: Set Public IP from ElasticIP pool during instance start-up OUTPUT="text" REGION="eu-west-1" -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -159,10 +159,10 @@ status () { if [ ${IPPRESENTINPOOL} -eq 1 ] then echo "Public IP ${EC2PUBLICIP} attached to ${EC2INSTANCE} instance in ${REGION} region belongs to ElasticIP pool" exit 0 else echo "Public IP ${EC2PUBLICIP} attached to ${EC2INSTANCE} instance in ${REGION} region is a generic public IP NOT present in ElasticIP pool" exit 2 fi } -
jsianes revised this gist
Jun 29, 2014 . 1 changed file with 19 additions and 20 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -55,7 +55,7 @@ check_ec2metadata() { EC2INSTANCE=`ec2metadata --instance-id` EC2PUBLICIP=`ec2metadata --public-ipv4` else # ec2metadata/ec2-metadata command not found LOG="ERROR: No ec2metadata or ec2-metadata command found"; write_log exit 127 fi @@ -67,46 +67,45 @@ check_awscli() { if [ $? -ne 0 ] then # Incorrect instance role or invalid AWS CLI configuration LOG="ERROR: AWS CLI command not available. Check instance role or AWS CLI installation/configuration"; write_log exit 128 fi } start() { check_ec2metadata # Check Public IP valid_ip if [ ${VALIDIP} -eq 0 ] then # No Public IP available. Can't use AWS CLI LOG="ERROR: There is no Public IP address assigned to ${EC2INSTANCE} instance. Unable to use AWS CLI."; write_log exit 3 fi # Check if AWS CLI is available check_awscli # Check if instance has a public IP from Elastic pool assigned PUBLICIPASSIGNED=`aws ec2 describe-addresses --output ${OUTPUT} --region ${REGION} | grep ${EC2INSTANCE} | wc -l` if [ ${PUBLICIPASSIGNED} -gt 0 ] then # Instance has (at least) one Pulic IP associated from Elastic pool IP=`aws ec2 describe-addresses --output ${OUTPUT} --region ${REGION} | grep ${EC2INSTANCE} | head -1 | awk '{ print $NF; }'` LOG="Instance ${EC2INSTANCE} has (at least) one Public IP address from Elastic pool in ${REGION} region (${IP})"; write_log exit 4 else # Get IP address from Elastic pool IP=`aws ec2 describe-addresses --output ${OUTPUT} --region ${REGION} | grep -v 'i-' | head -1 | awk '{ print $NF; }'` if [ "${IP}" = "" ] then # Public IP on Elastic pool unavailable LOG="ERROR: Public IP from Elastic pool in ${REGION} region unavailable"; write_log exit 5 else # Attach Public IP from Elastic pool @@ -116,9 +115,9 @@ start() { if [ ${EXITCODE} -eq 0 ] then LOG="${IP} Public IP from Elastic pool assigned to ${EC2INSTANCE} instance"; write_log else LOG="ERROR: (${EXITCODE}) Unable to attach ${IP} Public IP from Elastic pool to ${EC2INSTANCE} instance"; write_log BOOL=1 fi @@ -130,9 +129,9 @@ start() { if [ ${EXITCODE} -eq 0 ] || [ ${EXITCODE} -eq 255 ] then LOG="Default public IP ${EC2PUBLICIP} outside Elastic pool detached from ${EC2INSTANCE} instance"; write_log else LOG="ERROR: (${EXITCODE}) Unable to detach default public IP ${EC2PUBLICIP} from ${EC2INSTANCE} instance"; write_log BOOL=2 fi fi @@ -151,7 +150,7 @@ status () { if [ ${VALIDIP} -eq 0 ] then echo "No Public IP address attached to ${EC2INSTANCE} instance." exit 1 fi @@ -160,10 +159,10 @@ status () { if [ ${IPPRESENTINPOOL} -eq 1 ] then echo "Public IP ${EC2PUBLICIP} attached to ${EC2INSTANCE} instance belongs ElasticIP pool" exit 0 else echo "Public IP ${EC2PUBLICIP} attached to ${EC2INSTANCE} instance is a generic public IP NOT present in ElasticIP pool" exit 2 fi } -
jsianes revised this gist
Jun 28, 2014 . 1 changed file with 23 additions and 27 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -23,6 +23,11 @@ usage() { exit 255 } write_log() { timestamp echo "${TIMESTAMP} - ${LOG}" >> ${IPLOGFILE} } valid_ip() { RX='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])' @@ -42,17 +47,16 @@ check_ec2metadata() { EC2INSTANCE=`ec2-metadata -i | awk '{ print $NF; }'` EC2PUBLICIP=`ec2-metadata -v | awk '{ print $NF; }'` else # Checking ec2metadata command ec2metadata >/dev/null 2>&1 if [ $? -eq 0 ] then # ec2metadata command found EC2INSTANCE=`ec2metadata --instance-id` EC2PUBLICIP=`ec2metadata --public-ipv4` else # No ec2metadata command found LOG="ERROR: No ec2metadata or ec2-metadata command found"; write_log exit 127 fi fi @@ -63,15 +67,14 @@ check_awscli() { if [ $? -ne 0 ] then # Incorrect instance role or no valid AWS CLI configuration found LOG="ERROR: AWS CLI commands not available. Check AWS CLI installation or configuration"; write_log exit 128 fi } start() { # EC2 metadata: public IP and Instance ID check_ec2metadata # Checking if any Public IP is assigned @@ -80,63 +83,56 @@ start() { if [ ${VALIDIP} -eq 0 ] then # No Public IP available. Can't execute AWS CLI command LOG="ERROR: There is no Public IP address assigned. Can't run AWS CLI commands"; write_log exit 3 fi # Check if AWS CLI commands are available check_awscli # Checking if instance has a public IP from Elastic pool assigned PUBLICIPASSIGNED=`aws ec2 describe-addresses --output ${OUTPUT} --region ${REGION} | grep ${EC2INSTANCE} | wc -l` if [ ${PUBLICIPASSIGNED} -gt 0 ] then # Instance has at least one Pulic IP associated from Elastic pool IP=`aws ec2 describe-addresses --output ${OUTPUT} --region ${REGION} | grep ${EC2INSTANCE} | head -1 | awk '{ print $NF; }'` LOG="Instance ${EC2INSTANCE} has a Public IP address from Elastic pool in ${REGION} region (${IP})"; write_log exit 4 else # Get IP address from Elastic pool IP=`aws ec2 describe-addresses --output ${OUTPUT} --region ${REGION} | grep -v 'i-' | head -1 | awk '{ print $NF; }'` if [ "${IP}" = "" ] then # No unassigned Public IP on pool LOG="ERROR: No free Public IP from Elastic pool in ${REGION} region available"; write_log exit 5 else # Attach Public IP from Elastic pool BOOL=0 aws ec2 associate-address --output ${OUTPUT} --region ${REGION} --instance-id ${EC2INSTANCE} --public-ip ${IP} >/dev/null 2>&1 EXITCODE=$? if [ ${EXITCODE} -eq 0 ] then LOG="${IP} Public IP from pool assigned to ${EC2INSTANCE} instance"; write_log else LOG="ERROR: (${EXITCODE}) Unable to associate ${IP} Public IP from Elastic pool to ${EC2INSTANCE} instance"; write_log BOOL=1 fi if [ ${BOOL} -eq 0 ] then # Detaching default Public IP aws ec2 disassociate-address --output ${OUTPUT} --region ${REGION} --instance-id ${EC2INSTANCE} --public-ip ${EC2PUBLICIP} >/dev/null 2>&1 EXITCODE=$? if [ ${EXITCODE} -eq 0 ] || [ ${EXITCODE} -eq 255 ] then LOG="Default public IP ${EC2PUBLICIP} not from pool disassociated in ${EC2INSTANCE} instance"; write_log else LOG="ERROR: (${EXITCODE}) Unable to disassociate default public IP ${EC2PUBLICIP} in ${EC2INSTANCE} instance"; write_log BOOL=2 fi fi -
jsianes created this gist
Jun 28, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,188 @@ #!/bin/bash # chkconfig: 2345 99 10 # description: Auto assigns free EIP # processname: ipassign # Provides: ipassign # Required-Start: # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: # Short-Description: Set Public IP from ElasticIP pool during start-up OUTPUT="text" REGION="eu-west-1" IPLOGFILE="/var/log/ipassign.log" export PATH=/usr/share/pear:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin timestamp() { TIMESTAMP=`date -u +"%Y-%m-%dT%H:%M:%S.000Z"` } usage() { echo "$0 {start|stop|status}" exit 255 } valid_ip() { RX='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])' if [[ ${EC2PUBLICIP} =~ ^${RX}\.${RX}\.${RX}\.${RX}$ ]] then VALIDIP=1 else VALIDIP=0 fi } check_ec2metadata() { ec2-metadata >/dev/null 2>&1 if [ $? -eq 0 ] then # Amazon Linux ec2-metadata command found EC2INSTANCE=`ec2-metadata -i | awk '{ print $NF; }'` EC2PUBLICIP=`ec2-metadata -v | awk '{ print $NF; }'` else # Checking for ec2metadata command ec2metadata >/dev/null 2>&1 if [ $? -eq 0 ] then # ec2metadata command found EC2INSTANCE=`ec2metadata --instance-id` EC2PUBLICIP=`ec2metadata --public-ipv4` else # Error, no ec2metadata command found timestamp echo "${TIMESTAMP} - ERROR: No ec2metadata or ec2-metadata command found." >> ${IPLOGFILE} exit 127 fi fi } check_awscli() { aws ec2 describe-addresses >/dev/null 2>&1 if [ $? -ne 0 ] then # Error, no valid aws cli configuration timestamp echo "${TIMESTAMP} - ERROR: AWS CLI commands not available. Check AWS CLI installation or configuration." >> ${IPLOGFILE} exit 128 fi } start() { # Obtaining EC2 metadata: public IP and Instance ID check_ec2metadata # Checking if any Public IP is assigned valid_ip if [ ${VALIDIP} -eq 0 ] then # No Public IP available. Can't execute AWS CLI command timestamp echo "${TIMESTAMP} - ERROR: There is no Public IP address assigned. Can't run AWS CLI commands" >> ${IPLOGFILE} exit 3 fi # Check if AWS CLI commands are available check_awscli # Check if instance has a public IP from Elasticip pool assigned PUBLICIPASSIGNED=`aws ec2 describe-addresses --output ${OUTPUT} --region ${REGION} | grep ${EC2INSTANCE} | wc -l` if [ ${PUBLICIPASSIGNED} -gt 0 ] then # Instance has at least one Pulic IP associated from pool. So, nothing to do IP=`aws ec2 describe-addresses --output ${OUTPUT} --region ${REGION} | grep ${EC2INSTANCE} | head -1 | awk '{ print $NF; }'` timestamp echo "${TIMESTAMP} - Instance ${EC2INSTANCE} has a Public IP ${IP} address from pool associated. So, nothing required" >> ${IPLOGFILE} exit 4 else # Get a IP address from pool IP=`aws ec2 describe-addresses --output ${OUTPUT} --region ${REGION} | grep -v 'i-' | head -1 | awk '{ print $NF; }'` if [ "${IP}" = "" ] then # No unassigned Public IP on pool timestamp echo "${TIMESTAMP} - ERROR: can't find a free Public IP in the pool" >> ${IPLOGFILE} exit 5 else # Allocate Public IP from pool BOOL=0 aws ec2 associate-address --output ${OUTPUT} --region ${REGION} --instance-id ${EC2INSTANCE} --public-ip ${IP} >/dev/null 2>&1 EXITCODE=$? if [ ${EXITCODE} -eq 0 ] then timestamp echo "${TIMESTAMP} - ${IP} Public IP from pool assigned to ${EC2INSTANCE} instance" >> ${IPLOGFILE} else timestamp echo "${TIMESTAMP} - ERROR: (${EXITCODE}) Unable to associate ${IP} Public IP from pool to ${EC2INSTANCE} instance" >> ${IPLOGFILE} BOOL=1 fi if [ ${BOOL} -eq 0 ] then # Disassociating default Public IP aws ec2 disassociate-address --output ${OUTPUT} --region ${REGION} --instance-id ${EC2INSTANCE} --public-ip ${EC2PUBLICIP} >/dev/null 2>&1 EXITCODE=$? if [ ${EXITCODE} -eq 0 ] || [ ${EXITCODE} -eq 255 ] then timestamp echo "${TIMESTAMP} - Default public IP ${EC2PUBLICIP} not from pool disassociated in ${EC2INSTANCE} instance" >> ${IPLOGFILE} else timestamp echo "${TIMESTAMP} - ERROR: (${EXITCODE}) Unable to disassociate default public IP ${EC2PUBLICIP} in ${EC2INSTANCE} instance" >> ${IPLOGFILE} BOOL=2 fi fi exit ${BOOL} fi fi } stop() { exit 6 } status () { check_ec2metadata valid_ip if [ ${VALIDIP} -eq 0 ] then echo "No Public IP address assigned to ${EC2INSTANCE} instance." exit 1 fi check_awscli IPPRESENTINPOOL=`aws ec2 describe-addresses --output ${OUTPUT} --region ${REGION} | grep "${EC2INSTANCE}" | grep ${EC2PUBLICIP} | wc -l` if [ ${IPPRESENTINPOOL} -eq 1 ] then echo "Public IP ${EC2PUBLICIP} assigned to ${EC2INSTANCE} instance is part of the ElasticIP pool" exit 0 else echo "Public IP ${EC2PUBLICIP} assigned to ${EC2INSTANCE} instance is a generic public IP NOT present in ElasticIP pool" exit 2 fi } case "$1" in start) start ;; stop) stop ;; status) status ;; *) usage ;; esac