Skip to content

Commit bb72f09

Browse files
committed
reformat / de-obfuscate some vars
1 parent d4c1714 commit bb72f09

File tree

4 files changed

+63
-47
lines changed

4 files changed

+63
-47
lines changed

src/ec2_gha/scripts/check-runner-termination.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ exec >> /tmp/termination-check.log 2>&1
88
source /usr/local/bin/runner-common.sh
99

1010
# File paths for tracking
11-
A="/var/run/github-runner-last-activity"
12-
J="/var/run/github-runner-jobs"
13-
H="/var/run/github-runner-has-run-job"
11+
A="$RUNNER_STATE_DIR/last-activity"
12+
J="$RUNNER_STATE_DIR/jobs"
13+
H="$RUNNER_STATE_DIR/has-run-job"
1414

1515
# Current timestamp
1616
N=$(date +%s)

src/ec2_gha/scripts/job-completed-hook.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
exec >> /tmp/job-completed-hook.log 2>&1
77

8+
# Source common variables
9+
source /usr/local/bin/runner-common.sh
10+
811
# Get runner index from environment (defaults to 0 for single-runner instances)
912
I="${RUNNER_INDEX:-0}"
1013

@@ -13,7 +16,7 @@ I="${RUNNER_INDEX:-0}"
1316
echo "[$(date)] Runner-$I: LOG_PREFIX_JOB_COMPLETED ${GITHUB_JOB}"
1417

1518
# Remove the job tracking file to indicate this runner no longer has an active job
16-
rm -f /var/run/github-runner-jobs/${GITHUB_RUN_ID}-${GITHUB_JOB}-$I.job
19+
rm -f $RUNNER_STATE_DIR/jobs/${GITHUB_RUN_ID}-${GITHUB_JOB}-$I.job
1720

1821
# Update activity timestamp to reset the idle timer
19-
touch /var/run/github-runner-last-activity
22+
touch $RUNNER_STATE_DIR/last-activity

src/ec2_gha/scripts/job-started-hook.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
exec >> /tmp/job-started-hook.log 2>&1
77

8+
# Source common variables
9+
source /usr/local/bin/runner-common.sh
10+
811
# Get runner index from environment (defaults to 0 for single-runner instances)
912
I="${RUNNER_INDEX:-0}"
1013

@@ -14,8 +17,8 @@ echo "[$(date)] Runner-$I: LOG_PREFIX_JOB_STARTED Runner-$I: ${GITHUB_JOB}"
1417

1518
# Create a job tracking file to indicate this runner has an active job
1619
# Format: RUNID-JOBNAME-RUNNER.job
17-
mkdir -p /var/run/github-runner-jobs
18-
echo '{"status":"running","runner":"'$I'"}' > /var/run/github-runner-jobs/${GITHUB_RUN_ID}-${GITHUB_JOB}-$I.job
20+
mkdir -p $RUNNER_STATE_DIR/jobs
21+
echo '{"status":"running","runner":"'$I'"}' > $RUNNER_STATE_DIR/jobs/${GITHUB_RUN_ID}-${GITHUB_JOB}-$I.job
1922

2023
# Update activity timestamps to reset the idle timer
21-
touch /var/run/github-runner-last-activity /var/run/github-runner-has-run-job
24+
touch $RUNNER_STATE_DIR/last-activity $RUNNER_STATE_DIR/has-run-job

src/ec2_gha/scripts/runner-setup.sh

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ fi
4646
export homedir
4747
4848
# Set common paths
49-
B=/usr/local/bin
50-
V=/var/run/github-runner
49+
BIN_DIR=/usr/local/bin
50+
RUNNER_STATE_DIR=/var/run/github-runner
51+
mkdir -p $RUNNER_STATE_DIR
5152
5253
# Fetch shared functions from GitHub
5354
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Fetching shared functions from GitHub (SHA: ${action_sha})" | tee -a /var/log/runner-setup.log
@@ -59,44 +60,45 @@ if ! curl -sSL "$FUNCTIONS_URL" -o /tmp/shared-functions.sh && ! wget -q "$FUNCT
5960
fi
6061
6162
# Write shared functions that will be used by multiple scripts
62-
cat > $B/runner-common.sh << EOSF
63+
cat > $BIN_DIR/runner-common.sh << EOSF
6364
# Auto-generated shared functions and variables
6465
# Set homedir for scripts that source this file
6566
homedir="$homedir"
6667
debug="$debug"
67-
export homedir debug
68+
RUNNER_STATE_DIR="$RUNNER_STATE_DIR"
69+
export homedir debug RUNNER_STATE_DIR
6870
6971
EOSF
7072
7173
# Append the downloaded shared functions
72-
cat /tmp/shared-functions.sh >> $B/runner-common.sh
74+
cat /tmp/shared-functions.sh >> $BIN_DIR/runner-common.sh
7375
74-
chmod +x $B/runner-common.sh
75-
source $B/runner-common.sh
76+
chmod +x $BIN_DIR/runner-common.sh
77+
source $BIN_DIR/runner-common.sh
7678
7779
logger "EC2-GHA: Starting userdata script"
7880
trap 'logger "EC2-GHA: Script failed at line $LINENO with exit code $?"' ERR
7981
trap 'terminate_instance "Setup script failed with error on line $LINENO"' ERR
8082
# Handle watchdog termination signal
81-
trap 'if [ -f $V-watchdog-terminate ]; then terminate_instance "No runners registered within timeout"; else terminate_instance "Script terminated"; fi' TERM
83+
trap 'if [ -f $RUNNER_STATE_DIR/watchdog-terminate ]; then terminate_instance "No runners registered within timeout"; else terminate_instance "Script terminated"; fi' TERM
8284
8385
# Set up registration timeout failsafe - terminate if runner doesn't register in time
8486
REGISTRATION_TIMEOUT="$runner_registration_timeout"
8587
if ! [[ "$REGISTRATION_TIMEOUT" =~ ^[0-9]+$ ]]; then
8688
REGISTRATION_TIMEOUT=300
8789
fi
8890
# Create a marker file for watchdog termination request
89-
touch $V-watchdog-active
91+
touch $RUNNER_STATE_DIR/watchdog-active
9092
(
9193
sleep $REGISTRATION_TIMEOUT
92-
if [ ! -f $V-registered ]; then
93-
touch $V-watchdog-terminate
94+
if [ ! -f $RUNNER_STATE_DIR/registered ]; then
95+
touch $RUNNER_STATE_DIR/watchdog-terminate
9496
kill -TERM $$ 2>/dev/null || true
9597
fi
96-
rm -f $V-watchdog-active
98+
rm -f $RUNNER_STATE_DIR/watchdog-active
9799
) &
98100
REGISTRATION_WATCHDOG_PID=$!
99-
echo $REGISTRATION_WATCHDOG_PID > $V-watchdog.pid
101+
echo $REGISTRATION_WATCHDOG_PID > $RUNNER_STATE_DIR/watchdog.pid
100102
101103
# Run any custom user data script provided by the user
102104
if [ -n "$userdata" ]; then
@@ -151,21 +153,29 @@ if [ "$cloudwatch_logs_group" != "" ]; then
151153
rm amazon-cloudwatch-agent.rpm
152154
fi
153155
154-
# Build CloudWatch config with factored strings
155-
G=',"log_group_name":"'$cloudwatch_logs_group'","log_stream_name":"{instance_id}/'
156-
Z='","timezone":"UTC"}'
157-
H="$homedir"
156+
# Build CloudWatch config
158157
cat > /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json << EOF
159-
{"agent":{"run_as_user":"cwagent"},"logs":{"logs_collected":{"files":{"collect_list":[
160-
{"file_path":"/var/log/runner-setup.log${G}runner-setup$Z"},
161-
{"file_path":"/var/log/runner-debug.log${G}runner-debug$Z"},
162-
{"file_path":"/tmp/job-started-hook.log${G}job-started$Z"},
163-
{"file_path":"/tmp/job-completed-hook.log${G}job-completed$Z"},
164-
{"file_path":"/tmp/termination-check.log${G}termination$Z"},
165-
{"file_path":"/tmp/runner-*-config.log${G}runner-config$Z"},
166-
{"file_path":"$H/_diag/Runner_**.log${G}runner-diag$Z"},
167-
{"file_path":"$H/_diag/Worker_**.log${G}worker-diag$Z"}
168-
]}}}}
158+
{
159+
"agent": {
160+
"run_as_user": "cwagent"
161+
},
162+
"logs": {
163+
"logs_collected": {
164+
"files": {
165+
"collect_list": [
166+
{ "file_path": "/var/log/runner-setup.log" , "log_group_name": "$cloudwatch_logs_group", "log_stream_name": "{instance_id}/runner-setup" , "timezone": "UTC" },
167+
{ "file_path": "/var/log/runner-debug.log" , "log_group_name": "$cloudwatch_logs_group", "log_stream_name": "{instance_id}/runner-debug" , "timezone": "UTC" },
168+
{ "file_path": "/tmp/job-started-hook.log" , "log_group_name": "$cloudwatch_logs_group", "log_stream_name": "{instance_id}/job-started" , "timezone": "UTC" },
169+
{ "file_path": "/tmp/job-completed-hook.log" , "log_group_name": "$cloudwatch_logs_group", "log_stream_name": "{instance_id}/job-completed", "timezone": "UTC" },
170+
{ "file_path": "/tmp/termination-check.log" , "log_group_name": "$cloudwatch_logs_group", "log_stream_name": "{instance_id}/termination" , "timezone": "UTC" },
171+
{ "file_path": "/tmp/runner-*-config.log" , "log_group_name": "$cloudwatch_logs_group", "log_stream_name": "{instance_id}/runner-config", "timezone": "UTC" },
172+
{ "file_path": "$homedir/_diag/Runner_**.log", "log_group_name": "$cloudwatch_logs_group", "log_stream_name": "{instance_id}/runner-diag" , "timezone": "UTC" },
173+
{ "file_path": "$homedir/_diag/Worker_**.log", "log_group_name": "$cloudwatch_logs_group", "log_stream_name": "{instance_id}/worker-diag" , "timezone": "UTC" }
174+
]
175+
}
176+
}
177+
}
178+
}
169179
EOF
170180
171181
if ! /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json -s; then
@@ -229,7 +239,7 @@ log "Downloaded runner binary"
229239
fetch_script() {
230240
local script_name="$1"
231241
local url="${BASE_URL}/${script_name}"
232-
local dest="${B}/${script_name}"
242+
local dest="${BIN_DIR}/${script_name}"
233243
234244
if command -v curl >/dev/null 2>&1; then
235245
curl -fsSL "$url" -o "$dest" || {
@@ -257,14 +267,14 @@ fetch_script "job-completed-hook.sh"
257267
fetch_script "check-runner-termination.sh"
258268
259269
# Replace log prefix placeholders with actual values
260-
sed -i "s/LOG_PREFIX_JOB_STARTED/${log_prefix_job_started}/g" $B/job-started-hook.sh
261-
sed -i "s/LOG_PREFIX_JOB_COMPLETED/${log_prefix_job_completed}/g" $B/job-completed-hook.sh
270+
sed -i "s/LOG_PREFIX_JOB_STARTED/${log_prefix_job_started}/g" $BIN_DIR/job-started-hook.sh
271+
sed -i "s/LOG_PREFIX_JOB_COMPLETED/${log_prefix_job_completed}/g" $BIN_DIR/job-completed-hook.sh
262272
263-
chmod +x $B/job-started-hook.sh $B/job-completed-hook.sh $B/check-runner-termination.sh
273+
chmod +x $BIN_DIR/job-started-hook.sh $BIN_DIR/job-completed-hook.sh $BIN_DIR/check-runner-termination.sh
264274
265275
# Set up job tracking directory
266-
mkdir -p $V-jobs
267-
touch $V-last-activity
276+
mkdir -p $RUNNER_STATE_DIR/jobs
277+
touch $RUNNER_STATE_DIR/last-activity
268278
269279
# Set up periodic termination check using systemd
270280
cat > /etc/systemd/system/runner-termination-check.service << EOF
@@ -276,7 +286,7 @@ Type=oneshot
276286
Environment="RUNNER_GRACE_PERIOD=$runner_grace_period"
277287
Environment="RUNNER_INITIAL_GRACE_PERIOD=$runner_initial_grace_period"
278288
Environment="RUNNER_POLL_INTERVAL=$runner_poll_interval"
279-
ExecStart=$B/check-runner-termination.sh
289+
ExecStart=$BIN_DIR/check-runner-termination.sh
280290
EOF
281291
282292
cat > /etc/systemd/system/runner-termination-check.timer << EOF
@@ -367,21 +377,21 @@ fi
367377
368378
if [ $succeeded -gt 0 ]; then
369379
log "$succeeded runner(s) registered and started successfully"
370-
touch $V-registered
380+
touch $RUNNER_STATE_DIR/registered
371381
else
372382
log_error "No runners registered successfully"
373383
terminate_instance "No runners registered successfully"
374384
fi
375385
376386
# Kill registration watchdog now that runners are registered
377-
if [ -f $V-watchdog.pid ]; then
378-
WATCHDOG_PID=$(cat $V-watchdog.pid)
387+
if [ -f $RUNNER_STATE_DIR/watchdog.pid ]; then
388+
WATCHDOG_PID=$(cat $RUNNER_STATE_DIR/watchdog.pid)
379389
kill $WATCHDOG_PID 2>/dev/null || true
380-
rm -f $V-watchdog.pid
390+
rm -f $RUNNER_STATE_DIR/watchdog.pid
381391
fi
382392
383393
# Final setup - ensure runner directories are accessible for debugging
384-
touch $V-started
394+
touch $RUNNER_STATE_DIR/started
385395
chmod o+x $homedir
386396
for RUNNER_DIR in $homedir/runner-*; do
387397
[ -d "$RUNNER_DIR/_diag" ] && chmod 755 "$RUNNER_DIR/_diag"

0 commit comments

Comments
 (0)