Skip to content

Commit d438d2d

Browse files
authored
Merge branch 'main' into address-ghost-instance
2 parents 38aa70f + ffe2bab commit d438d2d

File tree

3 files changed

+139
-85
lines changed

3 files changed

+139
-85
lines changed
Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Filters instances from JSON data based on the tag "Class" with a value of "treehouses".
32
# Expects JSON data in the format returned by the AWS CLI command 'describe-instances'.
43
# Uses 'jq' to parse and filter the data.
@@ -9,43 +8,90 @@ function filterInstancesByTag {
98
echo "$jsonData" | jq '[.Reservations[].Instances[] | select(.Tags[]? | .Key=="Class" and .Value=="treehouses")]'
109
}
1110

12-
function waitForOutput(){
13-
local cmd=$1
14-
local result=$(eval $cmd)
15-
while [ -z "$result" ] || [ "$result" == "null" ]
16-
do
17-
sleep 5
18-
result=$(eval $cmd)
19-
done
20-
echo $result
11+
function waitForOutput() {
12+
local cmd=$1
13+
local result=$(eval $cmd)
14+
while [ -z "$result" ] || [ "$result" == "null" ]; do
15+
sleep 5
16+
result=$(eval $cmd)
17+
done
18+
echo $result
2119
}
2220

2321
setBalloonName() {
24-
if [ -z "$1" ]; then
25-
echo "luftballon"
26-
else
27-
echo "$1"
28-
fi
22+
if [ -z "$1" ]; then
23+
echo "luftballon"
24+
else
25+
echo "$1"
26+
fi
2927
}
3028

3129
function makePortArray {
32-
local portString="$1"
33-
local -a portArray
30+
local portString="$1"
31+
local -a portArray
3432

35-
IFS=',' read -ra pairs <<< "$portString"
33+
IFS=',' read -ra pairs <<<"$portString"
3634

37-
for pair in "${pairs[@]}"; do
38-
IFS=':' read -ra ports <<< "$pair"
39-
for port in "${ports[@]}"; do
40-
portArray+=("$port")
41-
done
35+
for pair in "${pairs[@]}"; do
36+
IFS=':' read -ra ports <<<"$pair"
37+
for port in "${ports[@]}"; do
38+
portArray+=("$port")
4239
done
40+
done
41+
42+
echo "${portArray[@]}"
43+
}
4344

44-
echo "${portArray[@]}"
45+
function getState() {
46+
local instanceId=$1
47+
local state=$(aws ec2 describe-instances --instance-ids $instanceId | jq '.Reservations[].Instances[].State.Name')
48+
echo $state
4549
}
4650

47-
function getState(){
48-
local instanceId=$1
49-
local state=$(aws ec2 describe-instances --instance-ids $instanceId | jq '.Reservations[].Instances[].State.Name')
50-
echo $state
51+
# Function: waitForConditionalOutput
52+
# Description: Waits for a command to produce a specific result based on the specified mode.
53+
# Parameters:
54+
# $1 - The command to execute
55+
# $2 - The target value to compare against
56+
# $3 - The mode of operation: 'match' or 'different'
57+
# 'match': Waits for the result to match the target value
58+
# 'different': Waits for the result to be different from the target value
59+
# Returns:
60+
# 0 - If the desired condition is met within the max attempts
61+
# 1 - If the desired condition is not met within the max attempts
62+
63+
function waitForConditionalOutput() {
64+
local cmd=$1
65+
local targetValue=$2
66+
local mode=$3
67+
local maxAttempts=3
68+
local attempt=0
69+
70+
# Validate mode
71+
if [[ "$mode" != "match" && "$mode" != "different" ]]; then
72+
echo "Error: Invalid mode. Use 'match' or 'different'."
73+
return 1
74+
fi
75+
76+
while [ $attempt -lt $maxAttempts ]; do
77+
local result=$(eval $cmd)
78+
if [ "$mode" == "match" ]; then
79+
# Wait for the result to match the target value
80+
if [ "$result" == "$targetValue" ]; then
81+
echo $result
82+
return 0
83+
fi
84+
elif [ "$mode" == "different" ]; then
85+
# Wait for the result to be different from the target value
86+
if [ "$result" != "$targetValue" ]; then
87+
echo $result
88+
return 0
89+
fi
90+
fi
91+
attempt=$((attempt + 1))
92+
sleep 10
93+
done
94+
95+
echo "Error: Failed to meet the condition after $maxAttempts attempts."
96+
return 1
5197
}

src/aws/start.sh

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,48 @@
33
#BASE=$HOME
44
BASE=/home/pi
55

6-
function start(){
7-
balloonName=$(setBalloonName "$1")
6+
function start() {
7+
balloonName=$(setBalloonName "$1")
88

9-
if ! isBalloonNameValid "$balloonName"; then
10-
echo "Please provide a valid balloon name"
11-
exit 1
12-
fi
9+
if ! isBalloonNameValid "$balloonName"; then
10+
echo "Please provide a valid balloon name"
11+
exit 1
12+
fi
1313

14-
instanceId=$(getValueByAttribute $balloonName instanceId)
14+
instanceId=$(getValueByAttribute "$balloonName" instanceId)
1515

16-
if [ "$instanceId" = "null" ]; then
17-
echo "$balloonName does not exist"
18-
exit 1
19-
fi
16+
if [ "$instanceId" = "null" ]; then
17+
echo "$balloonName does not exist"
18+
exit 1
19+
fi
2020

21-
oldPublicIp=$(getValueByAttribute $balloonName publicIp)
21+
# oldPublicIp=$(getValueByAttribute "$balloonName" publicIp)
2222

23-
state=$(getState $instanceId)
23+
state=$(waitForConditionalOutput "getState $instanceId" "\"stopping\"" "different")
24+
if [ $? -ne 0 ]; then
25+
echo "Wait for starting on start command until instance is stopped."
26+
exit 1
27+
fi
2428

25-
if [ "$state" == "\"running\"" ]; then
26-
echo "The instance is already running"
27-
exit 0
28-
fi
29+
if [ "$state" == "\"running\"" ]; then
30+
echo "The instance is already running"
31+
exit 0
32+
fi
2933

30-
if [ "$state" == "\"stopping\"" ]; then
31-
echo "The instance is stopping"
32-
exit 1
33-
fi
34+
aws ec2 start-instances --instance-ids "$instanceId"
3435

35-
aws ec2 start-instances --instance-ids $instanceId
36+
echo "get the new ip address. The procedure might take time for a while"
37+
publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
3638

37-
echo "get the new ip address. The procedure might take time for a while"
38-
publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
39+
# portConfigArray=$(getArrayValueAsStringByKey "$instanceName" tcpPortArray)
3940

40-
portConfigArray=$(getArrayValueAsStringByKey $instanceName tcpPortArray)
41+
echo "the new ip address is $publicIp"
42+
updateIPAddress "$balloonName" "$publicIp"
4143

42-
echo "the new ip address is $publicIp"
43-
updateIPAddress $balloonName $publicIp
44+
closeSSHTunnel
45+
echo "remove old ssh tunnel settings"
46+
sleep 5
4447

45-
closeSSHTunnel
46-
echo "remove old ssh tunnel settings"
47-
sleep 5
48-
49-
restartSSHTunnel $balloonName $publicIp
50-
echo "open new ssh tunnel"
48+
restartSSHTunnel "$balloonName" "$publicIp"
49+
echo "open new ssh tunnel"
5150
}

src/aws/up.sh

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ function up {
158158
echo "Success to add ssh key: $importedKeyName"
159159
else
160160
echo "The key pair $keyname already exists. Please use another key name."
161-
importedKeyName=$keyname
162161
fi
163162

164163
if ! checkSecurityGroup; then
@@ -169,44 +168,54 @@ function up {
169168
echo "Security Group already exists."
170169
fi
171170

172-
instanceId=$(checkInstance)
173-
if [ -z "$instanceId" ]; then
174-
instanceState=""
171+
createAndTagInstance() {
175172
instanceId=$(createEc2 | getValueByKeyword InstanceId)
176173
echo "Creating and running EC2 instance..."
177174
echo "Instance id is $instanceId"
175+
178176
aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
179177
aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
178+
180179
publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
181180
echo "Public IP Address is $publicIp"
182181
echo "Will open ssh tunnel soon"
182+
183183
isOpen=$(waitForOutput "ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256")
184184
echo "Opened ssh tunnel"
185+
185186
openSSHTunnel $instanceName $publicIp $portConfigArray
186-
storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupNameaws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
187+
storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupName
188+
}
189+
190+
instanceId=$(checkInstance)
191+
if [ -z "$instanceId" ]; then
192+
createAndTagInstance
193+
else
194+
instanceState=$(waitForConditionalOutput "checkInstanceState $instanceId" "\"stopping\"" "different")
195+
if [ $? -ne 0 ]; then
196+
echo "Wait for starting on start command until instance is stopped."
197+
exit 1
198+
fi
199+
echo "Success to add ssh key: $importedKeyName"
187200
else
188-
instanceState=$(checkInstanceState $instanceId)
189-
if [ "$instanceState" = "running" ]; then
201+
echo "The key pair $keyname already exists. Please use another key name."
202+
importedKeyName=$keyname
203+
fi
204+
205+
case "$instanceState" in
206+
"running")
190207
echo "EC2 instance is already running."
191-
elif [ "$instanceState" = "stopped" ]; then
208+
;;
209+
"stopped")
192210
echo "Starting stopped EC2 instance..."
193211
start $instanceName
194-
elif [ "$instanceState" = "terminated" ]; then
195-
instanceState=""
196-
instanceId=$(createEc2 | getValueByKeyword InstanceId)
197-
echo "Creating and running EC2 instance..."
198-
echo "Instance id is $instanceId"
199-
aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instanceName
200-
aws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
201-
publicIp=$(waitForOutput "getLatestIpAddress $instanceId")
202-
echo "Public IP Address is $publicIp"
203-
echo "Will open ssh tunnel soon"
204-
isOpen=$(waitForOutput "ssh-keyscan -H $publicIp | grep ecdsa-sha2-nistp256")
205-
echo "Opened ssh tunnel"
206-
openSSHTunnel $instanceName $publicIp $portConfigArray
207-
storeConfigIntoTreehousesConfigAsStringfiedJson $instanceName $importedKeyName $instanceId $publicIp $groupNameaws ec2 create-tags --resources $instanceId --tags Key=Class,Value=treehouses
208-
else
212+
;;
213+
"terminated")
214+
createAndTagInstance
215+
;;
216+
*)
209217
echo "EC2 instance is in state: $instanceState."
210-
fi
218+
;;
219+
esac
211220
fi
212221
}

0 commit comments

Comments
 (0)