-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspbuilder
More file actions
executable file
·519 lines (459 loc) · 16.6 KB
/
spbuilder
File metadata and controls
executable file
·519 lines (459 loc) · 16.6 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
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
#!/bin/sh
#
# ex:set ai shiftwidth=4 inputtab=spaces smarttab noautotab:
#
# Personal package builder for Slackware.
# Copyright (C) 2011-2016 Christoph Willing Brisbane, Australia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
PATH=/sbin:/usr/sbin:$PATH
# Must be root
#
[ $EUID -eq 0 ] || {
echo $"$0 must run as root."
exit 1
}
# Must have overlay kernel module loaded
# (actually only for the build command
# but better to know/fix early on).
#
lsmod | grep overlay 2>&1 >/dev/null
[ $? -eq 0 ] || {
modprobe overlay
sleep 2
}
lsmod | grep overlay 2>&1 >/dev/null
[ $? -eq 0 ] || {
echo $"The overlay kernel module could not be loaded. Exiting now ..."
exit 2
}
export PATH=/sbin:/usr/sbin:$PATH
SPBUILDER_VERSION=
SPBUILDER_DATA_DIR=/usr/share/spbuilder
SPBUILDER_SYSTEM_DIR=/etc/spbuilder
[ -f ${SPBUILDER_SYSTEM_DIR}/spbuilderrc ] && source ${SPBUILDER_SYSTEM_DIR}/spbuilderrc
ARCH=${ARCH:-`uname -m`}
subarch=
case $ARCH in
x86_64)
subarch=64
SPBUILDER_REPO_FILE=$SPBUILDER_DATA_DIR/spbuilder_repo.x86_64
SLAPT_PACKAGES="
slapt-get-0.10.2q-$ARCH-1_uqv.txz
slaptgetwrapper-1.0-noarch-3_uqv.txz
hoorex-0.6.0-$ARCH-1_uqv.txz
"
;;
i?86)
SPBUILDER_REPO_FILE=$SPBUILDER_DATA_DIR/spbuilder_repo.i686
SLAPT_PACKAGES="
slapt-get-0.10.2q-i386-1_uqv.txz
slaptgetwrapper-1.0-noarch-3_uqv.txz
hoorex-0.6.0-i486-1_SBo.txz
"
;;
*)
echo "Unknown ARCH ($ARCH). Exiting now ..."
exit 1
;;
esac
PYTHON_VERSION=${PYTHON_VERSION:-`python -c "import sys; print sys.version[:3]"`}
SPBUILDER_MASTER_NAME=${SPBUILDER_MASTER_NAME:-spbuilder-SL14.2-$ARCH}
SLAPT_MIRROR=${SLAPT_MIRROR:-http://darlo.selfip.org/slackware$subarch/14.2}
SPBUILDER_MIRROR=${SPBUILDER_MIRROR:-http://ftp.iinet.net.au/linux/slackware}
SPBUILDER_CONF=${SPBUILDER_CONF:-/etc/spbuilder/spbuilder-1402.conf}
SPBUILDER_BASE=${SPBUILDER_BASE:-/var/cache/spbuilder}
DEFAULT_BUILD_DIR=${SPBUILDER_BASE}/build
BUILD_LOGDIR=${SPBUILDER_BASE}/log
SPB_SHUTDOWN=${SPB_SHUTDOWN:-1}
LANG=${LANG:-"en_AU.utf8"}
echo "SPB_SHUTDOWN on error is $SPB_SHUTDOWN"
umask 022
unset CC CXX CFLAGS CXXFLAGS LDFLAGS
action=""
source_location=""
logfile=""
repo_rcfile=""
local_dirs=""
lxc_base_dir=/var/lib/lxc
trap early_out 1 2
function random_string ()
{
# Generate a random name for container & network device
#long_string=$(echo $$ |md5sum|md5sum)
long_string=$(mcookie -f /var/log/messages)
random_start=$(expr $RANDOM % 20 )
# Return result
echo ${long_string:$random_start:12}
}
our_name=$(random_string)
#echo "Our temporary name = $our_name"
function get_extra_dirs()
{
extra_dirs_result=""
# if we see a "SOURCE=file://..." entry,
# strip beginning and end of entry to reveal diectory path
# and add that path to our result (extra_dirs_result)
for entry in $(cat $repo_rcfile) ; do
case "$entry" in
SOURCE=file*)
leftstrip=${entry/SOURCE=file:\//}
dir=${leftstrip%%\/:*}
dir=${dir%%\/}
if [ -d "$dir" ]; then
extra_dirs_result="${extra_dirs_result} $dir"
fi
;;
esac
done
echo $extra_dirs_result
}
function _print_elapsed_time ()
{
local S=$1
((h = S / 3600))
((m = S % 3600 / 60))
((s = $S % 60))
if [ $h -gt 0 ]; then
printf "%dh %dm %ds\n" $h $m $s
else
if [ $m -gt 0 ]; then
printf "%dm %ds\n" $m $s
else
printf "%ds\n" $s
fi
fi
}
function show_usage ()
{
echo $"Usage: $0 [action] [options]"
echo $" [action] = { create | update | build | login | clean }"
}
# Function create_container() builds a minimal 'master' LXC container
# with additional packages for general development, from which
# snapshot containers will be cloned to do actual package building.
#
function create_container ()
{
echo $"Creating a new spbuilder container named: $SPBUILDER_MASTER_NAME"
CACHEDIR=${CACHEDIR:-/var/cache/lxc/spbuilder}
mkdir -p $CACHEDIR
echo -n $"Clearing out previously cached material ... "
rm -rf $CACHEDIR/*
echo $"OK"
MIRROR=$SPBUILDER_MIRROR \
lxc-create \
--name $SPBUILDER_MASTER_NAME \
-f $SPBUILDER_CONF \
--template spbuilder-1402 \
-- -a $ARCH
cd ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/root
for package in $SLAPT_PACKAGES ; do
this_package="$SLAPT_MIRROR/$package"
echo "Retrieving $this_package"
wget -nv -c $this_package
installpkg --root ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs --terse $package
done
cp ${SPBUILDER_DATA_DIR}/sb_autogen_slack-required.$PYTHON_VERSION ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/usr/bin/sb_autogen_slack-required
chmod 0755 ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/usr/bin/sb_autogen_slack-required
# Setup environment
cat <<EOF >>${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/root/.spbuilderrc
#!/bin/sh
export HOME=/root
export TERM=linux
export VISUAL=vi
. /etc/profile
cd
EOF
chmod a+x ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/root/.spbuilderrc
mv ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/etc/slapt-get/slapt-getrc ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/etc/slapt-get/slapt-getrc.orig
# SPBUILDER_REPO may be defined in /etc/spbuilder/spbuilderrc
if [ -n "$SPBUILDER_REPO" ]; then
echo "Adding slapt-getrc from host"
cp -p /etc/slapt-get/slapt-getrc ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/etc/slapt-get/slapt-getrc
else
echo "Adding slapt-getrc from $SPBUILDER_REPO_FILE"
mkdir -p ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/etc/slapt-get
. $SPBUILDER_REPO_FILE
for entry in $SPBUILDER_REPO ; do
echo "$entry" >>${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/etc/slapt-get/slapt-getrc
done
fi
# Add any directories implied in the slapt-getrc
repo_rcfile=${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/etc/slapt-get/slapt-getrc
xdirs=$(get_extra_dirs)
for xdir in $xdirs ; do
mkdir -p ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/$xdir
echo "$xdir ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/$xdir none rw,bind 0 0" \
>>${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/etc/fstab
done
# Enable DHCP networking
cd ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/
sed -i -e 's/USE_DHCP\[0\]=""/USE_DHCP[0]="yes"/' etc/rc.d/rc.inet1.conf
# Add slackbuilds & spbuilder directories
echo "Setting mount for $SPBUILDER_SLACKBUILDS"
sed -i -e "s:^lxc.mount.auto.:\
lxc.mount.entry = $SPBUILDER_SLACKBUILDS mnt/slackbuilds none bind,ro,create=dir 0 0\n\
lxc.mount.entry = $SPBUILDER_BASE var/cache/spbuilder none bind,create=dir 0 0\n\
&:" ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/config
# Setup hoorex
SPB_WRAPPER=$(tempfile)
sed -e "s/%SPB_SHUTDOWN%/$SPB_SHUTDOWN/" ${SPBUILDER_DATA_DIR}/spbuilder.create.in >$SPB_WRAPPER
cp $SPB_WRAPPER ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/usr/bin/spbuilder.create
chmod a+x ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/usr/bin/spbuilder.create
lxc-start -n ${SPBUILDER_MASTER_NAME}
lxc-wait -n ${SPBUILDER_MASTER_NAME} -s RUNNING
lxc-attach -n ${SPBUILDER_MASTER_NAME} --clear-env -a $ARCH -- /usr/bin/spbuilder.create
# Cleanup
rm $SPB_WRAPPER
# Insert a random password
echo $"Changing password to something obscure"
echo "root:$(random_string)" | chroot ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs chpasswd
echo $"New spbuilder container created at >${lxc_base_dir}/${SPBUILDER_MASTER_NAME}"
}
function update_container ()
{
lxc-info -n $SPBUILDER_MASTER_NAME -s | grep RUNNING 2>&1 >/dev/null
if [ "$?" = "0" ]; then
echo "$SPBUILDER_MASTER_NAME is already running. Exiting now ..."
exit 4
fi
echo $"Update an existing build container"
# # Add any directories implied in the slapt-getrc
# repo_rcfile=${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/etc/slapt-get/slapt-getrc
# xdirs=$(get_extra_dirs)
# for xdir in $xdirs ; do
# mkdir -p ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/$xdir
# echo "$xdir ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/$xdir none rw,bind 0 0" \
# >>${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/etc/fstab
# done
SPB_WRAPPER=$(tempfile)
sed -e "s/%SPB_SHUTDOWN%/$SPB_SHUTDOWN/" ${SPBUILDER_DATA_DIR}/spbuilder.update.in >$SPB_WRAPPER
cp $SPB_WRAPPER ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/usr/bin/spbuilder.update
chmod a+x ${lxc_base_dir}/${SPBUILDER_MASTER_NAME}/rootfs/usr/bin/spbuilder.update
# Now try to run it
echo -n "Trying to start container $SPBUILDER_MASTER_NAME for update ... "
lxc-start -n $SPBUILDER_MASTER_NAME
lxc-wait -n $SPBUILDER_MASTER_NAME -s RUNNING
echo "OK"
lxc-attach -n $SPBUILDER_MASTER_NAME --clear-env -a $ARCH -- /usr/bin/spbuilder.update
rm $SPB_WRAPPER
echo $"spbuilder update all done OK"
exit
}
function build_package ()
{
echo $"Build package - start time: $(date)"
local build_start_time=$(date +%s)
# Check that the right argument was passed
if [ -z "$source_location" ]; then
echo $"" >&2
echo $"Error! Invalid number of parameters passed." >&2
echo $"Usage: build -s <source directory | build tarball>" >&2
exit 1
fi
# source_location should be a category/pkgname based at $SPBUILDER_BASE/slackbuilds
if [ -d "$SPBUILDER_BASE/slackbuilds/$source_location" ]; then
cd $SPBUILDER_BASE/slackbuilds/$source_location
BUILD_DIR=$(pwd)
cd - >/dev/null
#echo $"Using directory: $BUILD_DIR for building"
else
echo $"BAD LOCATION for building ($source_location)"
exit 3
fi
env build_jobname=${SPBUILDER_BASE}/build/$(basename $source_location) \
lxc-copy \
--name $SPBUILDER_MASTER_NAME \
--newname=$our_name \
-B overlayfs \
--ephemeral
# -e|--ephemeral implies --snapshot
echo "Created snapshot: $our_name"
SPB_WRAPPER=$(tempfile)
sed -e "s/%SPB_SHUTDOWN%/$SPB_SHUTDOWN/" ${SPBUILDER_DATA_DIR}/spbuilder.wrapper.in >$SPB_WRAPPER
# -e|--ephemeral already starts the copied container lxc-start -n $our_name
lxc-wait -n $our_name -s RUNNING
lxc-attach -n $our_name -a $ARCH -- touch /usr/bin/spbuilder.build /usr/bin/spbuilder.wrapper
cp ${SPBUILDER_DATA_DIR}/spbuilder.build ${lxc_base_dir}/${our_name}/delta0/usr/bin/spbuilder.build
cp $SPB_WRAPPER ${lxc_base_dir}/${our_name}/delta0/usr/bin/spbuilder.wrapper
chmod a+x ${lxc_base_dir}/${our_name}/delta0/usr/bin/spbuilder.build ${lxc_base_dir}/${our_name}/delta0/usr/bin/spbuilder.wrapper
lxc-attach -n $our_name --clear-env -a $ARCH -- /usr/bin/spbuilder.wrapper $source_location
# Cleanup
rm $SPB_WRAPPER
echo "in spbuilder, SPB_SHUTDOWN=$SPB_SHUTDOWN"
if [ $SPB_SHUTDOWN -eq 1 ]; then
lxc-destroy -f -n $our_name
fi
exit
# Setup slapt-getrc and add any directories implied by it i.e. "SOURCE=file://..." entries
mv ${lxc_base_dir}/$our_name/rootfs/etc/slapt-get/slapt-getrc ${lxc_base_dir}/$our_name/rootfs/etc/slapt-get/slapt-getrc.orig
# SPBUILDER_REPO may be defined in /etc/spbuilder/spbuilderrc
if [ -z "$SPBUILDER_REPO" ]; then
cp -p /etc/slapt-get/slapt-getrc ${lxc_base_dir}/${our_name}/rootfs/etc/slapt-get/slapt-getrc
else
touch ${lxc_base_dir}/${our_name}/rootfs/etc/slapt-get/slapt-getrc
for entry in $SPBUILDER_REPO ; do
echo "$entry" >>${lxc_base_dir}/${our_name}/rootfs/etc/slapt-get/slapt-getrc
done
fi
echo "" >>${lxc_base_dir}/${our_name}/rootfs/etc/slapt-get/slapt-getrc
# Add any directories implied in the slapt-getrc
repo_rcfile=${lxc_base_dir}/${our_name}/rootfs/etc/slapt-get/slapt-getrc
xdirs=$(get_extra_dirs)
for xdir in $xdirs ; do
mkdir -p ${lxc_base_dir}/${our_name}/rootfs/$xdir
echo "$xdir ${lxc_base_dir}/${our_name}/rootfs/$xdir none rw,bind 0 0" \
>>${lxc_base_dir}/${our_name}/rootfs/etc/fstab
done
}
function early_out ()
{
echo
echo "Caught signal - finishing up now!"
chroot ${lxc_base_dir}/$our_name/rootfs shutdown -h now
lxc-stop -n $our_name
( cd ${lxc_base_dir}
[ -e ${our_name}/config.run ] && \
cp -p ${our_name}/config.run ${our_name}/config
)
lxc-destroy -n $our_name
/sbin/ifconfig $our_name down
/usr/sbin/brctl delbr $our_name
# If we used an spbuilder directory for building
# (this generally means building from a supplied tarball), remove it
rm -rf ${DEFAULT_BUILD_DIR}/$our_name
rm -rfv $our_pivotdir
echo " Finished (early)"
}
# Full cleanout of files likely to have been generated
# during normal operation
function full_cleanup ()
{
echo "Full cleanup (could take a while)"
echo -n "Remove /var/cache/lxc/spbuilder ... "
rm -rf /var/cache/lxc/spbuilder && echo "OK"
echo -n "Remove contents of ${SPBUILDER_BASE}/build ... "
rm -rf ${SPBUILDER_BASE}/build/* && echo "OK"
echo -n "Remove contents of ${SPBUILDER_BASE}/result ... "
rm -rf ${SPBUILDER_BASE}/result/* && echo "OK"
echo -n "Remove contents of ${SPBUILDER_BASE}/log ... "
rm -rf ${SPBUILDER_BASE}/log/* && echo "OK"
echo -n "Remove contents of ${SPBUILDER_BASE}/distfiles ... "
rm -rf ${SPBUILDER_BASE}/distfiles/* && echo "OK"
# Typical previous location for slackbuilds tree
mountpoint --quiet $SPBUILDER_BASE/slackbuilds
if [ $? -eq 0 ]; then
echo -n "Unmount spbuilder's slackbuilds ... "
umount ${SPBUILDER_BASE}/slackbuilds && echo "OK"
fi
}
function login2_container ()
{
echo $"Log into build container"
# Now try to run it
echo -n "Trying to start container $our_name ... "
lxc-start -n $SPBUILDER_MASTER_NAME -d
lxc-wait -n $SPBUILDER_MASTER_NAME -s RUNNING
echo "OK"; echo
echo "For basic environment, run:"
echo " . /root/.spbuilderrc"
echo
echo "To exit, run:"
echo " shutdown -h now"
echo
lxc-attach -n $SPBUILDER_MASTER_NAME --clear-env -a $ARCH
}
#############################
#### ####
#### Program Starts Here ####
#### ####
#############################
# Parse command line arguments
while [ $# -gt 0 ]; do
case $1 in
--source*|-s)
if echo $1 | grep '=' >/dev/null ; then
source_location=`echo $1 | sed 's/^.*=//'`
else
source_location="$2"
shift
fi
;;
--local-dir|-l)
if echo $1 | grep '=' >/dev/null ; then
this_local_dir=`echo $1 | sed 's/^.*=//'`
else
this_local_dir="$2"
shift
fi
if [ -z $local_dirs ]; then
local_dirs="$this_local_dir"
else
local_dirs="${local_dirs} ${this_local_dir}"
fi
;;
-*|--*)
echo $"" >&2
echo $"Error! Unknown option: $1" >&2
show_usage
exit 2
;;
*)
action="$action $1"
;;
esac
shift
done
[ -z $action ] && {
echo "" >&2
echo $"Error! No action was specified.">&2
show_usage
}
# Run the specified action
for action_to_run in $action; do
case "$action_to_run" in
create)
mkdir -p $BUILD_LOGDIR
logfile=${BUILD_LOGDIR}/create-$(date +"%Y%m%d-%H%M%S")
create_container
;;
update)
mkdir -p $BUILD_LOGDIR
logfile=${BUILD_LOGDIR}/update-$(date +"%Y%m%d-%H%M%S")
update_container
;;
build)
mkdir -p $BUILD_LOGDIR
logfile=${BUILD_LOGDIR}/build-$(date +"%Y%m%d-%H%M%S")
build_package
;;
clean)
full_cleanup
;;
login)
login2_container
;;
"")
echo "" >&2
echo $"Error! No action was specified.">&2
show_usage
;;
*)
echo "" >&2
echo $"Error! Unknown action specified: $action_to_run" >&2
show_usage
;;
esac
done