-
Notifications
You must be signed in to change notification settings - Fork 3
/
pkg_comp.sh
706 lines (598 loc) · 24.2 KB
/
pkg_comp.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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# Copyright 2013 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# \file pkg_comp.sh
# Entry point and main program logic.
shtk_import cleanup
shtk_import cli
shtk_import config
shtk_import cvs
shtk_import hw
: ${PKG_COMP_SHTK_MODULESDIR:="__PKG_COMP_SHTK_MODULESDIR__"}
SHTK_MODULESPATH="${PKG_COMP_SHTK_MODULESDIR}" shtk_import git
SHTK_MODULESPATH="${PKG_COMP_SHTK_MODULESDIR}" shtk_import pkgsrc
# List of valid configuration variables.
#
# Please remember to update pkg_comp.conf(5) if you change this list.
PKG_COMP_CONFIG_VARS="AUTO_PACKAGES CVS_ROOT CVS_TAG DISTDIR EXTRA_MKCONF
FETCH_VCS GIT_BRANCH GIT_URL LOCALBASE NJOBS PACKAGES
PBULK_LOG PBULK_PACKAGES PKG_DBDIR PKG_DEVELOPER PKGSRCDIR
SANDBOX_CONFFILE SYSCONFDIR UPDATE_SOURCES VARBASE"
# Paths to installed files.
#
# Can be overriden for test purposes only.
: ${PKG_COMP_ETCDIR="__PKG_COMP_ETCDIR__"}
: ${PKG_COMP_SHAREDIR="__PKG_COMP_SHAREDIR__"}
: ${SANDBOXCTL="__SANDBOXCTL__"}
# Sets defaults for configuration variables and hooks that need to exist.
#
# This function should be before the configuration file has been loaded. This
# means that the user can undefine a required configuration variable, but we let
# him shoot himself in the foot if he so desires.
pkg_comp_set_defaults() {
# Please remember to update pkg_comp.conf(5) if you change any default
# values.
shtk_config_set CVS_ROOT ":ext:[email protected]:/cvsroot"
shtk_config_set DISTDIR "/usr/pkgsrc/distfiles"
shtk_config_set FETCH_VCS "cvs"
shtk_config_set GIT_BRANCH "trunk"
shtk_config_set GIT_URL "https://github.com/NetBSD/pkgsrc.git"
shtk_config_set LOCALBASE "/usr/pkg"
shtk_config_set NJOBS "$(shtk_hw_ncpus)"
shtk_config_set PACKAGES "/usr/pkgsrc/packages"
shtk_config_set PBULK_LOG "/usr/pkgsrc/packages/log"
shtk_config_set PBULK_PACKAGES "/usr/pkgsrc/packages/pbulk"
shtk_config_set PKG_DBDIR "/usr/pkg/libdata/pkgdb"
shtk_config_set PKG_DEVELOPER "no"
shtk_config_set PKGSRCDIR "/usr/pkgsrc"
shtk_config_set SYSCONFDIR "/etc"
shtk_config_set UPDATE_SOURCES "true"
shtk_config_set VARBASE "/var"
post_fetch_hook() { true; }
}
# Executes sandboxctl based on the pkg_comp configuration.
#
# \params ... The options and arguments to forward to sandboxctl.
#
# \return The exit code of sandboxctl.
run_sandboxctl() {
# Generate a temporary configuration file for sandboxctl that stitches
# together our default hooks and the user-provided configuration.
#
# Note: it feels tempting to mix together the contents of pkg_comp.conf
# and sandboxctl.conf into the same file because their variables are
# disjoint. However, doing so is extremely confusing due to the way
# shtk_config works: for example, not all global variables would be visible
# in all hooks, and the -o flag would not be able to override sandbox.conf
# settings. Therefore, while it feels uglier to keep things separate, it
# will avoid confusion down the road.
if [ -z "${_PKG_COMP_SANDBOXCTL_CONFIG_FILE}" ]; then
local userconf=
if shtk_config_has SANDBOX_CONFFILE; then
userconf="$(shtk_config_get SANDBOX_CONFFILE)"
[ -e "${userconf}" ] || shtk_cli_error "sandbox configuration" \
"file ${userconf} does not exist"
fi
local pattern="${TMPDIR:-/tmp}/pkg_comp.XXXXXX"
_PKG_COMP_SANDBOXCTL_CONFIG_FILE="$(mktemp "${pattern}" 2>/dev/null)"
remove_conffile() {
rm -f "${_PKG_COMP_SANDBOXCTL_CONFIG_FILE}"
}
shtk_cleanup_register remove_conffile
{
cat "${PKG_COMP_SHAREDIR}/sandbox.conf.pre"
cat <<EOF
DISTDIR="$(shtk_config_has DISTDIR && shtk_config_get DISTDIR)"
PACKAGES="$(shtk_config_has PACKAGES && shtk_config_get PACKAGES)"
PBULK_LOG="$(shtk_config_has PBULK_LOG && shtk_config_get PBULK_LOG)"
PBULK_PACKAGES="$(shtk_config_has PBULK_PACKAGES \
&& shtk_config_get PBULK_PACKAGES)"
PKGSRCDIR="$(shtk_config_has PKGSRCDIR && shtk_config_get PKGSRCDIR)"
PKG_DEVELOPER="$(shtk_config_has PKG_DEVELOPER \
&& shtk_config_get PKG_DEVELOPER)"
EOF
[ -z "${userconf}" ] || cat "${userconf}"
cat "${PKG_COMP_SHAREDIR}/sandbox.conf.post"
} >>"${_PKG_COMP_SANDBOXCTL_CONFIG_FILE}" \
|| shtk_cli_error "Failed to create sandbox.conf file"
fi
local vflag=
if shtk_cli_log_level debug; then
vflag=-v
fi
"${SANDBOXCTL}" -c"${_PKG_COMP_SANDBOXCTL_CONFIG_FILE}" ${vflag} "${@}"
}
# Configures the bootstrap kit, either from a prebuilt binary or from sources.
#
# Does nothing if the bootstrap kit has already been configured.
#
# \param basename Unique name for the pkg tree being configured.
# \param root Path to the root of the sandbox.
# \param packages Value of PACKAGES for this bootstrap kit.
# \param pkgdbdir Value of PKG_DBDIR for this bootstrap kit.
# \param prefix Value of PREFIX for this bootstrap kit.
# \param sysconfdir Value of SYSCONFDIR for this bootstrap kit.
# \param varbase Value of VARBASE for this bootstrap kit.
setup_bootstrap() {
local basename="${1}"; shift
local root="${1}"; shift
local packages="${1}"; shift
local pkgdbdir="${1}"; shift
local prefix="${1}"; shift
local sysconfdir="${1}"; shift
local varbase="${1}"; shift
[ ! -f "${root}${prefix}/sbin/pkg_admin" ] || return 0
local binarykit="${packages}/bootstrap.tgz"
if [ -e "${binarykit}" ]; then
shtk_cli_info "Setting up bootstrap in ${prefix} using binary kit"
run_sandboxctl run /bin/sh -c \
"cd / && tar xzpf /pkg_comp/packages/${basename}/bootstrap.tgz" \
|| exit
else
# Wipe any previous bootstrap work directory. This is helpful in
# case a bootstrap execution failed for reasons unknown to us and
# the user wants to retry without recreating the sandbox from scratch.
rm -rf "${root}/pkg_comp/work/${basename}/bootstrap"
local bootstrap_sh=/bin/sh
local dash_echo_test="$("${bootstrap_sh}" -c 'echo "\\100"')"
if [ "$(uname -s)" != GNUkFreeBSD -a "${dash_echo_test}" = @ ]; then
local bash
bash="$(which bash)"
if [ ${?} -eq 0 ]; then
bootstrap_sh="${bash}"
else
shtk_cli_warning "/bin/sh appears to be dash and bash was not" \
"found; bootstrap will probably fail"
fi
fi
shtk_cli_info "Setting up bootstrap in ${prefix} from scratch"
run_sandboxctl run /bin/sh -c \
"cd /pkg_comp/pkgsrc/bootstrap && env \
DISTDIR=/pkg_comp/distfiles \
PACKAGES='/pkg_comp/packages/${basename}' \
SH='${bootstrap_sh}' \
./bootstrap \
--gzip-binary-kit=/pkg_comp/packages/${basename}/bootstrap.tgz\
--make-jobs='$(shtk_config_get NJOBS)' \
--pkgdbdir='${pkgdbdir}' \
--prefix='${prefix}' \
--sysconfdir='${sysconfdir}' \
--varbase='${varbase}' \
--workdir=/pkg_comp/work/${basename}/bootstrap" \
|| exit
fi
}
# Configures bmake within the sandbox.
#
# Creates a mk.conf file with pkg_comp-specific details, and also creates a
# convenience symlink in a fixed location for interactive uses of make.
#
# Does nothing if bmake has already been configured.
#
# \param basename Unique name for the pkg tree being configured.
# \param root Path to the root of the sandbox.
# \param sysconfdir Value of SYSCONFDIR for this bootstrap kit.
setup_make() {
local basename="${1}"; shift
local root="${1}"; shift
local sysconfdir="${1}"; shift
local mk_conf="/pkg_comp/${basename}.mk.conf"
local include=".include \"${mk_conf}\""
if ! grep -qF "${include}" "${root}${sysconfdir}/mk.conf"; then
echo ".ifdef BSD_PKG_MK" >>"${root}${sysconfdir}/mk.conf"
echo "${include}" >>"${root}${sysconfdir}/mk.conf"
echo ".endif" >>"${root}${sysconfdir}/mk.conf"
fi
local symlink="/pkg_comp/make-${basename}"
[ ! -e "${root}${mk_conf}" -o ! -e "${root}${symlink}" ] || return 0
shtk_cli_info "Setting up ${mk_conf}"
cat >"${root}${mk_conf}" <<EOF
DISTDIR=/pkg_comp/distfiles
PACKAGES=/pkg_comp/packages/${basename}
PKGSRCDIR=/pkg_comp/pkgsrc
WRKOBJDIR=/pkg_comp/work/${basename}
BINPKG_SITES=
DEPENDS_TARGET=bin-install
MAKE_JOBS=$(shtk_config_get NJOBS)
PKG_DEVELOPER=$(shtk_config_get PKG_DEVELOPER)
EOF
ln -s "${prefix}/bin/bmake" "${root}${symlink}"
}
# Configures pkg_install within the sandbox.
#
# Creates a pkg_install.conf file with pkg_comp-specific details.
#
# Does nothing if pkg_install has already been configured.
#
# \param basename Unique name for the pkg tree being configured.
# \param root Path to the root of the sandbox.
# \param sysconfdir Value of SYSCONFDIR for this bootstrap kit.
# \param pkgdbdir Value of PKG_DBDIR for this bootstrap kit.
setup_pkginstall() {
local basename="${1}"; shift
local root="${1}"; shift
local sysconfdir="${1}"; shift
local pkgdbdir="${1}"; shift
local pkginstall_conf="${sysconfdir}/pkg_install.conf"
[ ! -e "${root}${pkginstall_conf}" ] || return 0
shtk_cli_info "Setting up ${pkginstall_conf}"
cat >"${root}${pkginstall_conf}" <<EOF
PKG_DBDIR=${pkgdbdir}
PKG_PATH=/pkg_comp/packages/${basename}/All
EOF
}
# Sets up a pkg installation, including bootstrap, bmake, and pkg_install.
#
# This is idempotent: once the installation has been configured, this function
# does nothing. This is good for speed, but can lead to inconsistencies if the
# user changes the configuration without rebuilding the sandbox.
#
# \param basename Unique name for the pkg tree being configured.
# \param root Path to the root of the sandbox.
# \param packages Value of PACKAGES for this bootstrap kit.
# \param pkgdbdir Value of PKG_DBDIR for this bootstrap kit.
# \param prefix Value of PREFIX for this bootstrap kit.
# \param sysconfdir Value of SYSCONFDIR for this bootstrap kit.
# \param varbase Value of VARBASE for this bootstrap kit.
full_bootstrap() {
local basename="${1}"; shift
local root="${1}"; shift
local packages="${1}"; shift
local pkgdbdir="${1}"; shift
local prefix="${1}"; shift
local sysconfdir="${1}"; shift
local varbase="${1}"; shift
setup_bootstrap "${basename}" "${root}" "${packages}" "${pkgdbdir}" \
"${prefix}" "${sysconfdir}" "${varbase}"
setup_make "${basename}" "${root}" "${sysconfdir}"
setup_pkginstall "${basename}" "${root}" "${sysconfdir}" "${pkgdbdir}"
}
# Sets up a pkg installation in the location configured by the user.
#
# \param root Path to the root of the sandbox.
bootstrap_pkg() {
local root="${1}"; shift
full_bootstrap \
"pkg" \
"${root}" \
"$(shtk_config_get PACKAGES)" \
"$(shtk_config_get PKG_DBDIR)" \
"$(shtk_config_get LOCALBASE)" \
"$(shtk_config_get SYSCONFDIR)" \
"$(shtk_config_get VARBASE)" || exit
if shtk_config_has EXTRA_MKCONF; then
local extra_mkconf="$(shtk_config_get EXTRA_MKCONF)"
cat "$(shtk_config_get EXTRA_MKCONF)" >>"${root}/pkg_comp/pkg.mk.conf" \
|| shtk_cli_error "Failed to append ${extra_mkconf} to mk.conf"
fi
}
# Sets up a pkg installation for pbulk and also configures pbulk.
#
# \param root Path to the root of the sandbox.
bootstrap_pbulk() {
local root="${1}"; shift
full_bootstrap \
"pbulk" \
"${root}" \
"$(shtk_config_get PBULK_PACKAGES)" \
"/pkg_comp/pbulk/libdata/pkgdb" \
"/pkg_comp/pbulk" \
"/pkg_comp/pbulk/etc" \
"/pkg_comp/pbulk/var" || exit
cat >>"${root}/pkg_comp/pbulk.mk.conf" <<EOF
# Be permissive of warnings raised during the build of our own infrastructure.
# Linux is especially picky and it's easy to trip over different warnings on
# different platforms. We just don't want to abort the bootstrapping process
# for such a lame reason.
BUILDLINK_TRANSFORM+=rm:-Werror
EOF
run_sandboxctl run /bin/sh -c \
"cd /pkg_comp/pkgsrc/pkgtools/pbulk && \
/pkg_comp/pbulk/bin/bmake bin-install" || exit
# bin-install implies clean when building from source, so no need to do that
# on our own.
# Make sure pbulk.conf exists in the etc directory in case automatic
# installation of configuration files is disabled.
local pbulk_conf="${root}/pkg_comp/pbulk/etc/pbulk.conf"
cp "${root}/pkg_comp/pbulk/share/examples/pbulk/pbulk.conf" "${pbulk_conf}"
# Replaces the value of a single variable in pbulk.conf.
#
# This is inefficient and possibly fragile, but we cannot just append
# overrides to the end of the file because some variables are derived from
# others.
pbulk_set() {
local var="${1}"; shift
local value="${1}"; shift
if grep "^${var}=" "${pbulk_conf}" >/dev/null; then
sed "/^${var}=/s,^.*$,${var}='${value}'," "${pbulk_conf}" \
>"${pbulk_conf}.new"
mv "${pbulk_conf}.new" "${pbulk_conf}"
else
echo "${var}=${value}" >>"${pbulk_conf}"
fi
}
pbulk_set checksum_packages no
pbulk_set mail true
pbulk_set master_mode no
pbulk_set rsync true
pbulk_set unprivileged_user root
# Configure pbulk's file layout.
pbulk_set bulklog /pkg_comp/bulklog
pbulk_set limited_list /pkg_comp/pbulk/etc/pbulk.list
pbulk_set loc /pkg_comp/bulklog/SUMMARY
# Configure pkgsrc's file layout.
pbulk_set bootstrapkit "/pkg_comp/packages/pkg/bootstrap.tgz"
pbulk_set packages "/pkg_comp/packages/pkg"
pbulk_set pkgdb "$(shtk_config_get PKG_DBDIR)"
pbulk_set pkgsrc /pkg_comp/pkgsrc
pbulk_set prefix "$(shtk_config_get LOCALBASE)"
pbulk_set varbase "$(shtk_config_get VARBASE)"
# Extend pbulk's client-clean script so that, once the bootstrap kit has
# been extracted, we can modify the system-wide mk.conf to pull in our
# chroot-specific settings.
#
# TODO(jmmv): Ideally, we would either generate a wrapper script for
# client-clean and source it from the configuration (but that's not possible
# because some pbulk scripts hardcode the script they run), or we would
# extend pbulk to support some form of "mk-fragment" option like the
# bootstrap setup does.
cat >>"${root}/pkg_comp/pbulk/libexec/pbulk/client-clean" <<EOF
cat >>"$(shtk_config_get SYSCONFDIR)/mk.conf" <<SUBEOF
.ifdef BSD_PKG_MK
.include "/pkg_comp/pkg.mk.conf"
.endif
SUBEOF
EOF
}
# Generates pkg_summary for all built packages.
#
# pbulk does this on its own, but it only adds the packages that have just been
# built to pkg_summary. For pkg_comp, because we want to support the case where
# users request individual additional packages to be built, we have to
# regenerate pkg_summary using *all* existing binary packages and not only those
# that were handled by pbulk.
#
# \param root Path to the root of the sandbox.
generate_pkg_summary() {
local root="${1}"; shift
shtk_cli_info "Generating pkg_summary"
# We need to run this within the sandbox because we have to execute pbulk's
# copy of pkg_info.
cat >"${root}/tmp/pkg_summary.sh" <<EOF
#! /bin/sh
cd /pkg_comp/packages/pkg/All
rm -f pkg_summary.*
ls -1 *.tgz | xargs /pkg_comp/pbulk/sbin/pkg_info -X | gzip -c >pkg_summary.gz
gzip -dc <pkg_summary.gz | bzip2 -c >pkg_summary.bz2
EOF
chmod +x "${root}/tmp/pkg_summary.sh"
run_sandboxctl run /tmp/pkg_summary.sh
}
# Automatic mode.
#
# Updates the pkgsrc tree, creates a sandbox, bootstraps pkgsrc, builds a set of
# packages, and tears the sandbox down.
#
# \params ... The options and arguments to the command.
pkg_comp_auto() {
local OPTIND # Cope with bash failing to reinitialize getopt.
while getopts ':f' arg "${@}"; do
case "${arg}" in
f) # Convenience flag for a "fast mode".
shtk_config_set "UPDATE_SOURCES" "false"
;;
\?)
shtk_cli_usage_error "Unknown option -${OPTARG} in build"
;;
esac
done
shift $((${OPTIND} - 1))
if [ ${#} -eq 0 ]; then
if shtk_config_has AUTO_PACKAGES; then
set -- $(shtk_config_get AUTO_PACKAGES)
fi
fi
[ ${#} -gt 0 ] || shtk_cli_usage_error "auto requires at least one" \
"package name as an argument or in the AUTO_PACKAGES variable"
if shtk_config_get_bool UPDATE_SOURCES; then
pkg_comp_fetch
fi
# We must validate packages after invoking pkg_comp_fetch to ensure the
# pkgsrc tree exists.
local packages
packages="$(pkgsrc_expand_packages "$(shtk_config_get PKGSRCDIR)" "${@}")" \
|| shtk_cli_error "Some packages do not exist in pkgsrc; please fix" \
"and retry"
local root
root="$(run_sandboxctl config SANDBOX_ROOT)" || exit
if [ -d "${root}/pkg_comp" ]; then
shtk_cli_warning "Reusing existing sandbox; if this is not what you" \
"want to do, abort now with Ctrl-C and use sandbox-delete to" \
"wipe the previous sandbox"
else
cleanup() {
shtk_cli_info "Destroying sandbox"
run_sandboxctl destroy
}
shtk_cleanup_register cleanup
shtk_cli_info "Creating sandbox"
run_sandboxctl create || exit
fi
pkg_comp_build ${packages} || exit
}
# Bootstraps pkgsrc on the user's configured location and for pbulk.
pkg_comp_bootstrap() {
[ ${#} -eq 0 ] \
|| shtk_cli_usage_error "bootstrap does not take any arguments"
local root
root="$(run_sandboxctl config SANDBOX_ROOT)" || exit
[ ! -e "${root}/pkg_comp/done.bootstrap" ] || return 0
shtk_cli_info "Bootstrapping pkg tools"
bootstrap_pkg "${root}" || exit
shtk_cli_info "Bootstrapping pbulk tools"
bootstrap_pbulk "${root}" || exit
touch "${root}/pkg_comp/done.bootstrap"
}
# Builds one or more packages in an already-existing sandbox.
#
# \params ... The options and arguments to the command.
pkg_comp_build() {
[ ${#} -gt 0 ] \
|| shtk_cli_usage_error "build requires at least one package name"
local packages
packages="$(pkgsrc_expand_packages "$(shtk_config_get PKGSRCDIR)" "${@}")" \
|| shtk_cli_error "Some packages do not exist in pkgsrc; please fix" \
"and retry"
pkg_comp_bootstrap || exit
local root
root="$(run_sandboxctl config SANDBOX_ROOT)" || exit
shtk_cli_info "Adding packages to be built to pbulk.list"
local list="${root}/pkg_comp/pbulk/etc/pbulk.list"
rm -f "${list}"
for package in ${packages}; do
echo "${package}" >>"${list}"
done
local bulklog="$(shtk_config_get PBULK_LOG)"
# Removing bulklog/success seems to be necessary to restart a build, as
# otherwise bulkbuild does not build modified packages. Is this correct?
rm -f "${root}/pkg_comp/bulklog/success"
shtk_cli_info "Starting pbulk build in the sandbox"
run_sandboxctl run /pkg_comp/pbulk/bin/bulkbuild || \
shtk_cli_error "bulkbuild failed; see ${bulklog}/ for possible details"
generate_pkg_summary "${root}"
if [ -s "${bulklog}/SUMMARY/error" ]; then
local errors="$(cat ${bulklog}/SUMMARY/error)"
shtk_cli_error "Failed to build ${errors}; see ${bulklog}/ for" \
"detailed logs"
else
shtk_cli_info "All packages built successfully; summary reports are" \
"in ${bulklog}/SUMMARY/"
fi
}
# Dumps the loaded configuration.
pkg_comp_config() {
[ ${#} -eq 0 ] || shtk_cli_usage_error "config does not take any arguments"
for var in ${PKG_COMP_CONFIG_VARS}; do
if shtk_config_has "${var}"; then
echo "${var} = $(shtk_config_get "${var}")"
else
echo "${var} is undefined"
fi
done
echo
run_sandboxctl config || exit
}
# Fetches a copy of the pkgsrc tree, or updates an existing one.
pkg_comp_fetch() {
[ ${#} -eq 0 ] || shtk_cli_usage_error "fetch does not take any arguments"
shtk_cli_info "Updating pkgsrc tree"
case "$(shtk_config_get FETCH_VCS)" in
cvs)
shtk_cvs_fetch "$(shtk_config_get CVS_ROOT)" pkgsrc \
"$(shtk_config_get_default CVS_TAG '')" \
"$(shtk_config_get PKGSRCDIR)"
;;
git)
git_fetch "$(shtk_config_get GIT_URL)" \
"$(shtk_config_get GIT_BRANCH)" \
"$(shtk_config_get PKGSRCDIR)"
;;
*)
shtk_cli_error "Unknown VCS name in FETCH_VCS; found" \
"$(shtk_config_get FETCH_VCS)"
;;
esac
shtk_config_run_hook post_fetch_hook
}
# Loads the configuration file specified in the command line.
#
# \param config_name Name of the desired configuration. It can be either a
# configuration name (no slashes) or a path.
pkg_comp_config_load() {
local config_name="${1}"; shift
local config_file=
case "${config_name}" in
*/*|*.conf)
config_file="${config_name}"
;;
*)
config_file="${PKG_COMP_ETCDIR}/${config_name}.conf"
[ -e "${config_file}" ] \
|| shtk_cli_usage_error "Cannot locate configuration named" \
"'${config_name}'"
;;
esac
shtk_config_load "${config_file}"
}
# Entry point to the program.
#
# \param ... Command-line arguments to be processed.
#
# \return An exit code to be returned to the user.
main() {
local config_name="default"
shtk_config_init ${PKG_COMP_CONFIG_VARS}
local OPTIND
while getopts ':c:o:v' arg "${@}"; do
case "${arg}" in
c) # Name of the configuration to load.
config_name="${OPTARG}"
;;
o) # Override for a particular configuration variable.
shtk_config_override "${OPTARG}"
;;
v) # Be verbose.
shtk_cli_set_log_level debug
;;
\?)
shtk_cli_usage_error "Unknown option -${OPTARG}"
;;
esac
done
shift $((${OPTIND} - 1))
OPTIND=1 # Should not be necessary due to the 'local' above.
[ ${#} -ge 1 ] || shtk_cli_usage_error "No command specified"
local exit_code=0
local command="${1}"; shift
case "${command}" in
auto|bootstrap|build|config|fetch)
pkg_comp_set_defaults
pkg_comp_config_load "${config_name}"
"pkg_comp_${command}" "${@}" || exit_code="${?}"
;;
sandbox-*)
pkg_comp_set_defaults
pkg_comp_config_load "${config_name}"
local subcommand="$(echo "${command}" | cut -d - -f 2-)"
run_sandboxctl "${subcommand}" "${@}" || exit_code="${?}"
;;
*)
shtk_cli_usage_error "Unknown command ${command}"
;;
esac
return "${exit_code}"
}