Skip to content

Commit 749f553

Browse files
committed
### Refactor: Encapsulate code into functions and introduce state handling
- Encapsulate a chunk of code into several functions for better modularity. - Introduce binary state distinction functions. - Execute the newly created functions based on the current state.
1 parent 2e45f55 commit 749f553

File tree

2 files changed

+109
-69
lines changed

2 files changed

+109
-69
lines changed

src/aws/dependencies/detectIncompleteState.sh

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ checkKeyPairExists() {
6464
fi
6565
}
6666

67+
instanceExists() {
68+
local binaryState=$1
69+
! ((binaryState & 4))
70+
}
71+
72+
securityGroupExists() {
73+
local binaryState=$1
74+
! ((binaryState & 2))
75+
}
76+
77+
keyPairExists() {
78+
local binaryState=$1
79+
! ((binaryState & 1))
80+
}
81+
6782
# Parent function to detect the state of the instance, security group, and key pair
6883
# Accepts three arguments:
6984
# $1: instanceName (for describeInstanceByName)
@@ -101,50 +116,20 @@ detectIncompleteState() {
101116
# Bit 0 -> keyPairExists
102117
local binaryState=$(((instanceExists << 2) | (securityGroupExists << 1) | keyPairExists))
103118

104-
# Output the binary value (as a decimal number)
105-
echo "Binary state (in decimal): $binaryState"
106-
107-
# Return the binary state (as an integer)
119+
echo "Binary state (in binary): $(echo "obase=2; $binaryState" | bc)"
108120
return $binaryState
109121
}
110122

111-
# Example usage:
112-
# To use the parent function `detectIncompleteState`, provide three arguments:
113-
# 1. The name of the instance to check
114-
# 2. The name of the security group to check
115-
# 3. The name of the key pair to check
116-
#
117-
# Example:
118-
# detectIncompleteState "luftballon" "luftballons-sg" "luftballon"
119123
detectIncompleteState "luftballon" "luftballons-sg" "luftballon"
120124
binaryState=$?
121125

122-
# Output the binary state in binary format for easier understanding
123-
echo "Binary state (in binary): $(echo "obase=2; $binaryState" | bc)"
124-
125-
# Example: Interpret the binary state
126-
if [[ $binaryState -eq 0 ]]; then
127-
echo "All resources exist."
128-
else
129-
# Analyze each bit
130-
if ((binaryState & 4)); then
131-
echo "Instance does not exist."
132-
fi
133-
if ((binaryState & 2)); then
134-
echo "Security Group does not exist."
135-
fi
136-
if ((binaryState & 1)); then
137-
echo "Key Pair does not exist."
138-
fi
126+
# Analyze each bit
127+
if instanceExists $binaryState; then
128+
echo "Instance exists."
129+
fi
130+
if securityGroupExists $binaryState; then
131+
echo "Security Group exists."
132+
fi
133+
if keyPairExists $binaryState; then
134+
echo "Key Pair exists."
139135
fi
140-
141-
# Example usage:
142-
# To use the parent function `detectIncompleteState`, provide three arguments:
143-
# 1. The name of the instance to check
144-
# 2. The name of the security group to check
145-
# 3. The name of the key pair to check
146-
#
147-
# Example:
148-
# detectIncompleteState "my-instance-name" "my-security-group-name" "my-key-pair-name"
149-
detectIncompleteState "luftballon" "luftballons-sg" "luftballon"
150-
echo $?

src/aws/down.sh

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,113 @@
33
#BASE=$HOME
44
BASE=/home/pi
55

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
1110

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)
2012

2113
if [ "$instanceId" = "null" ]; then
2214
echo "$balloonName does not exist"
2315
exit 0
2416
fi
2517

26-
storePortArrayString $groupName tcp $balloonName
27-
storePortArrayString $groupName udp $balloonName
28-
updateSshtunnelConfig $balloonName
29-
3018
echo $instanceId
3119
aws ec2 terminate-instances --instance-ids $instanceId
32-
echo "ec2 instance delete"
20+
echo "EC2 instance terminated"
21+
}
3322

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
3728

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"
4067

41-
sleep 30
42-
echo $groupName
4368
while true; do
4469
output=$(aws ec2 delete-security-group --group-name "$groupName" 2>&1)
70+
4571
if [[ $? -eq 0 ]]; then
4672
echo "Security group '$groupName' successfully deleted."
4773
break
4874
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"
5177
else
5278
echo "An error occurred: $output"
5379
break
5480
fi
5581
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"
56111

57-
deleteSshConfig $balloonName
58-
deleteObsoleteKeyValue $balloonName
112+
deleteSshConfig "$balloonName"
113+
deleteObsoleteKeyValue "$balloonName"
59114

60115
}

0 commit comments

Comments
 (0)