workaround bizarre distro vs module confusion
[dbsrgits/DBIx-Class-Schema-Loader.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.%N')]" ; }
16
17 ci_vm_state_text() {
18   echo "
19 ========================== CI System information ============================
20
21 = CPUinfo
22 $(perl -0777 -p -e 's/.+\n\n(?!\z)//s' < /proc/cpuinfo)
23
24 = Meminfo
25 $(free -m -t)
26
27 = Diskinfo
28 $(sudo df -h)
29
30 $(mount | grep '^/')
31
32 = Kernel info
33 $(uname -a)
34
35 = Network Configuration
36 $(ip addr)
37
38 = Network Sockets Status
39 $(sudo netstat -an46p | grep -Pv '\s(CLOSING|(FIN|TIME|CLOSE)_WAIT.?|LAST_ACK)\s')
40
41 = Processlist
42 $(sudo ps fuxa)
43
44 = Environment
45 $(env | grep -P 'TEST|HARNESS|MAKE|TRAVIS|PERL|DBIC' | LC_ALL=C sort | cat -v)
46
47 = Perl in use
48 $(perl -V)
49 ============================================================================="
50 }
51
52 run_or_err() {
53   echo_err -n "$(tstamp) $1 ... "
54
55   LASTCMD="$2"
56   LASTEXIT=0
57   START_TIME=$SECONDS
58
59   PRMETER_PIDFILE="$(tempfile)_$SECONDS"
60   # the double bash is to hide the job control messages
61   bash -c "bash -c 'echo \$\$ >> $PRMETER_PIDFILE; while true; do sleep 10; echo -n \"\${SECONDS}s ... \"; done' &"
62
63   LASTOUT=$( eval "$2" 2>&1 ) || LASTEXIT=$?
64
65   # stop progress meter
66   for p in $(cat "$PRMETER_PIDFILE"); do kill $p ; done
67
68   DELTA_TIME=$(( $SECONDS - $START_TIME ))
69
70   if [[ "$LASTEXIT" != "0" ]] ; then
71     if [[ -z "$3" ]] ; then
72       echo_err "FAILED !!! (after ${DELTA_TIME}s)"
73       echo_err "Command executed:"
74       echo_err "$LASTCMD"
75       echo_err "STDOUT+STDERR:"
76       echo_err "$LASTOUT"
77     fi
78
79     return $LASTEXIT
80   else
81     echo_err "done (took ${DELTA_TIME}s)"
82   fi
83 }
84
85 apt_install() {
86   # flatten
87   pkgs="$@"
88
89   run_or_err "Installing Debian APT packages: $pkgs" "sudo apt-get install --allow-unauthenticated  --no-install-recommends -y $pkgs"
90 }
91
92 extract_prereqs() {
93   # once --verbose is set, --no-verbose can't disable it
94   # do this by hand
95   local PERL_CPANM_OPT="$( echo $PERL_CPANM_OPT | sed 's/--verbose\s*//' )"
96
97   # hack-hack-hack
98   LASTEXIT=0
99   COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
100     || LASTEXIT=$?
101
102   OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
103   ERR=${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}
104
105   if [[ "$LASTEXIT" != "0" ]] ; then
106     echo_err "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:"
107     echo_err "$ERR"
108     echo_err "$OUT"
109     exit 1
110   fi
111
112   # throw away warnings, up-to-date diag, ascii art, convert to modnames
113   PQ=$(perl -p -e '
114     s/^.*?is up to date.*$//;
115     s/^\!.*//;
116     s/^[^a-z]+//i;
117     s/\-[^\-]+$/ /; # strip version part
118     s/\-/::/g;
119     s/^\s*Snowball::Swedish\s*$/ Lingua::Stem::Snowball::Se /m; # distro->module
120     s/^\s*Snowball::Norwegian\s*$/ Lingua::Stem::Snowball::No /m;
121     s/^\s*Scalar::List::Utils\s*$/ List::Util /m;
122   ' <<< "$OUT")
123
124   # throw away what was in $@
125   for m in "$@" ; do
126     PQ=$( perl -p -e 's/(?:\s|^)\Q'"$m"'\E(?:\s|$)/ /mg' <<< "$PQ")
127   done
128
129   # RV
130   echo "$PQ"
131 }
132
133 listalldeps() {
134   # relies on sorted YAML
135   perl -lne '
136     next unless /^((?:build_)?requires:)/..($_ ne $1 and /^[^ ]/);
137     next if /^[^ ]/ or /^ *perl:/; # drop requires headers, or perl
138     s/^ *([^ ]*): .*/$1/;
139     print;
140   ' MYMETA.yml
141 }
142
143 parallel_installdeps_notest() {
144   if [[ -z "$@" ]] ; then return; fi
145
146   # one module spec per line
147   MODLIST="$(printf '%s\n' "$@")"
148
149   # We want to trap the output of each process and serially append them to
150   # each other as opposed to just dumping a jumbled up mass-log that would
151   # need careful unpicking by a human
152   #
153   # While cpanm does maintain individual buildlogs in more recent versions,
154   # we are not terribly interested in trying to figure out which log is which
155   # dist. The verbose-output + trap STDIO technique is vastly superior in this
156   # particular case
157   #
158   # Explanation of inline args:
159   #
160   # [09:38] <T> you need a $0
161   # [09:38] <G> hence the _
162   # [09:38] <G> bash -c '...' _
163   # [09:39] <T> I like -- because it's the magic that gnu getopts uses for somethign else
164   # [09:39] <G> or --, yes
165   # [09:39] <T> ribasushi: you could put "giant space monkey penises" instead of "--" and it would work just as well
166   #
167   run_or_err "Installing (without testing) $(echo $MODLIST)" \
168     "echo \\
169 \"$MODLIST\" \\
170       | xargs -d '\\n' -n 1 -P $NUMTHREADS bash -c \\
171         'OUT=\$($TIMEOUT_CMD cpanm --notest \"\$@\" 2>&1 ) || (LASTEXIT=\$?; echo \"\$OUT\"; exit \$LASTEXIT)' \\
172         'giant space monkey penises'
173     "
174 }
175
176 installdeps() {
177   if [[ -z "$@" ]] ; then return; fi
178
179   MODLIST=$(printf "%q " "$@" | perl -pe 's/^\s+|\s+$//g')
180
181   local -x HARNESS_OPTIONS
182
183   HARNESS_OPTIONS="j$NUMTHREADS"
184
185   if ! run_or_err "Attempting install of $# modules under parallel ($HARNESS_OPTIONS) testing ($MODLIST)" "_dep_inst_with_test $MODLIST" quiet_fail ; then
186     local errlog="failed after ${DELTA_TIME}s Exit:$LASTEXIT Log:$(/usr/bin/nopaste -q -s Shadowcat -d "Parallel testfail" <<< "$LASTOUT")"
187     echo "$errlog"
188
189     POSTMORTEM="$POSTMORTEM$(
190       echo
191       echo "Depinstall of $MODLIST under $HARNESS_OPTIONS parallel testing $errlog"
192     )"
193
194     HARNESS_OPTIONS=""
195     run_or_err "Retrying same $# modules without parallel testing" "_dep_inst_with_test $MODLIST"
196   fi
197
198   INSTALLDEPS_OUT="${INSTALLDEPS_OUT}${LASTOUT}"
199 }
200
201 _dep_inst_with_test() {
202   if [[ "$DEVREL_DEPS" == "true" ]] ; then
203     # --dev is already part of CPANM_OPT
204     LASTCMD="$TIMEOUT_CMD cpanm $@"
205     $LASTCMD 2>&1
206   else
207     LASTCMD="$TIMEOUT_CMD cpan $@"
208     $LASTCMD 2>&1
209
210     # older perls do not have a CPAN which can exit with error on failed install
211     for m in "$@"; do
212       if ! perl -e '
213
214 my $mod = (
215   $ARGV[0] =~ m{ \/ .*? ([^\/]+) $ }x
216     ? do { my @p = split (/\-/, $1); pop @p; join "::", @p }
217     : $ARGV[0]
218 );
219
220 $mod = q{List::Util} if $mod eq q{Scalar::List::Utils};
221
222 eval qq{require($mod)} or ( print $@ and exit 1)
223
224       ' "$m" 2> /dev/null ; then
225         echo -e "$m installation seems to have failed"
226         return 1
227       fi
228     done
229   fi
230 }
231
232 CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }
233
234 CPAN_supports_BUILDPL() { perl -MCPAN\ 1.9205 -e1 &>/dev/null; }