Last active
December 12, 2015 02:28
-
-
Save chrisramsay/4699223 to your computer and use it in GitHub Desktop.
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/sh | |
# set some vars/env vars up | |
HFILE=/etc/hosts | |
HNAME_S=`/bin/hostname -s` | |
HNAME_F=`/bin/hostname -f` | |
TDATE=`/bin/date '+%Y-%m-%d %H:%M:%S'` | |
export JAVA_HOME=/usr/java/default | |
export EC2_PRIVATE_KEY=/tmp/.ec2/your-key.pem | |
export EC2_CERT=/tmp/.ec2/your-cert.pem | |
export EC2_HOME=/root/ec2/ | |
# optional, depending on your location (default is us-east) | |
export EC2_URL=http://eu-west-1.ec2.amazonaws.com | |
PATH=$PATH:$HOME/bin:$EC2_HOME/bin | |
MAX_TRIES=60 | |
# start/stop functions for OS | |
start() { | |
# get ip address from api | |
ADD=`/usr/bin/curl http://169.254.169.254/latest/meta-data/local-ipv4 2> /dev/null` | |
CTR=0 | |
/usr/bin/printf 'Getting local ip address.\n'; | |
# now check var for not being dotted quad format | |
while [[ ! '$ADD' =~ ^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}$ ]]; do | |
/bin/sleep 1 | |
CTR=`expr $CTR + 1` | |
# if CTR has reached MAX_TRIES then bin out | |
if [ $CTR -eq $MAX_TRIES ]; then | |
/usr/bin/printf 'WARNING: Cannot write $ADD to $HFILE -- Giving up after $MAX_TRIES attempts\n' | |
exit 1 | |
fi | |
done | |
# if we get here $ADD is ok, so add required values to hosts file | |
/usr/bin/printf '# File written by /etc/init.d/getipadd at: $TDATE\n' > $HFILE | |
/usr/bin/printf '127.0.0.1\tlocalhost.localdomain\tlocalhost\n' >> $HFILE | |
/usr/bin/printf '$ADD\t$HNAME_F\t$HNAME_S\n' >> $HFILE | |
} | |
stop() { | |
# set host file to original value | |
/usr/bin/printf '# File written by /etc/init.d/getipadd\n at: $TDATE' > $HFILE | |
/usr/bin/printf '127.0.0.1\tlocalhost.localdomain\tlocalhost\n' >> $HFILE | |
} | |
case '$1' in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
restart) | |
stop | |
sleep 5 | |
start | |
;; | |
*) | |
echo 'Usage: $0 {start|stop|restart}' | |
exit 1 | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment