-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-and-associate.sh
More file actions
executable file
·84 lines (76 loc) · 2.34 KB
/
Copy pathrun-and-associate.sh
File metadata and controls
executable file
·84 lines (76 loc) · 2.34 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
# Run an EC2 instance, and associate it with an elastic IP address.
#
# usage:
# run-and-associate.sh -a ami_id -I elastic_ip_address -r region [ -t instance_type ]
# Note that EC2_PRIVATE_KEY and EC2_CERT must be set in your environment.
#
# Be careful that the elastic IP has already been allocated, and that
# it isn't already associated. If that happens, the program will
# still create the instance, not associate it with any elastic IP, and
# terminate with an error.
#
# This script's code is in the public domain.
# Development sponsored by Mobile Web Up ( http://mobilewebup.com )
function exit_usage {
ec=${1:-0}
echo "INFO Usage: run-and-associate.sh -a ami_id -I elastic_ip_address [ -t instance_type ] [ -r region ]"
exit $ec
}
region=$EC2_REGION
while getopts 'I:a:t:r:' arg; do
case $arg in
'a') ami="$OPTARG"
;;
'r') region="$OPTARG"
;;
'I') eip="$OPTARG"
;;
't') itype="$OPTARG"
;;
esac
done
for required in ami region eip; do
if [ -z "${!required}" ]; then
echo "ERROR $required option is required."
exit_usage 1
fi
done
if [ -z "$itype" ]; then
# Can we use minimal-itype.sh ?
which minimal-itype.sh > /dev/null 2>&1
if [ 0 == $? ]; then
itype=$(minimal-itype.sh -r $region -a $ami)
fi
if [ -z "$itype" ]; then
# Something went wrong!
echo "ERROR Cannot automatically determine instance type. Re-run with -t option"
exit_usage 1
fi
fi
instanceid=$(ec2-run-instances --region "$region" -t "$itype" "$ami" | grep '^INSTANCE' | cut -f 2)
# Wait on the instance to get up and running
function get_instance_status {
ec2-describe-instances --region $region $instanceid | grep '^INSTANCE' | cut -f 6
}
check_status=$(get_instance_status)
if [ -z "$check_status" ]; then
# Maybe it's just not visible yet. Wait and try again
sleep 60s
check_status=$(get_instance_status)
if [ -z "$check_status" ]; then
echo "ERROR Instance $instanceid not found!"
exit 2
fi
fi
while [ "$check_status" != "running" ]; do
echo "INFO waiting for $instanceid to reach status 'running'... (latest: '$check_status')"
sleep 60s
check_status=$(get_instance_status)
done
if [ 0 == $? ]; then
ec2-associate-address -i "$instanceid" "$eip"
else
echo "ERROR Unable to verify running of instance. *NOT* associating IP address."
exit 3
fi