From 59de2a1d7960ab5ffe2b95b700f372c9bcb2c948 Mon Sep 17 00:00:00 2001 From: nour-s Date: Tue, 22 Oct 2024 22:05:41 +0400 Subject: [PATCH] add a wait for ssh to be ready --- scripts/configure | 17 ++++++++++++++++- scripts/init | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/scripts/configure b/scripts/configure index 8972dab..b9f7b53 100755 --- a/scripts/configure +++ b/scripts/configure @@ -25,6 +25,21 @@ fi echo "Waiting for the EC2 instance to spin" aws ec2 wait instance-running --filters Name=ip-address,Values=${gateway_ip} +# Loop until SSH is ready +echo "Waiting for SSH to be ready" +attempt=0 +max_attempts=50 + +while ! ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i ./keys/ssh ubuntu@${gateway_ip} 'exit' 2>/dev/null; do + attempt=$((attempt + 1)) + if [ $attempt -ge $max_attempts ]; then + echo "SSH not ready after $max_attempts attempts, exiting." + exit 1 + fi + echo "SSH not ready, retrying in 2 seconds... (Attempt $attempt/$max_attempts)" + sleep 2 +done + pushd ansible ansible-playbook -i hosts $tags provision.yml -popd \ No newline at end of file +popd diff --git a/scripts/init b/scripts/init index de400cb..3a4cb96 100755 --- a/scripts/init +++ b/scripts/init @@ -54,4 +54,4 @@ if [ ! -f $ansible_vars ]; then > $ansible_vars else echo "Ansible vars file already exists, skipping" -fi \ No newline at end of file +fi