1 exit ()
2 {
3 if [ -n "$_CLEANUP_TEST" ]; then
4 rm -f "$WORKSPACE_ROOT/.testing" 2>/dev/null
5 rm -f "$WORKSPACE_ROOT/.testing.$$" 2>/dev/null
6 fi
8 if [ -n "$_SYSLOG_TRACE_PID" ]; then
9 case "$DISTRIBUTION" in
10 debian | \
11 ubuntu | \
12 redhat | \
13 centos | \
14 sl | \
15 opensuse | \
16 suse | \
17 freebsd | \
18 netbsd | \
19 openbsd | \
20 sunos )
21 kill -TERM "$_SYSLOG_TRACE_PID" 2>/dev/null &&
22 {
23 wait "$_SYSLOG_TRACE_PID"
24 unset _SYSLOG_TRACE_PID
25 }
26 ;;
27 esac
28 fi
30 builtin exit $*
31 }
33 append_path ()
34 {
35 if [ -d "$1" ]; then
36 if [ -n "$PATH" ]; then
37 echo "$PATH" | $GREP -q -- "$1:" 2>/dev/null && return;
38 echo "$PATH" | $GREP -q -- "$1\$" 2>/dev/null && return;
39 PATH="$PATH:$1"
40 else
41 PATH="$1"
42 fi
43 export PATH
44 fi
45 }
47 prepend_path ()
48 {
49 if [ -d "$1" ]; then
50 if [ -n "$PATH" ]; then
51 echo "$PATH" | $GREP -q -- "$1:" 2>/dev/null && return;
52 echo "$PATH" | $GREP -q -- "$1\$" 2>/dev/null && return;
53 PATH="$1:$PATH"
54 else
55 PATH="$1"
56 fi
57 export PATH
58 fi
59 }
61 append_cflags ()
62 {
63 if [ -n "$1" ]; then
64 if [ -n "$CFLAGS" ]; then
65 echo "$CFLAGS" | $GREP -q -- "$1 " 2>/dev/null && return;
66 echo "$CFLAGS" | $GREP -q -- "$1\$" 2>/dev/null && return;
67 CFLAGS="$CFLAGS $1"
68 else
69 CFLAGS="$1"
70 fi
71 export CFLAGS
72 fi
73 }
75 append_cppflags ()
76 {
77 if [ -n "$1" ]; then
78 if [ -n "$CPPFLAGS" ]; then
79 echo "$CPPFLAGS" | $GREP -q -- "$1 " 2>/dev/null && return;
80 echo "$CPPFLAGS" | $GREP -q -- "$1\$" 2>/dev/null && return;
81 CPPFLAGS="$CPPFLAGS $1"
82 else
83 CPPFLAGS="$1"
84 fi
85 export CPPFLAGS
86 fi
87 }
89 append_ldflags ()
90 {
91 if [ -n "$1" ]; then
92 if [ -n "$LDFLAGS" ]; then
93 echo "$LDFLAGS" | $GREP -q -- "$1 " 2>/dev/null && return;
94 echo "$LDFLAGS" | $GREP -q -- "$1\$" 2>/dev/null && return;
95 LDFLAGS="$LDFLAGS $1"
96 else
97 LDFLAGS="$1"
98 fi
99 export LDFLAGS
100 fi
101 }
103 append_ld_library_path ()
104 {
105 if [ -d "$1" ]; then
106 if [ -n "$LD_LIBRARY_PATH" ]; then
107 echo "$LD_LIBRARY_PATH" | $GREP -q -- "$1:" 2>/dev/null && return;
108 echo "$LD_LIBRARY_PATH" | $GREP -q -- "$1\$" 2>/dev/null && return;
109 LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$1"
110 else
111 LD_LIBRARY_PATH="$1"
112 fi
113 export LD_LIBRARY_PATH
114 fi
115 }
117 find_jenkins_workspace_root ()
118 {
119 if [ -z "$WORKSPACE" -o ! -d "$WORKSPACE" ]; then
120 echo "find_jenkins_workspace_root: Unable to find workspace root since no WORKSPACE has been defined" >&2
121 return 1
122 fi
124 local workspace="$WORKSPACE"
125 local max_iter=20
126 local currdir
128 while [ "$max_iter" -gt 0 ] 2>/dev/null; do
129 # check if the last dir on the path is workspace
130 currdir=`echo "$workspace" | sed 's%.*/%%' 2>/dev/null`
131 if [ "$currdir" = "workspace" ]; then
132 break
133 fi
135 # remove the last dir on the path
136 workspace=`echo "$workspace" | sed 's%/[^/]*$%%' 2>/dev/null`
138 max_iter=$(( max_iter - 1))
139 done
141 if [ -n "$workspace" -a "$max_iter" -gt 0 ] 2>/dev/null; then
142 WORKSPACE_ROOT="$workspace"
143 return 0
144 fi
146 echo "find_jenkins_workspace_root: Failed to find workspace root in WORKSPACE=$WORKSPACE" >&2
147 return 1
148 }
150 find_program ()
151 {
152 if [ -n "$1" ]; then
153 local path=`which "$1" 2>/dev/null`
154 if [ -n "$path" -a -x "$path" ]; then
155 echo "$path"
156 return 0
157 fi
158 fi
160 return 1
161 }
163 find_make ()
164 {
165 local make
166 local program
167 local programs="gmake make"
169 case "$DISTRIBUTION" in
170 freebsd )
171 programs="gmake"
172 ;;
173 esac
175 for program in $programs; do
176 make=`find_program "$program"`
177 if [ -n "$make" ]; then
178 export MAKE="$make"
179 return 0
180 fi
181 done
183 return 1
184 }
186 find_m4 ()
187 {
188 local m4
189 local program
191 for program in gm4 m4; do
192 m4=`find_program "$program"`
193 if [ -n "$m4" ]; then
194 export M4="$m4"
195 return 0
196 fi
197 done
199 return 1
200 }
202 find_md5sum ()
203 {
204 local md5sum
205 local program
207 for program in md5sum gmd5sum md5; do
208 md5sum=`find_program "$program"`
209 if [ -n "$md5sum" ]; then
210 MD5SUM="$md5sum"
211 case "$program" in
212 md5)
213 MD5SUM="$MD5SUM -q"
214 ;;
215 esac
216 export MD5SUM
217 return 0
218 fi
219 done
221 return 1
222 }
224 find_sha1sum ()
225 {
226 local shasum
227 local program
229 for program in sha1sum gsha1sum shasum sha1; do
230 shasum=`find_program "$program"`
231 if [ -n "$shasum" ]; then
232 SHA1SUM="$shasum"
233 case "$program" in
234 shasum)
235 SHA1SUM="$SHA1SUM -a 1"
236 ;;
237 sha1)
238 SHA1SUM="$SHA1SUM -q"
239 ;;
240 esac
241 export SHA1SUM
242 return 0
243 fi
244 done
246 return 1
247 }
249 find_sha256sum ()
250 {
251 local sha256sum
252 local program
254 for program in sha256sum gsha256sum shasum sha256; do
255 sha256sum=`find_program "$program"`
256 if [ -n "$sha256sum" ]; then
257 SHA256SUM="$shasum"
258 case "$program" in
259 shasum)
260 SHA256SUM="$SHA256SUM -a 256"
261 ;;
262 sha256)
263 SHA256SUM="$SHA256SUM -q"
264 ;;
265 esac
266 export SHA256SUM
267 return 0
268 fi
269 done
271 return 1
272 }
274 find_wget ()
275 {
276 local wget
277 local program
279 for program in wget; do
280 wget=`find_program "$program"`
281 if [ -n "$wget" ]; then
282 export WGET="$wget"
283 return 0
284 fi
285 done
287 return 1
288 }
290 find_ccache ()
291 {
292 local ccache
293 local program
294 local path
296 for program in ccache; do
297 ccache=`find_program "$program"`
298 if [ -n "$ccache" ]; then
299 export CCACHE="$ccache"
300 for path in /usr/lib64/ccache /usr/lib/ccache /usr/local/lib64/ccache /usr/local/lib/ccache; do
301 if [ -d "$path" ]; then
302 prepend_path "$path"
303 break
304 fi
305 done
306 return 0
307 fi
308 done
310 return 1
311 }
313 find_cc ()
314 {
315 local cc
316 local program
318 for program in cc gcc; do
319 cc=`find_program "$program"`
320 if [ -n "$cc" ]; then
321 export CC="$cc"
322 return 0
323 fi
324 done
326 return 1
327 }
329 find_cxx ()
330 {
331 local cxx
332 local program
334 for program in c++ g++; do
335 cxx=`find_program "$program"`
336 if [ -n "$cxx" ]; then
337 export CXX="$cxx"
338 return 0
339 fi
340 done
342 return 1
343 }
345 find_tee ()
346 {
347 local tee
348 local program
350 for program in tee; do
351 tee=`find_program "$program"`
352 if [ -n "$tee" ]; then
353 export TEE="$tee"
354 return 0
355 fi
356 done
358 return 1
359 }
361 find_date ()
362 {
363 local date
364 local program
365 local time_now
367 for program in date; do
368 date=`find_program "$program"`
369 if [ -n "$date" ]; then
370 time_now=`$date '+%s' 2>/dev/null`
371 if [ -n "$time_now" -a "$time_now" -gt 0 ] 2>/dev/null; then
372 export DATE="$date"
373 return 0
374 fi
375 fi
376 done
378 return 1
379 }
381 find_tail ()
382 {
383 local tail
384 local tail_follow
385 local program
386 local programs="tail"
388 case "$DISTRIBUTION" in
389 sunos )
390 programs="gtail"
391 ;;
392 esac
394 for program in $programs; do
395 tail=`find_program "$program"`
396 if [ -n "$tail" ]; then
397 break
398 fi
399 done
401 if [ -z "$tail" ]; then
402 return 1
403 fi
405 case "$DISTRIBUTION" in
406 debian | \
407 ubuntu | \
408 redhat | \
409 centos | \
410 sl | \
411 opensuse | \
412 suse | \
413 sunos )
414 tail_follow="$tail --follow=name -n 0"
415 ;;
416 freebsd | \
417 netbsd )
418 tail_follow="$tail -f -F -n 0"
419 ;;
420 openbsd )
421 tail_follow="$tail -f -n 0"
422 ;;
423 esac
425 if [ -z "$tail_follow" ]; then
426 return 1
427 fi
429 export TAIL="$tail"
430 export TAIL_FOLLOW="$tail_follow"
432 return 0
433 }
435 find_grep ()
436 {
437 local grep
438 local program
440 for program in ggrep grep; do
441 grep=`find_program "$program"`
442 if [ -n "$grep" ]; then
443 export GREP="$grep"
444 return
445 fi
446 done
448 return 1
449 }
451 setup_install_root ()
452 {
453 if [ -n "$INSTALL_ROOT" ]; then
454 if [ -d "$INSTALL_ROOT" ]; then
455 return 0
456 else
457 return 1
458 fi
459 fi
461 if [ ! -d "$WORKSPACE_ROOT/root" ]; then
462 if ! mkdir -p "$WORKSPACE_ROOT/root" 2>/dev/null; then
463 echo "setup_install_root: Unable to create INSTALL_ROOT at $WORKSPACE_ROOT/root" >&2
464 return 1
465 fi
466 fi
468 if [ -n "$INSTALL_TAG" ]; then
469 if [ ! -d "$WORKSPACE_ROOT/root/$INSTALL_TAG" ]; then
470 if ! mkdir -p "$WORKSPACE_ROOT/root/$INSTALL_TAG" 2>/dev/null; then
471 echo "setup_install_root: Unable to create INSTALL_ROOT at $WORKSPACE_ROOT/root/$INSTALL_TAG" >&2
472 return 1
473 fi
474 fi
476 INSTALL_ROOT="$WORKSPACE_ROOT/root/$INSTALL_TAG"
478 if [ -d "$INSTALL_ROOT/bin" ]; then
479 prepend_path "$INSTALL_ROOT/bin"
480 fi
481 if [ -d "$INSTALL_ROOT/sbin" ]; then
482 prepend_path "$INSTALL_ROOT/sbin"
483 fi
484 if [ -d "$INSTALL_ROOT/lib64" ]; then
485 append_ldflags "-L$INSTALL_ROOT/lib64"
486 append_ld_library_path "$INSTALL_ROOT/lib64"
487 fi
488 if [ -d "$INSTALL_ROOT/lib" ]; then
489 append_ldflags "-L$INSTALL_ROOT/lib"
490 append_ld_library_path "$INSTALL_ROOT/lib"
491 fi
492 if [ -d "$INSTALL_ROOT/include" ]; then
493 append_cflags "-I$INSTALL_ROOT/include"
494 append_cppflags "-I$INSTALL_ROOT/include"
495 fi
497 return 0
498 fi
500 echo "setup_install_root: INSTALL_TAG or INSTALL_ROOT is not set, need to know in where to build/test" >&2
501 return 1
502 }
504 detect_distribution ()
505 {
506 DISTRIBUTION="UNKNOWN"
508 if [ -f "/etc/debian_version" ]; then
509 if uname -a 2>/dev/null | $GREP -q -i ubuntu 2>/dev/null; then
510 DISTRIBUTION="ubuntu"
511 else
512 DISTRIBUTION="debian"
513 fi
514 elif [ -f "/etc/redhat-release" ]; then
515 if $GREP -q -i centos /etc/redhat-release 2>/dev/null; then
516 DISTRIBUTION="centos"
517 elif $GREP -q -i fedora /etc/redhat-release 2>/dev/null; then
518 DISTRIBUTION="fedora"
519 elif $GREP -q -i "scientific linux" /etc/redhat-release 2>/dev/null; then
520 DISTRIBUTION="sl"
521 else
522 DISTRIBUTION="redhat"
523 fi
524 elif [ -f "/etc/os-release" ]; then
525 if $GREP -q -i opensuse /etc/os-release 2>/dev/null; then
526 DISTRIBUTION="opensuse"
527 fi
528 elif [ -f "/etc/SuSE-release" ]; then
529 if $GREP -q -i "suse linux enterprise" /etc/SuSE-release 2>/dev/null; then
530 DISTRIBUTION="suse"
531 fi
532 elif uname -a 2>/dev/null | $GREP -q -i freebsd 2>/dev/null; then
533 DISTRIBUTION="freebsd"
534 elif uname -a 2>/dev/null | $GREP -q -i sunos 2>/dev/null; then
535 DISTRIBUTION="sunos"
536 elif uname -a 2>/dev/null | $GREP -q -i openbsd 2>/dev/null; then
537 DISTRIBUTION="openbsd"
538 elif uname -a 2>/dev/null | $GREP -q -i netbsd 2>/dev/null; then
539 DISTRIBUTION="netbsd"
540 fi
542 export DISTRIBUTION
543 }
545 init ()
546 {
547 unset _CLEANUP_TEST
548 unset _SYSLOG_TRACE_PID
549 unset PRE_TEST
550 unset POST_TEST
552 find_grep || exit 1
553 detect_distribution
554 find_jenkins_workspace_root || exit 1
555 setup_install_root || exit 1
556 find_make || exit 1
557 find_m4 || exit 1
558 find_wget || exit 1
559 find_md5sum || exit 1
560 find_sha1sum || exit 1
561 find_sha256sum || exit 1
562 find_ccache # ccache needs to be found before cc/cxx
563 find_cc || exit 1
564 find_cxx || exit 1
565 find_tee || exit 1
566 find_date || exit 1
567 find_tail || exit 1
569 return 0
570 }
572 finish ()
573 {
574 local core
576 find "$WORKSPACE" "$INSTALL_ROOT" -name '*core' -type f 2>/dev/null | while read core; do
577 chmod a+r "$core" 2>/dev/null
578 done
579 }
581 check_if_built ()
582 {
583 if [ -z "$1" ]; then
584 echo "usage: check_if_built <name tag>" >&2
585 exit 1
586 fi
588 if [ -z "$SVN_REVISION" ]; then
589 echo "check_if_built: No SVN_REVISION is set, can't check if build is ok!" >&2
590 exit 1
591 fi
593 local name_tag="$1"
595 if [ -f "$WORKSPACE/.$name_tag.build" ]; then
596 local build_svn_rev=`cat "$WORKSPACE/.$name_tag.build" 2>/dev/null`
598 if [ "$SVN_REVISION" = "$build_svn_rev" ]; then
599 if [ -f "$INSTALL_ROOT/.$name_tag.build" ]; then
600 local build_svn_rev=`cat "$INSTALL_ROOT/.$name_tag.build" 2>/dev/null`
602 if [ "$SVN_REVISION" = "$build_svn_rev" ]; then
603 return 0
604 fi
605 fi
606 fi
607 fi
609 return 1
610 }
612 start_build ()
613 {
614 if [ -z "$1" ]; then
615 echo "usage: start_build <name tag>" >&2
616 exit 1
617 fi
619 local name_tag="$1"
621 if [ -f "$INSTALL_ROOT/.$name_tag.ok" ]; then
622 if ! rm "$INSTALL_ROOT/.$name_tag.ok" 2>/dev/null; then
623 echo "start_build: can't remove old ok file $INSTALL_ROOT/.$name_tag.ok !" >&2
624 exit 1
625 fi
626 fi
628 echo "start_build: Starting build for $name_tag on $DISTRIBUTION"
629 return 0
630 }
632 set_build_ok ()
633 {
634 if [ -z "$1" ]; then
635 echo "usage: set_build_ok <name tag>" >&2
636 exit 1
637 fi
639 if [ -z "$SVN_REVISION" ]; then
640 echo "set_build_ok: No SVN_REVISION is set, can't check if build is ok!" >&2
641 exit 1
642 fi
644 local name_tag="$1"
646 if [ -f "$INSTALL_ROOT/.$name_tag.ok" ]; then
647 echo "set_build_ok: Build already mark ok, this should not happend. Did you forget to start_build?" >&2
648 exit 1
649 fi
651 echo "$SVN_REVISION" > "$WORKSPACE/.$name_tag.build"
653 if [ -f "$WORKSPACE/.$name_tag.build" ]; then
654 local build_svn_rev=`cat "$WORKSPACE/.$name_tag.build" 2>/dev/null`
656 if [ "$SVN_REVISION" != "$build_svn_rev" ]; then
657 echo "set_build_ok: Can't tag build file $WORKSPACE/.$name_tag.build !" >&2
658 return 1
659 fi
660 else
661 echo "set_build_ok: Can't tag build file $WORKSPACE/.$name_tag.build !" >&2
662 return 1
663 fi
665 echo "$SVN_REVISION" > "$INSTALL_ROOT/.$name_tag.build"
667 if [ -f "$INSTALL_ROOT/.$name_tag.build" ]; then
668 local build_svn_rev=`cat "$INSTALL_ROOT/.$name_tag.build" 2>/dev/null`
670 if [ "$SVN_REVISION" = "$build_svn_rev" ]; then
671 if ! touch "$INSTALL_ROOT/.$name_tag.ok" 2>/dev/null; then
672 echo "set_build_ok: Can't tag build ok $INSTALL_ROOT/.$name_tag.ok !" >&2
673 return 1
674 fi
675 return 0
676 fi
677 fi
679 echo "set_build_ok: Was not able to tag build ok!" >&2
680 return 1
681 }
683 check_if_tested ()
684 {
685 if [ -z "$1" ]; then
686 echo "usage: check_if_tested <name tag>" >&2
687 exit 1
688 fi
690 if [ -z "$SVN_REVISION" ]; then
691 echo "check_if_tested: No SVN_REVISION is set, can't check if test is ok!" >&2
692 exit 1
693 fi
695 local name_tag="$1"
697 if [ -f "$WORKSPACE/.$name_tag.test" ]; then
698 local build_svn_rev=`cat "$WORKSPACE/.$name_tag.test" 2>/dev/null`
700 if [ "$SVN_REVISION" = "$build_svn_rev" ]; then
701 if [ -f "$INSTALL_ROOT/.$name_tag.test" ]; then
702 local build_svn_rev=`cat "$INSTALL_ROOT/.$name_tag.test" 2>/dev/null`
704 if [ "$SVN_REVISION" = "$build_svn_rev" ]; then
705 return 0
706 fi
707 fi
708 fi
709 fi
711 return 1
712 }
714 start_test ()
715 {
716 if [ -z "$1" ]; then
717 echo "usage: start_test <name tag>" >&2
718 exit 1
719 fi
721 local name_tag="$1"
722 # local time_start=`$DATE '+%s' 2>/dev/null`
723 # local timeout=3600
724 # local time_stop=$(( time_start + timeout ))
725 # local time_now
726 # local build_tag
727 #
728 # echo "$BUILD_TAG $$" > "$WORKSPACE_ROOT/.testing.$$"
729 # build_tag=`cat "$WORKSPACE_ROOT/.testing.$$" 2>/dev/null`
730 # if [ "$build_tag" != "$BUILD_TAG $$" ]; then
731 # echo "start_test: Unable to add test lock!" >&2
732 # rm -f "$WORKSPACE_ROOT/.testing.$$" 2>/dev/null
733 # return 1
734 # fi
735 #
736 # while true; do
737 # if [ ! -f "$WORKSPACE_ROOT/.testing" ]; then
738 # if ln -s "$WORKSPACE_ROOT/.testing.$$" "$WORKSPACE_ROOT/.testing" 2>/dev/null; then
739 # build_tag=`cat "$WORKSPACE_ROOT/.testing" 2>/dev/null`
740 # if [ "$build_tag" = "$BUILD_TAG $$" ]; then
741 # if [ -f "$INSTALL_ROOT/.$name_tag.ok.test" ]; then
742 # if ! rm "$INSTALL_ROOT/.$name_tag.ok.test" 2>/dev/null; then
743 # echo "start_test: can't remove old ok file $INSTALL_ROOT/.$name_tag.ok.test !" >&2
744 # exit 1
745 # fi
746 # fi
747 # export _CLEANUP_TEST=1
748 # return 0
749 # fi
750 # fi
751 # fi
752 #
753 # if [ -z "$time_now" ]; then
754 # echo "start_test: waiting for other tests to finish (timeout $timeout)"
755 # fi
756 #
757 # time_now=`$DATE '+%s' 2>/dev/null`
758 # if [ "$time_now" -ge "$time_stop" ] 2>/dev/null; then
759 # break
760 # fi
761 # if [ -z "$time_now" -o ! "$time_now" -lt "$time_stop" ] 2>/dev/null; then
762 # echo "start_test: Invalid timestamp from date!" >&2
763 # exit 1
764 # fi
765 # sleep 2
766 # done
767 #
768 # echo "start_test: Unable to get test lock, timeout" >&2
769 # rm -f "$WORKSPACE_ROOT/.testing.$$" 2>/dev/null
770 # exit 1
772 if [ -f "$INSTALL_ROOT/.$name_tag.ok.test" ]; then
773 if ! rm "$INSTALL_ROOT/.$name_tag.ok.test" 2>/dev/null; then
774 echo "start_test: can't remove old ok file $INSTALL_ROOT/.$name_tag.ok.test !" >&2
775 exit 1
776 fi
777 fi
779 echo "start_test: Starting test for $name_tag on $DISTRIBUTION"
780 return 0
781 }
783 stop_test ()
784 {
785 # local build_tag
786 #
787 # if [ ! -f "$WORKSPACE_ROOT/.testing" ]; then
788 # echo "stop_test: Called without a test lock file, this should not happen!" >&2
789 # return 1
790 # fi
791 #
792 # build_tag=`cat "$WORKSPACE_ROOT/.testing.$$" 2>/dev/null`
793 # if [ "$build_tag" != "$BUILD_TAG $$" ]; then
794 # echo "stop_test: Our test lock does not exist or is not our own!" >&2
795 # return 1
796 # fi
797 #
798 # build_tag=`cat "$WORKSPACE_ROOT/.testing" 2>/dev/null`
799 # if [ "$build_tag" != "$BUILD_TAG $$" ]; then
800 # echo "stop_test: Content of test lock changed during test!" >&2
801 # rm -f "$WORKSPACE_ROOT/.testing.$$" 2>/dev/null
802 # return 1
803 # fi
804 #
805 # rm -f "$WORKSPACE_ROOT/.testing" 2>/dev/null
806 # rm -f "$WORKSPACE_ROOT/.testing.$$" 2>/dev/null
807 # export _CLEANUP_TEST=""
808 return 0
809 }
811 set_test_ok ()
812 {
813 if [ -z "$1" ]; then
814 echo "usage: set_test_ok <name tag>" >&2
815 exit 1
816 fi
818 if [ -z "$SVN_REVISION" ]; then
819 echo "set_test_ok: No SVN_REVISION is set, can't check if test is ok!" >&2
820 exit 1
821 fi
823 local name_tag="$1"
825 if [ -f "$INSTALL_ROOT/.$name_tag.ok.test" ]; then
826 echo "set_test_ok: Test already mark ok, this should not happend. Did you forget to start_test?" >&2
827 exit 1
828 fi
830 echo "$SVN_REVISION" > "$WORKSPACE/.$name_tag.test"
832 if [ -f "$WORKSPACE/.$name_tag.test" ]; then
833 local test_svn_rev=`cat "$WORKSPACE/.$name_tag.test" 2>/dev/null`
835 if [ "$SVN_REVISION" != "$test_svn_rev" ]; then
836 echo "set_test_ok: Can't tag test file $WORKSPACE/.$name_tag.test !" >&2
837 return 1
838 fi
839 else
840 echo "set_test_ok: Can't tag test file $WORKSPACE/.$name_tag.test !" >&2
841 return 1
842 fi
844 echo "$SVN_REVISION" > "$INSTALL_ROOT/.$name_tag.test"
846 if [ -f "$INSTALL_ROOT/.$name_tag.test" ]; then
847 local test_svn_rev=`cat "$INSTALL_ROOT/.$name_tag.test" 2>/dev/null`
849 if [ "$SVN_REVISION" = "$test_svn_rev" ]; then
850 if ! touch "$INSTALL_ROOT/.$name_tag.ok.test" 2>/dev/null; then
851 echo "set_test_ok: Can't tag test ok $INSTALL_ROOT/.$name_tag.ok.test !" >&2
852 return 1
853 fi
854 return 0
855 fi
856 fi
858 echo "set_test_ok: Was not able to tag test ok!" >&2
859 return 1
860 }
863 require ()
864 {
865 if [ -z "$1" ]; then
866 echo "usage: require <name tag>" >&2
867 exit 1
868 fi
870 local name_tag="$1"
872 if [ ! -f "$INSTALL_ROOT/.$name_tag.ok" ]; then
873 echo "require: Required program $name_tag not found or not built!" >&2
874 exit 1
875 fi
877 if [ ! -f "$INSTALL_ROOT/.$name_tag.build" ]; then
878 echo "require: Required program $name_tag corrupt, can't find build version!" >&2
879 exit 1
880 fi
882 local require_svn_rev=`cat "$INSTALL_ROOT/.$name_tag.build" 2>/dev/null`
884 if [ -z "$require_svn_rev" ]; then
885 echo "require: There is no build version for $name_tag!" >&2
886 exit 1
887 fi
889 export SVN_REVISION="$SVN_REVISION-$name_tag:$require_svn_rev"
890 }
892 check_hash ()
893 {
894 if [ -z "$1" -o -z "$2" -o -z "$3" ]; then
895 echo "usage: check_hash <filename> <type> <hash>" >&2
896 exit 1
897 fi
899 local filename="$1"
900 local type="$2"
901 local hash="$3"
902 local file_hash
904 if [ -f "$filename" ]; then
905 case "$type" in
906 md5)
907 file_hash=`$MD5SUM "$filename" 2>/dev/null | awk '{print $1}'`
908 ;;
909 sha1)
910 file_hash=`$SHA1SUM "$filename" 2>/dev/null | awk '{print $1}'`
911 ;;
912 sha256)
913 file_hash=`$SHA256SUM "$filename" 2>/dev/null | awk '{print $1}'`
914 ;;
915 esac
916 if [ -n "$file_hash" -a "$hash" = "$file_hash" ]; then
917 return 0
918 fi
919 fi
921 return 1
922 }
924 fetch_src ()
925 {
926 if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" ]; then
927 echo "usage: fetch_src <url> <filename> <type> <hash>" >&2
928 exit 1
929 fi
931 local url="$1"
932 local filename="$2"
933 local type="$3"
934 local hash="$4"
935 local path_filename
937 if [ ! -d "$WORKSPACE_ROOT/cache" ]; then
938 if ! mkdir -p "$WORKSPACE_ROOT/cache" 2>/dev/null; then
939 echo "fetch_src: Unable to create cache directory $WORKSPACE_ROOT/cache!" >&2
940 exit 1
941 fi
942 fi
944 path_filename="$WORKSPACE_ROOT/cache/$filename"
946 if [ -f "$path_filename" ]; then
947 if check_hash "$path_filename" "$type" "$hash"; then
948 echo "$path_filename"
949 return 0
950 fi
951 if ! rm "$path_filename" 2>/dev/null; then
952 echo "fetch_src: Unable to remove old invalid file $path_filename!" >&2
953 exit 1
954 fi
955 fi
957 if ! $WGET -O "$path_filename" "$url" 2>/dev/null; then
958 echo "fetch_src: wget failed!" >&2
959 rm -f "$path_filename" 2>/dev/null
960 exit 1
961 fi
963 if [ ! -f "$path_filename" ]; then
964 echo "fetch_src: File at $url not found at $path_filename!" >&2
965 exit 1
966 fi
968 if ! check_hash "$path_filename" "$type" "$hash"; then
969 echo "fetch_src: Checksum does not match for $path_filename!" >&2
970 exit 1
971 fi
973 echo "$path_filename"
974 }
976 log_init ()
977 {
978 if [ -z "$1" ]; then
979 echo "usage: log_this <log name>" >&2
980 exit 1
981 fi
983 local name="$1"
984 local log_stderr="_log.$BUILD_TAG.$name.stderr"
985 local log_stdout="_log.$BUILD_TAG.$name.stdout"
987 touch "$log_stderr" "$log_stdout"
988 }
990 log_this ()
991 {
992 if [ -z "$1" -o -z "$2" ]; then
993 echo "usage: log_this <log name> <command> [options ...]" >&2
994 exit 1
995 fi
997 local name="$1"
998 local log_stderr="_log.$BUILD_TAG.$name.stderr"
999 local log_stdout="_log.$BUILD_TAG.$name.stdout"
1000 shift
1002 touch "$log_stderr" "$log_stdout"
1004 echo "log_this: logging $name for command: $*"
1005 $* 2>>"$log_stderr" >>"$log_stdout"
1006 }
1008 log_this_timeout ()
1009 {
1010 if [ -z "$1" -o -z "$2" ]; then
1011 echo "usage: log_this_timeout <log name> <timeout in seconds> <command ... >" >&2
1012 exit 1
1013 fi
1015 local name="$1"
1016 local log_stderr="_log.$BUILD_TAG.$name.stderr"
1017 local log_stdout="_log.$BUILD_TAG.$name.stdout"
1018 local time_start=`$DATE '+%s' 2>/dev/null`
1019 local time_stop
1020 local time_now
1021 local timeout="$2"
1022 local pid
1023 shift 2
1025 if [ ! "$time_start" -gt 0 ] 2>/dev/null; then
1026 echo "log_this_timeout: Unable to get start time!" >&2
1027 exit 1
1028 fi
1030 if [ ! "$timeout" -gt 0 ] 2>/dev/null; then
1031 echo "log_this_timeout: Wrong timeout value or 0!" >&2
1032 exit 1
1033 fi
1035 if [ "$timeout" -gt 3600 ] 2>/dev/null; then
1036 echo "log_this_timeout: Too long timeout used, can't be over 3600 seconds!" >&2
1037 exit 1
1038 fi
1040 time_stop=$(( time_start + timeout ))
1042 touch "$log_stderr" "$log_stdout"
1044 echo "log_this_timeout: logging $name with timeout $timeout for command: $*"
1045 ( $* 2>>"$log_stderr" >>"$log_stdout" ) &
1046 pid="$!"
1048 if [ -z "$pid" -o "$pid" -le 0 ] 2>/dev/null; then
1049 echo "log_this_timeout: No pid from backgrounded program?" >&2
1050 return 1
1051 fi
1053 while true; do
1054 time_now=`$DATE '+%s' 2>/dev/null`
1055 if [ "$time_now" -ge "$time_stop" ] 2>/dev/null; then
1056 break
1057 fi
1058 if [ -z "$time_now" -o ! "$time_now" -lt "$time_stop" ] 2>/dev/null; then
1059 echo "log_this_timeout: Invalid timestamp from date!" >&2
1060 exit 1
1061 fi
1062 if ! kill -0 "$pid" 2>/dev/null; then
1063 wait "$pid"
1064 return "$?"
1065 fi
1066 sleep 1
1067 done
1069 kill -TERM "$pid"
1070 sleep 1
1071 if kill -0 "$pid" 2>/dev/null; then
1072 kill -KILL "$pid"
1073 fi
1074 return 1
1075 }
1077 log_force_stop ()
1078 {
1079 if [ -z "$1" ]; then
1080 echo "usage: log_force_stop <log name> [grace period in seconds]" >&2
1081 exit 1
1082 fi
1084 local name="$1"
1085 local grace="$2"
1086 local log_stderr_pid="_log_pid.$BUILD_TAG.$name.stderr"
1087 local log_stdout_pid="_log_pid.$BUILD_TAG.$name.stdout"
1088 local stderr_pid
1089 local stdout_pid
1090 local time_start=`$DATE '+%s' 2>/dev/null`
1091 local time_stop=$(( time_start + 5 ))
1092 local time_now
1094 if [ -n "$grace" -a "$grace" -gt 0 ] 2>/dev/null; then
1095 time_stop=$(( time_start + grace ))
1096 fi
1098 while true; do
1099 if [ ! -f "$log_stderr_pid" -a ! -f "$log_stdout_pid" ]; then
1100 break
1101 fi
1102 time_now=`$DATE '+%s' 2>/dev/null`
1103 if [ "$time_now" -ge "$time_stop" ] 2>/dev/null; then
1104 break;
1105 fi
1106 if [ -z "$time_now" -o ! "$time_now" -lt "$time_stop" ] 2>/dev/null; then
1107 echo "log_force_stop: Invalid timestamp from date!" >&2
1108 return 1
1109 fi
1110 sleep 1
1111 done
1113 if [ -f "$log_stderr_pid" ]; then
1114 stderr_pid=`cat "$log_stderr_pid"`
1115 if [ "$stderr_pid" -gt 0 ] 2>/dev/null; then
1116 kill -TERM "$stderr_pid" 2>/dev/null
1117 fi
1118 rm -f "$log_stderr_pid"
1119 fi
1121 if [ -f "$log_stdout_pid" ]; then
1122 stdout_pid=`cat "$log_stdout_pid"`
1123 if [ "$stdout_pid" -gt 0 ] 2>/dev/null; then
1124 kill -TERM "$stdout_pid" 2>/dev/null
1125 fi
1126 rm -f "$log_stdout_pid"
1127 fi
1129 return 0
1130 }
1132 log_grep ()
1133 {
1134 local output=""
1135 OPTIND=1
1136 while getopts ":o" opt; do
1137 case "$opt" in
1138 o)
1139 output=1
1140 ;;
1141 \?)
1142 echo "log_grep: Invalid option: -$OPTARG" >&2
1143 exit 1
1144 ;;
1145 esac
1146 done
1147 shift $((OPTIND-1))
1149 if [ -z "$1" -o -z "$2" -o -z "$3" ]; then
1150 echo "usage: log_grep [-o] <log name> <stdout|stderr|both> <grep string ...>" >&2
1151 exit 1
1152 fi
1154 local name="$1"
1155 local log_stderr="_log.$BUILD_TAG.$name.stderr"
1156 local log_stdout="_log.$BUILD_TAG.$name.stdout"
1157 local type="$2"
1158 local grep_string="$3"
1159 local log_files
1161 case "$type" in
1162 stdout)
1163 if [ ! -f "$log_stdout" ]; then
1164 return 1
1165 fi
1166 log_files="$log_stdout"
1167 ;;
1168 stderr)
1169 if [ ! -f "$log_stderr" ]; then
1170 return 1
1171 fi
1172 log_files="$log_stderr"
1173 ;;
1174 both)
1175 if [ ! -f "$log_stdout" -a ! -f "$log_stderr" ]; then
1176 return 1
1177 fi
1178 log_files="$log_stdout $log_stderr"
1179 ;;
1180 esac
1182 if [ -z "$log_files" ]; then
1183 echo "log_grep: Wrong type of log file specified, should be stdout, stderr or both!" >&2
1184 exit 1
1185 fi
1187 if [ -n "$output" ]; then
1188 $GREP -- "$grep_string" $log_files 2>/dev/null
1189 else
1190 echo "log_grep: greping in $name for: $grep_string"
1191 $GREP -q -- "$grep_string" $log_files 2>/dev/null
1192 fi
1193 }
1195 log_grep_count ()
1196 {
1197 if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$3" ]; then
1198 echo "usage: log_grep_count <log name> <stdout|stderr|both> <count> <grep string ...>" >&2
1199 exit 1
1200 fi
1202 local name="$1"
1203 local log_stderr="_log.$BUILD_TAG.$name.stderr"
1204 local log_stdout="_log.$BUILD_TAG.$name.stdout"
1205 local type="$2"
1206 local grep_string="$3"
1207 local count="$4"
1208 local log_files
1209 local count_found
1211 case "$type" in
1212 stdout)
1213 if [ ! -f "$log_stdout" ]; then
1214 return 1
1215 fi
1216 log_files="$log_stdout"
1217 ;;
1218 stderr)
1219 if [ ! -f "$log_stderr" ]; then
1220 return 1
1221 fi
1222 log_files="$log_stderr"
1223 ;;
1224 both)
1225 if [ ! -f "$log_stdout" -a ! -f "$log_stderr" ]; then
1226 return 1
1227 fi
1228 log_files="$log_stdout $log_stderr"
1229 ;;
1230 esac
1232 if [ -z "$log_files" ]; then
1233 echo "log_grep_count: Wrong type of log file specified, should be stdout, stderr or both!" >&2
1234 exit 1
1235 fi
1237 echo "log_grep_count: greping in $name, should find $count of: $grep_string"
1238 count_found=`$GREP -- "$grep_string" $log_files 2>/dev/null | wc -l 2>/dev/null`
1240 if [ "$count_found" -eq "$count" ] 2>/dev/null; then
1241 return 0
1242 fi
1244 return 1
1245 }
1247 log_waitfor ()
1248 {
1249 if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$3" ]; then
1250 echo "usage: log_waitfor <log name> <stdout|stderr|both> <timeout in seconds> <grep string ...>" >&2
1251 exit 1
1252 fi
1254 local name="$1"
1255 local log_stderr="_log.$BUILD_TAG.$name.stderr"
1256 local log_stdout="_log.$BUILD_TAG.$name.stdout"
1257 local type="$2"
1258 local timeout="$3"
1259 local grep_string="$4"
1260 local time_start=`$DATE '+%s' 2>/dev/null`
1261 local time_stop
1262 local time_now
1263 local log_files
1265 case "$type" in
1266 stdout)
1267 if [ ! -f "$log_stdout" ]; then
1268 return 1
1269 fi
1270 log_files="$log_stdout"
1271 ;;
1272 stderr)
1273 if [ ! -f "$log_stderr" ]; then
1274 return 1
1275 fi
1276 log_files="$log_stderr"
1277 ;;
1278 both)
1279 if [ ! -f "$log_stdout" -a ! -f "$log_stderr" ]; then
1280 return 1
1281 fi
1282 log_files="$log_stdout $log_stderr"
1283 ;;
1284 esac
1286 if [ -z "$log_files" ]; then
1287 echo "log_waitfor: Wrong type of log file specified, should be stdout, stderr or both!" >&2
1288 exit 1
1289 fi
1291 if [ ! "$time_start" -gt 0 ] 2>/dev/null; then
1292 echo "log_waitfor: Unable to get start time!" >&2
1293 exit 1
1294 fi
1296 if [ ! "$timeout" -gt 0 ] 2>/dev/null; then
1297 echo "log_waitfor: Wrong timeout value or 0!" >&2
1298 exit 1
1299 fi
1301 if [ "$timeout" -gt 3600 ] 2>/dev/null; then
1302 echo "log_waitfor: Too long timeout used, can't be over 3600 seconds!" >&2
1303 exit 1
1304 fi
1306 time_stop=$(( time_start + timeout ))
1308 echo "log_waitfor: waiting for log $name to contain (timeout $timeout): $grep_string"
1309 while true; do
1310 if $GREP -q -- "$grep_string" $log_files 2>/dev/null; then
1311 return 0
1312 fi
1313 time_now=`$DATE '+%s' 2>/dev/null`
1314 if [ "$time_now" -ge "$time_stop" ] 2>/dev/null; then
1315 break
1316 fi
1317 if [ -z "$time_now" -o ! "$time_now" -lt "$time_stop" ] 2>/dev/null; then
1318 echo "log_waitfor: Invalid timestamp from date!" >&2
1319 exit 1
1320 fi
1321 sleep 2
1322 done
1323 return 1
1324 }
1326 log_cleanup ()
1327 {
1328 local pid_file
1329 local pid
1331 ls _log_pid* 2>/dev/null | while read pid_file; do
1332 pid=`cat $pid_file 2>/dev/null`
1334 if [ -n "$pid" -a "$pid" -gt 0 ] 2>/dev/null; then
1335 kill -TERM "$pid" 2>/dev/null
1336 rm -f "$pid_file" 2>/dev/null
1337 fi
1338 done
1340 rm -f "_log.$BUILD_TAG"* 2>/dev/null
1341 }
1343 log_remove ()
1344 {
1345 if [ -z "$1" ]; then
1346 echo "usage: log_remove <log name>" >&2
1347 exit 1
1348 fi
1350 local name="$1"
1351 local log_stderr="_log.$BUILD_TAG.$name.stderr"
1352 local log_stdout="_log.$BUILD_TAG.$name.stdout"
1354 rm -f "$log_stderr" "$log_stdout" 2>/dev/null
1355 }
1357 log_save_try ()
1358 {
1359 if [ -z "$1" ]; then
1360 echo "usage: log_save_try <try>" >&2
1361 exit 1
1362 fi
1364 local try="$1"
1365 local log_file
1367 ls "_log.$BUILD_TAG"* 2>/dev/null | while read log_file; do
1368 if ! mv "$log_file" "$log_file-try-$try" 2>/dev/null; then
1369 echo "log_save_try: Unable to save log file $log_file to $log_file-try-$try"
1370 return 1
1371 fi
1372 done
1373 return 0
1374 }
1376 run_tests ()
1377 {
1378 if [ -z "$1" ]; then
1379 echo "usage: run_tests <tests directory>" >&2
1380 exit 1
1381 fi
1383 local test_dir="$1"
1384 local entry
1385 local test=()
1386 local test_num=0
1387 local test_iter=0
1388 local test_path
1389 local test_status
1390 local test_failed=0
1391 local pwd=`pwd`
1392 local pwd2
1393 local retry
1395 if [ -n "$PRE_TEST" ]; then
1396 if ! declare -F "$PRE_TEST" >/dev/null 2>/dev/null; then
1397 unset PRE_TEST
1398 fi
1399 fi
1401 if [ -n "$POST_TEST" ]; then
1402 if ! declare -F "$POST_TEST" >/dev/null 2>/dev/null; then
1403 unset POST_TEST
1404 fi
1405 fi
1407 if [ -n "$RETRY_TEST" ]; then
1408 if [ ! "$RETRY_TEST" -gt 0 ] 2>/dev/null; then
1409 RETRY_TEST=0
1410 fi
1411 else
1412 RETRY_TEST=0
1413 fi
1415 if [ -n "$RETRY_SLEEP" ]; then
1416 if [ ! "$RETRY_SLEEP" -ge 0 ] 2>/dev/null; then
1417 RETRY_SLEEP=10
1418 fi
1419 else
1420 RETRY_SLEEP=10
1421 fi
1423 if ! cd "$test_dir" 2>/dev/null; then
1424 echo "run_tests: unable to change to test directory $test_dir!" >&2
1425 return 1
1426 fi
1428 ls -1 2>/dev/null | $GREP '^[0-9]*' | $GREP -v '\.off$' 2>/dev/null >"_tests.$BUILD_TAG"
1429 while read entry; do
1430 if [ -d "$entry" -a -f "$entry/test.sh" -a ! -f "$entry/off" ]; then
1431 test[test_num]="$entry"
1432 test_num=$(( test_num + 1 ))
1433 fi
1434 done <"_tests.$BUILD_TAG"
1435 rm -f "_tests.$BUILD_TAG" 2>/dev/null
1437 if [ "$test_num" -le 0 ] 2>/dev/null; then
1438 echo "run_tests: no tests found!" >&2
1439 cd "$pwd"
1440 return 1
1441 fi
1443 echo "Running tests ..."
1444 while [ "$test_iter" -lt "$test_num" ] 2>/dev/null; do
1445 retry=0
1446 test_path="${test[test_iter]}"
1447 test_iter=$(( test_iter + 1 ))
1448 echo "##### `date` $test_iter/$test_num $test_path ... "
1449 pwd2=`pwd`
1450 cd "$test_path" 2>/dev/null &&
1451 while [ "$retry" -le "$RETRY_TEST" ] 2>/dev/null; do
1452 if [ "$retry" -gt 0 ] 2>/dev/null; then
1453 syslog_stop &&
1454 log_save_try "$retry" &&
1455 syslog_save_try "$retry" ||
1456 {
1457 echo "##### `date` $test_iter/$test_num $test_path ... Unable to retry"
1458 test_status=1
1459 break
1460 }
1461 echo "##### `date` $test_iter/$test_num $test_path ... RETRY $retry in $RETRY_SLEEP seconds"
1462 sleep "$RETRY_SLEEP"
1463 fi
1464 syslog_trace &&
1465 if [ -n "$PRE_TEST" ]; then
1466 $PRE_TEST "$test_path"
1467 fi &&
1468 ( source ./test.sh )
1469 test_status="$?"
1470 if [ "$test_status" -eq 0 ] 2>/dev/null; then
1471 break
1472 fi
1473 retry=$(( retry + 1 ))
1474 done
1475 syslog_stop
1476 if [ -n "$POST_TEST" ]; then
1477 $POST_TEST "$test_path" "$test_status"
1478 fi
1479 if [ "$test_status" -eq 0 ] 2>/dev/null; then
1480 echo "##### `date` $test_iter/$test_num $test_path ... OK"
1481 log_cleanup
1482 syslog_cleanup
1483 else
1484 test_failed=$(( test_failed + 1 ))
1485 echo "##### `date` $test_iter/$test_num $test_path ... FAILED!"
1486 fi
1488 if ! cd "$pwd2" 2>/dev/null; then
1489 echo "run_tests: unable to change back to test directory $pwd2 after running a test!" >&2
1490 test_failed=1
1491 break
1492 fi
1493 done
1495 if ! cd "$pwd" 2>/dev/null; then
1496 echo "run_tests: unable to change back to directory $pwd after running tests!" >&2
1497 return 1
1498 fi
1500 if [ "$test_failed" -gt 0 ] 2>/dev/null; then
1501 return 1
1502 fi
1503 }
1505 run_test ()
1506 {
1507 if [ -z "$1" -o -z "$2" ]; then
1508 echo "usage: run_test <test name> <test directory>" >&2
1509 exit 1
1510 fi
1512 local test_name="$1"
1513 local test_dir="$2"
1514 local test_status
1515 local pwd=`pwd`
1517 if [ -n "$PRE_TEST" ]; then
1518 if ! declare -F "$PRE_TEST" >/dev/null 2>/dev/null; then
1519 unset PRE_TEST
1520 fi
1521 fi
1523 if [ -n "$POST_TEST" ]; then
1524 if ! declare -F "$POST_TEST" >/dev/null 2>/dev/null; then
1525 unset POST_TEST
1526 fi
1527 fi
1529 if [ ! -f "$test_dir/test.sh" ]; then
1530 echo "run_test: no test.sh in test $test_name ($test_dir)!" >&2
1531 return 1
1532 fi
1534 if ! cd "$test_dir" 2>/dev/null; then
1535 echo "run_test: unable to change to test $test_name directory $test_dir!" >&2
1536 return 1
1537 fi
1539 echo "##### Running test $test_name ..."
1540 if [ -n "$PRE_TEST" ]; then
1541 $PRE_TEST "$test_name"
1542 fi &&
1543 syslog_trace &&
1544 ( source ./test.sh )
1545 test_status="$?"
1546 syslog_stop
1547 if [ -n "$POST_TEST" ]; then
1548 $POST_TEST "$test_name" "$test_status"
1549 fi
1550 if [ "$test_status" -eq 0 ] 2>/dev/null; then
1551 echo "##### Test $test_name ... OK"
1552 log_cleanup
1553 syslog_cleanup
1554 else
1555 echo "##### Test $test_name ... FAILED!"
1556 fi
1558 if ! cd "$pwd" 2>/dev/null; then
1559 echo "run_test: unable to change back to directory $pwd after running test $test_name!" >&2
1560 return 1
1561 fi
1563 if [ "$test_status" -ne 0 ] 2>/dev/null; then
1564 return 1
1565 fi
1566 }
1568 syslog_trace ()
1569 {
1570 if [ -n "$_SYSLOG_TRACE_PID" ]; then
1571 echo "syslog_trace: Syslog trace already running (pid $_SYSLOG_TRACE_PID)!" >&2
1572 exit 1
1573 fi
1575 local syslog_file
1577 case "$DISTRIBUTION" in
1578 debian | \
1579 ubuntu )
1580 syslog_file="/var/log/syslog"
1581 ;;
1582 redhat | \
1583 centos | \
1584 sl | \
1585 opensuse | \
1586 suse | \
1587 freebsd | \
1588 netbsd | \
1589 openbsd )
1590 syslog_file="/var/log/messages"
1591 ;;
1592 sunos )
1593 syslog_file="/var/adm/messages"
1594 ;;
1595 esac
1597 if [ -z "$syslog_file" ]; then
1598 echo "syslog_trace: Unable to start trace of syslog: no syslog file set" >&2
1599 exit 1
1600 fi
1602 if [ ! -r "$syslog_file" ]; then
1603 echo "syslog_trace: Unable to start trace of syslog: no access to $syslog_file" >&2
1604 exit 1
1605 fi
1607 $TAIL_FOLLOW "$syslog_file" >"_syslog.$BUILD_TAG" 2>/dev/null &
1608 _SYSLOG_TRACE_PID="$!"
1610 if [ -z "$_SYSLOG_TRACE_PID" -o ! "$_SYSLOG_TRACE_PID" -gt 0 ] 2>/dev/null; then
1611 echo "syslog_trace: Unable to start trace of syslog!" >&2
1612 exit 1
1613 fi
1615 if ! kill -0 "$_SYSLOG_TRACE_PID" 2>/dev/null >/dev/null; then
1616 wait "$_SYSLOG_TRACE_PID"
1617 echo "syslog_trace: Unable to start trace of syslog: exited with status $?"
1618 exit 1
1619 fi
1621 echo "syslog_trace: trace started (pid $_SYSLOG_TRACE_PID)"
1622 }
1624 syslog_stop ()
1625 {
1626 if [ -z "$_SYSLOG_TRACE_PID" ]; then
1627 echo "syslog_stop: Syslog trace not started!" >&2
1628 exit 1
1629 fi
1631 if kill -TERM "$_SYSLOG_TRACE_PID" 2>/dev/null; then
1632 wait "$_SYSLOG_TRACE_PID"
1633 unset _SYSLOG_TRACE_PID
1634 fi
1636 if [ -n "$_SYSLOG_TRACE_PID" ]; then
1637 echo "syslog_stop: Unable to stop trace of syslog!" >&2
1638 exit 1
1639 fi
1641 echo "syslog_stop: trace stopped"
1642 }
1644 syslog_waitfor ()
1645 {
1646 if [ -z "$1" -o -z "$2" ]; then
1647 echo "usage: syslog_waitfor <timeout in seconds> <grep string ...>" >&2
1648 exit 1
1649 fi
1651 local time_start=`$DATE '+%s' 2>/dev/null`
1652 local time_stop
1653 local time_now
1654 local timeout="$1"
1655 local grep_string="$2"
1657 if [ ! -f "_syslog.$BUILD_TAG" ]; then
1658 echo "syslog_waitfor: No syslog file to grep from!" >&2
1659 exit 1
1660 fi
1662 if [ ! "$time_start" -gt 0 ] 2>/dev/null; then
1663 echo "syslog_waitfor: Unable to get start time!" >&2
1664 exit 1
1665 fi
1667 if [ ! "$timeout" -gt 0 ] 2>/dev/null; then
1668 echo "syslog_waitfor: Wrong timeout value or 0!" >&2
1669 exit 1
1670 fi
1672 if [ "$timeout" -gt 3600 ] 2>/dev/null; then
1673 echo "syslog_waitfor: Too long timeout used, can't be over 3600 seconds!" >&2
1674 exit 1
1675 fi
1677 time_stop=$(( time_start + timeout ))
1679 echo "syslog_waitfor: waiting for syslog to contain (timeout $timeout): $grep_string"
1680 while true; do
1681 if $GREP -q -- "$grep_string" "_syslog.$BUILD_TAG" 2>/dev/null; then
1682 return 0
1683 fi
1684 time_now=`$DATE '+%s' 2>/dev/null`
1685 if [ -z "$_SYSLOG_TRACE_PID" -o "$time_now" -ge "$time_stop" ] 2>/dev/null; then
1686 break
1687 fi
1688 if [ -z "$time_now" -o ! "$time_now" -lt "$time_stop" ] 2>/dev/null; then
1689 echo "syslog_waitfor: Invalid timestamp from date!" >&2
1690 exit 1
1691 fi
1692 sleep 2
1693 done
1695 return 1
1696 }
1698 syslog_waitfor_count ()
1699 {
1700 if [ -z "$1" -o -z "$2" -o -z "$3" ]; then
1701 echo "usage: syslog_waitfor_count <timeout in seconds> <count> <grep string ...>" >&2
1702 exit 1
1703 fi
1705 local time_start=`$DATE '+%s' 2>/dev/null`
1706 local time_stop
1707 local time_now
1708 local timeout="$1"
1709 local count="$2"
1710 local grep_string="$3"
1711 local count_found
1713 if [ ! -f "_syslog.$BUILD_TAG" ]; then
1714 echo "syslog_waitfor_count: No syslog file to grep from!" >&2
1715 exit 1
1716 fi
1718 if [ ! "$time_start" -gt 0 ] 2>/dev/null; then
1719 echo "syslog_waitfor_count: Unable to get start time!" >&2
1720 exit 1
1721 fi
1723 if [ ! "$timeout" -gt 0 ] 2>/dev/null; then
1724 echo "syslog_waitfor_count: Wrong timeout value or 0!" >&2
1725 exit 1
1726 fi
1728 if [ "$timeout" -gt 3600 ] 2>/dev/null; then
1729 echo "syslog_waitfor_count: Too long timeout used, can't be over 3600 seconds!" >&2
1730 exit 1
1731 fi
1733 time_stop=$(( time_start + timeout ))
1735 echo "syslog_waitfor_count: waiting for syslog to contain $count counts of (timeout $timeout): $grep_string"
1736 while true; do
1737 count_found=`$GREP -- "$grep_string" "_syslog.$BUILD_TAG" 2>/dev/null | wc -l 2>/dev/null`
1738 if [ "$count_found" -eq "$count" ] 2>/dev/null; then
1739 return 0
1740 fi
1742 time_now=`$DATE '+%s' 2>/dev/null`
1743 if [ -z "$_SYSLOG_TRACE_PID" -o "$time_now" -ge "$time_stop" ] 2>/dev/null; then
1744 break
1745 fi
1746 if [ -z "$time_now" -o ! "$time_now" -lt "$time_stop" ] 2>/dev/null; then
1747 echo "syslog_waitfor_count: Invalid timestamp from date!" >&2
1748 exit 1
1749 fi
1750 sleep 2
1751 done
1753 return 1
1754 }
1756 syslog_grep ()
1757 {
1758 if [ -z "$1" ]; then
1759 echo "usage: syslog_grep <grep string ...>" >&2
1760 exit 1
1761 fi
1763 local grep_string="$1"
1765 if [ ! -f "_syslog.$BUILD_TAG" ]; then
1766 echo "syslog_grep: No syslog file to grep from!" >&2
1767 exit 1
1768 fi
1770 echo "syslog_grep: greping syslog for: $grep_string"
1771 $GREP -q -- "$grep_string" "_syslog.$BUILD_TAG" 2>/dev/null
1772 }
1774 syslog_grep_count ()
1775 {
1776 if [ -z "$1" -o -z "$2" ]; then
1777 echo "usage: syslog_grep_count <count> <grep string ...>" >&2
1778 exit 1
1779 fi
1781 local count="$1"
1782 local grep_string="$2"
1783 local count_found
1785 if [ ! -f "_syslog.$BUILD_TAG" ]; then
1786 echo "syslog_grep_count: No syslog file to grep from!" >&2
1787 exit 1
1788 fi
1790 echo "syslog_grep_count: greping syslog, should find $count of: $grep_string"
1791 count_found=`$GREP -- "$grep_string" "_syslog.$BUILD_TAG" 2>/dev/null | wc -l 2>/dev/null`
1793 if [ "$count_found" -eq "$count" ] 2>/dev/null; then
1794 return 0
1795 fi
1797 return 1
1798 }
1800 syslog_cleanup ()
1801 {
1802 rm -f "_syslog.$BUILD_TAG"* 2>/dev/null
1803 }
1805 syslog_save_try ()
1806 {
1807 if [ -z "$1" ]; then
1808 echo "usage: syslog_save_try <try>" >&2
1809 exit 1
1810 fi
1812 local try="$1"
1814 if ! mv "_syslog.$BUILD_TAG" "_syslog.$BUILD_TAG-try-$try" 2>/dev/null; then
1815 echo "syslog_save_try: Unable to save syslog file _syslog.$BUILD_TAG to _syslog.$BUILD_TAG-try-$try"
1816 return 1
1817 fi
1818 return 0
1819 }
1821 apply_parameter ()
1822 {
1823 if [ -z "$1" -o -z "$2" -o -z "$3" ]; then
1824 echo "usage: apply_parameter <parameter tag> <parameter value> <files ... >" >&2
1825 echo " ex: apply_parameter \"INSTALL_ROOT\" \"\$INSTALL_ROOT\" conf.xml" >&2
1826 exit 1
1827 fi
1829 local parameter_tag="$1"
1830 local parameter_value="$2"
1831 shift 2
1832 local files="$*"
1833 local file
1835 if echo "$parameter_tag" | grep -q "@" 2>/dev/null; then
1836 echo "apply_parameter: parameter tag contains '@', it must not" >&2
1837 return 1
1838 fi
1840 for file in $files; do
1841 if [ ! -f "$file" ]; then
1842 echo "apply_parameter: File $file not found" >&2
1843 return 1
1844 fi
1845 if [ -f "$file.$$" ]; then
1846 echo "apply_parameter: Temporary file $file.$$ exists but it should not" >&2
1847 return 1
1848 fi
1849 done
1851 for file in $files; do
1852 sed 's%@'"$parameter_tag"'@%'"$parameter_value"'%g' "$file" > "$file.$$" 2>/dev/null &&
1853 mv "$file.$$" "$file" 2>/dev/null ||
1854 {
1855 echo "apply_parameter: Unable to apply parameter $parameter_tag value $parameter_value to file $file" >&2
1856 return 1
1857 }
1858 done
1860 return 0
1861 }
1863 sed_inplace ()
1864 {
1865 if [ -z "$1" -o -z "$2" ]; then
1866 echo "usage: sed_inplace <expression> <files ... >" >&2
1867 exit 1
1868 fi
1870 local expression="$1"
1871 shift 1
1872 local files="$*"
1873 local file
1875 for file in $files; do
1876 if [ ! -f "$file" ]; then
1877 echo "sed_inplace: File $file not found" >&2
1878 return 1
1879 fi
1880 if [ -f "$file.$$" ]; then
1881 echo "sed_inplace: Temporary file $file.$$ exists but it should not" >&2
1882 return 1
1883 fi
1884 done
1886 for file in $files; do
1887 sed "$expression" "$file" > "$file.$$" 2>/dev/null &&
1888 mv "$file.$$" "$file" 2>/dev/null ||
1889 {
1890 echo "sed_inplace: Unable to sed inplace file $file" >&2
1891 return 1
1892 }
1893 done
1895 return 0
1896 }
1898 try_run ()
1899 {
1900 if [ -z "$1" -o -z "$2" ]; then
1901 echo "usage: try_run <timeout in seconds> <command ... >" >&2
1902 exit 1
1903 fi
1905 local time_start=`$DATE '+%s' 2>/dev/null`
1906 local time_stop
1907 local time_now
1908 local timeout="$1"
1909 local pid
1910 shift
1912 if [ ! "$time_start" -gt 0 ] 2>/dev/null; then
1913 echo "try_run: Unable to get start time!" >&2
1914 exit 1
1915 fi
1917 if [ ! "$timeout" -gt 0 ] 2>/dev/null; then
1918 echo "try_run: Wrong timeout value or 0!" >&2
1919 exit 1
1920 fi
1922 if [ "$timeout" -gt 3600 ] 2>/dev/null; then
1923 echo "try_run: Too long timeout used, can't be over 3600 seconds!" >&2
1924 exit 1
1925 fi
1927 time_stop=$(( time_start + timeout ))
1929 ( $* ) &
1930 pid="$!"
1932 if [ -z "$pid" -o "$pid" -le 0 ] 2>/dev/null; then
1933 echo "try_run: No pid from backgrounded program?" >&2
1934 return 1
1935 fi
1937 while true; do
1938 time_now=`$DATE '+%s' 2>/dev/null`
1939 if [ "$time_now" -ge "$time_stop" ] 2>/dev/null; then
1940 break
1941 fi
1942 if [ -z "$time_now" -o ! "$time_now" -lt "$time_stop" ] 2>/dev/null; then
1943 echo "try_run: Invalid timestamp from date!" >&2
1944 exit 1
1945 fi
1946 if ! kill -0 "$pid" 2>/dev/null; then
1947 wait "$pid"
1948 return "$?"
1949 fi
1950 sleep 1
1951 done
1953 kill -TERM "$pid"
1954 sleep 1
1955 if kill -0 "$pid" 2>/dev/null; then
1956 kill -KILL "$pid"
1957 fi
1958 return 1
1959 }
1961 waitfor_this ()
1962 {
1963 if [ -z "$1" -o -z "$2" -o -z "$3" ]; then
1964 echo "usage: waitfor_this <file to grep> <timeout in seconds> <grep string ...>" >&2
1965 exit 1
1966 fi
1968 local time_start=`$DATE '+%s' 2>/dev/null`
1969 local time_stop
1970 local time_now
1971 local file="$1"
1972 local timeout="$2"
1973 local grep_string="$3"
1975 if [ ! -f "$file" ]; then
1976 echo "waitfor_this: No file to grep from!" >&2
1977 exit 1
1978 fi
1980 if [ ! "$time_start" -gt 0 ] 2>/dev/null; then
1981 echo "waitfor_this: Unable to get start time!" >&2
1982 exit 1
1983 fi
1985 if [ ! "$timeout" -gt 0 ] 2>/dev/null; then
1986 echo "waitfor_this: Wrong timeout value or 0!" >&2
1987 exit 1
1988 fi
1990 if [ "$timeout" -gt 3600 ] 2>/dev/null; then
1991 echo "waitfor_this: Too long timeout used, can't be over 3600 seconds!" >&2
1992 exit 1
1993 fi
1995 time_stop=$(( time_start + timeout ))
1997 echo "waitfor_this: waiting for $file to contain (timeout $timeout): $grep_string"
1998 while true; do
1999 if $GREP -q -- "$grep_string" "$file" 2>/dev/null; then
2000 return 0
2001 fi
2002 time_now=`$DATE '+%s' 2>/dev/null`
2003 if [ "$time_now" -ge "$time_stop" ] 2>/dev/null; then
2004 break
2005 fi
2006 if [ -z "$time_now" -o ! "$time_now" -lt "$time_stop" ] 2>/dev/null; then
2007 echo "waitfor_this: Invalid timestamp from date!" >&2
2008 exit 1
2009 fi
2010 sleep 2
2011 done
2013 return 1
2014 }
2016 waitfor_count_this ()
2017 {
2018 if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" ]; then
2019 echo "usage: waitfor_count_this <file to grep> <timeout in seconds> <count> <grep string ...>" >&2
2020 exit 1
2021 fi
2023 local time_start=`$DATE '+%s' 2>/dev/null`
2024 local time_stop
2025 local time_now
2026 local file="$1"
2027 local timeout="$2"
2028 local count="$3"
2029 local grep_string="$4"
2030 local count_found
2032 if [ ! -f "$file" ]; then
2033 echo "waitfor_count_this: No file to grep from!" >&2
2034 exit 1
2035 fi
2037 if [ ! "$time_start" -gt 0 ] 2>/dev/null; then
2038 echo "waitfor_count_this: Unable to get start time!" >&2
2039 exit 1
2040 fi
2042 if [ ! "$timeout" -gt 0 ] 2>/dev/null; then
2043 echo "waitfor_count_this: Wrong timeout value or 0!" >&2
2044 exit 1
2045 fi
2047 if [ "$timeout" -gt 3600 ] 2>/dev/null; then
2048 echo "waitfor_count_this: Too long timeout used, can't be over 3600 seconds!" >&2
2049 exit 1
2050 fi
2052 time_stop=$(( time_start + timeout ))
2054 echo "waitfor_count_this: waiting for $file to contain $count counts of (timeout $timeout): $grep_string"
2055 while true; do
2056 count_found=`$GREP -- "$grep_string" "$file" 2>/dev/null | wc -l 2>/dev/null`
2057 if [ "$count_found" -eq "$count" ] 2>/dev/null; then
2058 return 0
2059 fi
2061 time_now=`$DATE '+%s' 2>/dev/null`
2062 if [ "$time_now" -ge "$time_stop" ] 2>/dev/null; then
2063 break
2064 fi
2065 if [ -z "$time_now" -o ! "$time_now" -lt "$time_stop" ] 2>/dev/null; then
2066 echo "waitfor_count_this: Invalid timestamp from date!" >&2
2067 exit 1
2068 fi
2069 sleep 2
2070 done
2072 return 1
2073 }
2075 grep_this ()
2076 {
2077 if [ -z "$1" -o -z "$2" ]; then
2078 echo "usage: grep_this <file to grep> <grep string ...>" >&2
2079 exit 1
2080 fi
2082 local file="$1"
2083 local grep_string="$2"
2085 if [ ! -f "$file" ]; then
2086 echo "grep_this: No file to grep from!" >&2
2087 exit 1
2088 fi
2090 echo "grep_this: greping in $file for: $grep_string"
2091 $GREP -q -- "$grep_string" "$file" 2>/dev/null
2092 }
2094 grep_count_this ()
2095 {
2096 if [ -z "$1" -o -z "$2" -o -z "$3" ]; then
2097 echo "usage: grep_count_this <file to grep> <count> <grep string ...>" >&2
2098 exit 1
2099 fi
2101 local file="$1"
2102 local count="$2"
2103 local grep_string="$3"
2104 local count_found
2106 if [ ! -f "$file" ]; then
2107 echo "grep_count_this: No file to grep from!" >&2
2108 exit 1
2109 fi
2111 echo "grep_count_this: greping in $file, should find $count of: $grep_string"
2112 count_found=`$GREP -- "$grep_string" "$file" 2>/dev/null | wc -l 2>/dev/null`
2114 if [ "$count_found" -eq "$count" ] 2>/dev/null; then
2115 return 0
2116 fi
2118 return 1
2119 }