-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathawsstop
executable file
·55 lines (44 loc) · 1.34 KB
/
awsstop
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
#!/bin/bash
#
# Given an aws instance name or other identifying information from
# format_aws_json.js output, stop it
#
# Depends on:
# 1) cache_run executable
# 2) ec2list executable
# 3) awscli python package and "aws" executable
# how long to cache aws info
CACHE_SECONDS=300
instance_name="$1"
if [ ! "$instance_name" ]
then
echo "usage: $0 <instance_name_or_info>"
exit 2
fi
header=$(cache_run $CACHE_SECONDS "ec2list" | head -1)
data=$(cache_run $CACHE_SECONDS "ec2list $instance_name" | tail -1)
# account for weird lost data
#
if [ \( ! "$data" \) -o "$(echo \"$data\" | grep running,\*$)" -o "$data" = "$header" ]
then
echo "retrying to poll aws data"
header=$(cache_run 0 "ec2list $instance_name" | head -1)
data=$(cache_run $CACHE_SECONDS "ec2list $instance_name" | 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 ($data)"
exit 3
fi
if [ "$state" != "running" ]
then
echo "Invalid state to stop instance from: $state"
exit 4
fi
echo aws ec2 stop-instances --instance-id $id
aws ec2 stop-instances --instance-id $id