convert CI
[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   ' <<< "$OUT")
120
121   # throw away what was in $@
122   for m in "$@" ; do
123     PQ=$( perl -p -e 's/(?:\s|^)\Q'"$m"'\E(?:\s|$)/ /mg' <<< "$PQ")
124   done
125
126   # RV
127   echo "$PQ"
128 }
129
130 listalldeps() {
131   # relies on sorted YAML
132   perl -lne '
133     next unless /^((?:build_)?requires:)/..($_ ne $1 and /^[^ ]/);
134     next if /^[^ ]/ or /^ *perl:/; # drop requires headers, or perl
135     s/^ *([^ ]*): .*/$1/;
136     print;
137   ' MYMETA.yml
138 }
139
140 parallel_installdeps_notest() {
141   if [[ -z "$@" ]] ; then return; fi
142
143   # one module spec per line
144   MODLIST="$(printf '%s\n' "$@")"
145
146   # We want to trap the output of each process and serially append them to
147   # each other as opposed to just dumping a jumbled up mass-log that would
148   # need careful unpicking by a human
149   #
150   # While cpanm does maintain individual buildlogs in more recent versions,
151   # we are not terribly interested in trying to figure out which log is which
152   # dist. The verbose-output + trap STDIO technique is vastly superior in this
153   # particular case
154   #
155   # Explanation of inline args:
156   #
157   # [09:38] <T> you need a $0
158   # [09:38] <G> hence the _
159   # [09:38] <G> bash -c '...' _
160   # [09:39] <T> I like -- because it's the magic that gnu getopts uses for somethign else
161   # [09:39] <G> or --, yes
162   # [09:39] <T> ribasushi: you could put "giant space monkey penises" instead of "--" and it would work just as well
163   #
164   run_or_err "Installing (without testing) $(echo $MODLIST)" \
165     "echo \\
166 \"$MODLIST\" \\
167       | xargs -d '\\n' -n 1 -P $NUMTHREADS bash -c \\
168         'OUT=\$($TIMEOUT_CMD cpanm --notest \"\$@\" 2>&1 ) || (LASTEXIT=\$?; echo \"\$OUT\"; exit \$LASTEXIT)' \\
169         'giant space monkey penises'
170     "
171 }
172
173 installdeps() {
174   if [[ -z "$@" ]] ; then return; fi
175
176   MODLIST=$(printf "%q " "$@" | perl -pe 's/^\s+|\s+$//g')
177
178   local -x HARNESS_OPTIONS
179
180   HARNESS_OPTIONS="j$NUMTHREADS"
181
182   if ! run_or_err "Attempting install of $# modules under parallel ($HARNESS_OPTIONS) testing ($MODLIST)" "_dep_inst_with_test $MODLIST" quiet_fail ; then
183     local errlog="failed after ${DELTA_TIME}s Exit:$LASTEXIT Log:$(/usr/bin/nopaste -q -s Shadowcat -d "Parallel testfail" <<< "$LASTOUT")"
184     echo "$errlog"
185
186     POSTMORTEM="$POSTMORTEM$(
187       echo
188       echo "Depinstall of $MODLIST under $HARNESS_OPTIONS parallel testing $errlog"
189     )"
190
191     HARNESS_OPTIONS=""
192     run_or_err "Retrying same $# modules without parallel testing" "_dep_inst_with_test $MODLIST"
193   fi
194
195   INSTALLDEPS_OUT="${INSTALLDEPS_OUT}${LASTOUT}"
196 }
197
198 _dep_inst_with_test() {
199   if [[ "$DEVREL_DEPS" == "true" ]] ; then
200     # --dev is already part of CPANM_OPT
201     LASTCMD="$TIMEOUT_CMD cpanm $@"
202     $LASTCMD 2>&1
203   else
204     LASTCMD="$TIMEOUT_CMD cpan $@"
205     $LASTCMD 2>&1
206
207     # older perls do not have a CPAN which can exit with error on failed install
208     for m in "$@"; do
209       if ! perl -e '
210
211 my $mod = (
212   $ARGV[0] =~ m{ \/ .*? ([^\/]+) $ }x
213     ? do { my @p = split (/\-/, $1); pop @p; join "::", @p }
214     : $ARGV[0]
215 );
216
217 $mod = q{List::Util} if $mod eq q{Scalar::List::Utils};
218
219 eval qq{require($mod)} or ( print $@ and exit 1)
220
221       ' "$m" 2> /dev/null ; then
222         echo -e "$m installation seems to have failed"
223         return 1
224       fi
225     done
226   fi
227 }
228
229 CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }
230
231 CPAN_supports_BUILDPL() { perl -MCPAN\ 1.9205 -e1 &>/dev/null; }