(travis) Attempt to grab more info on parallel_installdeps_notest failures
[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   LASTCMD="$2"
21   LASTEXIT=0
22   START_TIME=$SECONDS
23
24   PRMETER_PIDFILE="$(tempfile)_$SECONDS"
25   # the double bash is to hide the job control messages
26   bash -c "bash -c 'echo \$\$ >> $PRMETER_PIDFILE; while true; do sleep 10; echo -n \"\${SECONDS}s ... \"; done' &"
27
28   LASTOUT=$( eval "$2" 2>&1 ) || LASTEXIT=$?
29
30   # stop progress meter
31   for p in $(cat "$PRMETER_PIDFILE"); do kill $p ; done
32
33   DELTA_TIME=$(( $SECONDS - $START_TIME ))
34
35   if [[ "$LASTEXIT" != "0" ]] ; then
36     if [[ -z "$3" ]] ; then
37       echo_err "FAILED !!! (after ${DELTA_TIME}s)"
38       echo_err "Command executed:"
39       echo_err "$LASTCMD"
40       echo_err "STDOUT+STDERR:"
41       echo_err "$LASTOUT"
42     fi
43
44     return $LASTEXIT
45   else
46     echo_err "done (took ${DELTA_TIME}s)"
47   fi
48 }
49
50 apt_install() {
51   # flatten
52   pkgs="$@"
53
54   # Need to do this at every step, the sources list may very well have changed
55   run_or_err "Updating APT available package list" "sudo apt-get update"
56
57   run_or_err "Installing Debian APT packages: $pkgs" "sudo apt-get install --allow-unauthenticated  --no-install-recommends -y $pkgs"
58 }
59
60 extract_prereqs() {
61   # once --verbose is set, --no-verbose can't disable it
62   # do this by hand
63   local PERL_CPANM_OPT="$( echo $PERL_CPANM_OPT | sed 's/--verbose\s*//' )"
64
65   # hack-hack-hack
66   LASTEXIT=0
67   COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
68     || LASTEXIT=$?
69
70   OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
71   ERR=${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}
72
73   if [[ "$LASTEXIT" != "0" ]] ; then
74     echo_err "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:"
75     echo_err "$ERR"
76     echo_err "$OUT"
77     exit 1
78   fi
79
80   # throw away warnings, up-to-date diag, ascii art, convert to modnames
81   PQ=$(perl -p -e '
82     s/^.*?is up to date.*$//;
83     s/^\!.*//;
84     s/^[^a-z]+//i;
85     s/\-[^\-]+$/ /; # strip version part
86     s/\-/::/g
87   ' <<< "$OUT")
88
89   # throw away what was in $@
90   for m in "$@" ; do
91     PQ=$( perl -p -e 's/(?:\s|^)\Q'"$m"'\E(?:\s|$)/ /mg' <<< "$PQ")
92   done
93
94   # RV
95   echo "$PQ"
96 }
97
98 parallel_installdeps_notest() {
99   if [[ -z "$@" ]] ; then return; fi
100
101   # one module spec per line
102   MODLIST="$(printf '%s\n' "$@")"
103
104   # We want to trap the output of each process and serially append them to
105   # each other as opposed to just dumping a jumbled up mass-log that would
106   # need careful unpicking by a human
107   #
108   # While cpanm does maintain individual buildlogs in more recent versions,
109   # we are not terribly interested in trying to figure out which log is which
110   # dist. The verbose-output + trap STDIO technique is vastly superior in this
111   # particular case
112   #
113   # Explanation of inline args:
114   #
115   # [09:38] <T> you need a $0
116   # [09:38] <G> hence the _
117   # [09:38] <G> bash -c '...' _
118   # [09:39] <T> I like -- because it's the magic that gnu getopts uses for somethign else
119   # [09:39] <G> or --, yes
120   # [09:39] <T> ribasushi: you could put "giant space monkey penises" instead of "--" and it would work just as well
121   #
122   run_or_err "Installing (without testing) $(echo $MODLIST)" \
123     "echo \\
124 \"$MODLIST\" \\
125       | xargs -d '\\n' -n 1 -P $NUMTHREADS bash -c \\
126         'OUT=\$(maint/getstatus $TIMEOUT_CMD cpanm --notest \"\$@\" 2>&1 ) || (LASTEXIT=\$?; echo \"\$OUT\"; exit \$LASTEXIT)' \\
127         'giant space monkey penises'
128     "
129 }
130
131 installdeps() {
132   if [[ -z "$@" ]] ; then return; fi
133
134   MODLIST=$(printf "%q " "$@" | perl -pe 's/^\s+|\s+$//g')
135
136   local -x HARNESS_OPTIONS
137
138   HARNESS_OPTIONS="j$NUMTHREADS"
139
140   if ! run_or_err "Attempting install of $# modules under parallel ($HARNESS_OPTIONS) testing ($MODLIST)" "_dep_inst_with_test $MODLIST" quiet_fail ; then
141     local errlog="failed after ${DELTA_TIME}s Exit:$LASTEXIT Log:$(/usr/bin/nopaste -q -s Shadowcat -d "Parallel testfail" <<< "$LASTOUT")"
142     echo "$errlog"
143
144     POSTMORTEM="$POSTMORTEM$(
145       echo
146       echo "Depinstall of $MODLIST under $HARNESS_OPTIONS parallel testing $errlog"
147     )"
148
149     HARNESS_OPTIONS=""
150     run_or_err "Retrying same $# modules without parallel testing" "_dep_inst_with_test $MODLIST"
151   fi
152
153   INSTALLDEPS_OUT="${INSTALLDEPS_OUT}${LASTOUT}"
154 }
155
156 _dep_inst_with_test() {
157   if [[ "$DEVREL_DEPS" == "true" ]] ; then
158     # --dev is already part of CPANM_OPT
159     LASTCMD="$TIMEOUT_CMD cpanm $@"
160     $LASTCMD 2>&1
161   else
162     LASTCMD="$TIMEOUT_CMD cpan $@"
163     $LASTCMD 2>&1
164
165     # older perls do not have a CPAN which can exit with error on failed install
166     for m in "$@"; do
167       if ! perl -e '
168
169 my $mod = (
170   $ARGV[0] =~ m{ \/ .*? ([^\/]+) $ }x
171     ? do { my @p = split (/\-/, $1); pop @p; join "::", @p }
172     : $ARGV[0]
173 );
174
175 $mod = q{List::Util} if $mod eq q{Scalar::List::Utils};
176
177 eval qq{require($mod)} or ( print $@ and exit 1)
178
179       ' "$m" 2> /dev/null ; then
180         echo -e "$m installation seems to have failed"
181         return 1
182       fi
183     done
184   fi
185 }
186
187 CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }
188
189 CPAN_supports_BUILDPL() { perl -MCPAN\ 1.9205 -e1 &>/dev/null; }