|
3 | 3 | #BASE=$HOME |
4 | 4 | BASE=/home/pi |
5 | 5 |
|
6 | | -function down() { |
7 | | - |
8 | | - balloonName=$(setBalloonName "$1") |
9 | | - keyName=$(getValueByAttribute $balloonName key) |
10 | | - groupName=$(getValueByAttribute $balloonName groupName) |
| 6 | +# Fetch the instance ID based on balloonName, then terminate the EC2 instance if found. |
| 7 | +# If the instance does not exist, the function exits early. |
| 8 | +fetchInstanceIdAndTerminateEc2() { |
| 9 | + local balloonName=$1 |
11 | 10 |
|
12 | | - detectIncompleteState "luftballon" "luftballons-sg" "luftballon" |
13 | | - |
14 | | - if ! isBalloonNameValid "$balloonName"; then |
15 | | - echo "Please provide a valid balloon name" |
16 | | - exit 1 |
17 | | - fi |
18 | | - |
19 | | - instanceId=$(getValueByAttribute $balloonName instanceId) |
| 11 | + instanceId=$(getValueByAttribute "$balloonName" instanceId) |
20 | 12 |
|
21 | 13 | if [ "$instanceId" = "null" ]; then |
22 | 14 | echo "$balloonName does not exist" |
23 | 15 | exit 0 |
24 | 16 | fi |
25 | 17 |
|
26 | | - storePortArrayString $groupName tcp $balloonName |
27 | | - storePortArrayString $groupName udp $balloonName |
28 | | - updateSshtunnelConfig $balloonName |
29 | | - |
30 | 18 | echo $instanceId |
31 | 19 | aws ec2 terminate-instances --instance-ids $instanceId |
32 | | - echo "ec2 instance delete" |
| 20 | + echo "EC2 instance terminated" |
| 21 | +} |
33 | 22 |
|
34 | | - echo $keyName |
35 | | - aws ec2 delete-key-pair --key-name $keyName |
36 | | - echo "security key delete" |
| 23 | +# Store TCP/UDP port information for a group, then attempt to delete the security group. |
| 24 | +# If a dependency prevents deletion, it prints a dependency violation message. |
| 25 | +storePortAndDeleteSecurityGroup() { |
| 26 | + local groupName=$1 |
| 27 | + local balloonName=$2 |
37 | 28 |
|
38 | | - treehouses sshtunnel remove all |
39 | | - echo "remove all sshtunnel" |
| 29 | + storePortArrayString "$groupName" tcp "$balloonName" |
| 30 | + storePortArrayString "$groupName" udp "$balloonName" |
| 31 | + |
| 32 | + echo "$groupName" |
| 33 | + output=$(aws ec2 delete-security-group --group-name "$groupName" 2>&1) |
| 34 | + |
| 35 | + if [[ $? -eq 0 ]]; then |
| 36 | + echo "Security group '$groupName' successfully deleted." |
| 37 | + elif [[ $output == *"DependencyViolation"* ]]; then |
| 38 | + echo "Dependency violation. Security group could not be deleted." |
| 39 | + else |
| 40 | + echo "An error occurred: $output" |
| 41 | + fi |
| 42 | +} |
| 43 | + |
| 44 | +# Delete an EC2 key pair by its name. |
| 45 | +deleteEc2KeyPair() { |
| 46 | + local keyName=$1 |
| 47 | + |
| 48 | + echo "$keyName" |
| 49 | + aws ec2 delete-key-pair --key-name "$keyName" |
| 50 | + echo "EC2 key pair deleted" |
| 51 | +} |
| 52 | + |
| 53 | +# Store TCP/UDP port information for a group, sleep for a given duration, and then attempt |
| 54 | +# to delete the security group. If a dependency violation occurs, it retries after sleeping. |
| 55 | +storePortAndDeleteSecurityGroupWithSleepAndRetry() { |
| 56 | + local groupName=$1 |
| 57 | + local balloonName=$2 |
| 58 | + local sleepDuration=$3 # Third argument for sleep duration |
| 59 | + |
| 60 | + storePortArrayString "$groupName" tcp "$balloonName" |
| 61 | + storePortArrayString "$groupName" udp "$balloonName" |
| 62 | + |
| 63 | + echo "Sleeping for $sleepDuration seconds before attempting to delete security group..." |
| 64 | + sleep "$sleepDuration" |
| 65 | + |
| 66 | + echo "$groupName" |
40 | 67 |
|
41 | | - sleep 30 |
42 | | - echo $groupName |
43 | 68 | while true; do |
44 | 69 | output=$(aws ec2 delete-security-group --group-name "$groupName" 2>&1) |
| 70 | + |
45 | 71 | if [[ $? -eq 0 ]]; then |
46 | 72 | echo "Security group '$groupName' successfully deleted." |
47 | 73 | break |
48 | 74 | elif [[ $output == *"DependencyViolation"* ]]; then |
49 | | - echo "Dependency violation. Retrying in 5 seconds..." |
50 | | - sleep 10 |
| 75 | + echo "Dependency violation. Retrying in $sleepDuration seconds..." |
| 76 | + sleep "$sleepDuration" |
51 | 77 | else |
52 | 78 | echo "An error occurred: $output" |
53 | 79 | break |
54 | 80 | fi |
55 | 81 | done |
| 82 | +} |
| 83 | + |
| 84 | +function down() { |
| 85 | + |
| 86 | + balloonName=$(setBalloonName "$1") |
| 87 | + keyName=$(getValueByAttribute "$balloonName" key) |
| 88 | + groupName=$(getValueByAttribute "$balloonName" groupName) |
| 89 | + |
| 90 | + detectIncompleteState "$balloonName" "$groupName" "$groupName" |
| 91 | + binaryState=$? |
| 92 | + |
| 93 | + if instanceExists "$binaryState" && securityGroupExists "$binaryState"; then |
| 94 | + fetchInstanceIdAndTerminateEc2 "$balloonName" |
| 95 | + storePortAndDeleteSecurityGroupWithSleepAndRetry "$groupName" "$balloonName" 30 |
| 96 | + else |
| 97 | + if instanceExists $binaryState; then |
| 98 | + fetchInstanceIdAndTerminateEc2 "$balloonName" |
| 99 | + fi |
| 100 | + if securityGroupExists $binaryState; then |
| 101 | + storePortAndDeleteSecurityGroup "$groupName" "$balloonName" |
| 102 | + fi |
| 103 | + fi |
| 104 | + if keyPairExists $binaryState; then |
| 105 | + deleteEc2KeyPair "$keyName" |
| 106 | + fi |
| 107 | + |
| 108 | + updateSshtunnelConfig "$balloonName" |
| 109 | + treehouses sshtunnel remove all |
| 110 | + echo "remove all sshtunnel" |
56 | 111 |
|
57 | | - deleteSshConfig $balloonName |
58 | | - deleteObsoleteKeyValue $balloonName |
| 112 | + deleteSshConfig "$balloonName" |
| 113 | + deleteObsoleteKeyValue "$balloonName" |
59 | 114 |
|
60 | 115 | } |
0 commit comments