|
| 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 $? |
0 commit comments