Skip to content

Instantly share code, notes, and snippets.

@bgdnlp
Created November 6, 2016 07:45
Show Gist options
  • Save bgdnlp/9fd326dc4a23f46bab93a1eade023fe4 to your computer and use it in GitHub Desktop.
Save bgdnlp/9fd326dc4a23f46bab93a1eade023fe4 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# efsaz Set availability zone for EFS mounts in fstab
#
# chkconfig: 345 24 74
# description: Replaces the availability zone for EFS mount points in fstab \
# with the proper value for the current instance, which it gets \
# from instance metadata. This script must run before the \
# netfs script, which mounts network filesystems. It is useful \
# in autoscaling groups, where the instance starting might \
# be in a different AZ than the one the image was built from. \
# DEPENDS ON curl BEING INSTALLED ON THE SYSTEM. Adjust if needed.
### BEGIN INIT INFO
# Short-Description: Set AZ for EFS mounts in fstab
# Description: Set AZ for EFS mounts in fstab
### END INIT INFO
# Uncommenting this line along with echo_success and echo_failure lines below
# on RHEL-like Linus distributions offers some pretty printing
#. /etc/init.d/functions
case "$1" in
start)
echo -n "Updating EFS AZ in fstab"
# the region defined here will be replaced with the current AZ
region="eu-west"
efs_server={{ tal_efs_server }}
if [[ $efs_server != *amazonaws.com* ]]; then
# the server name is probably wrong. exit, rather than being sorry
#echo_failure
exit 1
fi
az=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
if [[ $? -ne 0 ]]; then
#echo_failure
exit 2
fi
# 'sed -i' doesn't exist on BSD and it's not safe
fstab_new=`cat /etc/fstab | \
sed s/^${region}-..\.${efs_server}/${az}\.${efs_server}/`
cp /etc/fstab /tmp/fstab.efsaz.bak
echo "$fstab_new" > /etc/fstab
if [[ $? -eq 0 ]]; then
#echo_success
#echo
exit 0
else
#echo_failure
#echo
exit 3
fi
*)
# anything else besides start will do nothing. maybe we could output
# the relevant contents of fstab on status or something, but it's not
# worth the effort
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment