-
Notifications
You must be signed in to change notification settings - Fork 161
/
common.sh
executable file
·4890 lines (4214 loc) · 130 KB
/
common.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
# Copyright (c) 2010-2013 Baptiste Daroussin <bapt@FreeBSD.org>
# Copyright (c) 2010-2011 Julien Laffaye <jlaffaye@FreeBSD.org>
# Copyright (c) 2012-2014 Bryan Drewery <bdrewery@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. 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.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
BSDPLATFORM=`uname -s | tr '[:upper:]' '[:lower:]'`
. ${SCRIPTPREFIX}/include/common.sh.${BSDPLATFORM}
BLACKLIST=""
EX_SOFTWARE=70
# Return true if ran from bulk/testport, ie not daemon/status/jail
was_a_bulk_run() {
[ "${SCRIPTPATH##*/}" = "bulk.sh" -o "${SCRIPTPATH##*/}" \
= "testport.sh" ]
}
# Return true if in a bulk or other jail run that needs to shutdown the jail
was_a_jail_run() {
was_a_bulk_run || [ "${SCRIPTPATH##*/}" = "pkgclean.sh" ]
}
# Return true if output via msg() should show elapsed time
should_show_elapsed() {
[ -z "${TIME_START}" ] && return 1
[ "${NO_ELAPSED_IN_MSG:-0}" -eq 1 ] && return 1
case "${SCRIPTPATH##*/}" in
daemon.sh) ;;
help.sh) ;;
queue.sh) ;;
status.sh) ;;
version.sh) ;;
*) return 0 ;;
esac
return 1
}
not_for_os() {
local os=$1
shift
[ "${os}" = "${BSDPLATFORM}" ] && err 1 "This is not supported on ${BSDPLATFORM}: $@"
}
err() {
trap '' SIGINFO
export CRASHED=1
if [ $# -ne 2 ]; then
err 1 "err expects 2 arguments: exit_number \"message\""
fi
# Try to set status so other processes know this crashed
# Don't set it from children failures though, only master
if [ -z "${PARALLEL_CHILD}" ] && was_a_bulk_run; then
if [ -n "${MY_JOBID}" ]; then
bset ${MY_JOBID} status "${EXIT_STATUS:-crashed:}" \
2>/dev/null || :
else
bset status "${EXIT_STATUS:-crashed:}" 2>/dev/null || :
fi
fi
msg_error "$2" || :
# Avoid recursive err()->exit_handler()->err()... Just let
# exit_handler() cleanup.
if [ ${ERRORS_ARE_FATAL:-1} -eq 1 ]; then
exit $1
else
return 0
fi
}
# Message functions that depend on VERBOSE are stubbed out in post_getopts.
msg_n() {
local -; set +x
local now elapsed
elapsed=
if should_show_elapsed; then
now=$(clock_monotonic)
calculate_duration elapsed "$((${now} - ${TIME_START:-0}))"
elapsed="[${elapsed}] "
fi
if [ -n "${COLOR_ARROW}" ] || [ -z "${1##*\033[*}" ]; then
printf "${elapsed}${DRY_MODE}${COLOR_ARROW}====>>${COLOR_RESET} ${1}${COLOR_RESET_REAL}"
else
printf "${elapsed}${DRY_MODE}====>> ${1}"
fi
}
msg() {
msg_n "$@""\n"
}
msg_verbose() {
msg_n "$@""\n"
}
msg_error() {
local -; set +x
if [ -n "${MY_JOBID}" ]; then
# Send colored msg to bulk log...
COLOR_ARROW="${COLOR_ERROR}" job_msg "${COLOR_ERROR}Error: $1"
# And non-colored to buld log
msg "Error: $1" >&2
elif [ ${OUTPUT_REDIRECTED:-0} -eq 1 ]; then
# Send to true stderr
COLOR_ARROW="${COLOR_ERROR}" msg "${COLOR_ERROR}Error: $1" >&4
else
COLOR_ARROW="${COLOR_ERROR}" msg "${COLOR_ERROR}Error: $1" >&2
fi
return 0
}
msg_debug() {
COLOR_ARROW="${COLOR_DEBUG}" \
msg_n "${COLOR_DEBUG}Debug: $@""\n" >&2
}
msg_warn() {
COLOR_ARROW="${COLOR_WARN}" \
msg_n "${COLOR_WARN}Warning: $@""\n" >&2
}
job_msg() {
local -; set +x
local now elapsed NO_ELAPSED_IN_MSG output
if [ -n "${MY_JOBID}" ]; then
NO_ELAPSED_IN_MSG=0
now=$(clock_monotonic)
calculate_duration elapsed "$((${now} - ${TIME_START_JOB:-${TIME_START:-0}}))"
output="[${COLOR_JOBID}${MY_JOBID}${COLOR_RESET}][${elapsed}] $1"
else
output="$@"
fi
if [ ${OUTPUT_REDIRECTED:-0} -eq 1 ]; then
# Send to true stdout (not any build log)
msg_n "${output}\n" >&3
else
msg_n "${output}\n"
fi
}
# Stubbed until post_getopts
job_msg_verbose() {
local -; set +x
job_msg "$@"
}
prompt() {
[ $# -eq 1 ] || eargs prompt message
local message="$1"
local answer
msg_n "${message} [y/N] "
read answer
case "${answer}" in
[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
return 0
;;
esac
return 1
}
confirm_if_tty() {
[ $# -eq 1 ] || eargs confirm_if_tty message
local message="${1}"
[ -t 0 ] || return 0
prompt "${message}"
}
# Handle needs after processing arguments.
post_getopts() {
# Short-circuit verbose functions to save CPU
if ! [ ${VERBOSE} -gt 1 ]; then
msg_debug() { }
fi
if ! [ ${VERBOSE} -gt 0 ]; then
msg_verbose() { }
job_msg_verbose() { }
fi
}
_mastermnt() {
local hashed_name mnt mnttest mnamelen
mnamelen=$(grep "#define[[:space:]]MNAMELEN" \
/usr/include/sys/mount.h 2>/dev/null | awk '{print $3}')
mnt="${POUDRIERE_DATA}/.m/${MASTERNAME}/ref"
mnttest="${mnt}/compat/linux/proc"
if [ -n "${mnamelen}" ] && \
[ ${#mnttest} -ge $((${mnamelen} - 1)) ]; then
hashed_name=$(sha256 -qs "${MASTERNAME}" | \
awk '{print substr($0, 0, 6)}')
mnt="${POUDRIERE_DATA}/.m/${hashed_name}/ref"
mnttest="${mnt}/var/db/ports"
[ ${#mnttest} -ge $((${mnamelen} - 1)) ] && \
err 1 "Mountpath '${mnt}' exceeds system MNAMELEN limit of ${mnamelen}. Unable to mount. Try shortening BASEFS."
msg_warn "MASTERNAME '${MASTERNAME}' too long for mounting, using hashed version of '${hashed_name}'"
fi
setvar "$1" "${mnt}"
# MASTERMNTROOT
setvar "${1}ROOT" "${mnt%/ref}"
}
_my_path() {
if [ -z "${MY_JOBID}" ]; then
setvar "$1" "${MASTERMNT}"
elif [ -n "${MASTERMNTROOT}" ]; then
setvar "$1" "${MASTERMNTROOT}/${MY_JOBID}"
else
setvar "$1" "${MASTERMNT}/../${MY_JOBID}"
fi
}
_my_name() {
setvar "$1" "${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}}"
}
_log_path_top() {
setvar "$1" "${POUDRIERE_DATA}/logs/${POUDRIERE_BUILD_TYPE}"
}
_log_path_jail() {
local log_path_top
_log_path_top log_path_top
setvar "$1" "${log_path_top}/${MASTERNAME}"
}
_log_path() {
local log_path_jail
_log_path_jail log_path_jail
setvar "$1" "${log_path_jail}/${BUILDNAME}"
}
relpath() {
[ $# -eq 2 ] || eargs relpath dir1 dir2
local dir1=$(realpath -q "$1" || echo "${1}")
local dir2=$(realpath -q "$2" || echo "${2}")
local common
if [ "${#dir1}" -ge "${#dir2}" ]; then
common="${dir1}"
other="${dir2}"
else
common="${dir2}"
other="${dir1}"
fi
# Trim away path components until they match
while [ "${other#${common}}" = "${other}" -a -n "${common}" ]; do
common="${common%/*}"
done
common="${common:-/}"
dir1="${dir1#${common}}"
dir1="${dir1#/}"
dir1="${dir1:-.}"
dir2="${dir2#${common}}"
dir2="${dir2#/}"
dir2="${dir2:-.}"
echo "${common} ${dir1} ${dir2}"
}
injail() {
if [ "${USE_JEXECD}" = "no" ]; then
injail_tty "$@"
else
local name
_my_name name
rexec -s ${MASTERMNT}/../${name}${JNETNAME:+-${JNETNAME}}.sock \
-u ${JUSER:-root} "$@"
fi
}
injail_tty() {
local name
_my_name name
jexec -U ${JUSER:-root} ${name}${JNETNAME:+-${JNETNAME}} \
${MAX_MEMORY_JEXEC} "$@"
}
jstart() {
local name network
network="${localipargs}"
[ "${RESTRICT_NETWORKING}" = "yes" ] || network="${ipargs}"
_my_name name
jail -c persist name=${name} \
path=${MASTERMNT}${MY_JOBID+/../${MY_JOBID}} \
host.hostname=${BUILDER_HOSTNAME-${name}} \
${network} ${JAIL_PARAMS} \
allow.socket_af allow.raw_sockets allow.chflags allow.sysvipc
[ "${USE_JEXECD}" = "yes" ] && \
jexecd -j ${name} -d ${MASTERMNT}/../ ${MAX_MEMORY_BYTES+-m ${MAX_MEMORY_BYTES}}
jail -c persist name=${name}-n \
path=${MASTERMNT}${MY_JOBID+/../${MY_JOBID}} \
host.hostname=${BUILDER_HOSTNAME-${name}} \
${ipargs} ${JAIL_PARAMS} \
allow.socket_af allow.raw_sockets allow.chflags allow.sysvipc
[ "${USE_JEXECD}" = "yes" ] && \
jexecd -j ${name}-n -d ${MASTERMNT}/../ ${MAX_MEMORY_BYTES+-m ${MAX_MEMORY_BYTES}}
injail id >/dev/null 2>&1 || \
err 1 "Unable to execute id(1) in jail. Emulation or ABI wrong."
if ! injail id ${PORTBUILD_USER} >/dev/null 2>&1 ; then
msg_n "Creating user/group ${PORTBUILD_USER}"
injail pw groupadd ${PORTBUILD_USER} -g ${PORTBUILD_UID} || \
err 1 "Unable to create group ${PORTBUILD_USER}"
injail pw useradd ${PORTBUILD_USER} -u ${PORTBUILD_UID} -d /nonexistent -c "Package builder" || \
err 1 "Unable to create user ${PORTBUILD_USER}"
echo " done"
fi
}
jkill() {
injail kill -9 -1 2>/dev/null || :
}
jstop() {
local name
_my_name name
jail -r ${name} 2>/dev/null || :
jail -r ${name}-n 2>/dev/null || :
}
eargs() {
local fname="$1"
shift
case $# in
0) err ${EX_SOFTWARE} "${fname}: No arguments expected" ;;
1) err ${EX_SOFTWARE} "${fname}: 1 argument expected: $1" ;;
*) err ${EX_SOFTWARE} "${fname}: $# arguments expected: $*" ;;
esac
}
run_hook() {
local hookfile=${HOOKDIR}/${1}.sh
local build_url log_url
shift
build_url build_url || :
log_url log_url || :
[ -f ${hookfile} ] &&
BUILD_URL="${build_url}" \
LOG_URL="${log_url}" \
POUDRIERE_BUILD_TYPE=${POUDRIERE_BUILD_TYPE} \
POUDRIERED="${POUDRIERED}" \
POUDRIERE_DATA="${POUDRIERE_DATA}" \
MASTERNAME="${MASTERNAME}" \
MASTERMNT="${MASTERMNT}" \
MY_JOBID="${MY_JOBID}" \
BUILDNAME="${BUILDNAME}" \
JAILNAME="${JAILNAME}" \
PTNAME="${PTNAME}" \
SETNAME="${SETNAME}" \
PACKAGES="${PACKAGES}" \
PACKAGES_ROOT="${PACKAGES_ROOT}" \
/bin/sh ${hookfile} "$@"
return 0
}
log_start() {
[ $# -eq 1 ] || eargs log_start need_tee
local need_tee="$1"
local log log_top
local latest_log
_log_path log
_log_path_top log_top
logfile="${log}/logs/${PKGNAME}.log"
latest_log=${log_top}/latest-per-pkg/${PKGNAME%-*}/${PKGNAME##*-}
# Make sure directory exists
mkdir -p ${log}/logs ${latest_log}
:> ${logfile}
# Link to BUILD_TYPE/latest-per-pkg/PORTNAME/PKGVERSION/MASTERNAME.log
ln -f ${logfile} ${latest_log}/${MASTERNAME}.log
# Link to JAIL/latest-per-pkg/PKGNAME.log
ln -f ${logfile} ${log}/../latest-per-pkg/${PKGNAME}.log
# Save stdout/stderr for restoration later for bulk/testport -i
exec 3>&1 4>&2
OUTPUT_REDIRECTED=1
# Pipe output to tee(1) or timestamp if needed.
if [ ${need_tee} -eq 1 ] || [ "${TIMESTAMP_LOGS}" = "yes" ]; then
[ ! -e ${logfile}.pipe ] && mkfifo ${logfile}.pipe
if [ ${need_tee} -eq 1 ]; then
if [ "${TIMESTAMP_LOGS}" = "yes" ]; then
timestamp < ${logfile}.pipe | tee ${logfile} &
else
tee ${logfile} < ${logfile}.pipe &
fi
elif [ "${TIMESTAMP_LOGS}" = "yes" ]; then
timestamp > ${logfile} < ${logfile}.pipe &
fi
tpid=$!
exec > ${logfile}.pipe 2>&1
# Remove fifo pipe file right away to avoid orphaning it.
# The pipe will continue to work as long as we keep
# the FD open to it.
rm -f ${logfile}.pipe
else
# Send output directly to file.
tpid=
exec > ${logfile} 2>&1
fi
}
buildlog_start() {
local portdir=$1
local mnt
local var
_my_path mnt
echo "build started at $(date)"
echo "port directory: ${portdir}"
echo "building for: $(injail uname -a)"
echo "maintained by: $(injail /usr/bin/make -C ${portdir} maintainer)"
echo "Makefile ident: $(ident ${mnt}/${portdir}/Makefile|sed -n '2,2p')"
echo "Poudriere version: ${POUDRIERE_VERSION}"
echo "Host OSVERSION: ${HOST_OSVERSION}"
echo "Jail OSVERSION: ${JAIL_OSVERSION}"
echo
if [ ${JAIL_OSVERSION} -gt ${HOST_OSVERSION} ]; then
echo
echo
echo
echo "!!! Jail is newer than host. (Jail: ${JAIL_OSVERSION}, Host: ${HOST_OSVERSION}) !!!"
echo "!!! This is not supported. !!!"
echo "!!! Host kernel must be same or newer than jail. !!!"
echo "!!! Expect build failures. !!!"
echo
echo
echo
fi
echo "---Begin Environment---"
injail env ${PKGENV} ${PORT_FLAGS}
echo "---End Environment---"
echo ""
echo "---Begin OPTIONS List---"
injail /usr/bin/make -C ${portdir} showconfig || :
echo "---End OPTIONS List---"
echo ""
for var in CONFIGURE_ARGS CONFIGURE_ENV MAKE_ENV; do
echo "--${var}--"
echo "$(injail env ${PORT_FLAGS} /usr/bin/make -C ${portdir} -V ${var})"
echo "--End ${var}--"
echo ""
done
echo "--PLIST_SUB--"
echo "$(injail env ${PORT_FLAGS} /usr/bin/make -C ${portdir} -V PLIST_SUB | tr ' ' '\n' | grep -v '^$')"
echo "--End PLIST_SUB--"
echo ""
echo "--SUB_LIST--"
echo "$(injail env ${PORT_FLAGS} /usr/bin/make -C ${portdir} -V SUB_LIST | tr ' ' '\n' | grep -v '^$')"
echo "--End SUB_LIST--"
echo ""
echo "---Begin make.conf---"
cat ${mnt}/etc/make.conf
echo "---End make.conf---"
if [ -f "${mnt}/etc/make.nxb.conf" ]; then
echo "---Begin make.nxb.conf---"
cat ${mnt}/etc/make.nxb.conf
echo "---End make.nxb.conf---"
fi
}
buildlog_stop() {
[ $# -eq 3 ] || eargs buildlog_stop pkgname origin build_failed
local pkgname="$1"
local origin=$2
local build_failed="$3"
local log
local buildtime
_log_path log
buildtime=$( \
stat -f '%N %B' ${log}/logs/${pkgname}.log | awk -v now=$(date +%s) \
-f ${AWKPREFIX}/siginfo_buildtime.awk |
awk -F'!' '{print $2}' \
)
echo "build of ${origin} ended at $(date)"
echo "build time: ${buildtime}"
[ ${build_failed} -gt 0 ] && echo "!!! build failure encountered !!!"
return 0
}
log_stop() {
if [ ${OUTPUT_REDIRECTED:-0} -eq 1 ]; then
exec 1>&3 3>&- 2>&4 4>&-
OUTPUT_REDIRECTED=0
fi
if [ -n "${tpid}" ]; then
# Give tee a moment to flush buffers
timed_wait_and_kill 5 $tpid
unset tpid
fi
}
read_file() {
[ $# -eq 2 ] || eargs read_file var_return file
local var_return="$1"
local file="$2"
local _data line
local ret -
set +e
_data=
_read_file_lines_read=0
if [ ${READ_FILE_USE_CAT:-0} -eq 1 ]; then
if [ -f "${file}" ]; then
_data="$(cat "${file}")"
_read_file_lines_read=$(wc -l < "${file}")
_read_file_lines_read=${_read_file_lines_read##* }
ret=0
else
ret=1
fi
else
while :; do
read -r line
ret=$?
case ${ret} in
# Success, process data and keep reading.
0) ;;
# EOF
1)
ret=0
break
;;
# Some error or interruption/signal. Reread.
*) continue ;;
esac
[ ${_read_file_lines_read} -gt 0 ] && _data="${_data}
"
_data="${_data}${line}"
_read_file_lines_read=$((${_read_file_lines_read} + 1))
done < "${file}" || ret=$?
fi
setvar "${var_return}" "${_data}"
return ${ret}
}
# Read a file until 0 status is found. Partial reads not accepted.
read_line() {
[ $# -eq 2 ] || eargs read_line var_return file
local var_return="$1"
local file="$2"
local max_reads reads ret line
ret=0
line=
if [ -f "${file}" ]; then
max_reads=100
reads=0
# Read until a full line is returned.
until [ ${reads} -eq ${max_reads} ] || \
read -t 1 -r line < "${file}"; do
sleep 0.1
reads=$((${reads} + 1))
done
[ ${reads} -eq ${max_reads} ] && ret=1
else
ret=1
fi
setvar "${var_return}" "${line}"
return ${ret}
}
attr_set() {
local type=$1
local name=$2
local property=$3
shift 3
mkdir -p ${POUDRIERED}/${type}/${name}
echo "$@" > ${POUDRIERED}/${type}/${name}/${property} || :
}
jset() { attr_set jails "$@" ; }
pset() { attr_set ports "$@" ; }
_attr_get() {
[ $# -eq 4 ] || eargs _attr_get var_return type name property
local var_return="$1"
local type="$2"
local name="$3"
local property="$4"
read_file "${var_return}" \
"${POUDRIERED}/${type}/${name}/${property}" && return 0
setvar "${var_return}" ""
return 1
}
attr_get() {
local attr_get_data
if _attr_get attr_get_data "$@"; then
[ -n "${attr_get_data}" ] && echo "${attr_get_data}"
return 0
fi
return 1
}
jget() { attr_get jails "$@" ; }
_jget() {
[ $# -eq 3 ] || eargs _jget var_return ptname property
local var_return="$1"
shift
_attr_get "${var_return}" jails "$@"
}
pget() { attr_get ports "$@" ; }
_pget() {
[ $# -eq 3 ] || eargs _pget var_return ptname property
local var_return="$1"
shift
_attr_get "${var_return}" ports "$@"
}
#build getter/setter
_bget() {
local var_return id property mnt log file READ_FILE_USE_CAT
var_return="$1"
_log_path log
shift
if [ $# -eq 2 ]; then
id="$1"
shift
fi
file=".poudriere.${1}${id:+.${id}}"
# Use cat(1) to read long list files.
[ -z "${1##ports.*}" ] && READ_FILE_USE_CAT=1
read_file "${var_return}" "${log}/${file}" && return 0
setvar "${var_return}" ""
return 1
}
bget() {
local bget_data
if _bget bget_data "$@"; then
[ -n "${bget_data}" ] && echo "${bget_data}"
return 0
fi
return 1
}
bset() {
was_a_bulk_run || return 0
local id property mnt log
_log_path log
if [ $# -eq 3 ]; then
id=$1
shift
fi
property="$1"
file=.poudriere.${property}${id:+.${id}}
shift
[ "${property}" = "status" ] && \
echo "$@" >> ${log}/${file}.journal% || :
if echo "$@" > ${log}/.tmp.${file}; then
mv -f ${log}/.tmp.${file} ${log}/${file}
fi
}
bset_job_status() {
[ $# -eq 2 ] || eargs bset_job_status status origin
local status="$1"
local origin="$2"
bset ${MY_JOBID} status "${status}:${origin}:${PKGNAME}:${TIME_START_JOB:-${TIME_START}}:$(clock_monotonic)"
}
badd() {
local id property mnt log
_log_path log
if [ $# -eq 3 ]; then
id=$1
shift
fi
file=.poudriere.${1}${id:+.${id}}
shift
echo "$@" >> ${log}/${file} || :
}
update_stats() {
local type unused
local -
set +e
lock_acquire update_stats || return 1
for type in built failed ignored; do
_bget unused "ports.${type}"
bset "stats_${type}" ${_read_file_lines_read}
done
# Skipped may have duplicates in it
bset stats_skipped $(bget ports.skipped | awk '{print $1}' | \
sort -u | wc -l)
lock_release update_stats
}
sigpipe_handler() {
EXIT_STATUS="sigpipe:"
SIGNAL="SIGPIPE"
sig_handler
}
sigint_handler() {
EXIT_STATUS="sigint:"
SIGNAL="SIGINT"
sig_handler
}
sigterm_handler() {
EXIT_STATUS="sigterm:"
SIGNAL="SIGTERM"
sig_handler
}
sig_handler() {
# Reset SIGTERM handler, just exit if another is received.
trap - SIGTERM
# Ignore SIGPIPE for messages
trap '' SIGPIPE
# Ignore SIGINT while cleaning up
trap '' SIGINT
trap '' SIGINFO
err 1 "Signal ${SIGNAL} caught, cleaning up and exiting"
}
exit_handler() {
# Ignore errors while cleaning up
set +e
ERRORS_ARE_FATAL=0
trap '' SIGINFO
# Avoid recursively cleaning up here
trap - EXIT SIGTERM
# Ignore SIGPIPE for messages
trap '' SIGPIPE
# Ignore SIGINT while cleaning up
trap '' SIGINT
if was_a_bulk_run; then
log_stop
fi
parallel_shutdown
[ ${STATUS} -eq 1 ] && cleanup
if was_a_bulk_run; then
coprocess_stop html_json
coprocess_stop pkg_cacher
fi
[ -n ${CLEANUP_HOOK} ] && ${CLEANUP_HOOK}
}
build_url() {
if [ -z "${URL_BASE}" ]; then
setvar "$1" ""
return 1
fi
setvar "$1" "${URL_BASE}/build.html?mastername=${MASTERNAME}&build=${BUILDNAME}"
}
log_url() {
if [ -z "${URL_BASE}" ]; then
setvar "$1" ""
return 1
fi
setvar "$1" "${URL_BASE}/data/${MASTERNAME}/${BUILDNAME}/logs"
}
show_log_info() {
local log build_url
_log_path log
msg "Logs: ${log}"
build_url build_url && \
msg "WWW: ${build_url}"
return 0
}
show_build_summary() {
local status nbb nbf nbs nbi nbq ndone nbtobuild buildname
local log now elapsed buildtime queue_width
update_stats 2>/dev/null || return 0
_bget nbq stats_queued 2>/dev/null || nbq=0
_bget status status 2>/dev/null || status=unknown
_bget nbf stats_failed 2>/dev/null || nbf=0
_bget nbi stats_ignored 2>/dev/null || nbi=0
_bget nbs stats_skipped 2>/dev/null || nbs=0
_bget nbb stats_built 2>/dev/null || nbb=0
ndone=$((nbb + nbf + nbi + nbs))
nbtobuild=$((nbq - ndone))
if [ ${nbq} -gt 9999 ]; then
queue_width=5
elif [ ${nbq} -gt 999 ]; then
queue_width=4
elif [ ${nbq} -gt 99 ]; then
queue_width=3
else
queue_width=2
fi
_log_path log
_bget buildname buildname 2>/dev/null || :
now=$(date +%s)
calculate_elapsed_from_log ${now} ${log} || return 1
elapsed=${_elapsed_time}
calculate_duration buildtime ${elapsed}
printf "[${MASTERNAME}] [${buildname}] [${status}] \
Queued: %-${queue_width}d ${COLOR_SUCCESS}Built: %-${queue_width}d \
${COLOR_FAIL}Failed: %-${queue_width}d ${COLOR_SKIP}Skipped: \
%-${queue_width}d ${COLOR_IGNORE}Ignored: %-${queue_width}d${COLOR_RESET} \
Tobuild: %-${queue_width}d Time: %s\n" \
${nbq} ${nbb} ${nbf} ${nbs} ${nbi} ${nbtobuild} "${buildtime}"
}
siginfo_handler() {
trappedinfo=1
[ "${POUDRIERE_BUILD_TYPE}" != "bulk" ] && return 0
local status
local now
local j elapsed job_id_color
local pkgname origin phase buildtime started
local format_origin_phase format_phase
local -
set +e
trap '' SIGINFO
_bget status status 2>/dev/null || status=unknown
if [ "${status}" = "index:" -o "${status#stopped:}" = "crashed:" ]; then
enable_siginfo_handler
return 0
fi
_bget nbq stats_queued 2>/dev/null || nbq=0
if [ -z "${nbq}" ]; then
enable_siginfo_handler
return 0
fi
show_build_summary
now=$(clock_monotonic)
# Skip if stopping or starting jobs or stopped.
if [ -n "${JOBS}" -a "${status#starting_jobs:}" = "${status}" \
-a "${status}" != "stopping_jobs:" -a -n "${MASTERMNT}" ] && \
! status_is_stopped "${status}"; then
for j in ${JOBS}; do
# Ignore error here as the zfs dataset may not be cloned yet.
_bget status ${j} status 2>/dev/null || :
# Skip builders not started yet
[ -z "${status}" ] && continue
# Hide idle workers
[ "${status}" = "idle:" ] && continue
phase="${status%%:*}"
status="${status#*:}"
origin="${status%%:*}"
status="${status#*:}"
pkgname="${status%%:*}"
status="${status#*:}"
started="${status%%:*}"
colorize_job_id job_id_color "${j}"
# Must put colors in format
format_origin_phase="\t[${job_id_color}%s${COLOR_RESET}]: ${COLOR_PORT}%-32s ${COLOR_PHASE}%-15s${COLOR_RESET} (%s)\n"
format_phase="\t[${job_id_color}%s${COLOR_RESET}]: ${COLOR_PHASE}%15s${COLOR_RESET}\n"
if [ -n "${pkgname}" ]; then
elapsed=$((${now} - ${started}))
calculate_duration buildtime "${elapsed}"
printf "${format_origin_phase}" "${j}" \
"${origin}" "${phase}" ${buildtime}
else
printf "${format_phase}" "${j}" "${phase}"
fi
done
fi
show_log_info
enable_siginfo_handler
}
jail_exists() {
[ $# -ne 1 ] && eargs jail_exists jailname
local jname=$1
[ -d ${POUDRIERED}/jails/${jname} ] && return 0
return 1
}
jail_runs() {
[ $# -ne 1 ] && eargs jail_runs jname
local jname=$1
jls -j $jname >/dev/null 2>&1 && return 0
return 1
}
porttree_list() {
local name method mntpoint
[ -d ${POUDRIERED}/ports ] || return 0
for p in $(find ${POUDRIERED}/ports -type d -maxdepth 1 -mindepth 1 -print); do
name=${p##*/}
_pget mnt ${name} mnt 2>/dev/null || :
_pget method ${name} method 2>/dev/null || :
echo "${name} ${method:--} ${mnt}"
done
}
porttree_exists() {
[ $# -ne 1 ] && eargs porttree_exists portstree_name
porttree_list |
awk -v portstree_name=$1 '
BEGIN { ret = 1 }
$1 == portstree_name {ret = 0; }
END { exit ret }
' && return 0
return 1
}
get_data_dir() {
local data
if [ -n "${POUDRIERE_DATA}" ]; then
echo ${POUDRIERE_DATA}
return
fi
if [ -z "${NO_ZFS}" ]; then
data=$(zfs list -rt filesystem -H -o ${NS}:type,mountpoint ${ZPOOL}${ZROOTFS} 2>/dev/null |
awk '$1 == "data" { print $2; exit; }')
if [ -n "${data}" ]; then
echo "${data}"
return