-
Notifications
You must be signed in to change notification settings - Fork 72
/
spot
46 lines (37 loc) · 1.47 KB
/
spot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh
# script that runs on the permanent control host to reclaim IP address when spot instance dies.
# This is really just a backstop in case the spot instance doesn't voluntarily reassign it before dying.
if [ -f /etc/ec2spotter ]
then
# safety check - I'm not the control, I'm the spot instance!
exit 0
fi
PATH=/usr/local/bin/:$PATH
cd $(dirname $0)
CONF=$(hostname -s).conf
. ./$CONF || exit 1
#
# First check if the IP address needs to be reclaimed. This could happen if the spot instance had a sudden failure.
#
ipOwner=$(aws ec2 describe-addresses --allocation-ids $ec2spotter_elastic_ip | jq -r '.Addresses[0].InstanceId') &&
self=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) &&
if [ "$ipOwner" = "null" ] || [ "$ipOwner" = "" ]
then
# reclaim the public IP address
aws ec2 associate-address --instance-id $self --allocation-id $ec2spotter_elastic_ip --allow-reassociation
echo "Elastic IP $ec2spotter_elastic_ip reclaimed to instance $self"
fi
#
# Next check if the spot instance needs to be relaunched.
#
if [ -f spot.tmp ]
then
spotId=$(jq -r '.SpotInstanceRequests[0].SpotInstanceRequestId' < spot.tmp)
state=$(aws ec2 describe-spot-instance-requests --spot-instance-request-ids $spotId | jq -r '.SpotInstanceRequests[0].State')
if [ ! "$state" = "active" ] && [ ! "$state" = "open" ]
then
echo self=$self oldSpotId=$spotId state=$state $(hostname)
mv -f spot.tmp spot.tmp-old # remove to avoid weird loops and race conditions
./relaunch $CONF
fi
fi