-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpmbuild-docker
executable file
·533 lines (479 loc) · 17.3 KB
/
rpmbuild-docker
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
#!/bin/bash
#
# License: MIT
# Author: Benoit DOLEZ <[email protected]>
# Copyright: 2019
#
function usage() {
local IFS=$'\t'
exec >&2
if [[ $0 == "-bash" ]] ; then return 1 ; fi
(( $# > 0 )) && echo "ERROR: $*"
version
echo "Usage: ${0##*/} [options...] specfile|specdir"
echo "Options:"
sed -nr "s/^[[:space:]]*## ([^:]*): /\1\t/p" -- "$0" |
while read OPT DESC ; do
printf " %-20s %s\n" "$OPT" "$DESC"
done
echo
return 0
}
function version() {
local PROGFILE=$0
local VERSION=$(sed -n 's/^# Version: //p' $PROGFILE)
local AUTHOR=$(sed -n 's/^# Author: //p' $PROGFILE)
local LICENSE=$(sed -n 's/^# License: //p' $PROGFILE)
echo "${PROGFILE##*/}${VERSION:+ $VERSION} - $AUTHOR - $LICENSE"
}
function on_exit() {
if [[ -n $GITREV && $SPECFILE == *.gitrev ]]; then
rm -f "$SPECDIR/$SPECFILE"
fi
if [[ -n $GIT_ARCHIVE_TMP && -d $GIT_ARCHIVE_TMP ]]; then
rm -rf "$GIT_ARCHIVE_TMP"
fi
}
trap on_exit EXIT
declare QUIET=$QUIET
declare VERBOSE=$VERBOSE
declare XDEBUG=$XDEBUG
declare ARGS=()
declare CACHEBASE=${CACHEBASE:-/tmp}
declare DIST=
declare REPO=$REPO
declare UPDATE=
declare KEEP=
declare NOKEEP=
declare GITREV=
declare SOURCEDIR=$SOURCEDIR
declare OUTPUTDIR=$OUTPUTDIR
declare PREBUILDSCRIPT=$PREBUILDSCRIPT
declare DOWNLOAD=1
declare RPMOPTS=()
declare RPMBUILDOPTS=()
declare RPMSPECOPTS=()
if [[ -n $VERBOSE ]] ; then
( IFS='' ; echo "[DEBUG] '${0}${*/#/' '}'" )
fi
while (( $# > 0 )); do
case "$1" in
## -h, --help: This help
-h|--help) usage && exit 0 ;;
## -V, --version: Show version
-V|--version) version && exit 0 ;;
## --x-debug: Enable bash debug mode
--x-debug) XDEBUG=1 ;;
## -v, --verbose: Define verbose level (must be repeat)
-v|--verbose) ((VERBOSE++)) ;;
## -d, --dist DIST: Define dist version (eg: el8)
-d|--dist) DIST=$2 ; shift ;;
## -I, --image IMAGE: Build using a specific docker image
-I|--image) IMAGE=$2 ; shift ;;
## -s, --source PATH: define source directory
-s|--sourcedir) SOURCEDIR=${2%/} ; shift ;;
## -O, --outputdir PATH: define output directory for generated packages
-O|--outputdir) OUTPUTDIR=${2%/} ; shift ;;
## --no-download: skip downloading sources if needed
--no-download) DOWNLOAD=0 ;;
## --repo PATH: local repository
--repo) REPO=$2 ; shift ;;
## --update: update repository with new packages
--update) UPDATE=1 ;;
## --keep: keep build environment after build_ldflags
--keep) KEEP=1 ;;
## --nokeep: never keep build environment on error
--nokeep) NOKEEP=1 ;;
## --gitrev: create source0 tarball from git revision
--gitrev) GITREV=$2 ; shift ;;
# generic args
--) shift ; break ;;
-*) usage "Unknown parameter '$1'" && exit 1 ;;
*) ARGS+=( "$1" ) ;;
esac
shift
done
[[ $XDEBUG == 1 ]] && set -x
RPMOPTS+=( "$@" )
declare SPECFILE=${ARGS[0]}
[[ ${SPECFILE:0:1} != "/" ]] && SPECFILE=$PWD/$SPECFILE
# cleanup potential trailing slashes
SPECFILE=${SPECFILE%${SPECFILE##*[!/]}}
if [[ ! -r $SPECFILE ]] ; then
echo "FATAL: can't open $SPECFILE for reading" >&2
exit 1
fi
if [[ -d $SPECFILE ]] ; then
if [[ -d $SPECFILE/SPECS ]] ; then
SPECFILE=( $SPECFILE/SPECS/*.spec )
else
SPECFILE=( $SPECFILE/*.spec )
fi
if [[ ${#SPECFILE[@]} != 1 ]]; then
echo "FATAL: undefined spec file : '${SPECFILE[*]}' " >&2
exit 1
fi
fi
# define SPECDIR & SPECFILE from SPECFILE
SPECDIR=${SPECFILE%/*}
SPECFILE=${SPECFILE##*/}
if [[ -z $SOURCEDIR && -d ${SPECDIR%/*}/SOURCES ]] ; then
SOURCEDIR=${SPECDIR%/*}/SOURCES
fi
if [[ -n $PREBUILDSCRIPT ]]; then
[[ ${PREBUILDSCRIPT:0:1} == / ]] || PREBUILDSCRIPT="$PWD/$PREBUILDSCRIPT"
elif [[ -z $PREBUILDSCRIPT ]]; then
# auto locate prebuild script if not given
for dir in "$SPECDIR" "$PWD"; do
for ext in sh inc; do
if [[ -e "$dir/prebuild.$ext" ]]; then
PREBUILDSCRIPT="$dir/prebuild.$ext"
break 2
fi
done
done
fi
if [[ -n $PREBUILDSCRIPT ]]; then
if [[ ! -r $PREBUILDSCRIPT ]]; then
echo "FATAL: can't open $PREBUILDSCRIPT for reading" >&2
exit 1
elif [[ ${PREBUILDSCRIPT##*.} == .sh && ! -x $PREBUILDSCRIPT ]]; then
echo "FATAL: prebuild script $PREBUILDSCRIPT not executable" >&2
exit 1
fi
fi
[[ -z $SOURCEDIR ]] && SOURCEDIR=$PWD
[[ -z $OUTPUTDIR ]] && OUTPUTDIR=$PWD
[[ ${SOURCEDIR:0:1} != "/" ]] && SOURCEDIR=$PWD/$SOURCEDIR
[[ ${OUTPUTDIR:0:1} != "/" ]] && OUTPUTDIR=$PWD/$OUTPUTDIR
[[ -n $REPO && ${REPO:0:1} != "/" ]] && REPO=$PWD/$REPO
echo "SPECFILE: $SPECFILE"
echo "SPECDIR: $SPECDIR"
echo "SOURCEDIR: $SOURCEDIR"
echo "OUTPUTDIR: $OUTPUTDIR"
echo "PREBUILDSCRIPT: ${PREBUILDSCRIPT:-none}"
echo "REPO: ${REPO:-none}"
# Yum might take for ages to complete because it CLOEXEC on all available
# file descriptors and recent dockers sets a very high default limit.
# https://bugzilla.redhat.com/show_bug.cgi?id=1537564
# https://github.com/rpm-software-management/rpm/pull/444
DOCKER_OPTS_DIST_el6=( --ulimit 'nofile=1024' )
function dockerfile_for_dist_el6() {
cat <<'EOF'
FROM centos:6
RUN set -x && \
ulimit -n 1024 && \
sed -i -re 's,mirror\.centos\.org,vault.centos.org,; s,^(mirrorlist),#\1,; s,^#(baseurl),\1,' /etc/yum.repos.d/CentOS-*.repo && \
yum update -y && \
yum install -y createrepo gcc make rpm-build && \
echo '* - nproc 2048' >> /etc/security/limits.d/90-nproc.conf && \
sed -r -i -e 's/^(keepcache)=.*/\1=1/' /etc/yum.conf && \
yum install -y bzip2-devel db4-devel elfutils-devel elfutils-libelf-devel file-devel gawk gettext-devel libacl-devel libcap-devel libtool lua-devel ncurses-devel nss-devel nss-softokn-freebl-devel popt-devel python-devel readline-devel redhat-rpm-config xz-devel zlib-devel && \
{ curl -OL --connect-timeout 15 http://ftp.rpm.org/releases/rpm-4.11.x/rpm-4.11.3.tar.bz2 || \
curl -OL --connect-timeout 15 http://ftp.lip6.fr/pub/linux/rpm/releases/rpm-4.11.x/rpm-4.11.3.tar.bz2; } && \
tar xvjf rpm-4.11.3.tar.bz2 && \
cd rpm-4.11.3 && \
export CFLAGS=$(rpm -E '%{optflags}') && \
export CPPFLAGS="$(pkg-config --cflags nss) -USYSCONFDIR -DSYSCONFDIR=\\\"/etc\\\" -ULOCALSTATEDIR -DLOCALSTATEDIR=\\\"/var\\\" -URPMCONFIGDIR -DRPMCONFIGDIR=\\\"/usr/lib/rpm\\\" -URPMCONFIGDIR -DRPMCONFIGDIR=\\\"/usr/lib/rpm\\\"" && \
autoreconf -i && \
./configure --enable-static=yes --enable-shared=no --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --with-vendor=redhat --with-external-db --with-lua --with-cap --with-acl --without-selinux --disable-python --disable-plugins --disable-nls && \
make && \
cp -a rpmspec /usr/local/bin/ && \
cd .. && \
rm -rfv rpm-4.11.3.tar.bz2 rpm-4.11.3 && \
yum history undo -y $(yum history |awk '$1 ~ /^[0-9]$/ { print $1; exit; }') && \
yum clean all
EOF
}
# Yum might take for ages to complete because it CLOEXEC on all available
# file descriptors and recent dockers sets a very high default limit.
# https://bugzilla.redhat.com/show_bug.cgi?id=1537564
# https://github.com/rpm-software-management/rpm/pull/444
DOCKER_OPTS_DIST_el7=( --ulimit 'nofile=1024' )
function dockerfile_for_dist_el7() {
cat <<'EOF'
FROM centos:7
RUN set -x && \
ulimit -n 1024 && \
sed -i -re 's,mirror\.centos\.org,vault.centos.org,; s,^(mirrorlist),#\1,; s,^#(baseurl),\1,' /etc/yum.repos.d/CentOS-*.repo && \
yum update -y && \
yum install -y createrepo gcc make rpm-build && \
echo '* - nproc 2048' >> /etc/security/limits.d/90-nproc.conf && \
sed -r -i -e 's/^(keepcache)=.*/\1=1/' /etc/yum.conf && \
yum clean all
EOF
}
function dockerfile_for_dist_el8() {
cat <<'EOF'
FROM rockylinux:8
RUN set -x && \
yum update -y && \
yum install -y createrepo gcc make rpm-build && \
echo '* - nproc 2048' >> /etc/security/limits.d/90-nproc.conf && \
echo keepcache=1 >> /etc/yum.conf && \
echo keepcache=1 >> /etc/dnf/dnf.conf && \
sed -i -re s,enabled=0,enabled=1, /etc/yum.repos.d/Rocky-PowerTools.repo && \
yum clean all
EOF
}
# Lookup "docker build" source to build a base image for $DIST.
function dockerfile_for_dist() {
if [[ -f ~/.config/${0##*/}/Dockerfile.$DIST ]]; then
cat ~/.config/"${0##*/}/Dockerfile.$DIST"
elif declare -f -F "dockerfile_for_dist_$DIST" >/dev/null; then
"dockerfile_for_dist_$DIST"
else
return 1
fi
}
# normal dockerfile method may work with recent distro/docker
DOCKER_OPTS_DIST_el9=( --security-opt seccomp=unconfined )
function dockerscript_for_dist_el9() {
IMAGE=$1; shift
echo "# Build $IMAGE..."
set -xe
id=$(docker run "$@" -d rockylinux:9 sleep infinity)
docker exec "$id" dnf update -y
docker exec "$id" dnf install -y createrepo gcc make rpm-build
docker exec "$id" sh -c "echo '* - nproc 2048' >> /etc/security/limits.d/90-nproc.conf"
docker exec "$id" sh -c "echo keepcache=1 >> /etc/dnf/dnf.conf"
docker exec "$id" sh -c $'awk \x27/^\[/ { inside=$1 } inside=="[crb]"&&/enabled/{print "enabled=1"; next}{print}\x27 /etc/yum.repos.d/rocky.repo > /etc/yum.repos.d/rocky.repo.new && mv /etc/yum.repos.d/rocky.repo{.new,}'
docker exec "$id" dnf clean all
docker container commit "$id" "$IMAGE"
docker kill "$id"
docker rm "$id"
}
# Lookup docker script source to build a base image for $DIST.
function dockerscript_for_dist() {
if [[ -f ~/.config/${0##*/}/build-image.$DIST ]]; then
cat ~/.config/"${0##*/}/build-image.$DIST"
elif declare -f "dockerscript_for_dist_$DIST"; then
echo "'dockerscript_for_dist_$DIST' \"\$@\""
else
return 1
fi
}
if [[ -f ~/.config/"${0##*/}/before-image.hook" ]] &&
! source ~/.config/"${0##*/}/before-image.hook"; then
echo "FATAL: failed to source before-image.hook" >&2
exit 1
fi
DOCKER_OPTS_DIST="DOCKER_OPTS_DIST_$DIST[@]"
DOCKER_OPTS_DIST=( "${!DOCKER_OPTS_DIST}" )
if [[ -z $IMAGE ]]; then
if [[ -z $DIST ]]; then
echo "FATAL: require -d DIST or -I IMAGE (or both)" >&2
exit 1
fi
# Set $IMAGE from $DIST
image_src=; image_script=;
if ! image_src=$(dockerfile_for_dist) &&
! image_script=$(dockerscript_for_dist); then
echo "FATAL: no image source for $DIST" >&2
exit 1
fi
image_sig=$(echo "${image_src:-$image_script}" |md5sum |sed -re 's,^(.{7}).*,\1,')
IMAGE="rpmbuild/$DIST:$image_sig"
if ! docker images -q "$IMAGE" |grep -q .; then
# Build docker image
if [[ -n $image_src ]] && ! echo "$image_src" |docker build -t "$IMAGE" -; then
echo "FATAL: failed to build image $IMAGE for $DIST" >&2
exit 1
elif ! echo "$image_script" |bash -s -- "$IMAGE" "${DOCKER_OPTS_DIST[@]}"; then
echo "FATAL: image $IMAGE for $DIST, build scripit failed" >&2
exit 1
fi
fi
elif [[ -z $DIST ]]; then
# Set $DIST from $IMAGE
DIST=$(docker run --rm "$IMAGE" rpm -E '%{dist}' |sed 's,^\.\+,,')
if [[ -z $DIST ]]; then
echo "FATAL: failed to identify dist in image $IMAGE" >&2
exit 1
fi
fi
if [[ -f ~/.config/"${0##*/}/after-image.hook" ]] &&
! source ~/.config/"${0##*/}/after-image.hook"; then
echo "FATAL: failed to source after-image.hook" >&2
exit 1
fi
# Extract type and version from $DIST for convenience
DIST_VERSION=${DIST##*[!0-9]}
DIST_TYPE=${DIST%$DIST_VERSION}
echo "DIST: $DIST"
echo "DIST_TYPE: $DIST_TYPE"
echo "DIST_VERSION: $DIST_VERSION"
echo "IMAGE: $IMAGE"
# Create missing directories
for i in "$CACHEBASE"/cache-{yum-"$DIST",shared} \
"$OUTPUTDIR"/{RPMS,SRPMS} \
${REPO:+"$REPO"}; do
if [[ ! -d $i ]] && ! mkdir -p "$i"; then
echo "FATAL: cannot create directory: $i" >&2
exit 1
fi
done
# Need rpmbuild --noclean option to keep generated files in place
if [[ $DIST_TYPE == el && $DIST_VERSION -ge 7 ]] ||
docker run --rm "${DOCKER_OPTS_DIST[@]}" "$IMAGE" rpm --noclean >/dev/null 2>&1
then
RPMBUILDOPTS+=( --noclean )
fi
# Prepare env and script to be run inside the container
BGROUP=$(id -gn)
BUSER=${USER:-$(id -un)}
BHOME=~
# Handle $GITREV:
# - create a source tarball from the current directory, assuming it is a git
# - use it as source0
# - change version accordingly (git describe) in spec file
# $1: git rev
# $2: output filename without extension (.tar.gz auto added)
# $3: prefix without trailing slash
function git_archive_mtime() {
GIT_ARCHIVE_TMP=$(mktemp -d) || return 1
git archive --format tar --prefix "$3/" \
"$1" ./ ':!rpmbuild-docker' ':!*.spec' ':!SPECS/' |
tar x -C "$GIT_ARCHIVE_TMP/" || return 1
find "$GIT_ARCHIVE_TMP/$3/" -not -type d -printf '%P\n' |
xargs -n 1 -I{} sh -c 'touch -d "$(git log --pretty=format:%ci -n 1 "$1" -- "$3")" "$2/$3"' -- \
"$1" "$GIT_ARCHIVE_TMP/$3/" {} || return 1
tar czf "$2.tar.gz" -C "$GIT_ARCHIVE_TMP/" "$3"
}
function gitrev2rpm() {
local gitdesc gitdate
gitdesc=$(git describe --tags --match 'v?*' --abbrev=7 --always "$1")
if [[ $gitdesc =~ ^(v(.+)(-[0-9]+-g))?([0-9a-f]+)$ ]]; then
gitdate=$(date -d "$(git log -n 1 --format='%ci' "$1")" +%Y%m%d%H%M%S)
REPLY=( "${BASH_REMATCH[2]:-0.0.0}^${gitdate}g${BASH_REMATCH[4]}.${BUSER:0:3}" "${BASH_REMATCH[4]}" )
else
REPLY=( "${gitdesc:1}" "${gitdesc:1}" )
fi
}
if [[ -n $GITREV ]]; then
gitrev2rpm "$GITREV"
project=${PWD##*/}
sed -re "s,(^\\s*Version\\s*:\\s*).*,\\1${REPLY[0]}," \
-e "s|(^\\s*source0\\s*:\\s*).*|\\1$project-dev.tar.gz|i" \
"$SPECDIR/$SPECFILE" > "$SPECDIR/$SPECFILE.gitrev"
SPECFILE="$SPECFILE.gitrev"
git_archive_mtime "$GITREV" "$SOURCEDIR/$project-dev" "$project-${REPLY[1]}"
fi
# Options for docker
DOCKER_OPTS=(
"${DOCKER_OPTS_DIST[@]}"
--tmpfs /tmp:rw,exec
--mount "type=bind,src=$SPECDIR/$SPECFILE,dst=/tmp/$SPECFILE"
--mount "type=bind,src=$CACHEBASE/cache-yum-$DIST,dst=/var/cache/yum"
--mount "type=bind,src=$CACHEBASE/cache-yum-$DIST,dst=/var/cache/dnf"
--mount "type=bind,src=$CACHEBASE/cache-shared,dst=/tmp/cache-shared"
--mount "type=bind,src=$SOURCEDIR,dst=/tmp/SOURCES"
--mount "type=bind,src=$OUTPUTDIR/SRPMS,dst=/tmp/SRPMS"
--mount "type=bind,src=$OUTPUTDIR/RPMS,dst=/tmp/RPMS"
)
if [[ -n $REPO ]] ; then
DOCKER_OPTS+=( --mount "type=bind,src=$REPO,dst=/tmp/local-repo" )
fi
if [[ -n $PREBUILDSCRIPT ]] ; then
DOCKER_OPTS+=( --mount "type=bind,src=$PREBUILDSCRIPT,dst=/tmp/${PREBUILDSCRIPT##*/}" )
fi
function build_exit() {
local status=${?:-0}
[[ -n $NOKEEP ]] && exit "$status"
[[ -n $KEEP || $status != 0 ]] && sleep infinity
exit "$status"
}
function build_runas() {
printf -v qcmd "%q " "${@:2}"
runuser -l "$1" -c "$qcmd"
}
function build_dl() {
local curlopts=( -fRL --connect-timeout 15 ) url=$1 dest=${2:-$CACHEDIR} retval
if [[ $url =~ ^([^#]+)(#?)/([^/]+)$ ]]; then
[[ -f $dest/${BASH_REMATCH[3]} ]] && return 0
[[ ${BASH_REMATCH[2]} == '#' ]] && url=${BASH_REMATCH[1]}
${RUNAS:+build_runas "$RUNAS" }\
curl "${curlopts[@]}" -o "$dest/${BASH_REMATCH[3]}" "$url"
retval=$?
REPLY="cURL return code $retval"
return "$retval"
else
REPLY='URL not supported'
return 1
fi
}
function build_main() {
set -fxe
trap build_exit EXIT
OIFS=$IFS
mkdir -p "$BHOME"
chown "$BUSER:$BGROUP" "$BHOME"
if [[ -n "$REPO" ]]; then
cat >/etc/yum.repos.d/local-repo.repo <<'EOF'
[local-repo]
name=local-repo
baseurl=file:///tmp/local-repo/RPMS
enabled=1
gpgcheck=0
EOF
if [[ ! -d /tmp/local-repo/RPMS/repodata ]]; then
build_runas "$BUSER" mkdir -p /tmp/local-repo/RPMS
build_runas "$BUSER" createrepo /tmp/local-repo/RPMS
fi
fi
if [[ -n $PREBUILDSCRIPT ]]; then
case "$PREBUILDSCRIPT" in
*.sh) "/tmp/$PREBUILDSCRIPT" ;;
*.inc) source "/tmp/$PREBUILDSCRIPT" ;;
*) echo 'Invalid prebuild script/source file'; exit 1 ;;
esac
fi
RPMOPTS+=( --define '_topdir /tmp/../tmp/../tmp' )
rpmbuildopts=( "${RPMOPTS[@]}" "${RPMBUILDOPTS[@]}" )
rpmspecopts=( "${RPMOPTS[@]}" "${RPMSPECOPTS[@]}" )
IFS=$'\n'
buildreq=( $(rpmspec -P "/tmp/$SPECFILE" "${rpmspecopts[@]}" |
sed -ne 's/^BuildRequires: *//p' |
sed -re 's/(( +[^ ,]|[^ ,])*),/\1\n/g' |
sed -re 's/^ *([^ ]( +[^ ]|[^ ])*) *$/\1/') )
IFS=$OIFS
if [[ -n $buildreq ]]; then
yum install -y "${buildreq[@]}"
fi
# download sources if needed
if [[ $DOWNLOAD == 1 ]] ; then
dlsources=( $(rpmspec -P "/tmp/$SPECFILE" "${rpmspecopts[@]}" |
sed -nr -e 's,^(source|patch)[0-9]*\s*:\s*(\S+).*,\2,i;T;/:\/\//p') )
for i in "${dlsources[@]}"; do
RUNAS="$BUSER" build_dl "$i" /tmp/SOURCES
done
fi
# Having _topdir /tmp causes problems in el6/el7 because it is not long enough
# https://access.redhat.com/solutions/1426113
build_runas "$BUSER" rpmbuild "${rpmbuildopts[@]}" -ba "/tmp/$SPECFILE"
if [[ -n $REPO && -n $UPDATE ]] ; then
for repo in RPMS SRPMS; do
build_runas "$BUSER" cp -aT "/tmp/$repo/" "/tmp/local-repo/$repo/"
build_runas "$BUSER" createrepo --update "/tmp/local-repo/$repo"
done
fi
}
# Build RPM in the container
CTNAME="build_${RANDOM}${RANDOM}${RANDOM}${RANDOM}"
DOCKER_OPTS+=( --name "$CTNAME" -i --rm )
trap 'docker kill "$CTNAME"; exit;' INT
cat <<__EOF__ |docker run "${DOCKER_OPTS[@]}" "$IMAGE" /bin/bash &
$(declare -p BGROUP BHOME BUSER DIST DIST_TYPE DIST_VERSION DOWNLOAD KEEP NOKEEP REPO RPMOPTS RPMBUILDOPTS RPMSPECOPTS SPECFILE UPDATE)
$(printf 'PREBUILDSCRIPT=%q\n' "${PREBUILDSCRIPT##*/}")
$(declare -f build_exit)
$(declare -f build_runas)
$(declare -f build_dl)
$(declare -f build_main)
echo "$(getent group "$BGROUP")" >> /etc/group
echo "$(getent passwd "$BUSER")" >> /etc/passwd
# exports for prebuild script
export -f build_dl build_runas
export CACHEDIR=/tmp/cache-shared DIST DIST_TYPE DIST_VERSION
build_main
__EOF__
wait "$!"