-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreproduce.sh
More file actions
executable file
·1122 lines (868 loc) · 28.7 KB
/
reproduce.sh
File metadata and controls
executable file
·1122 lines (868 loc) · 28.7 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
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) 2023-2026, Jesús Daniel Colmenares Oviedo <DtxdF@disroot.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:
#
# * 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 the copyright holder 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 HOLDER 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.
# Reproduce version.
VERSION="%%VERSION%%"
# Used by sig_handler.
LAST_EXIT_STATUS=0
# Used by sig_handler_remove_last_jail.
LAST_JAIL=
# Used by sig_handler_terminate_last_pid.
LAST_PIDS=
# Used by sig_handler_remove_lock.
LOCKS=
# Colors
COLOR_DEFAULT="\033[39;49m"
COLOR_RED="\033[0;31m"
COLOR_LIGHT_YELLOW="\033[0;93m"
COLOR_LIGHT_BLUE="\033[0;94m"
COLOR_GRAY="\033[0;90m"
# see sysexits(3)
EX_OK=0
EX_USAGE=64
EX_DATAERR=65
EX_NOINPUT=66
EX_NOUSER=67
EX_NOHOST=68
EX_UNAVAILABLE=69
EX_SOFTWARE=70
EX_OSERR=71
EX_OSFILE=72
EX_CANTCREAT=73
EX_IOERR=74
EX_TEMPFAIL=75
EX_PROTOCOL=76
EX_NOPERM=77
EX_CONFIG=78
# Home directory.
UID=`id -u`
HOMEDIR=`getent passwd ${UID} | cut -d: -f6` || exit $?
if [ ! -d "${HOMEDIR}" ]; then
err "Cannot find home directory '${HOMEDIR}'"
fi
# Configuration file.
CONFIG="${HOMEDIR}/.config/appjail-reproduce/config.conf"
# Reproduce directory prefix.
REPRODUCEDIR="${HOMEDIR}/.reproduce"
# Signals.
IGNORED_SIGNALS="SIGALRM SIGVTALRM SIGPROF SIGUSR1 SIGUSR2"
HANDLER_SIGNALS="SIGHUP SIGINT SIGQUIT SIGTERM SIGXCPU SIGXFSZ"
# Defaults.
PROJECTSDIR="${REPRODUCEDIR}/projects"
LOGSDIR="${REPRODUCEDIR}/logs"
RUNDIR="${REPRODUCEDIR}/run"
if [ `id -u` -eq 0 ]; then
LOCKSDIR="/tmp/reproduce/locks"
else
LOCKSDIR="${REPRODUCEDIR}/locks"
fi
JAIL_PREFIX="reproduce_"
BEFORE_MAKEJAILS=
AFTER_MAKEJAILS=
MIRRORS=
DEBUG="NO"
COMPRESS_ALGO="xz"
main()
{
local _o
local images=
local opt_build=0
local opt_force=0
local opt_check_config=0
local opt_show_images=0
local errlevel
set -T
trap '' ${IGNORED_SIGNALS}
trap 'sig_handler; exit 70' ${HANDLER_SIGNALS}
trap 'LAST_EXIT_STATUS=$?; sig_handler; exit ${LAST_EXIT_STATUS}' EXIT
if [ $# -eq 0 ]; then
usage
exit ${EX_USAGE}
fi
if ! which -s appjail; then
err "Cannot find appjail, please install it using 'pkg-install(8)':"
err
err "# pkg install -y appjail # or"
err "# pkg install -y appjail-devel"
exit ${EX_UNAVAILABLE}
fi
if ! which -s appjail-config; then
err "Cannot find appjail-config!"
exit ${EX_UNAVAILABLE}
fi
while getopts ":bdfsvA:B:C:c:j:L:l:m:p:r:" _o; do
case "${_o}" in
A|B|C|c|j|L|l|m|p|r)
if [ -z "${OPTARG}" ]; then
usage
exit ${EX_USAGE}
fi
;;
esac
case "${_o}" in
b)
opt_build=1
;;
d)
DEBUG="YES"
;;
f)
opt_force=1
;;
s)
opt_show_images=1
;;
v)
version
exit 0
;;
A)
AFTER_MAKEJAILS="${OPTARG}"
;;
B)
BEFORE_MAKEJAILS="${OPTARG}"
;;
C)
COMPRESS_ALGO="${OPTARG}"
;;
c)
CONFIG="${OPTARG}"
opt_check_config=1
;;
j)
JAIL_PREFIX="${OPTARG}"
;;
L)
LOCKSDIR="${OPTARG}"
;;
l)
LOGSDIR="${OPTARG}"
;;
m)
MIRRORS="${OPTARG}"
;;
p)
PROJECTSDIR="${OPTARG}"
;;
r)
RUNDIR="${OPTARG}"
;;
*)
usage
exit ${EX_USAGE}
;;
esac
done
shift $((OPTIND-1))
if [ ${opt_build} -eq 0 ]; then
usage
exit ${EX_USAGE}
fi
if [ -f "${CONFIG}" ]; then
if [ ${opt_check_config} -eq 1 ]; then
err "Cannot find configuration file '${CONFIG}'"
exit ${errlevel}
fi
debug "Loading configuration file '${CONFIG}'"
. "${CONFIG}"
errlevel=$?
if [ ${errlevel} -ne 0 ]; then
err "Error loading '${CONFIG}'"
exit ${errlevel}
fi
fi
if [ ! -d "${PROJECTSDIR}" ]; then
debug "Creating projects directory '${PROJECTSDIR}'"
safe_exc mkdir -p -- "${PROJECTSDIR}" || exit $?
fi
if [ ! -d "${RUNDIR}" ]; then
debug "Creating run directory '${RUNDIR}'"
safe_exc mkdir -p -- "${RUNDIR}" || exit $?
fi
local total
if [ $# -gt 0 ]; then
PROJECTS="$@"
if [ -z "${PROJECTS}" ]; then
usage
exit ${EX_USAGE}
fi
debug "Projects defined from positional arguments: ${PROJECTS}"
total="$#"
else
PROJECTS=`ls -- "${PROJECTSDIR}"` || exit $?
if [ -z "${PROJECTS}" ]; then
err "There are no projects to build."
exit ${EX_NOINPUT}
fi
debug "Projects found in projects directory:" ${PROJECTS}
total=`printf "%s\n" "${PROJECTS}" | wc -l | tr -d ' '`
fi
local total_errors=0 total_hits=0 index=1 project
local makejail after_makejails_args=
for makejail in ${AFTER_MAKEJAILS}; do
if [ ! -f "${makejail}" ]; then
err "'${makejail}' Makejail (type:after) not found."
exit ${EX_NOINPUT}
fi
debug "Processing Makejail (type:after): ${makejail}"
makejail=`safe_exc realpath -- "${makejail}"` || exit $?
if [ -z "${after_makejails_args}" ]; then
after_makejails_args="-a \"${makejail}\""
else
after_makejails_args="${after_makejails_args} -a \"${makejail}\""
fi
done
local before_makejails_args=
for makejail in ${BEFORE_MAKEJAILS}; do
if [ ! -f "${makejail}" ]; then
err "'${makejail}' Makejail (type:before) not found."
exit ${EX_NOINPUT}
fi
debug "Processing Makejail (type:before): ${makejail}"
makejail=`safe_exc realpath -- "${makejail}"` || exit $?
if [ -z "${after_makejails_args}" ]; then
before_makejails_args="-B \"${makejail}\""
else
before_makejails_args="${before_makejails_args} -B \"${makejail}\""
fi
done
local init_build_time=`date +"%s"`
info "Started at `date`"
for project in ${PROJECTS}; do
local _project
_project=`basename -- "${project}" 2>&1`
if [ $? -ne 0 ]; then
err "${_project}"
total_errors=$((total_errors+1))
continue
fi
if [ "${project}" != "${_project}" ]; then
err "Invalid project name '${project}'"
total_errors=$((total_errors+1))
continue
fi
project="${_project}"
local tags2build
tags2build=`getvalue : "${project}"`
project=`getkey : "${project}"`
local arch2build
arch2build=`getvalue % "${project}"`
project=`getkey % "${project}"`
if [ -z "${project}" ]; then
err "Project number '${index}' has an empty name."
total_errors=$((total_errors+1))
continue
fi
if [ "${total}" -gt 1 ]; then
info "[${project}] (${index}/${total}):"
else
info "[${project}]:"
fi
index=$((index+1))
local projectdir="${PROJECTSDIR}/${project}"
if [ -d "${projectdir}" ]; then
export REPRODUCE_PROJECT="${project}"
export REPRODUCE_PROJECTDIR="${projectdir}"
else
err "Project not found."
total_errors=$((total_errors+1))
continue
fi
local makejail="${projectdir}/Makejail"
if [ ! -f "${makejail}" ]; then
err "Makejail not found."
total_errors=$((total_errors+1))
continue
fi
projectdir=`realpath -- "${projectdir}" 2>&1`
if [ $? -ne 0 ]; then
err "${projectdir}"
total_errors=$((total_errors+1))
continue
fi
local rundir="${RUNDIR}/${project}"
if [ ! -d "${rundir}" ]; then
debug "Creating run directory '${rundir}'"
if ! safe_exc mkdir -p -- "${rundir}"; then
total_errors=$((total_errors+1))
continue
fi
fi
local done_file="${rundir}/done"
if [ -f "${done_file}" ] && [ ${opt_force} -eq 0 ]; then
info "Project '${project}' is marked as completed."
total_hits=$((total_hits+1))
continue
fi
local lockdir="${LOCKSDIR}/${project}"
if [ ! -d "${lockdir}" ]; then
debug "Creating lock directory '${lockdir}'"
if ! safe_exc mkdir -p -- "${lockdir}"; then
total_errors=$((total_errors+1))
continue
fi
fi
local lock_file="${lockdir}/lock"
if [ -f "${lock_file}" ]; then
err "Project '${project}' is locked!"
err "If you are sure that project '${project}' is not being used by another process, run 'rm -f \"${lock_file}\"'"
total_errors=$((total_errors+1))
continue
else
LOCKS="${LOCKS} ${project}"
debug "Locking '${project}'"
if ! safe_exc touch -- "${lock_file}"; then
total_errors=$((total_errors+1))
continue
fi
fi
if [ -f "${done_file}" ]; then
debug "Removing 'done' file"
if ! safe_exc rm -f -- "${done_file}"; then
total_errors=$((total_errors+1))
continue
fi
fi
local logdir="${LOGSDIR}/${project}"
if [ ! -d "${logdir}" ]; then
debug "Creating log directory '${logdir}'"
if ! safe_exc mkdir -p -- "${logdir}"; then
total_errors=$((total_errors+1))
continue
fi
fi
local config="${projectdir}/reproduce.conf"
local reproduce_jail_name=
local reproduce_name=
local reproduce_version=
local reproduce_tags=
local reproduce_arch=
local reproduce_release=
local reproduce_ignore_osarch=
local reproduce_ignore_osversion=
local reproduce_ignore_release=
local reproduce_args=
local reproduce_remove_rc_vars=
local reproduce_mirrors=
if [ -f "${rundir}/jail_name" ]; then
reproduce_jail_name=`head -1 -- "${rundir}/jail_name"`
if [ $? -ne 0 ]; then
err "The jail name cannot be obtained."
total_errors=$((total_errors+1))
continue
fi
fi
if [ -z "${reproduce_jail_name}" ]; then
reproduce_jail_name="${JAIL_PREFIX}`uuidgen -r`"
safe_exc printf "%s" "${reproduce_jail_name}" > "${rundir}/jail_name"
fi
export REPRODUCE_JAIL_NAME="${reproduce_jail_name}"
debug "Jail name for ${project} is ${reproduce_jail_name}"
if [ -f "${config}" ]; then
local _key _value _err=0
for _key in name release ignore_external ignore_osarch ignore_osversion ignore_release; do
_value=`getconf "${config}" ${_key}`
if [ $? -ne 0 ]; then
_err=1
break
fi
setvar "reproduce_${_key}" "${_value}"
done
if [ ${_err} -eq 1 ]; then
total_errors=$((total_errors+1))
continue
fi
for _key in tags arch args remove_rc_vars mirrors; do
_value=`getallconf "${config}" ${_key}`
if [ $? -ne 0 ]; then
_err=1
break
fi
setvar "reproduce_${_key}" "${_value}"
done
if [ ${_err} -eq 1 ]; then
total_errors=$((total_errors+1))
continue
fi
fi
reproduce_name="${reproduce_name:-${project}}"
if [ -z "${images}" ]; then
images="${reproduce_name}"
else
images="${images} ${reproduce_name}"
fi
reproduce_version=`freebsd-version | grep -Eo '[0-9]+\.[0-9]+-[a-zA-Z0-9]+'`
reproduce_tags="${reproduce_tags:-latest/${reproduce_version}}"
reproduce_arch="${reproduce_arch:-`uname -p`}"
reproduce_ignore_external="${reproduce_ignore_external:-NO}"
reproduce_ignore_osarch="${reproduce_ignore_osarch:-NO}"
reproduce_ignore_osversion="${reproduce_ignore_osversion:-NO}"
reproduce_ignore_release="${reproduce_ignore_release:-NO}"
reproduce_release="${reproduce_release:-default}"
export REPRODUCE_OSRELEASE="${reproduce_release}"
if trace_exc appjail image get -- "${reproduce_name}" name > /dev/null 2>&1; then
debug "Removing image '${reproduce_name}'"
if ! trace_exc appjail image remove -- "${reproduce_name}"; then
err "Error removing image '${reproduce_name}'"
total_errors=$((total_errors+1))
continue
fi
fi
local arch
local logfile="${logdir}/`date +"%Y-%m-%d_%Hh%Mm%Ss"`.log"
info "Logs: ${logfile}"
for arch in ${reproduce_arch}; do
errlevel=0
if [ -n "${arch2build}" ]; then
if ! checkmatch , "${arch}" "${arch2build}"; then
debug "Ignoring arch '${arch}'"
continue
fi
fi
export REPRODUCE_OSARCH="${arch}"
local tag
for tag in ${reproduce_tags}; do
version=`getvalue / "${tag}"`
local _version=`basename -- "${version}"`
if [ "${version}" != "${_version}" ]; then
err "Invalid OS version '${version}'"
total_errors=$((total_errors+1))
continue
fi
export REPRODUCE_OSVERSION="${version}"
local _tag=`getkey / "${tag}"`
if [ -z "${_tag}" ]; then
err "'${_tag}' incorrect format: tag/osversion"
total_errors=$((total_errors+1))
continue
fi
tag="${_tag}"
_tag=`basename -- "${tag}"`
if [ "${tag}" != "${_tag}" ]; then
err "Invalid tag '${tag}'"
total_errors=$((total_errors+1))
continue
fi
if [ -n "${tags2build}" ]; then
if ! checkmatch , "${tag}" "${tags2build}"; then
debug "Ignoring tag '${tag}'"
continue
fi
fi
export REPRODUCE_TAG="${tag}"
info "> [${project}] (osarch:${arch}, osversion:${version}, tag:${tag}):"
local _err=0
local arg args=
for arg in ${reproduce_args}; do
local value=
checkparam "${config}" "${tag}.args.${arg}"
errlevel=$?
if [ ${errlevel} -eq 0 ]; then
value=`getconf "${config}" "${tag}.args.${arg}"`
elif [ ${errlevel} -eq 1 ]; then
continue
else
_err=1
break
fi
if [ -z "${args}" ]; then
args="\"--${arg}\" \"${value}\""
else
args="${args} \"--${arg}\" \"${value}\""
fi
done
if [ ${_err} -eq 1 ]; then
total_errors=$((total_errors+1))
continue
fi
info "Executing Makejail: jail:${reproduce_jail_name}, release:${reproduce_release}, args:${args}" 2>&1 | tee -a "${logfile}" >&2
stop_and_destroy_jail "${reproduce_jail_name}"
local prehook="${projectdir}/prehook.sh"
if [ -f "${prehook}" ]; then
if [ ! -x "${prehook}" ]; then
warn "'${prehook}' prehook does not have the execution bit permission set."
total_errors=$((total_errors+1))
continue
fi
info "Executing prehook" 2>&1 | tee -a "${logfile}" >&2
trace_exc appjail cmd jaildir "${prehook}" >> "${logfile}" 2>&1
errlevel=$?
if [ ${errlevel} -ne 0 ]; then
err "Hook exits with a non-zero exit status (${errlevel})." 2>&1 | tee -a "${logfile}" >&2
fi
fi
local osversion_arg=
if ! checkyesno "ignore_osversion" "${reproduce_ignore_osversion}"; then
osversion_arg="-o osversion=\"${version}\""
fi
local osarch_arg=
if ! checkyesno "ignore_osarch" "${reproduce_ignore_osarch}"; then
osarch_arg="-o osarch=\"${arch}\""
fi
local release_arg=
if ! checkyesno "ignore_release" "${reproduce_ignore_release}"; then
release_arg="-o release=\"${reproduce_release}\""
fi
local external_makejails_args=
if ! checkyesno "ignore_external" "${reproduce_ignore_external}"; then
external_makejails_args="${before_makejails_args} ${after_makejails_args}"
fi
LAST_JAIL="${reproduce_jail_name}"
local init_makejail_time=`date +"%s"`
eval trace_exc appjail makejail \
-j "${reproduce_jail_name}" \
-f "${makejail}" \
${osversion_arg} \
${osarch_arg} \
${release_arg} \
${external_makejails_args} \
-- ${args} >> "${logfile}" 2>&1
errlevel=$?
info "Execution time: `calc_build_time ${init_makejail_time}`" 2>&1 | tee -a "${logfile}" >&2
if [ ${errlevel} -ne 0 ]; then
err "Makejail exits with a non-zero exit status (${errlevel})." 2>&1 | tee -a "${logfile}" >&2
total_errors=$((total_errors+1))
stop_and_destroy_jail "${reproduce_jail_name}"
continue
fi
stop_jail "${reproduce_jail_name}"
if [ -f "${projectdir}/toremove.lst" ]; then
local file
while IFS= read -r file; do
info "Removing: rm ${file}" 2>&1 | tee -a "${logfile}" >&2
if ! trace_exc appjail cmd local "${reproduce_jail_name}" sh -c "rm ${file}"; then
warn "Error removing ${file}" 2>&1 | tee -a "${logfile}" >&2
fi
done < "${projectdir}/toremove.lst"
fi
local rc_var
for rc_var in ${reproduce_remove_rc_vars}; do
info "Removing rc variable: ${rc_var}" 2>&1 | tee -a "${logfile}" >&2
trace_exc appjail cmd local "${reproduce_jail_name}" sysrc -f etc/rc.conf -ix -- "${rc_var}" 2>&1 | tee -a "${logfile}" >&2
done
local hook="${projectdir}/hook.sh"
if [ -f "${hook}" ]; then
if [ ! -x "${hook}" ]; then
warn "'${hook}' hook does not have the execution bit permission set."
total_errors=$((total_errors+1))
continue
fi
info "Executing hook" 2>&1 | tee -a "${logfile}" >&2
trace_exc appjail cmd jaildir "${hook}" >> "${logfile}" 2>&1
errlevel=$?
if [ ${errlevel} -ne 0 ]; then
err "Hook exits with a non-zero exit status (${errlevel})." 2>&1 | tee -a "${logfile}" >&2
fi
fi
info "Exporting ${reproduce_name}" 2>&1 | tee -a "${logfile}" >&2
local init_export_time=`date +"%s"`
if trace_exc appjail image export -f -c "${COMPRESS_ALGO}" -t "${tag}" -n "${reproduce_name}" -- "${reproduce_jail_name}" >> "${logfile}" 2>&1; then
total_hits=$((total_hits+1))
else
err "Error exporting '${reproduce_name}'"
total_errors=$((total_errors+1))
fi
info "Export time: `calc_build_time ${init_export_time}`" 2>&1 | tee -a "${logfile}" >&2
stop_and_destroy_jail "${reproduce_jail_name}"
local mirror mirrors=
for mirror in ${MIRRORS}; do
if [ -z "${mirrors}" ]; then
mirrors="${mirror}/${reproduce_name}"
else
mirrors="${mirrors} ${mirror}/${reproduce_name}"
fi
done
for mirror in ${mirrors} ${reproduce_mirrors}; do
mirror="${mirror}/${tag}-${arch}-image.appjail"
debug "Adding mirror (tag:${tag}, arch:${arch}): ${mirror}"
if ! trace_exc appjail image metadata set -t "${tag}" -I -- "${reproduce_name}" "source:${arch}+=${mirror}"; then
err "Error adding mirror: ${mirror}"
total_errors=$((total_errors+1))
fi
done
safe_exc touch -- "${rundir}/done" || exit $?
done
done
done
info "---"
# Dramatic delay.
sleep 1
info "Ended at `date`"
info "Build time: `calc_build_time ${init_build_time}`"
info "Hits: ${total_hits}"
info "Errors: ${total_errors}"
if [ ${opt_show_images} -eq 1 ]; then
stdout "${images}"
fi
if [ ${total_errors} -eq 0 ]; then
exit ${EX_OK}
else
exit ${EX_SOFTWARE}
fi
}
getkey()
{
printf "%s" "$2" | cut -d"$1" -f1
}
getvalue()
{
printf "%s" "$2" | cut -s -d"$1" -f2-
}
sig_handler()
{
trap '' ${HANDLER_SIGNALS} EXIT
sig_handler_unset_IFS
sig_handler_terminate_last_pid
sig_handler_remove_last_jail
sig_handler_remove_lock
trap - ${HANDLER_SIGNALS} ${IGNORED_SIGNALS} EXIT
}
sig_handler_unset_IFS()
{
unset IFS
}
sig_handler_remove_lock()
{
if [ -n "${LOCKS}" ]; then
local project
for project in ${LOCKS}; do
local lock_file
lock_file="${LOCKSDIR}/${project}/lock"
if [ ! -f "${lock_file}" ]; then
continue
fi
debug "Unlocking '${project}' ..."
rm -f -- "${LOCKSDIR}/${project}/lock"
done
fi
}
sig_handler_terminate_last_pid()
{
if [ -n "${LAST_PIDS}" ]; then
local pid
for pid in ${LAST_PIDS}; do
safe_kill ${pid}
done
fi
}
sig_handler_remove_last_jail()
{
if [ -n "${LAST_JAIL}" ]; then
stop_and_destroy_jail "${LAST_JAIL}"
fi
}
trace_exc()
{
local pid
"$@" &
pid=$!
LAST_PIDS="${LAST_PIDS} ${pid}"
wait ${pid}
}
safe_kill()
{
local pid
pid=$1
if [ -z "${pid}" ]; then
echo "safe_kill pid"
exit ${EX_USAGE}
fi
local ppid=`get_ppid ${pid}`
# Process does not exist.
if [ -z "${ppid}" ]; then
return
fi
local expected_pid=$$
if [ ${ppid} -eq ${expected_pid} ]; then
appjail cmd jaildir kill ${pid} > /dev/null 2>&1
appjail cmd jaildir pwait -o -t 30 ${pid} > /dev/null 2>&1
if [ $? -eq 124 ]; then
warn "Timeout has been reached, pid ${pid} is still running!"
fi
fi
}
get_ppid()
{
local pid="$1"
if [ -z "${pid}" ]; then
echo "usage: get_pid pid"
exit ${EX_USAGE}
fi
appjail cmd jaildir ps -p "${pid}" -o ppid | grep -v PPID | awk '{print $1}'
}
usage()
{
cat << EOF
usage: appjail-reproduce -v
appjail-reproduce -b [-dfs] [-A <file>] [-B <file>] [-C <algo>] [-c <file>]
[-j <prefix>] [-L <directory>] [-l <directory>] [-m <mirrors>]
[-p <directory>] [-r <directory>]
[<project>[%<arch>[[,<arch>] ...][:<tag>[[,<tag>] ...]]]]
EOF
}
version()
{
echo "${VERSION}"
}
calc_build_time()
{
local init_time="$1"
local current_seconds=`date +"%s"`
current_seconds=$((current_seconds-$init_time))
printf "%02d:%02d:%02d" $((current_seconds/3600)) $((current_seconds%3600/60)) $((current_seconds%60))
}
stop_and_destroy_jail()
{
debug "Trying to stop and destroy '$1'"
stop_jail "$1" || return 0
debug "Destroying jail $1"
appjail jail destroy -Rf -- "$1" > /dev/null 2>&1
}
stop_jail()
{
if ! appjail jail get -- "$1" name > /dev/null 2>&1; then
debug "Jail '$1' not found."
return 1
fi
if appjail status -q -- "$1" > /dev/null 2>&1; then
debug "Stopping jail $1"
appjail stop -- "$1" > /dev/null 2>&1
fi
return 0
}
checkmatch()
{
local item
IFS="$1"
for item in $3; do
if [ -z "${item}" ]; then
continue
fi
if [ "${item}" = "$2" ]; then
unset IFS
return 0
fi
done
unset IFS
return 1