-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdown.sh
More file actions
executable file
Β·134 lines (105 loc) Β· 3.64 KB
/
down.sh
File metadata and controls
executable file
Β·134 lines (105 loc) Β· 3.64 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
#BASE=$HOME
BASE=/home/pi
# Fetch the instance ID based on balloonName, then terminate the EC2 instance if found.
# If the instance does not exist, the function exits early.
fetchInstanceIdAndTerminateEc2() {
local balloonName=$1
instanceId=$(getValueByAttribute "$balloonName" instanceId)
if [ "$instanceId" = "null" ]; then
echo "$balloonName does not exist"
exit 0
fi
echo $instanceId
aws ec2 terminate-instances --instance-ids $instanceId
echo "EC2 instance terminated"
}
# Store TCP/UDP port information for a group, then attempt to delete the security group.
# If a dependency prevents deletion, it prints a dependency violation message.
storePortAndDeleteSecurityGroup() {
local groupName=$1
local balloonName=$2
storePortArrayString "$groupName" tcp "$balloonName"
storePortArrayString "$groupName" udp "$balloonName"
echo "$groupName"
output=$(aws ec2 delete-security-group --group-name "$groupName" 2>&1)
if [[ $? -eq 0 ]]; then
echo "Security group '$groupName' successfully deleted."
elif [[ $output == *"DependencyViolation"* ]]; then
echo "Dependency violation. Security group could not be deleted."
else
echo "An error occurred: $output"
fi
}
# Delete an EC2 key pair by its name.
deleteEc2KeyPair() {
local keyName=$1
echo "$keyName"
aws ec2 delete-key-pair --key-name "$keyName"
echo "EC2 key pair deleted"
}
# Store TCP/UDP port information for a group, sleep for a given duration, and then attempt
# to delete the security group. If a dependency violation occurs, it retries after sleeping.
storePortAndDeleteSecurityGroupWithSleepAndRetry() {
local groupName=$1
local balloonName=$2
local sleepDuration=$3 # Third argument for sleep duration
storePortArrayString "$groupName" tcp "$balloonName"
storePortArrayString "$groupName" udp "$balloonName"
echo "Sleeping for $sleepDuration seconds before attempting to delete security group..."
sleep "$sleepDuration"
echo "$groupName"
while true; do
output=$(aws ec2 delete-security-group --group-name "$groupName" 2>&1)
if [[ $? -eq 0 ]]; then
echo "Security group '$groupName' successfully deleted."
break
elif [[ $output == *"DependencyViolation"* ]]; then
echo "Dependency violation. Retrying in $sleepDuration seconds..."
sleep "$sleepDuration"
else
echo "An error occurred: $output"
break
fi
done
}
treehousesConfigHas() {
local keyName=$1
local groupName=$2
if [ "$keyName" == "null" ] || [ "$groupName" == "null" ]; then
return 1
fi
return 0
}
function down() {
balloonName=$(setBalloonName "$1")
keyName=$(getValueByAttribute "$balloonName" key)
groupName=$(getValueByAttribute "$balloonName" groupName)
treehousesConfigHas "$keyName" "$groupName"
checkResult=$?
if [ $checkResult -eq 1 ]; then
echo "Treehouses config is corrupted, manual cleanup required"
exit 1
fi
detectIncompleteState "$balloonName" "$groupName" "$keyName"
binaryState=$?
if instanceExists "$binaryState" && securityGroupExists "$binaryState"; then
fetchInstanceIdAndTerminateEc2 "$balloonName"
storePortAndDeleteSecurityGroupWithSleepAndRetry "$groupName" "$balloonName" 30
else
if instanceExists $binaryState; then
fetchInstanceIdAndTerminateEc2 "$balloonName"
fi
if securityGroupExists $binaryState; then
storePortAndDeleteSecurityGroup "$groupName" "$balloonName"
fi
fi
if keyPairExists $binaryState; then
deleteEc2KeyPair "$keyName"
fi
updateSshtunnelConfig "$balloonName"
treehouses sshtunnel remove all
echo "remove all sshtunnel"
deleteSshConfig "$balloonName"
deleteObsoleteKeyValue "$balloonName"
}