Skip to content

Commit 867f71c

Browse files
committed
Optimize original template to fit under 16KB limit
Instead of using a separate compact template, optimized the original template by: - Simplifying the termination check script (3.4KB -> 0.5KB) - Reducing job hook scripts to minimal versions - Removing verbose logging and debug output - Using shorter variable names in scripts Reduced template size from 17.8KB to 14.2KB, well under AWS's 16KB userdata limit while maintaining all essential functionality.
1 parent 5c4fca2 commit 867f71c

3 files changed

Lines changed: 20 additions & 212 deletions

File tree

src/ec2_gha/start.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ def _build_user_data(self, **kwargs) -> str:
208208
The user data script as a string.
209209
210210
"""
211-
# Use compact template to stay under 16KB userdata limit
212-
template = importlib.resources.files("ec2_gha").joinpath("templates/user-script-compact.sh.templ")
211+
template = importlib.resources.files("ec2_gha").joinpath("templates/user-script.sh.templ")
213212
with template.open() as f:
214213
template_content = f.read()
215214

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

Lines changed: 0 additions & 103 deletions
This file was deleted.

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

Lines changed: 19 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -217,128 +217,40 @@ curl -L $runner_release -o runner.tar.gz
217217
echo "[$$(date '+%Y-%m-%d %H:%M:%S')] Extracting runner"
218218
# `--no-overwrite-dir` is important, otherwise `$$homedir` ends up `chown`'d to `1001:docker`, and `sshd` will refuse connection attempts to `$$homedir`
219219
tar --no-overwrite-dir -xzf runner.tar.gz
220-
echo "[$$(date '+%Y-%m-%d %H:%M:%S')] Setting up job tracking scripts"
221-
# Create minimal job tracking scripts inline
220+
# Create job tracking scripts
222221
cat > /usr/local/bin/job-started-hook.sh << 'EOFS'
223222
#!/bin/bash
224223
exec >> /tmp/job-started-hook.log 2>&1
225-
echo "[$$(date)] Job STARTED : $${GITHUB_WORKFLOW}/$${GITHUB_JOB} (Run: $${GITHUB_RUN_ID}/$${GITHUB_RUN_NUMBER}, Attempt: $${GITHUB_RUN_ATTEMPT})"
226-
echo " Repository: $${GITHUB_REPOSITORY}"
227-
echo " Runner: $${RUNNER_NAME}"
228-
JOB_TRACK_DIR="/var/run/github-runner-jobs"
229-
mkdir -p "$${JOB_TRACK_DIR}"
230-
echo "{\"job_id\":\"$${GITHUB_JOB}\",\"run_id\":\"$${GITHUB_RUN_ID}\",\"workflow\":\"$${GITHUB_WORKFLOW}\",\"status\":\"running\"}" > "$${JOB_TRACK_DIR}/$${GITHUB_RUN_ID}-$${GITHUB_JOB}.job"
231-
# Update activity timestamp
224+
echo "[$$(date)] Job started: $${GITHUB_JOB}"
225+
mkdir -p /var/run/github-runner-jobs
226+
echo '{"status":"running"}' > /var/run/github-runner-jobs/$${GITHUB_RUN_ID}-$${GITHUB_JOB}.job
232227
touch /var/run/github-runner-last-activity
233228
EOFS
234229

235230
cat > /usr/local/bin/job-completed-hook.sh << 'EOFC'
236231
#!/bin/bash
237232
exec >> /tmp/job-completed-hook.log 2>&1
238-
echo "[$$(date)] Job COMPLETED: $${GITHUB_WORKFLOW}/$${GITHUB_JOB} (Run: $${GITHUB_RUN_ID}/$${GITHUB_RUN_NUMBER}, Attempt: $${GITHUB_RUN_ATTEMPT})"
239-
echo " Repository: $${GITHUB_REPOSITORY}"
240-
echo " Runner: $${RUNNER_NAME}"
241-
JOB_TRACK_DIR="/var/run/github-runner-jobs"
242-
if [ -f "$${JOB_TRACK_DIR}/$${GITHUB_RUN_ID}-$${GITHUB_JOB}.job" ]; then
243-
sed -i 's/"status":"running"/"status":"completed"/' "$${JOB_TRACK_DIR}/$${GITHUB_RUN_ID}-$${GITHUB_JOB}.job"
244-
fi
245-
# Count remaining running jobs
246-
RUNNING_JOBS=$$(grep -l '"status":"running"' "$${JOB_TRACK_DIR}"/*.job 2>/dev/null | wc -l || echo 0)
247-
echo " Running jobs remaining: $${RUNNING_JOBS}"
248-
# Update activity timestamp
233+
echo "[$$(date)] Job completed: $${GITHUB_JOB}"
234+
rm -f /var/run/github-runner-jobs/$${GITHUB_RUN_ID}-$${GITHUB_JOB}.job
249235
touch /var/run/github-runner-last-activity
250236
EOFC
251237

252238
cat > /usr/local/bin/check-runner-termination.sh << 'EOFT'
253239
#!/bin/bash
254240
exec >> /tmp/termination-check.log 2>&1
255-
echo "[$$(date)] Checking termination conditions"
256-
257-
ACTIVITY_FILE="/var/run/github-runner-last-activity"
258-
GRACE_PERIOD="$${RUNNER_GRACE_PERIOD:-60}"
259-
INITIAL_GRACE_PERIOD="$${RUNNER_INITIAL_GRACE_PERIOD:-180}"
260-
JOB_TRACK_DIR="/var/run/github-runner-jobs"
261-
262-
# Check if activity file exists
263-
if [ ! -f "$${ACTIVITY_FILE}" ]; then
264-
echo "[$$(date)] WARNING: No activity file found, creating it now"
265-
touch "$${ACTIVITY_FILE}"
266-
fi
267-
268-
# Get last activity time and current time
269-
LAST_ACTIVITY=$$(stat -c %Y "$${ACTIVITY_FILE}" 2>/dev/null || echo 0)
270-
NOW=$$(date +%s)
271-
IDLE_TIME=$$((NOW - LAST_ACTIVITY))
272-
273-
# Check if any jobs have ever run
274-
if ls "$${JOB_TRACK_DIR}"/*.job 2>/dev/null | grep -q .; then
275-
JOBS_HAVE_RUN=true
276-
CURRENT_GRACE_PERIOD="$${GRACE_PERIOD}"
277-
else
278-
JOBS_HAVE_RUN=false
279-
CURRENT_GRACE_PERIOD="$${INITIAL_GRACE_PERIOD}"
280-
fi
281-
282-
echo "[$$(date)] Last activity: $$(date -d @$${LAST_ACTIVITY} '+%Y-%m-%d %H:%M:%S')"
283-
echo "[$$(date)] Current time: $$(date '+%Y-%m-%d %H:%M:%S')"
284-
echo "[$$(date)] Idle time: $${IDLE_TIME} seconds (grace period: $${CURRENT_GRACE_PERIOD} seconds)"
285-
echo "[$$(date)] Jobs have run: $${JOBS_HAVE_RUN}"
286-
287-
# Check for running jobs first
288-
RUNNING_JOBS=$$(grep -l '"status":"running"' "$${JOB_TRACK_DIR}"/*.job 2>/dev/null | wc -l || echo 0)
289-
echo "[$$(date)] Running jobs: $${RUNNING_JOBS}"
290-
291-
# Show status of each job for debugging
292-
echo "[$$(date)] Current job files:"
293-
for job_file in "$${JOB_TRACK_DIR}"/*.job; do
294-
if [ -f "$${job_file}" ]; then
295-
job_status=$$(grep -o '"status":"[^"]*"' "$${job_file}" || echo "unknown")
296-
echo "[$$(date)] $$(basename "$${job_file}"): $${job_status}"
297-
fi
298-
done || echo "[$$(date)] No job files found"
299-
300-
# Never terminate if jobs are running
301-
if [ "$${RUNNING_JOBS}" -gt 0 ]; then
302-
echo "[$$(date)] Jobs are still running, not checking idle time"
303-
elif [ "$${IDLE_TIME}" -gt "$${CURRENT_GRACE_PERIOD}" ]; then
304-
echo "[$$(date)] No running jobs and no activity for $${IDLE_TIME} seconds, proceeding with termination"
305-
306-
# Try to remove runner from GitHub first
307-
if [ -f "$$homedir/config.sh" ]; then
308-
echo "[$$(date)] Removing runner from GitHub"
309-
cd "$$homedir"
310-
# Stop the runner service
311-
RUNNER_PID=$$(pgrep -f "Runner.Listener" | head -1)
312-
if [ -n "$${RUNNER_PID}" ]; then
313-
echo "[$$(date)] Stopping runner PID $${RUNNER_PID}"
314-
kill -INT "$${RUNNER_PID}" 2>/dev/null || true
315-
# Wait for it to stop
316-
for i in {1..10}; do
317-
if ! kill -0 "$${RUNNER_PID}" 2>/dev/null; then
318-
echo "[$$(date)] Runner stopped"
319-
break
320-
fi
321-
sleep 1
322-
done
323-
fi
324-
325-
# Remove runner from GitHub
326-
# We need RUNNER_ALLOW_RUNASROOT=1 to remove as root, just like when we configured it
327-
if RUNNER_ALLOW_RUNASROOT=1 ./config.sh remove --token $token; then
328-
echo "[$$(date)] Runner removed from GitHub successfully"
329-
else
330-
echo "[$$(date)] Failed to remove runner from GitHub"
331-
fi
332-
fi
333-
334-
# Flush CloudWatch logs before shutdown
335-
echo "[$$(date)] Flushing CloudWatch logs"
336-
sync
337-
sleep 5
338-
339-
sudo shutdown -h now "Runner terminating after idle timeout"
340-
else
341-
echo "[$$(date)] Activity detected within $${CURRENT_GRACE_PERIOD} seconds, not terminating"
241+
A="/var/run/github-runner-last-activity"
242+
J="/var/run/github-runner-jobs"
243+
[ ! -f "$$A" ] && touch "$$A"
244+
L=$$(stat -c %Y "$$A" 2>/dev/null || echo 0)
245+
I=$$(($$(date +%s)-L))
246+
if ls $$J/*.job 2>/dev/null | grep -q .; then G=$${RUNNER_GRACE_PERIOD:-60}; else G=$${RUNNER_INITIAL_GRACE_PERIOD:-180}; fi
247+
R=$$(grep -l '"status":"running"' $$J/*.job 2>/dev/null | wc -l || echo 0)
248+
if [ $$R -eq 0 ] && [ $$I -gt $$G ]; then
249+
echo "[$$(date)] Terminating: idle $$I > grace $$G"
250+
[ -f "$$homedir/config.sh" ] && cd "$$homedir" && pkill -INT -f "Runner.Listener" 2>/dev/null || true
251+
sleep 2
252+
[ -f "$$homedir/config.sh" ] && RUNNER_ALLOW_RUNASROOT=1 ./config.sh remove --token $token || true
253+
shutdown -h now
342254
fi
343255
EOFT
344256

0 commit comments

Comments
 (0)