diff --git a/src/aws/dependencies/detectIncompleteState.sh b/src/aws/dependencies/detectIncompleteState.sh index dcc2e37..99eacbe 100644 --- a/src/aws/dependencies/detectIncompleteState.sh +++ b/src/aws/dependencies/detectIncompleteState.sh @@ -64,6 +64,21 @@ checkKeyPairExists() { fi } +instanceExists() { + local binaryState=$1 + ! ((binaryState & 4)) +} + +securityGroupExists() { + local binaryState=$1 + ! ((binaryState & 2)) +} + +keyPairExists() { + local binaryState=$1 + ! ((binaryState & 1)) +} + # Parent function to detect the state of the instance, security group, and key pair # Accepts three arguments: # $1: instanceName (for describeInstanceByName) @@ -101,50 +116,20 @@ detectIncompleteState() { # Bit 0 -> keyPairExists local binaryState=$(((instanceExists << 2) | (securityGroupExists << 1) | keyPairExists)) - # Output the binary value (as a decimal number) - echo "Binary state (in decimal): $binaryState" - - # Return the binary state (as an integer) + echo "Binary state (in binary): $(echo "obase=2; $binaryState" | bc)" return $binaryState } -# Example usage: -# To use the parent function `detectIncompleteState`, provide three arguments: -# 1. The name of the instance to check -# 2. The name of the security group to check -# 3. The name of the key pair to check -# -# Example: -# detectIncompleteState "luftballon" "luftballons-sg" "luftballon" detectIncompleteState "luftballon" "luftballons-sg" "luftballon" binaryState=$? -# Output the binary state in binary format for easier understanding -echo "Binary state (in binary): $(echo "obase=2; $binaryState" | bc)" - -# Example: Interpret the binary state -if [[ $binaryState -eq 0 ]]; then - echo "All resources exist." -else - # Analyze each bit - if ((binaryState & 4)); then - echo "Instance does not exist." - fi - if ((binaryState & 2)); then - echo "Security Group does not exist." - fi - if ((binaryState & 1)); then - echo "Key Pair does not exist." - fi +# Analyze each bit +if instanceExists $binaryState; then + echo "Instance exists." +fi +if securityGroupExists $binaryState; then + echo "Security Group exists." +fi +if keyPairExists $binaryState; then + echo "Key Pair exists." fi - -# Example usage: -# To use the parent function `detectIncompleteState`, provide three arguments: -# 1. The name of the instance to check -# 2. The name of the security group to check -# 3. The name of the key pair to check -# -# Example: -# detectIncompleteState "my-instance-name" "my-security-group-name" "my-key-pair-name" -detectIncompleteState "luftballon" "luftballons-sg" "luftballon" -echo $? diff --git a/src/aws/down.sh b/src/aws/down.sh index 25a53d0..f214a92 100755 --- a/src/aws/down.sh +++ b/src/aws/down.sh @@ -3,58 +3,113 @@ #BASE=$HOME BASE=/home/pi -function down() { - - balloonName=$(setBalloonName "$1") - keyName=$(getValueByAttribute $balloonName key) - groupName=$(getValueByAttribute $balloonName groupName) +# 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 - detectIncompleteState "luftballon" "luftballons-sg" "luftballon" - - if ! isBalloonNameValid "$balloonName"; then - echo "Please provide a valid balloon name" - exit 1 - fi - - instanceId=$(getValueByAttribute $balloonName instanceId) + instanceId=$(getValueByAttribute "$balloonName" instanceId) if [ "$instanceId" = "null" ]; then echo "$balloonName does not exist" exit 0 fi - storePortArrayString $groupName tcp $balloonName - storePortArrayString $groupName udp $balloonName - updateSshtunnelConfig $balloonName - echo $instanceId aws ec2 terminate-instances --instance-ids $instanceId - echo "ec2 instance delete" + echo "EC2 instance terminated" +} - echo $keyName - aws ec2 delete-key-pair --key-name $keyName - echo "security key delete" +# 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 - treehouses sshtunnel remove all - echo "remove all sshtunnel" + 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" - sleep 30 - 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 5 seconds..." - sleep 10 + echo "Dependency violation. Retrying in $sleepDuration seconds..." + sleep "$sleepDuration" else echo "An error occurred: $output" break fi done +} + +function down() { + + balloonName=$(setBalloonName "$1") + keyName=$(getValueByAttribute "$balloonName" key) + groupName=$(getValueByAttribute "$balloonName" groupName) + + detectIncompleteState "$balloonName" "$groupName" "$groupName" + 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 + deleteSshConfig "$balloonName" + deleteObsoleteKeyValue "$balloonName" }