b64d414c6b4f84799942fb64127861439a01a94a
[dbsrgits/DBIx-Class.git] / maint / travis-ci_scripts / common.bash
1 #!/bin/bash
2
3 set -e
4
5 TEST_STDERR_LOG=/tmp/dbictest.stderr
6 TIMEOUT_CMD="/usr/bin/timeout --kill-after=9.5m --signal=TERM 9m"
7
8 echo_err() { echo "$@" 1>&2 ; }
9
10 if [[ "$TRAVIS" != "true" ]] ; then
11   echo_err "Running this script makes no sense outside of travis-ci"
12   exit 1
13 fi
14
15 tstamp() { echo -n "[$(date '+%H:%M:%S')]" ; }
16
17 run_or_err() {
18   echo_err -n "$(tstamp) $1 ... "
19
20   LASTEXIT=0
21   START_TIME=$SECONDS
22   # the tee is a handy debugging tool when stumpage is exceedingly strong
23   #LASTOUT=$( bash -c "$2" 2>&1 | tee /dev/stderr) || LASTEXIT=$?
24   LASTOUT=$( bash -c "$2" 2>&1 ) || LASTEXIT=$?
25   DELTA_TIME=$(( $SECONDS - $START_TIME ))
26
27   if [[ "$LASTEXIT" != "0" ]] ; then
28     echo_err "FAILED !!! (after ${DELTA_TIME}s)"
29     echo_err "Command executed:"
30     echo_err "$2"
31     echo_err "STDOUT+STDERR:"
32     echo_err "$LASTOUT"
33
34     return $LASTEXIT
35   else
36     echo_err "done (took ${DELTA_TIME}s)"
37   fi
38 }
39
40 apt_install() {
41   # flatten
42   pkgs="$@"
43
44   # Need to do this at every step, the sources list may very well have changed
45   run_or_err "Updating APT available package list" "sudo apt-get update"
46
47   run_or_err "Installing Debian APT packages: $pkgs" "sudo apt-get install --allow-unauthenticated  --no-install-recommends -y $pkgs"
48 }
49
50 extract_prereqs() {
51   # once --verbose is set, --no-verbose can't disable it
52   # do this by hand
53   local PERL_CPANM_OPT="$( echo $PERL_CPANM_OPT | sed 's/--verbose\s*//' )"
54
55   # hack-hack-hack
56   LASTEXIT=0
57   COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
58     || LASTEXIT=$?
59
60   OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
61   ERR=$(grep -v " is up to date." <<< "${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}")
62
63   if [[ "$LASTEXIT" != "0" ]] ; then
64     echo_err "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:"
65     echo_err "$ERR"
66     echo_err "$OUT"
67     exit 1
68   fi
69
70   # throw away warnings, ascii art, convert to modnames
71   PQ=$(perl -p -e 's/^\!.*//; s/^[^a-z]+//i; s/\-[^\-]+$/ /; s/\-/::/g' <<< "$OUT")
72
73   # throw away what was in $@
74   for m in "$@" ; do
75     PQ=$( perl -p -e 's/(?:\s|^)\Q'"$m"'\E(?:\s|$)/ /mg' <<< "$PQ")
76   done
77
78   # RV
79   echo "$PQ"
80 }
81
82 parallel_installdeps_notest() {
83   if [[ -z "$@" ]] ; then return; fi
84
85   # one module spec per line
86   MODLIST="$(printf '%s\n' "$@")"
87
88   # The reason we do things so "non-interactively" is that xargs -P will have the
89   # latest cpanm instance overwrite the buildlog. There seems to be no way to
90   # specify a custom buildlog, hence we just collect the verbose output
91   # and display it in case of "worker" failure
92   #
93   # Explanation of inline args:
94   #
95   # [09:38] <T> you need a $0
96   # [09:38] <G> hence the _
97   # [09:38] <G> bash -c '...' _
98   # [09:39] <T> I like -- because it's the magic that gnu getopts uses for somethign else
99   # [09:39] <G> or --, yes
100   # [09:39] <T> ribasushi: you could put "giant space monkey penises" instead of "--" and it would work just as well
101   #
102   run_or_err "Installing (without testing) $(echo $MODLIST)" \
103     "echo \\
104 \"$MODLIST\" \\
105       | xargs -d '\\n' -n 1 -P $NUMTHREADS bash -c \\
106         'OUT=\$($TIMEOUT_CMD cpanm --notest \"\$@\" 2>&1 ) || (LASTEXIT=\$?; echo \"\$OUT\"; exit \$LASTEXIT)' \\
107         'giant space monkey penises'
108     "
109 }
110
111 installdeps() {
112   if [[ -z "$@" ]] ; then return; fi
113
114   echo_err "$(tstamp) Processing dependencies: $@"
115
116   local -x HARNESS_OPTIONS
117
118   HARNESS_OPTIONS="j$NUMTHREADS"
119
120   echo_err -n "Attempting install of $# modules under parallel ($HARNESS_OPTIONS) testing ... "
121
122   LASTEXIT=0
123   START_TIME=$SECONDS
124   LASTOUT=$( _dep_inst_with_test "$@" ) || LASTEXIT=$?
125   DELTA_TIME=$(( $SECONDS - $START_TIME ))
126
127   if [[ "$LASTEXIT" = "0" ]] ; then
128     echo_err "done (took ${DELTA_TIME}s)"
129   else
130     local errlog="after ${DELTA_TIME}s Exit:$LASTEXIT Log:$(/usr/bin/nopaste -q -s Shadowcat -d "Parallel testfail" <<< "$LASTOUT")"
131     echo_err -n "failed ($errlog) retrying with sequential testing ... "
132     POSTMORTEM="$POSTMORTEM$(
133       echo
134       echo "Depinstall under $HARNESS_OPTIONS parallel testing failed $errlog"
135       echo "============================================================="
136       echo "Attempted installation of: $@"
137       echo "============================================================="
138     )"
139
140     HARNESS_OPTIONS=""
141     LASTEXIT=0
142     START_TIME=$SECONDS
143     LASTOUT=$( _dep_inst_with_test "$@" ) || LASTEXIT=$?
144     DELTA_TIME=$(( $SECONDS - $START_TIME ))
145
146     if [[ "$LASTEXIT" = "0" ]] ; then
147       echo_err "done (took ${DELTA_TIME}s)"
148     else
149       echo_err "FAILED !!! (after ${DELTA_TIME}s)"
150       echo_err "STDOUT+STDERR:"
151       echo_err "$LASTOUT"
152       exit 1
153     fi
154   fi
155
156   INSTALLDEPS_OUT="${INSTALLDEPS_OUT}${LASTOUT}"
157 }
158
159 _dep_inst_with_test() {
160   if [[ "$DEVREL_DEPS" == "true" ]] ; then
161     # --dev is already part of CPANM_OPT
162     $TIMEOUT_CMD cpanm "$@" 2>&1
163   else
164     $TIMEOUT_CMD cpan "$@" 2>&1
165
166     # older perls do not have a CPAN which can exit with error on failed install
167     for m in "$@"; do
168       if ! perl -e '
169
170 my $mod = (
171   $ARGV[0] =~ m{ \/ .*? ([^\/]+) $ }x
172     ? do { my @p = split (/\-/, $1); pop @p; join "::", @p }
173     : $ARGV[0]
174 );
175
176 $mod = q{List::Util} if $mod eq q{Scalar::List::Utils};
177
178 eval qq{require($mod)} or ( print $@ and exit 1)
179
180       ' "$m" 2> /dev/null ; then
181         echo -e "$m installation seems to have failed"
182         return 1
183       fi
184     done
185   fi
186 }
187
188 CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }
189
190 CPAN_supports_BUILDPL() { perl -MCPAN\ 1.9205 -e1 &>/dev/null; }