@@ -73,39 +73,49 @@ test_ssh_connection() {
7373 exit 1
7474}
7575
76- # Wait for cloud-init and Docker to be ready
76+ # Wait for cloud-init to complete using robust detection method
7777wait_for_system_ready () {
7878 local vm_ip=" $1 "
7979 local max_attempts=30 # 15 minutes (30 * 30 seconds) for cloud-init completion
8080 local attempt=1
8181
82- log_info " Waiting for system initialization ( cloud-init and Docker) to complete..."
82+ log_info " Waiting for cloud-init to complete using robust detection method ..."
8383
8484 while [[ ${attempt} -le ${max_attempts} ]]; do
8585 log_info " Checking system readiness (attempt ${attempt} /${max_attempts} )..."
8686
87- # Check if cloud-init is done
87+ # Primary check: Official cloud-init status
8888 cloud_init_status=$( ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 " torrust@${vm_ip} " " cloud-init status" 2> /dev/null || echo " failed" )
8989
9090 if [[ " ${cloud_init_status} " == * " done" * ]]; then
9191 log_info " Cloud-init completed: ${cloud_init_status} "
9292
93- # Check if Docker is available
94- docker_available =$( ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 " torrust@${vm_ip} " " docker --version >/dev/null 2>&1 && echo 'available ' || echo 'not-available '" 2> /dev/null || echo " not-available " )
93+ # Secondary check: Custom completion marker file
94+ completion_marker_exists =$( ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 " torrust@${vm_ip} " " test -f /var/lib/cloud/torrust-setup-complete && echo 'exists ' || echo 'not-exists '" 2> /dev/null || echo " not-exists " )
9595
96- if [[ " ${docker_available} " == " available" ]]; then
97- # Check if Docker daemon is running
98- docker_running=$( ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 " torrust@${vm_ip} " " docker info >/dev/null 2>&1 && echo 'running' || echo 'not-running'" 2> /dev/null || echo " not-running" )
96+ if [[ " ${completion_marker_exists} " == " exists" ]]; then
97+ log_success " Setup completion marker found - all cloud-init tasks completed"
98+
99+ # Tertiary check: Verify system services are ready (only if needed for deployment)
100+ # Note: This check is deployment-specific, not cloud-init specific
101+ systemd_ready=$( ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 " torrust@${vm_ip} " " systemctl is-system-running --quiet && echo 'ready' || echo 'not-ready'" 2> /dev/null || echo " not-ready" )
99102
100- if [[ " ${docker_running } " == " running " ]]; then
101- log_success " System is ready: cloud-init done, Docker available and running "
103+ if [[ " ${systemd_ready } " == " ready " ]]; then
104+ log_success " System is fully ready for application deployment "
102105 return 0
103106 else
104- log_info " Docker installed but daemon not running yet , waiting..."
107+ log_info " System services still starting up , waiting..."
105108 fi
106109 else
107- log_info " Docker not available yet, cloud-init may still be installing it ..."
110+ log_info " Setup completion marker not found yet, cloud-init tasks may still be running ..."
108111 fi
112+ elif [[ " ${cloud_init_status} " == * " error" * ]]; then
113+ log_error " Cloud-init failed with error status: ${cloud_init_status} "
114+
115+ # Show detailed error information
116+ detailed_status=$( ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 " torrust@${vm_ip} " " cloud-init status --long" 2> /dev/null || echo " unknown" )
117+ log_error " Detailed cloud-init status: ${detailed_status} "
118+ return 1
109119 else
110120 log_info " Cloud-init status: ${cloud_init_status} , waiting for completion..."
111121 fi
@@ -116,22 +126,22 @@ wait_for_system_ready() {
116126 done
117127
118128 log_error " Timeout waiting for system to be ready after ${max_attempts} attempts (15 minutes)"
119- log_error " Cloud-init may have failed or Docker installation encountered issues"
129+ log_error " Cloud-init may have failed or system setup encountered issues"
120130
121- # Show diagnostic information
131+ # Show diagnostic information using robust detection methods
122132 vm_exec " ${vm_ip} " "
123133 echo '=== System Diagnostic Information ==='
124134 echo 'Cloud-init status:'
125135 cloud-init status --long || echo 'cloud-init command failed'
126- echo ''
127- echo 'Docker version :'
128- docker --version || echo 'Docker not available '
129- echo ''
130- echo 'Docker service status :'
131- systemctl status docker || echo 'Docker service status unavailable '
132- echo ''
133- echo 'Recent cloud-init logs :'
134- tail -20 /var/log/cloud-init.log || echo 'Cloud-init logs unavailable '
136+ echo
137+ echo 'Setup completion marker :'
138+ ls -la /var/lib/cloud/torrust-setup-complete 2>/dev/null || echo 'Completion marker not found '
139+ echo
140+ echo 'Cloud-init logs (last 20 lines) :'
141+ tail -20 /var/log/cloud-init.log 2>/dev/null || echo 'Cloud-init log not available '
142+ echo
143+ echo 'System service status :'
144+ systemctl is-system-running || echo 'System status check failed '
135145 " " Dumping diagnostic information"
136146
137147 exit 1
0 commit comments