-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.sh
executable file
·97 lines (83 loc) · 2.28 KB
/
stop.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /bin/bash
#
# A little script to stop and clean up artifacts created by the project.
#
# Usage:
# ./start.sh [terminate | clean]
#
# For instance:
# ./start.sh
# ./start.sh terminate
# ./start.sh clean
#
# Author: Kevin S. Clarke <[email protected]>
# URL: http://github.com/ksclarke/packer-aws-grinder
#
function clean_up_ami {
if [ -f $1 ]; then
AMI=`cat ${1}`
if [[ "$1" == *console* ]]; then
AMI_TYPE="Console"
elif [[ "$1" == *agent* ]]; then
AMI_TYPE="Agent"
else
echo "ERROR: Don't recognize the type of the AMI to clean up"
exit 1
fi
if [[ "$AMI" == ami* ]]; then
aws ec2 deregister-image --image-id $AMI
echo ""
echo "$AMI_TYPE AMI (${AMI}) cleaned up"
AMI_SNAP=`aws ec2 describe-snapshots --filters Name=description,Values=*${AMI}* | cut -f 6`
if [[ "$AMI_SNAP" == snap* ]]; then
aws ec2 delete-snapshot --snapshot-id $AMI_SNAP
echo "$AMI_TYPE AMI snapshot (${AMI_SNAP}) cleaned up"
fi
else
echo "Failed to find AMI details for ${1}"
exit 1
fi
fi
}
# Shutdown up the agent instances
echo ""
echo "Shutting down Grinder Agents..."
echo ""
for FILE in `ls ec2-agent-*.instance 2>/dev/null`; do
INSTANCE=`cat $FILE`
RESULT=`aws ec2 stop-instances --instance-ids $INSTANCE 2>&1`
if [[ "$1" == "terminate" || "$1" == "clean" ]]; then
RESULT=`aws ec2 terminate-instances --instance-ids $INSTANCE 2>&1`
rm -f "$FILE"
echo " $INSTANCE stopped and terminated"
else
echo " $INSTANCE stopped"
fi
done
# Shutdown the console instance
echo ""
echo "Shutting down Grinder Console..."
echo ""
if [ -f ec2-console.instance ]; then
INSTANCE=`cat ec2-console.instance`
RESULT=`aws ec2 stop-instances --instance-ids $INSTANCE 2>&1`
echo " $INSTANCE stopped"
fi
# Clean up all the project artifacts
if [ "$1" == "clean" ]; then
if [ -f ec2-console.instance ]; then
RESULT=`aws ec2 terminate-instances --instance-ids $INSTANCE 2>&1`
echo " $INSTANCE terminated"
fi
clean_up_ami "ec2-console.ami"
clean_up_ami "ec2-agent.ami"
rm -f *-build.log
rm -f *.instance
rm -f *.ami
echo ""
echo "All the project's artifacts have been cleaned up"
else
echo ""
echo "Grinder Console instance is now available for the next run"
fi
echo ""