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