Created
September 6, 2016 18:19
-
-
Save mrchristine/08268c731da486ed9790544711b34209 to your computer and use it in GitHub Desktop.
Assign an elastic ip with a bootstrap script.
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 characters
#/bin/bash | |
# Set Params | |
k=YOUR_AWS_KEYS | |
s=YOU_AWS_SECRETE | |
r=YOUR_REGION | |
# Assign EIP ID | |
eip_id=eipalloc-XXXXXXX | |
# Install awscli | |
pip install awscli | |
mkdir ~/.aws | |
echo "[default] | |
aws_access_key_id = $k | |
aws_secret_access_key = $s | |
region = $r" > ~/.aws/config | |
echo "[default] | |
aws_access_key_id = $k | |
aws_secret_access_key = $s" > ~/.aws/credentials | |
cat <<EOT >> /tmp/install_eip.sh | |
#!/bin/bash | |
# Variable to test if driver is online | |
is_driver_online=\$(sudo jps | grep Driver | wc -l) | |
is_worker_online=\$(sudo jps | grep Worker | wc -l) | |
# If driver and worker are both not online, keep waiting | |
echo "Working on EIP" | |
while [ "\$is_driver_online" == "0" -a "\$is_worker_online" == "0" ]; | |
do | |
sleep 30 | |
is_driver_online=\$(sudo jps | grep Driver | wc -l) | |
is_worker_online=\$(sudo jps | grep Worker | wc -l) | |
echo "Driver value: \$is_driver_online" | |
echo "Worker value: \$is_worker_online" | |
echo "Sleeping 30 until Spark driver is online..." | |
done | |
echo | |
# Loop broke so are we on the driver or worker | |
if [ "\$is_driver_online" == "1" ]; then | |
instance=\$(curl -s http://169.254.169.254/latest/meta-data/instance-id) | |
echo "Running on the driver ... attempting to associate EIP ..." | |
aws ec2 associate-address --instance-id \$instance --allocation-id $eip_id | |
echo "Completed EIP assignment!" | |
else | |
echo "Running on worker - noop ..." | |
fi | |
touch /tmp/dbc_eip_completed | |
EOT | |
# Create cleanup script to remove initial script | |
cat <<EOT >> /tmp/cleanup_eip.sh | |
#!/bin/bash | |
is_done="/tmp/dbc_eip_completed" | |
for i in {1..300}; | |
do | |
if [ -e \$is_done ]; then | |
echo "Cleanup the eip script ..." | |
rm -rf /tmp/install_eip.sh | |
exit 0; | |
fi | |
echo "Success file not found, sleep and retry ..." | |
echo \$i | |
sleep 2 | |
done | |
EOT | |
chmod +x /tmp/install_eip.sh | |
chmod +x /tmp/cleanup_eip.sh | |
/tmp/install_eip.sh >> /tmp/install_eip.log 2>&1 & disown | |
/tmp/cleanup_eip.sh >> /tmp/cleanup_eip.log 2>&1 & disown | |
echo | |
echo "Exiting shell. EIP assignment will occur in background task." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment