-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2-management.sh
executable file
·54 lines (48 loc) · 1.47 KB
/
ec2-management.sh
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
#!/usr/bin/env bash
# Start instances for project $1
start-ec2(){
PROJECT=$1
INSTANCES=$(instance-discover off)
if [[ $INSTANCES == "" ]]; then
echo "No instances are currently stopped for project $PROJECT"
else
echo "Starting Amazon instances for project $PROJECT..."
ec2-start-instances $INSTANCES >/dev/null
echo -e "The following instances have been started:"
instance-names $INSTANCES
fi
}
# Stop instances for project $1
stop-ec2(){
PROJECT=$1
INSTANCES=$(instance-discover on)
if [[ $INSTANCES == "" ]]; then
echo "No instances are currently running for project $PROJECT"
else
echo "Stopping Amazon instances for project $PROJECT..."
ec2-stop-instances $INSTANCES >/dev/null
echo -e "The following instances have been stopped:"
instance-names $INSTANCES
fi
}
# Find all instances or those in a state of off/on
instance-discover(){
case $1 in
"off") ARGS='-F "instance-state-name=stopped"';;
"on") ARGS='-F "instance-state-name=running,instance-state-name=pending"';;
*)
esac
ec2-describe-instances $ARGS -F "tag:Project=$PROJECT" | grep INSTANCE | cut -f 2
}
# Find and display names for Amazon instances
instance-names(){
for INSTANCE in $@; do
NAME=$(ec2-describe-tags -F "key=Name" -F "resource-type=instance" -F "resource-id=$INSTANCE" | cut -f 5)
echo "[$INSTANCE] $NAME"
done
}
case $1 in
"off") stop-ec2 $2 ;;
"on") start-ec2 $2 ;;
*) echo "Options are off/on"; exit 1 ;;
esac