Skip to content

Commit 44760e8

Browse files
committed
Make system dependencies more robust and cross-platform
Added checks for critical system dependencies and made the script more portable across different Linux distributions: - Check for Python3 availability (critical dependency) - Support both dpkg (Debian/Ubuntu) and rpm (RHEL/CentOS/Amazon Linux) for CloudWatch agent installation - Support both curl and wget for downloading files - Fail gracefully with clear error messages when critical tools are missing This makes ec2-gha more flexible for use with different AMIs beyond just Ubuntu, while maintaining backward compatibility.
1 parent 1c2ba16 commit 44760e8

1 file changed

Lines changed: 47 additions & 18 deletions

File tree

src/ec2_gha/templates/user-script.sh.templ

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,23 +173,36 @@ if [ "$cloudwatch_logs_group" != "" ]; then
173173
# Use a subshell to prevent CloudWatch failures from stopping the entire script
174174
(
175175

176-
# Wait for dpkg lock to be released (up to 2 minutes)
177-
log "Waiting for dpkg lock to be released..."
178-
timeout=120
179-
while fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1 || fuser /var/lib/dpkg/lock >/dev/null 2>&1; do
180-
if [ $$timeout -le 0 ]; then
181-
log "WARNING: dpkg lock timeout, proceeding anyway"
182-
break
183-
fi
184-
log "dpkg is locked, waiting... ($$timeout seconds remaining)"
185-
sleep 5
186-
timeout=$$((timeout - 5))
187-
done
188-
189-
# Download and install CloudWatch agent
190-
wget -q https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
191-
dpkg -i -E ./amazon-cloudwatch-agent.deb
192-
rm amazon-cloudwatch-agent.deb
176+
# Detect package manager and install CloudWatch agent
177+
if command -v dpkg >/dev/null 2>&1; then
178+
# Debian/Ubuntu
179+
log "Detected dpkg-based system"
180+
181+
# Wait for dpkg lock to be released (up to 2 minutes)
182+
log "Waiting for dpkg lock to be released..."
183+
timeout=120
184+
while fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1 || fuser /var/lib/dpkg/lock >/dev/null 2>&1; do
185+
if [ $$timeout -le 0 ]; then
186+
log "WARNING: dpkg lock timeout, proceeding anyway"
187+
break
188+
fi
189+
log "dpkg is locked, waiting... ($$timeout seconds remaining)"
190+
sleep 5
191+
timeout=$$((timeout - 5))
192+
done
193+
194+
wget -q https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
195+
dpkg -i -E ./amazon-cloudwatch-agent.deb
196+
rm amazon-cloudwatch-agent.deb
197+
elif command -v rpm >/dev/null 2>&1; then
198+
# RHEL/CentOS/Amazon Linux
199+
log "Detected rpm-based system"
200+
wget -q https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
201+
rpm -U ./amazon-cloudwatch-agent.rpm
202+
rm amazon-cloudwatch-agent.rpm
203+
else
204+
log "WARNING: Unable to detect package manager, skipping CloudWatch agent installation"
205+
fi
193206

194207
# Configure CloudWatch agent
195208
cat > /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json << EOF
@@ -269,7 +282,16 @@ else
269282
RUNNER_URL="$runner_release"
270283
log "x64 detected, using: $$RUNNER_URL"
271284
fi
272-
curl -L $$RUNNER_URL -o /tmp/runner.tar.gz
285+
286+
# Check for curl or wget
287+
if command -v curl >/dev/null 2>&1; then
288+
curl -L $$RUNNER_URL -o /tmp/runner.tar.gz
289+
elif command -v wget >/dev/null 2>&1; then
290+
wget -q $$RUNNER_URL -O /tmp/runner.tar.gz
291+
else
292+
log_error "Neither curl nor wget found. Cannot download runner."
293+
terminate_instance "No download tool available"
294+
fi
273295
log "Downloaded runner binary"
274296
# Create shared job tracking scripts (used by all runners)
275297
cat > /usr/local/bin/job-started-hook.sh << 'EOFS'
@@ -379,6 +401,13 @@ fi
379401

380402
# Process each runner configuration
381403
log "Setting up $$RUNNERS_PER_INSTANCE runner(s)"
404+
405+
# Check for Python3 availability
406+
if ! command -v python3 >/dev/null 2>&1; then
407+
log_error "Python3 is required but not found. Cannot configure runners."
408+
terminate_instance "Python3 not available on this AMI"
409+
fi
410+
382411
python3 -c "
383412
import json, sys, os, subprocess, traceback, base64
384413

0 commit comments

Comments
 (0)