-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathawsreboot
executable file
·61 lines (49 loc) · 1.36 KB
/
awsreboot
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
#!/bin/bash
#
# Reboot an AWS instance, given an aws ec2 listing pattern match string
#
# Depends on:
# 1) ec2list and clear_cache executables
# 2) awscli python package and "aws" executable
#
set -o errexit
set -o nounset
set -o pipefail
instance_search_string="${1:-}"
if [ ! "$instance_search_string" ]
then
echo "usage: $0 <instance_search_string>"
exit 2
fi
if [ ! "$AWS_SECRET_ACCESS_KEY" -o ! "$AWS_ACCESS_KEY_ID" ]
then
. ~/amazon-build.sh
fi
header=$(ec2list | head -1)
data=$(ec2list $instance_search_string | tail -1)
# account for weird lost data
#
if [ \( ! "$data" \) -o "$(echo \"$data\" | grep running,\*$)" -o "$data" = "$header" ]
then
echo "retrying to poll aws data"
clear_cache
header=$(ec2list | head -1)
data=$(ec2list $instance_search_string | tail -1)
fi
# get column numbers
id_column="$(echo "$header" | tr , \\n | grep -n InstanceId | cut -f1 -d: )"
state_column="$(echo "$header" | tr , \\n | grep -n State.Name | cut -f1 -d: )"
id="$(echo "$data" | cut -f$id_column -d,)"
state="$(echo "$data" | cut -f$state_column -d,)"
if [ ! "$id" -o "$id" = "running" ]
then
echo "Can't find dns info for server (id: $id, data: $data)"
exit 3
fi
if [ "$state" != "running" ]
then
echo "Invalid state to reboot instance from: $state"
exit 4
fi
echo aws ec2 reboot-instances --instance-id $id
aws ec2 reboot-instances --instance-id $id