blob: 31469263e0c4194de973f8519214d77b2f14dbde (
plain) (
blame)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
#!/bin/sh
# configurable variables
pb=/var/portbuild
# XXX unused
get_latest_snap() {
snap=$1
zfs list -rHt snapshot ${snap} | tail -1 | awk '{print $1}'
}
now() {
date +%Y%m%d%H%M%S
}
do_list() {
arch=$1
branch=$2
buildpar=/var/portbuild/${arch}/${branch}/builds
if [ -d ${buildpar} ]; then
snaps=$(cd ${buildpar}; ls -1d 2* 2> /dev/null)
echo "The following builds are active:"
echo ${snaps}
if [ -L ${buildpar}/latest -a -d ${buildpar}/latest/ ]; then
link=$(readlink ${buildpar}/latest)
link=${link%/}
link=${link##*/}
echo "Latest build is: ${link}"
fi
else
echo "No such build environment ${arch}/${branch}"
exit 1
fi
}
do_create() {
arch=$1
branch=$2
buildid=$3
shift 3
archdir=${pbab}/builds
if [ ! -d ${archdir} ]; then
mkdir -p ${archdir} || exit 1
chown -R ports-${arch}:portmgr ${pbab}
chmod -R g+w ${pbab}
fi
builddir=$(realpath ${archdir})/${buildid}
if [ -d ${builddir} ]; then
echo "Can't create ${builddir}, it already exists"
exit 1
fi
# XXX needed?
buildenv ${pb} ${arch} ${branch} ${builddir}
mountpoint=${builddir}
newfs=a/portbuild/${arch}/${buildid}
zfs create -o mountpoint=${mountpoint} ${newfs} || exit 1
chown -R ports-${arch}:portmgr ${mountpoint}
chmod g+w ${mountpoint}
do_portsupdate_inner ${arch} ${branch} ${buildid} ${builddir} $@
do_srcupdate_inner ${arch} ${branch} ${buildid} ${builddir} $@
ln -sf ${builddir} ${pbab}/builds/latest
echo "New build ID is ${buildid}"
}
do_clone() {
arch=$1
branch=$2
buildid=$3
builddir=$4
shift 4
if [ "$#" -gt 0 ]; then
newid=$1
shift
else
newid=$(now)
fi
tmp=$(realpath ${builddir})
tmp=${tmp%/}
newbuilddir="${tmp%/*}/${newid}"
oldfs=a/portbuild/${arch}/${buildid}
newfs=a/portbuild/${arch}/${newid}
zfs snapshot ${oldfs}@${newid}
zfs clone ${oldfs}@${newid} ${newfs}
zfs set mountpoint=${newbuilddir} ${newfs}
zfs promote ${newfs}
if zfs list -H -t filesystem ${oldfs}/ports 2> /dev/null; then
portsnap=${oldfs}/ports@${newid}
zfs snapshot ${portsnap}
zfs clone ${portsnap} ${newfs}/ports
zfs promote ${newfs}/ports
fi
if zfs list -H -t filesystem ${oldfs}/src 2> /dev/null; then
srcsnap=${oldfs}/src@${newid}
zfs snapshot ${srcsnap}
zfs clone ${srcsnap} ${newfs}/src
zfs promote ${newfs}/src
fi
if [ -d ${newbuilddir} ]; then
if [ ! -f ${pbab}/builds/previous/.keep ]; then
/var/portbuild/scripts/build destroy ${arch} ${branch} previous
fi
rm -f ${pbab}/builds/previous
mv ${pbab}/builds/latest ${pbab}/builds/previous
ln -sf ${newbuilddir} ${pbab}/builds/latest
fi
echo "New build ID is ${newid}"
}
do_portsupdate() {
arch=$1
branch=$2
buildid=$3
builddir=$4
shift 4
if [ $# -gt 0 ]; then
arg=$1
shift
fi
destroy_fs a/portbuild/${arch} ${buildid} /ports || exit 1
if [ "${arg}" = "-umount" ]; then
return
fi
do_portsupdate_inner ${arch} ${branch} ${buildid} ${builddir} $@
}
do_portsupdate_inner() {
arch=$1
branch=$2
buildid=$3
builddir=$4
shift 4
echo "================================================"
echo "Reimaging ZFS ports tree on ${builddir}/ports"
echo "================================================"
portsfs=a/portbuild/${arch}/${buildid}/ports
now=$(now)
zfs snapshot a/snap/ports@${now}
zfs clone a/snap/ports@${now} ${portsfs}
zfs set mountpoint=${builddir}/ports ${portsfs}
cp ${builddir}/ports/cvsdone ${builddir}
}
do_srcupdate() {
arch=$1
branch=$2
buildid=$3
builddir=$4
shift 4
if [ $# -gt 0 ]; then
arg=$1
shift
fi
destroy_fs a/portbuild/${arch} ${buildid} /src || exit 1
if [ "${arg}" = "-umount" ]; then
return
fi
do_srcupdate_inner ${arch} ${branch} ${buildid} ${builddir} $@
}
do_srcupdate_inner() {
arch=$1
branch=$2
buildid=$3
builddir=$4
shift 4
echo "================================================"
echo "Reimaging ZFS src tree on ${builddir}/src"
echo "================================================"
strippedbranch=${branch%%-exp}
now=$(now)
srcfs=a/portbuild/${arch}/${buildid}/src
zfs snapshot a/snap/src-${strippedbranch}@${now}
zfs clone a/snap/src-${strippedbranch}@${now} ${srcfs}
zfs set mountpoint=${builddir}/src ${srcfs}
}
cleanup_client() {
arch=$1
branch=$2
buildid=$3
mach=$4
arg=$5
# XXX use same exclusion protocol as claim-chroot
echo "Started cleaning up ${arch}/${branch} build ID ${buildid} on ${mach}"
test -f ${pb}/${arch}/portbuild.${mach} && . ${pb}/${arch}/portbuild.${mach}
# Kill off builds and clean up chroot
${pb}/scripts/dosetupnode ${arch} ${branch} ${buildid} ${mach} -nocopy -queue -full
echo "Finished cleaning up ${arch}/${branch} build ID ${buildid} on ${mach}"
}
do_cleanup() {
arch=$1
branch=$2
buildid=$3
builddir=$4
arg=$5
shift 5
for i in `cat ${pb}/${arch}/mlist`; do
cleanup_client ${arch} ${branch} ${buildid} ${i} ${arg} &
done
wait
}
do_upload() {
arch=$1
branch=$2
buildid=$3
builddir=$4
shift 4
echo "Not implemented yet"
exit 1
}
test_fs() {
local fs=$1
zfs list -Ht filesystem ${fs} > /dev/null 2>&1
}
get_latest_child() {
local fs=$1
# Return the child of this filesystem with lexicographically
# highest name
#
# XXX if a filesystem is cloned into a different prefix
# (e.g. different arch) then we may not get the most recent one
# but that should not happen.
zfs get -H -o name,value origin | grep ${fs} | sort | \
(while read zfs origin; do
if [ "${origin%@*}" = "${fs}" ]; then
child=${zfs}
fi
done; echo ${child})
}
get_parent() {
local fs=$1
# Check whether this filesystem has a parent
zfs get -H -o value origin ${fs} | \
(read snap;
case "${snap}" in
-|a/snap/*)
;;
*)
parent=${snap}
;;
esac; echo ${parent})
}
destroy_fs() {
fs=$1
buildid=$2
subfs=$3
fullfs=${fs}/${buildid}${subfs}
if test_fs "${fullfs}"; then
# We can destroy a leaf filesystem (having no dependent
# clones) with no further effort. However if we are
# destroying the root of the clone tree then we have to
# promote a child to be the new root.
#
# XXX In principle we might have to iterate until we end up as
# a leaf but I don't know if this can happen.
echo "Filesystem ${fullfs}"
child=$(get_latest_child ${fullfs})
parent=$(get_parent ${fullfs})
echo "Filesystem has parent ${parent}"
if [ -z "${child}" ]; then
echo "Filesystem is a leaf"
else
echo "Filesystem has latest child ${child}"
# Check whether filesystem is root
if [ -z "${parent}" ]; then
echo "Filesystem is root; promoting ${child}"
zfs promote ${child}
parent=$(get_parent ${fullfs})
echo "New parent is ${parent}"
else
echo "Filesystem has parent ${parent} and cannot be destroyed"
return 1
fi
fi
# We might have snapshots on the target filesystem, e.g. if it
# is both the head and tail of its clone tree. They should be
# unreferenced.
# We have to grep because zfs list -H returns an error instead of
# a null list if no snapshots exist
if ! (zfs list -r -H -o name -t snapshot ${fullfs} | grep "^${fullfs}@" | xargs -n 1 zfs destroy); then
return 1
fi
# The target filesystem should now be unreferenced
if ! zfs destroy -f "${fullfs}"; then
return 1
fi
# Destroy the origin snapshot, which should be unreferenced
if [ ! -z "${parent}" ]; then
if ! zfs destroy -f ${parent}; then
return 1
fi
fi
fi
}
do_destroy() {
arch=$1
branch=$2
buildid=$3
builddir=$4
shift 4
buildid=$(resolve ${pb} ${arch} ${branch} ${buildid})
if [ -z "${buildid}" ]; then
echo "Invalid build ID ${buildid}"
exit 1
fi
latestid=$(resolve ${pb} ${arch} ${branch} latest)
if [ "${buildid}" = "${latestid}" ]; then
echo "Cannot destroy latest build"
exit 1
fi
destroy_fs a/portbuild/${arch} ${buildid} /ports || exit 1
destroy_fs a/portbuild/${arch} ${buildid} /src || exit 1
destroy_fs a/portbuild/${arch} ${buildid} || exit 1
rmdir ${builddir}
}
# Run a command as root if running as user
# Authentication and command validation is taken care of by buildproxy
proxy_root() {
cmd=$1
arch=$2
branch=$3
buildid=$4
builddir=$5
shift 5
args=$@
id=$(id -u)
if [ ${id} != "0" ]; then
/var/portbuild/scripts/buildproxy-client "build ${cmd} ${arch} ${branch} ${buildid} ${args}"
error=$?
if [ ${error} -eq 254 ]; then
echo "Proxy error"
fi
else
eval "do_${cmd} ${arch} ${branch} ${buildid} ${builddir} ${args}"
error=$?
fi
exit ${error}
}
# Run a command as the ports-${arch} user if root
proxy_user() {
cmd=$1
arch=$2
branch=$3
buildid=$4
builddir=$5
shift 5
args=$@
id=$(id -u)
if [ ${id} != "0" ]; then
eval "do_${cmd} ${arch} ${branch} ${buildid} \"${builddir}\" ${args}"
error=$?
else
su ports-${arch} -c "/var/portbuild/scripts/build ${cmd} ${arch} ${branch} ${buildid} \"${builddir}\" ${args}"
error=$?
fi
exit ${error}
}
usage () {
echo "usage: build <command> <arch> <branch> [<buildid>] [<options> ...]"
exit 1
}
##################
if [ $# -lt 3 ]; then
usage
fi
cmd=$1
arch=$2
branch=$3
shift 3
. ${pb}/${arch}/portbuild.conf
. ${pb}/scripts/buildenv
pbab=${pb}/${arch}/${branch}
validate_env ${arch} ${branch} || exit 1
# Not every command requires a buildid as arg
if [ $# -ge 1 ]; then
buildid=$1
shift 1
# Most commands require a buildid that is valid on the server. The
# exception is "cleanup" which is cleaning up a client build that may
# already be destroyed on the server.
case "$cmd" in
cleanup)
# Resolve symlinks but don't bail if the build doesn't exist.
newbuildid=$(resolve ${pb} ${arch} ${branch} ${buildid})
if [ ! -z "${newbuildid}" -a "${newbuildid}" != "${buildid}" ]; then
echo "Resolved ${buildid} to ${newbuildid}"
buildid=${newbuildid}
builddir=$(realpath ${pbab}/builds/${buildid}/)
# We can't rely on buildenv for this code path
fi
;;
create)
# XXX some way to avoid the latest/previous dance?
if [ "${buildid}" = "latest" ]; then
buildid=$(now)
elif [ "${buildid}" = "previous" ]; then
echo "Use build clone latest instead"
exit 1
fi
# We can't rely on buildenv for this code path
;;
*)
newbuildid=$(resolve ${pb} ${arch} ${branch} ${buildid})
if [ -z "${newbuildid}" ]; then
echo "Build ID ${buildid} does not exist"
exit 1
fi
if [ ${newbuildid} != ${buildid} ]; then
echo "Resolved ${buildid} to ${newbuildid}"
buildid=${newbuildid}
fi
builddir=$(realpath ${pbab}/builds/${buildid}/)
buildenv ${pb} ${arch} ${branch} ${builddir}
;;
esac
fi
# Unprivileged commands
case "$cmd" in
list)
do_list ${arch} ${branch} $@ || exit 1
;;
create)
# XXX some way to avoid the latest/previous dance?
if [ -z "${buildid}" ]; then
buildid=$(now)
else
buildid=${buildid%/}
fi
proxy_root create ${arch} ${branch} ${buildid} $@ || exit 1
;;
clone)
if [ -z "${buildid}" ]; then
usage
fi
proxy_root clone ${arch} ${branch} ${buildid} ${builddir} $@ || exit 1
;;
portsupdate)
if [ -z "${buildid}" ]; then
usage
fi
proxy_root portsupdate ${arch} ${branch} ${buildid} ${builddir} $@ || exit 1
;;
srcupdate)
if [ -z "${buildid}" ]; then
usage
fi
proxy_root srcupdate ${arch} ${branch} ${buildid} ${builddir} $@ || exit 1
;;
cleanup)
if [ -z "${buildid}" ]; then
usage
fi
# builddir may be null if cleaning up a destroyed build
proxy_user cleanup ${arch} ${branch} ${buildid} "${builddir}" $@ || exit 1
;;
upload)
if [ -z "${buildid}" ]; then
usage
fi
proxy_user upload ${arch} ${branch} ${buildid} ${builddir} $@ || exit 1
;;
destroy)
if [ -z "${buildid}" ]; then
usage
fi
proxy_root destroy ${arch} ${branch} ${buildid} ${builddir} $@ || exit 1
;;
*)
echo "build: invalid command: $cmd"
exit 1
;;
esac
|