Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
222 lines (201 loc) · 9.12 KB
/
Copy pathaction.yml
File metadata and controls
222 lines (201 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
name: 'Begin Testbox'
description: 'Initialize a Blacksmith Testbox session. Phones home to receive config, installs SSH key and rsync.'
inputs:
testbox_id:
description: 'Testbox session ID (from workflow_dispatch input). Leave empty for validation mode.'
required: false
default: ''
api_url:
description: 'Override the backend API URL (default: discovered from VM metadata)'
required: false
runs:
using: 'composite'
steps:
- name: Check mode
shell: bash
run: |
if [ -z "${{ inputs.testbox_id }}" ]; then
echo "::notice::Running in validation mode — setup steps will be validated without starting a testbox session."
fi
- name: Discover metadata service
id: metadata
shell: bash
run: |
if [ -z "${BLACKSMITH_AGENT_ADDR:-}" ]; then
echo "::warning::BLACKSMITH_AGENT_ADDR is not set; testbox is only supported on Blacksmith runners. Skipping testbox setup."
echo "available=false" >> "$GITHUB_OUTPUT"
exit 0
fi
METADATA_PORT="${METADATA_PORT:-}"
if [ -z "$METADATA_PORT" ]; then
METADATA_PORT=$(tr ' ' '\n' < /proc/cmdline | grep '^metadata_port=' | cut -d'=' -f2 || true)
if [ -z "$METADATA_PORT" ]; then
echo "::warning::metadata_port not found in kernel cmdline; testbox is only supported on Blacksmith runners. Skipping testbox setup."
echo "available=false" >> "$GITHUB_OUTPUT"
exit 0
fi
export METADATA_PORT
fi
echo "available=true" >> "$GITHUB_OUTPUT"
echo "metadata_addr=${BLACKSMITH_AGENT_ADDR}:${METADATA_PORT}" >> "$GITHUB_OUTPUT"
- name: Phone home and configure testbox
if: steps.metadata.outputs.available == 'true' && inputs.testbox_id
shell: bash
env:
TESTBOX_ID: ${{ inputs.testbox_id }}
API_URL_OVERRIDE: ${{ inputs.api_url }}
run: |
METADATA_ADDR="${{ steps.metadata.outputs.metadata_addr }}"
STATE=/tmp/.testbox
mkdir -p "$STATE"
chmod 700 "$STATE"
# The runner exports the host-minted per-VM token as BLACKSMITH_VM_TOKEN
# and the metadata service only answers requests that present it in
# X-Blacksmith-VM-Token.
VM_TOKEN="${BLACKSMITH_VM_TOKEN:-}"
if [ -n "$VM_TOKEN" ]; then
echo "::add-mask::${VM_TOKEN}"
fi
metadata_get() {
curl -sf --connect-timeout 2 --max-time 5 \
-H "X-Blacksmith-VM-Token: ${VM_TOKEN}" \
"http://${METADATA_ADDR}/$1" || true
}
INSTALLATION_MODEL_ID=$(metadata_get installationModelID)
if [ -n "$API_URL_OVERRIDE" ]; then
API_URL="$API_URL_OVERRIDE"
else
API_URL=$(metadata_get backendURL)
fi
# Piggyback on the VM's stickyDiskToken for phone-home authentication.
# It proves the caller is a real Blacksmith VM belonging to the claimed
# installation, and the backend binds it to the runner that adopted this
# testbox's run. It's only accessible from inside the VM via the metadata
# service and is not publicly visible.
AUTH_TOKEN=$(metadata_get stickyDiskToken)
# The host agent serves the literal "unset" when the backend minted no token.
if [ "$AUTH_TOKEN" = "unset" ]; then
AUTH_TOKEN=""
fi
if [ -z "$API_URL" ] || [ -z "$INSTALLATION_MODEL_ID" ] || [ -z "$AUTH_TOKEN" ]; then
echo "::error::Could not read required metadata (backendURL, installationModelID, stickyDiskToken); the testbox cannot phone home."
exit 1
fi
if [ -n "$BLACKSMITH_HOSTNAME" ]; then
RUNNER_HOST="$BLACKSMITH_HOSTNAME"
else
RUNNER_HOST="$BLACKSMITH_HOST_PUBLIC_IP"
fi
RUNNER_SSH_PORT="${BLACKSMITH_SSH_PORT:-22}"
# The first phone-home can race warmup's write of the dispatched run ID
# and the adoption webhook. The backend answers 503 + Retry-After while
# that binding is pending, so retry transient failures up to a deadline
# and fail the job if the handshake never lands: without it the CLI's
# SSH key is never installed and the testbox is unusable.
PHONE_HOME_DEADLINE=$(( $(date +%s) + 90 ))
PHONE_HOME_RETRY_DELAY=2
PHONE_HOME_RESPONSE_FILE="$STATE/phone_home_response"
ATTEMPT=0
while true; do
ATTEMPT=$((ATTEMPT + 1))
HTTP_CODE=$(curl -s -o "$PHONE_HOME_RESPONSE_FILE" -w '%{http_code}' --max-time 30 \
-X POST "${API_URL}/api/testbox/phone-home" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-d "{
\"testbox_id\": \"${TESTBOX_ID}\",
\"installation_model_id\": ${INSTALLATION_MODEL_ID},
\"status\": \"hydrating\",
\"ip_address\": \"${RUNNER_HOST}\",
\"ssh_port\": \"${RUNNER_SSH_PORT}\",
\"working_directory\": \"${GITHUB_WORKSPACE}\",
\"adopted_run_id\": \"${GITHUB_RUN_ID}\",
\"metadata\": {}
}") || HTTP_CODE=000
case "$HTTP_CODE" in
2??)
break
;;
000|408|425|429|5??)
;;
*)
echo "::error::Testbox phone-home rejected (HTTP ${HTTP_CODE}): $(cat "$PHONE_HOME_RESPONSE_FILE" 2>/dev/null)"
exit 1
;;
esac
if [ "$(date +%s)" -ge "$PHONE_HOME_DEADLINE" ]; then
echo "::error::Testbox phone-home did not succeed before the deadline (last HTTP ${HTTP_CODE}, ${ATTEMPT} attempts): $(cat "$PHONE_HOME_RESPONSE_FILE" 2>/dev/null)"
exit 1
fi
echo "Phone-home not accepted yet (HTTP ${HTTP_CODE}, attempt ${ATTEMPT}); retrying in ${PHONE_HOME_RETRY_DELAY}s"
sleep "$PHONE_HOME_RETRY_DELAY"
done
RESPONSE=$(cat "$PHONE_HOME_RESPONSE_FILE")
rm -f "$PHONE_HOME_RESPONSE_FILE"
echo "$TESTBOX_ID" > "$STATE/testbox_id"
echo "$INSTALLATION_MODEL_ID" > "$STATE/installation_model_id"
echo "$AUTH_TOKEN" > "$STATE/auth_token"
echo "$API_URL" > "$STATE/api_url"
echo "$RUNNER_HOST" > "$STATE/runner_host"
echo "$RUNNER_SSH_PORT" > "$STATE/runner_ssh_port"
echo "$GITHUB_WORKSPACE" > "$STATE/working_directory"
echo "$GITHUB_RUN_ID" > "$STATE/adopted_run_id"
echo "$RESPONSE" | jq -r '.ssh_public_key // empty' > "$STATE/ssh_public_key"
IDLE_TIMEOUT=$(echo "$RESPONSE" | jq -r '.idle_timeout // empty')
echo "${IDLE_TIMEOUT:-10}" > "$STATE/idle_timeout"
echo "Testbox initialized: testbox_id=${TESTBOX_ID} (phone-home accepted after ${ATTEMPT} attempt(s))"
- name: Start heartbeat
if: steps.metadata.outputs.available == 'true' && inputs.testbox_id
shell: bash
run: |
STATE=/tmp/.testbox
if [ ! -f "$STATE/testbox_id" ]; then
echo "Warning: state directory not initialized, skipping heartbeat"
exit 0
fi
# Launch a background heartbeat loop that POSTs to the backend
# every 30 seconds. The backend uses this to detect dead VMs
# whose phone-home (completed) never arrived.
#
# nohup + fd redirects + disown fully detaches the process
# from this step's shell. Without this, GitHub Actions may
# wait on or SIGTERM child processes between composite action
# steps, killing the heartbeat before run-testbox starts.
#
# The state files are read inside the loop so the script is
# self-contained and does not depend on shell variable scope.
nohup bash -c 'STATE=/tmp/.testbox
API_URL=$(cat "$STATE/api_url")
AUTH_TOKEN=$(cat "$STATE/auth_token")
TESTBOX_ID=$(cat "$STATE/testbox_id")
INSTALLATION_MODEL_ID=$(cat "$STATE/installation_model_id")
while true; do
sleep 30
curl -s -f --max-time 10 -X POST "${API_URL}/api/testbox/heartbeat" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-d "{\"testbox_id\":\"${TESTBOX_ID}\",\"installation_model_id\":${INSTALLATION_MODEL_ID}}" \
>/dev/null 2>&1 || true
done' </dev/null >/dev/null 2>&1 &
HEARTBEAT_PID=$!
echo "$HEARTBEAT_PID" > "$STATE/heartbeat_pid"
disown "$HEARTBEAT_PID"
echo "Heartbeat started (PID $HEARTBEAT_PID, every 30s, disowned)"
- name: Install SSH public key
if: steps.metadata.outputs.available == 'true' && inputs.testbox_id
shell: bash
run: |
STATE=/tmp/.testbox
SSH_PUBLIC_KEY=$(cat "$STATE/ssh_public_key" 2>/dev/null)
if [ -n "$SSH_PUBLIC_KEY" ]; then
mkdir -p ~/.ssh
echo "$SSH_PUBLIC_KEY" >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
echo "SSH public key installed"
fi
- name: Install rsync
if: steps.metadata.outputs.available == 'true' && inputs.testbox_id
shell: bash
run: |
which rsync || sudo apt-get install -y rsync