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