-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_inventory.sh
65 lines (52 loc) · 1.87 KB
/
write_inventory.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# This is the previous script.
# The problem here is that the delay needed to wait for the instances to be fully provisioned could be innacurate
# Thereby resulting in the inventory file being empty if too soon.
# #!/bin/bash
#
# sleep 80
# # Capture public IPs from Terraform output and write to inventory file
# terraform output -json instance_public_ips | jq -r '.[]' > inventory
# # Check if the output file is populated
# if [ -s inventory ]; then
# echo "Inventory file created successfully."
# echo '{"status": "completed"}'
# else
# echo "Failed to create inventory file."
# echo '{"status": "failed"}'
# fi
#!/bin/bash
# Function to check if all instances are in the 'running' state
check_instances_running() {
instance_ids=$(terraform output -json instance_ids | jq -r '.[]')
running_count=0
total_count=$(echo "$instance_ids" | wc -l)
for instance_id in $instance_ids; do
state=$(aws ec2 describe-instances --instance-ids "$instance_id" --query "Reservations[].Instances[].State.Name" --output text)
if [ "$state" == "running" ]; then
((running_count++))
fi
done
[ "$running_count" -eq "$total_count" ]
}
# Wait for instances to be fully provisioned
max_wait_time=180 # Maximum wait time in seconds
wait_time=0
check_interval=5 # Check every 5 seconds
while ! check_instances_running; do
if [ "$wait_time" -ge "$max_wait_time" ]; then
echo "Timed out waiting for instances to be running."
exit 1
fi
sleep "$check_interval"
wait_time=$((wait_time + check_interval))
done
# Capture public IPs from Terraform output and write to inventory file
terraform output -json instance_public_ips | jq -r '.[]' > inventory
# Check if the output file is populated
if [ -s inventory ]; then
echo "Inventory file created successfully."
echo '{"status": "completed"}'
else
echo "Failed to create inventory file."
echo '{"status": "failed"}'
fi