-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolo5-vm-build.sh
executable file
·360 lines (318 loc) · 10.6 KB
/
solo5-vm-build.sh
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
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
#
# Build job driver for surf-build using "vm" for ephemeral VM management.
#
# Example deployment:
#
# $ GITHUB_USER=... GITHUB_TOKEN=... surf-run \
# -r https://github.com/USER/REPO -- /path/to/surf-vm-build
#
# Optionally, set SURF_LOGDIR to a directory where build job outputs will
# be logged. If unset they go to stdout, which is probably not what you want.
#
# You will want to modify the list of builds and VM templates at the end
# of this script (see the calls to do_build()).
prog_NAME=$(basename $0)
log()
{
echo "$(date -Iseconds) ${prog_NAME}[${SURF_SHA1:-$$}]:" \
"(${SURF_BUILD_NAME:-none}) $@" 1>&2
}
err()
{
log "ERROR: $@"
}
warn()
{
log "WARNING: $@"
}
die()
{
err "$@"
exit 1
}
[ -z "${GITHUB_USER}" ] && die "GITHUB_USER must be set"
[ -z "${GITHUB_TOKEN}" ] && die "GITHUB_TOKEN must be set"
[ -z "${SURF_REPO}" ] && die "SURF_REPO must be set"
[ -z "${SURF_SHA1}" ] && die "SURF_SHA1 must be set"
[ -z "${SURF_NWO}" ] && die "SURF_NWO must be set"
gh_status()
{
[ -z "${SURF_BUILD_NAME}" ] && die "gh_status(): SURF_BUILD_NAME not set"
[ $# -ne 2 ] && die "gh_status(): usage: STATE DESCRIPTION"
curl -s -f --output /dev/null --data @- \
-u "${GITHUB_USER}:${GITHUB_TOKEN}" \
"https://api.github.com/repos/${SURF_NWO}/statuses/${SURF_SHA1}" \
<<EOM
{ "context":"${SURF_BUILD_NAME}", "state":"$1", "description":"$2" }
EOM
# Failure here is deliberately ignored
}
gh_meta_status()
{
[ $# -ne 3 ] && die "gh_meta_status(): usage: CONTEXT STATE DESCRIPTION"
SURF_BUILD_NAME="$1" gh_status "$2" "$3"
}
gh_die()
{
gh_status error "$@"
die "$@"
}
cleanup()
{
if [ -n "${vm_ID}" ]; then
sudo vm stop -f "${vm_ID}"
sudo vm remove "${vm_ID}"
fi
}
trap cleanup 0 INT TERM
sepa()
{
echo -n "----------------------------------------"
echo "----------------------------------------"
}
do_build()
{
[ $# -ne 3 ] && die "do_build(): usage: CONTEXT TEMPLATE BUILD_TYPE"
SURF_BUILD_NAME="$1"
vm_TEMPLATE="$2"
SURF_BUILD_TYPE="$3"
case ${SURF_BUILD_NAME} in
*OpenBSD*)
SURF_SUDO=doas
;;
*)
SURF_SUDO=sudo
;;
esac
#sepa
log "New job: ${SURF_NWO}@${SURF_SHA1}"
log "Github context: ${SURF_BUILD_NAME}, VM template: ${vm_TEMPLATE}"
vm_ID=$(sudo vm clone "${vm_TEMPLATE}") \
|| gh_die "Clone failed ($?)"
log "Booting VM: ${vm_ID}"
gh_status pending "Waiting for ${vm_ID}"
sudo vm start "${vm_ID}" \
|| gh_die "Start ${vm_ID} failed ($?)"
vm_IP=$(timeout 30 sudo vm wait "${vm_ID}") \
|| gh_die "Wait ${vm_ID} failed ($?)"
log "Boot complete, IP address: ${vm_IP}"
#sepa
# Log job output in ${SURF_LOGDIR} if set.
if [ -n "${SURF_LOGDIR}" -a -d "${SURF_LOGDIR}" ]; then
log_FILE="${SURF_LOGDIR}/${SURF_SHA1}.$(date +%s).$$.${SURF_BUILD_NAME}"
log "Logging job output to: ${log_FILE}"
exec 3>${log_FILE}
exec 4>&3
else
exec 3>&1
exec 4>&2
fi
gh_status pending "Building on ${vm_ID}"
timeout 420 ssh -v ${vm_IP} env - \
HOME="/home/build" \
PATH="/home/build/bin:/home/build/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin" \
TMPDIR="/home/build" \
GITHUB_TOKEN="${GITHUB_TOKEN}" \
SURF_REPO="${SURF_REPO}" \
SURF_SHA1="${SURF_SHA1}" \
SURF_RUN_TESTS="yes" \
SURF_BUILD_TYPE="${SURF_BUILD_TYPE}" \
SURF_SUDO="${SURF_SUDO}" \
surf-build -n "${SURF_BUILD_NAME}" \
'; E=$?; if [ $E -eq 255 ]; then exit 99; else exit $E; fi' \
1>&3 2>&4
job_STATUS=$?
# This command can exit with the following status:
# 0: Job succeeded
# 99: Job OR invocation failed (translated from 255 on remote end)
# 124: Timed out
# 255: The SSH connection failed
case "${job_STATUS}" in
124)
gh_status error "Build timed out"
job_TIMEOUT=1
;;
255)
gh_status error "Connection to ${vm_ID} failed"
;;
0)
# The job succeeded. surf-build hopefully published a status,
# so there's nothing we need to do here.
;;
99)
# The job failed OR surf-build died with some fatal error.
# In the latter case, it won't have published a status, which
# leaves the build stuck as "pending" on GitHub.
# The following is a half-hearted attempt to work around this
# stupidity by introspecting the output from the job log.
if [ -n "${log_FILE}" -a -f "${log_FILE}" ]; then
if tail -20 "${log_FILE}" \
| egrep -q '^Failed with exit code: [0-9]+$'; \
then
# Job failed, status hopefully got published.
:
else
# Surf-build probably died, publish a status.
gh_status error "Internal error"
fi
fi
;;
*)
# Catch-all in case of unexpected exit status
gh_status error "Internal error (${job_STATUS})"
;;
esac
#sepa
log "Exit status: ${job_STATUS}"
if [ -n "${job_TIMEOUT}" ]; then
# If the job timed out, kill the VM with prejudice.
# XXX Note, this will leak a DHCP lease.
log "Job timed out, killing VM: ${vm_ID}"
sudo vm stop -f ${vm_ID}
else
# Otherwise, give it some time to gracefully shut down.
log "Stopping and removing VM: ${vm_ID}"
sudo vm stop -t 30 ${vm_ID}
fi
sudo vm remove ${vm_ID}
vm_ID=
log "Done"
}
do_docker_build()
{
[ $# -ne 4 ] && die "do_docker_build(): usage: CONTEXT IMAGE BUILDHOST BUILD_TYPE"
SURF_BUILD_NAME="$1"
DOCKER_IMAGE="$2"
BUILDHOST="$3"
SURF_BUILD_TYPE="$4"
SURF_SUDO=
#sepa
log "New job: ${SURF_NWO}@${SURF_SHA1}"
log "Github context: ${SURF_BUILD_NAME}, for host: ${BUILDHOST}"
#sepa
# Log job output in ${SURF_LOGDIR} if set.
if [ -n "${SURF_LOGDIR}" -a -d "${SURF_LOGDIR}" ]; then
log_FILE="${SURF_LOGDIR}/${SURF_SHA1}.$(date +%s).$$.${SURF_BUILD_NAME}"
log "Logging job output to: ${log_FILE}"
exec 3>${log_FILE}
exec 4>&3
else
exec 3>&1
exec 4>&2
fi
gh_status pending "Building on ${BUILDHOST}"
# XXX Check if "docker run" can interpose another status code here.
# XXX EMAIL needed here otherwise git/dugite complain about unconfigured
# XXX Git.
timeout 420 ssh -v ${BUILDHOST} docker run --rm \
-e GITHUB_TOKEN="${GITHUB_TOKEN}" \
-e SURF_REPO="${SURF_REPO}" \
-e SURF_SHA1="${SURF_SHA1}" \
-e SURF_RUN_TESTS="yes" \
-e SURF_BUILD_TYPE="${SURF_BUILD_TYPE}" \
-e SURF_SUDO="${SURF_SUDO}" \
-e EMAIL="Solo5-CI\ \<[email protected]\>" \
--device /dev/net/tun \
--device /dev/kvm \
--cap-add NET_ADMIN \
--tmpfs /tmp:rw,exec \
${DOCKER_IMAGE} \
surf-build -n "${SURF_BUILD_NAME}" \
'; E=$?; if [ $E -eq 255 ]; then exit 99; else exit $E; fi' \
1>&3 2>&4
job_STATUS=$?
# This command can exit with the following status:
# 0: Job succeeded
# 99: Job OR invocation failed (translated from 255 on remote end)
# 124: Timed out
# 255: The SSH connection failed
case "${job_STATUS}" in
124)
gh_status error "Build timed out"
job_TIMEOUT=1
;;
255)
gh_status error "Connection to ${vm_ID} failed"
;;
0)
# The job succeeded. surf-build hopefully published a status,
# so there's nothing we need to do here.
;;
99)
# The job failed OR surf-build died with some fatal error.
# In the latter case, it won't have published a status, which
# leaves the build stuck as "pending" on GitHub.
# The following is a half-hearted attempt to work around this
# stupidity by introspecting the output from the job log.
if [ -n "${log_FILE}" -a -f "${log_FILE}" ]; then
if tail -20 "${log_FILE}" \
| egrep -q '^FAILURE: .+ failed: Status: [0-9]+$'; \
then
# Job failed, status hopefully got published.
:
else
# Surf-build probably died, publish a status.
gh_status error "Internal error"
fi
fi
;;
*)
# Catch-all in case of unexpected exit status
gh_status error "Internal error (${job_STATUS})"
;;
esac
#sepa
log "Exit status: ${job_STATUS}"
# TODO: Needs docker create/docker logs/docker kill combo
# if [ -n "${job_TIMEOUT}" ]; then
# # If the job timed out, kill the VM with prejudice.
# # XXX Note, this will leak a DHCP lease.
# log "Job timed out, killing VM: ${vm_ID}"
# sudo vm stop -f ${vm_ID}
# else
# # Otherwise, give it some time to gracefully shut down.
# log "Stopping and removing VM: ${vm_ID}"
# sudo vm stop -t 30 ${vm_ID}
# fi
# sudo vm remove ${vm_ID}
# vm_ID=
log "Done"
}
log "Builds in progress"
gh_meta_status 00-info pending "Builds in progress ($(date --utc))"
# First group that can run in parallel
GROUP=()
( do_build 10-basic-x86_64-Debian10 ci-solo5-debian10 basic ) &
GROUP+=($!)
( do_build 11-basic-x86_64-FreeBSD11 ci-solo5-freebsd11 basic ) &
GROUP+=($!)
# Don't care about this one (remote)
( do_docker_build 12-basic-aarch64-Debian10 mato/solo5-builder:aarch64-Debian10-gcc830 rpi-builder.ci.lan basic ) &
wait ${GROUP[*]}
# Second group
GROUP=()
( do_build 13-basic-x86_64-OpenBSD66 ci-solo5-openbsd66 basic ) &
GROUP+=($!)
( do_build 14-basic-x86_64-FreeBSD12 ci-solo5-freebsd12 basic ) &
GROUP+=($!)
wait ${GROUP[*]}
# Third group
GROUP=()
( do_build 15-basic-x86_64-OpenBSD67 ci-solo5-openbsd67 basic ) &
GROUP+=($!)
( do_build 16-basic-x86_64-Debian11 ci-solo5-debian11 basic ) &
GROUP+=($!)
wait ${GROUP[*]}
# Run E2E on it's own as it's fairly CPU intensive
# Temporarily disabled.
# GROUP=()
# ( do_build 20-e2e-x86_64-Debian10 ci-e2e-debian10 e2e ) &
# GROUP+=($!)
#
# wait ${GROUP[*]}
# Wait for ALL builders to finish, including remote
wait
log "All builders finished"
gh_meta_status 00-info success "All builders finished ($(date --utc))"
exit 0