-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.sh
More file actions
executable file
·349 lines (311 loc) · 13.3 KB
/
main.sh
File metadata and controls
executable file
·349 lines (311 loc) · 13.3 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/bash
set -e
type az gh > /dev/null
template_setup() {
template_file="setup.sh"
sed_script="s|{{token}}|${RUNNER_TOKEN}|g"
sed_script="${sed_script};s|{{repo}}|${GITHUB_REPO}|g"
sed_script="${sed_script};s|{{label}}|${LABEL}|g"
sed_script="${sed_script};s|{{vm_username}}|${VM_USERNAME}|g"
sed "${sed_script}" "${template_file}.template" > "${template_file}"
}
if [[ -z "${GITHUB_REPO}" ]];then
>&2 echo "env var GITHUB_REPO not defined"
exit 1
fi
if [[ -z "${GH_TOKEN}" ]];then
>&2 echo "env var GH_TOKEN not defined"
exit 1
fi
if [[ -z "${RUN_ID}" ]];then
>&2 echo "env var RUN_ID not defined"
exit 1
fi
: "${RESOURCE_GROUP_NAME:=rgghrunner${RUN_ID}}"
: "${LOCATION:=westus2}"
: "${VM_IMAGE:=canonical:ubuntu-24_04-lts:server:latest}"
: "${VM_SPOT:=False}"
: "${VM_SIZE:=Standard_D4ms}"
: "${VM_DISK_SIZE:=127}"
: "${VM_NAME:=ghrunner${RUN_ID}}"
: "${VM_USERNAME:=ghradmin}"
: "${STORAGE_BLOB_URI:=}"
: "${SSH_KEY_BASENAME:=id_rsa}"
if [[ -z "${UNIQ_LABEL}" ]]; then
UNIQ_LABEL=$(shuf -er -n8 {a..z} | paste -sd "")
fi
LABEL="azure,${UNIQ_LABEL}"
# Get runner token with error handling
if ! RUNNER_TOKEN=$(gh api -XPOST --jq '.token' "repos/${GITHUB_REPO}/actions/runners/registration-token" 2>/dev/null); then
>&2 echo "Error: Failed to get GitHub runner registration token"
>&2 echo "Check GH_TOKEN permissions and repository access"
exit 1
fi
if [[ $1 = '--destroy' ]]; then
>&2 echo "Starting destroy operation for VM: ${VM_NAME}"
# Set up destroy script
template_setup
# Initialize cleanup success flag
cleanup_success=false
# Try to get VM IP, but don't fail if we can't
>&2 echo "Retrieving VM IP address..."
if VM_IP=$(az vm show --show-details --resource-group "${RESOURCE_GROUP_NAME}" --name "${VM_NAME}" --query publicIps --output tsv 2>/dev/null); then
if [[ -n "$VM_IP" && "$VM_IP" != "null" ]]; then
>&2 echo "VM IP found: ${VM_IP}"
# Attempt SSH cleanup with proper error handling
>&2 echo "Attempting to clean up GitHub runner service via SSH..."
# Add VM to known hosts (ignore errors)
ssh-keyscan -T 10 "${VM_IP}" >> "${HOME}/.ssh/known_hosts" 2>/dev/null || true
# Try SSH cleanup with timeouts and error handling
if timeout 60 ssh \
-o ConnectTimeout=15 \
-o ServerAliveInterval=5 \
-o ServerAliveCountMax=3 \
-o StrictHostKeyChecking=accept-new \
-o UserKnownHostsFile="${HOME}/.ssh/known_hosts" \
-o BatchMode=yes \
"${VM_USERNAME}@${VM_IP}" 'bash -s -- --destroy' < setup.sh 2>/dev/null; then
>&2 echo "Successfully cleaned up GitHub runner service"
cleanup_success=true
else
>&2 echo "Warning: SSH cleanup failed or timed out"
>&2 echo "Possible causes:"
>&2 echo " - VM is unreachable (spot eviction, network issues)"
>&2 echo " - SSH service not available"
>&2 echo " - GitHub runner service already stopped"
>&2 echo " - VM is in process of shutting down"
fi
# Clean up known hosts entry
ssh-keygen -R "${VM_IP}" 2>/dev/null || true
else
>&2 echo "Warning: VM IP is null or empty"
fi
else
>&2 echo "Warning: Could not retrieve VM information"
>&2 echo "VM may already be deleted or resource group may not exist"
fi
# Check if VM actually exists before trying to delete it
>&2 echo "Checking if VM exists before deletion..."
if az vm show --resource-group "${RESOURCE_GROUP_NAME}" --name "${VM_NAME}" --output none 2>/dev/null; then
>&2 echo "VM exists, proceeding with deletion..."
# Delete the VM with retry logic
max_delete_attempts=3
delete_success=false
for ((attempt=1; attempt<=max_delete_attempts; attempt++)); do
>&2 echo "VM deletion attempt ${attempt}/${max_delete_attempts}..."
if az vm delete --resource-group "${RESOURCE_GROUP_NAME}" --name "${VM_NAME}" --yes --output none 2>/dev/null; then
>&2 echo "VM deletion successful"
delete_success=true
break
else
>&2 echo "VM deletion attempt ${attempt} failed"
if [[ $attempt -lt $max_delete_attempts ]]; then
>&2 echo "Waiting 30 seconds before retry..."
sleep 30
fi
fi
done
if [[ "$delete_success" != "true" ]]; then
>&2 echo "Warning: VM deletion failed after ${max_delete_attempts} attempts"
>&2 echo "VM may have been deleted externally or there may be a permission issue"
fi
else
>&2 echo "VM does not exist (already deleted or never created)"
fi
# Check for other VMs in the resource group
>&2 echo "Checking for other VMs in resource group..."
if _vms=$(az vm list --resource-group "${RESOURCE_GROUP_NAME}" --query "[].name" --output tsv 2>/dev/null); then
if [[ -z "${_vms}" ]]; then
>&2 echo "No other VMs found in resource group, initiating resource group deletion..."
# Delete the resource group with error handling
if az group delete --name "${RESOURCE_GROUP_NAME}" --no-wait --yes --output none 2>/dev/null; then
>&2 echo "Resource group deletion initiated successfully"
else
>&2 echo "Warning: Resource group deletion failed"
>&2 echo "Manual cleanup may be required:"
>&2 echo " az group delete --name ${RESOURCE_GROUP_NAME} --yes"
fi
else
>&2 echo "Other VMs still exist in resource group:"
>&2 echo "${_vms}" | sed 's/^/ - /'
>&2 echo "Resource group will be preserved"
fi
else
>&2 echo "Warning: Could not list VMs in resource group (may already be deleted)"
fi
# Final status
if [[ "$cleanup_success" == "true" ]]; then
>&2 echo "Destroy operation completed successfully"
else
>&2 echo "Destroy operation completed with warnings (SSH cleanup failed but VM was deleted)"
fi
exit 0
fi
# Force destroy mode (skip SSH cleanup entirely)
if [[ $1 = '--force-destroy' ]]; then
>&2 echo "Starting force destroy operation (skipping SSH cleanup)..."
# Skip SSH cleanup, go straight to VM deletion
>&2 echo "Skipping GitHub runner service cleanup"
if az vm show --resource-group "${RESOURCE_GROUP_NAME}" --name "${VM_NAME}" --output none 2>/dev/null; then
>&2 echo "Force deleting VM: ${VM_NAME}"
az vm delete --resource-group "${RESOURCE_GROUP_NAME}" --name "${VM_NAME}" --yes --output none || {
>&2 echo "Warning: Force VM deletion failed"
}
else
>&2 echo "VM does not exist"
fi
# Check for other VMs and clean up resource group if empty
_vms=$(az vm list --resource-group "${RESOURCE_GROUP_NAME}" --query "[].name" --output tsv 2>/dev/null || echo "")
if [[ -z "${_vms}" ]]; then
>&2 echo "Force deleting resource group: ${RESOURCE_GROUP_NAME}"
az group delete --name "${RESOURCE_GROUP_NAME}" --no-wait --yes --output none || {
>&2 echo "Warning: Force resource group deletion failed"
}
fi
>&2 echo "Force destroy operation completed"
exit 0
fi
# Create the resource group
>&2 echo "Checking/creating resource group: ${RESOURCE_GROUP_NAME}"
_rg_exists=$(az group show --name "${RESOURCE_GROUP_NAME}" --output none &> /dev/null;echo $?)
if [[ $_rg_exists -ne 0 ]];then
>&2 echo "Creating resource group..."
az group create --name "${RESOURCE_GROUP_NAME}" --location "${LOCATION}" --output none
else
>&2 echo "Resource group already exists"
fi
# Set up setup script
template_setup
_vm_exists=$(az vm show --resource-group "${RESOURCE_GROUP_NAME}" --name "${VM_NAME}" --output none &> /dev/null;echo $?)
if [[ $_vm_exists -ne 0 ]];then
>&2 echo "Creating VM: ${VM_NAME}"
>&2 echo " Type: ${VM_SPOT}"
>&2 echo " Size: ${VM_SIZE}"
>&2 echo " Image: ${VM_IMAGE}"
if [[ ! -z ${STORAGE_BLOB_URI} ]];then
case ${VM_SPOT} in
"True")
>&2 echo "Creating spot instance VM with boot diagnostics storage..."
# Create the spot instance vm
az vm create \
--resource-group "${RESOURCE_GROUP_NAME}" \
--name "${VM_NAME}" \
--image "${VM_IMAGE}" \
--admin-username "${VM_USERNAME}" \
--security-type "Standard" \
--size "${VM_SIZE}" \
--priority "Spot" \
--max-price "-1" \
--eviction-policy "Delete" \
--ssh-key-values "${HOME}/.ssh/${SSH_KEY_BASENAME}.pub" \
--custom-data setup.sh \
--public-ip-sku Standard \
--boot-diagnostics-storage ${STORAGE_BLOB_URI} \
--os-disk-delete-option Delete \
--os-disk-size-gb ${VM_DISK_SIZE} \
--output none \
--verbose
;;
*)
>&2 echo "Creating regular VM with boot diagnostics storage..."
# Create the regular vm
az vm create \
--resource-group "${RESOURCE_GROUP_NAME}" \
--name "${VM_NAME}" \
--image "${VM_IMAGE}" \
--admin-username "${VM_USERNAME}" \
--security-type "Standard" \
--size "${VM_SIZE}" \
--ssh-key-values "${HOME}/.ssh/${SSH_KEY_BASENAME}.pub" \
--custom-data setup.sh \
--public-ip-sku Standard \
--boot-diagnostics-storage ${STORAGE_BLOB_URI} \
--os-disk-delete-option Delete \
--os-disk-size-gb ${VM_DISK_SIZE} \
--output none \
--verbose
;;
esac
else
case ${VM_SPOT} in
"True")
>&2 echo "Creating spot instance VM with managed boot diagnostics..."
# Create the spot instance vm
az vm create \
--resource-group "${RESOURCE_GROUP_NAME}" \
--name "${VM_NAME}" \
--image "${VM_IMAGE}" \
--admin-username "${VM_USERNAME}" \
--security-type "Standard" \
--size "${VM_SIZE}" \
--priority "Spot" \
--max-price "-1" \
--eviction-policy "Delete" \
--ssh-key-values "${HOME}/.ssh/${SSH_KEY_BASENAME}.pub" \
--custom-data setup.sh \
--public-ip-sku Standard \
--os-disk-delete-option Delete \
--os-disk-size-gb ${VM_DISK_SIZE} \
--output none \
--verbose
az vm boot-diagnostics enable \
--resource-group "${RESOURCE_GROUP_NAME}" \
--name "${VM_NAME}" \
--output none \
--verbose
;;
*)
>&2 echo "Creating regular VM with managed boot diagnostics..."
# Create the regular vm
az vm create \
--resource-group "${RESOURCE_GROUP_NAME}" \
--name "${VM_NAME}" \
--image "${VM_IMAGE}" \
--security-type "Standard" \
--admin-username "${VM_USERNAME}" \
--size "${VM_SIZE}" \
--ssh-key-values "${HOME}/.ssh/${SSH_KEY_BASENAME}.pub" \
--custom-data setup.sh \
--public-ip-sku Standard \
--os-disk-delete-option Delete \
--os-disk-size-gb ${VM_DISK_SIZE} \
--output none \
--verbose
az vm boot-diagnostics enable \
--resource-group "${RESOURCE_GROUP_NAME}" \
--name "${VM_NAME}" \
--output none \
--verbose
;;
esac
fi
>&2 echo "VM creation completed"
else
>&2 echo "VM already exists: ${VM_NAME}"
fi
# Get VM IP with error handling
>&2 echo "Retrieving VM IP address..."
if VM_IP=$(az vm show --show-details --resource-group "${RESOURCE_GROUP_NAME}" --name "${VM_NAME}" --query publicIps --output tsv 2>/dev/null); then
if [[ -n "$VM_IP" && "$VM_IP" != "null" ]]; then
>&2 echo "VM IP: ${VM_IP}"
else
>&2 echo "Warning: VM IP is null or empty"
VM_IP=""
fi
else
>&2 echo "Warning: Could not retrieve VM IP"
VM_IP=""
fi
# Output results
jq -n \
--arg ip "${VM_IP}" \
--arg resource_group "${RESOURCE_GROUP_NAME}" \
--arg location "${LOCATION}" \
--arg vm_image "${VM_IMAGE}" \
--arg vm_size "${VM_SIZE}" \
--arg vm_name "${VM_NAME}" \
--arg vm_username "${VM_USERNAME}" \
--arg vm_disk_size "${VM_DISK_SIZE}" \
--arg uniq_label "${UNIQ_LABEL}" \
--arg ssh_key_basename "${SSH_KEY_BASENAME}" \
'$ARGS.named'