Skip to content

Commit 38aa70f

Browse files
committed
feat: Add binary representation for resource existence check
- Updated detectIncompleteState function to return a binary value indicating the existence of instance, security group, and key pair. - Binary format: Bit 2 (instance), Bit 1 (security group), Bit 0 (key pair). - Outputs binary state and interprets which resources exist or are missing. - Added logic to handle binary state representation and provide detailed feedback.
1 parent af2fcdf commit 38aa70f

File tree

2 files changed

+160
-10
lines changed

2 files changed

+160
-10
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#!/bin/bash
2+
3+
# Function to describe an EC2 instance by its Name tag
4+
# Accepts one argument: instanceName
5+
describeInstanceByName() {
6+
local instanceName=$1
7+
8+
# Check if instanceName was provided
9+
if [ -z "$instanceName" ]; then
10+
echo "Error: Please provide an instance name."
11+
return 1
12+
fi
13+
14+
# Run the AWS CLI command with the provided instanceName and capture the result in JSON format
15+
local result=$(aws ec2 describe-instances \
16+
--filters "Name=tag:Name,Values=$instanceName" \
17+
--query 'Reservations[*].Instances[*].{Instance:InstanceId,State:State.Name,PublicIP:PublicIpAddress}' \
18+
--output json)
19+
20+
# Check if result is empty, meaning no instances found
21+
if [ -z "$result" ] || [ "$result" = "[]" ]; then
22+
echo "No instances found with Name tag: $instanceName"
23+
return 1
24+
fi
25+
26+
# Use jq to parse and display InstanceId, State, and PublicIP
27+
echo "$result" | jq -r '.[] | .[] | "\nInstanceId: \(.Instance)\nState: \(.State)\nPublicIP: \(.PublicIP // "N/A")\n"'
28+
return 0
29+
}
30+
31+
# Function to check if a security group exists by its name
32+
# Accepts one argument: groupName
33+
checkSecurityGroupExists() {
34+
local groupName=$1
35+
36+
# Describe security groups filtered by group name
37+
local result=$(aws ec2 describe-security-groups --group-names "$groupName" 2>&1)
38+
39+
# Check if the result contains an error (security group not found)
40+
if echo "$result" | grep -q "InvalidGroup.NotFound"; then
41+
echo "Security group '$groupName' does not exist."
42+
return 1
43+
else
44+
echo "Security group '$groupName' exists."
45+
return 0
46+
fi
47+
}
48+
49+
# Function to check if a key pair exists by its name
50+
# Accepts one argument: keyName
51+
checkKeyPairExists() {
52+
local keyName=$1
53+
54+
# Describe key pairs filtered by key name
55+
local result=$(aws ec2 describe-key-pairs --key-name "$keyName" 2>&1)
56+
57+
# Check if the result contains an error (key pair not found)
58+
if echo "$result" | grep -q "InvalidKeyPair.NotFound"; then
59+
echo "Key pair '$keyName' does not exist."
60+
return 1
61+
else
62+
echo "Key pair '$keyName' exists."
63+
return 0
64+
fi
65+
}
66+
67+
# Parent function to detect the state of the instance, security group, and key pair
68+
# Accepts three arguments:
69+
# $1: instanceName (for describeInstanceByName)
70+
# $2: groupName (for checkSecurityGroupExists)
71+
# $3: keyName (for checkKeyPairExists)
72+
# Returns a binary value representing the state:
73+
# - Bit 2 (100): Instance exists
74+
# - Bit 1 (010): Security group exists
75+
# - Bit 0 (001): Key pair exists
76+
detectIncompleteState() {
77+
local instanceName=$1
78+
local groupName=$2
79+
local keyName=$3
80+
81+
# Initialize existence status variables (0 = exists, 1 = doesn't exist)
82+
local keyPairExists=0
83+
local securityGroupExists=0
84+
local instanceExists=0
85+
86+
# Check if the instance exists
87+
describeInstanceByName "$instanceName"
88+
instanceExists=$? # 0 if exists, 1 if doesn't exist
89+
90+
# Check if the security group exists
91+
checkSecurityGroupExists "$groupName"
92+
securityGroupExists=$? # 0 if exists, 1 if doesn't exist
93+
94+
# Check if the key pair exists
95+
checkKeyPairExists "$keyName"
96+
keyPairExists=$? # 0 if exists, 1 if doesn't exist
97+
98+
# Construct a binary value based on the resource existence statuses
99+
# Bit 2 -> instanceExists (shift left by 2 positions)
100+
# Bit 1 -> securityGroupExists (shift left by 1 position)
101+
# Bit 0 -> keyPairExists
102+
local binaryState=$(((instanceExists << 2) | (securityGroupExists << 1) | keyPairExists))
103+
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)
108+
return $binaryState
109+
}
110+
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"
119+
detectIncompleteState "luftballon" "luftballons-sg" "luftballon"
120+
binaryState=$?
121+
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
139+
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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
#BASE=$HOME
44
BASE=/home/pi
55

6-
function down(){
6+
function down() {
77

88
balloonName=$(setBalloonName "$1")
9+
keyName=$(getValueByAttribute $balloonName key)
10+
groupName=$(getValueByAttribute $balloonName groupName)
11+
12+
detectIncompleteState "luftballon" "luftballons-sg" "luftballon"
913

1014
if ! isBalloonNameValid "$balloonName"; then
11-
echo "Please provide a valid balloon name"
12-
exit 1
15+
echo "Please provide a valid balloon name"
16+
exit 1
1317
fi
1418

1519
instanceId=$(getValueByAttribute $balloonName instanceId)
1620

1721
if [ "$instanceId" = "null" ]; then
18-
echo "$balloonName does not exist"
19-
exit 0
22+
echo "$balloonName does not exist"
23+
exit 0
2024
fi
2125

22-
keyName=$(getValueByAttribute $balloonName key)
23-
groupName=$(getValueByAttribute $balloonName groupName)
24-
25-
2626
storePortArrayString $groupName tcp $balloonName
2727
storePortArrayString $groupName udp $balloonName
2828
updateSshtunnelConfig $balloonName
2929

3030
echo $instanceId
31-
aws ec2 terminate-instances --instance-ids $instanceId
31+
aws ec2 terminate-instances --instance-ids $instanceId
3232
echo "ec2 instance delete"
3333

3434
echo $keyName

0 commit comments

Comments
 (0)