(travis) Multiple CI improvements lifted from blead
[dbsrgits/DBIx-Class.git] / maint / travis-ci_scripts / common.bash
CommitLineData
b58ecb01 1#!/bin/bash
2
c4c7254d 3# "autodie"
b58ecb01 4set -e
5
b9970637 6TEST_STDERR_LOG=/tmp/dbictest.stderr
c4c7254d 7TIMEOUT_CMD="/usr/bin/timeout --kill-after=16m --signal=TERM 15m"
b9970637 8
b58ecb01 9echo_err() { echo "$@" 1>&2 ; }
10
11if [[ "$TRAVIS" != "true" ]] ; then
12 echo_err "Running this script makes no sense outside of travis-ci"
13 exit 1
14fi
15
16tstamp() { echo -n "[$(date '+%H:%M:%S')]" ; }
17
5c0b7a18 18ci_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
cdda207b 29$(df -h)
5c0b7a18 30
31$(mount | grep '^/')
32
33= Kernel info
34$(uname -a)
35
36= Network Configuration
37$(ip addr)
38
39= Network Sockets Status
cdda207b 40$( (sudo netstat -an46p || netstat -an46p) | grep -Pv '\s(CLOSING|(FIN|TIME|CLOSE)_WAIT.?|LAST_ACK)\s')
5c0b7a18 41
42= Processlist
cdda207b 43$(ps fuxa)
5c0b7a18 44
45= Environment
cdda207b 46$(env | grep -P 'TEST|HARNESS|MAKE|TRAVIS|PERL|DBIC|PATH|SHELL' | LC_ALL=C sort | cat -v)
5c0b7a18 47
48= Perl in use
49$(perl -V)
50============================================================================="
51}
52
b58ecb01 53run_or_err() {
54 echo_err -n "$(tstamp) $1 ... "
55
47749813 56 LASTCMD="$2"
b58ecb01 57 LASTEXIT=0
58 START_TIME=$SECONDS
8f1a96a2 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
47749813 64 LASTOUT=$( eval "$2" 2>&1 ) || LASTEXIT=$?
8f1a96a2 65
66 # stop progress meter
67 for p in $(cat "$PRMETER_PIDFILE"); do kill $p ; done
68
b58ecb01 69 DELTA_TIME=$(( $SECONDS - $START_TIME ))
70
71 if [[ "$LASTEXIT" != "0" ]] ; then
47749813 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"
cdda207b 78 if [[ "$(dmesg)" =~ $( echo "\\bOOM\\b" ) ]] ; then
79 echo_err "=== dmesg ringbuffer"
80 echo_err "$(dmesg)"
81 fi
47749813 82 fi
4242985f 83
b58ecb01 84 return $LASTEXIT
85 else
86 echo_err "done (took ${DELTA_TIME}s)"
87 fi
88}
89
003e97c5 90apt_install() {
91 # flatten
92 pkgs="$@"
93
003e97c5 94 run_or_err "Installing Debian APT packages: $pkgs" "sudo apt-get install --allow-unauthenticated --no-install-recommends -y $pkgs"
95}
96
b58ecb01 97extract_prereqs() {
71dd2ecc 98 # once --verbose is set, --no-verbose can't disable it
99 # do this by hand
b2435630 100 local PERL_CPANM_OPT="$( echo $PERL_CPANM_OPT | sed 's/--verbose\s*//' )"
71dd2ecc 101
b58ecb01 102 # hack-hack-hack
103 LASTEXIT=0
104 COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
105 || LASTEXIT=$?
106
107 OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
777447e9 108 ERR=${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}
b58ecb01 109
50e644e5 110 if [[ "$LASTEXIT" != "0" ]] ; then
4242985f 111 echo_err "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:"
112 echo_err "$ERR"
113 echo_err "$OUT"
b58ecb01 114 exit 1
115 fi
116
777447e9 117 # throw away warnings, up-to-date diag, ascii art, convert to modnames
118 PQ=$(perl -p -e '
119 s/^.*?is up to date.*$//;
120 s/^\!.*//;
121 s/^[^a-z]+//i;
122 s/\-[^\-]+$/ /; # strip version part
123 s/\-/::/g
124 ' <<< "$OUT")
f207111d 125
126 # throw away what was in $@
127 for m in "$@" ; do
128 PQ=$( perl -p -e 's/(?:\s|^)\Q'"$m"'\E(?:\s|$)/ /mg' <<< "$PQ")
129 done
130
131 # RV
132 echo "$PQ"
b58ecb01 133}
134
135parallel_installdeps_notest() {
136 if [[ -z "$@" ]] ; then return; fi
137
4242985f 138 # one module spec per line
139 MODLIST="$(printf '%s\n' "$@")"
b58ecb01 140
23905f5f 141 # We want to trap the output of each process and serially append them to
142 # each other as opposed to just dumping a jumbled up mass-log that would
143 # need careful unpicking by a human
144 #
145 # While cpanm does maintain individual buildlogs in more recent versions,
146 # we are not terribly interested in trying to figure out which log is which
147 # dist. The verbose-output + trap STDIO technique is vastly superior in this
148 # particular case
10e248bf 149 #
150 # Explanation of inline args:
151 #
152 # [09:38] <T> you need a $0
153 # [09:38] <G> hence the _
154 # [09:38] <G> bash -c '...' _
155 # [09:39] <T> I like -- because it's the magic that gnu getopts uses for somethign else
156 # [09:39] <G> or --, yes
157 # [09:39] <T> ribasushi: you could put "giant space monkey penises" instead of "--" and it would work just as well
158 #
4242985f 159 run_or_err "Installing (without testing) $(echo $MODLIST)" \
160 "echo \\
161\"$MODLIST\" \\
cdda207b 162 | xargs -d '\\n' -n 1 -P $VCPU_USE bash -c \\
83da25f0 163 'OUT=\$(maint/getstatus $TIMEOUT_CMD cpanm --notest \"\$@\" 2>&1 ) || (LASTEXIT=\$?; echo \"\$OUT\"; exit \$LASTEXIT)' \\
4242985f 164 'giant space monkey penises'
10e248bf 165 "
b58ecb01 166}
167
cdda207b 168export -f parallel_installdeps_notest run_or_err echo_err tstamp
169
579472df 170installdeps() {
171 if [[ -z "$@" ]] ; then return; fi
172
47749813 173 MODLIST=$(printf "%q " "$@" | perl -pe 's/^\s+|\s+$//g')
579472df 174
175 local -x HARNESS_OPTIONS
176
cdda207b 177 HARNESS_OPTIONS="j$VCPU_USE"
579472df 178
47749813 179 if ! run_or_err "Attempting install of $# modules under parallel ($HARNESS_OPTIONS) testing ($MODLIST)" "_dep_inst_with_test $MODLIST" quiet_fail ; then
cdda207b 180 local errlog="failed after ${DELTA_TIME}s Exit:$LASTEXIT Log:$(/usr/bin/perl /usr/bin/nopaste -q -s Shadowcat -d "Parallel testfail" <<< "$LASTOUT")"
47749813 181 echo "$errlog"
579472df 182
a9fc70ee 183 POSTMORTEM="$POSTMORTEM$(
184 echo
47749813 185 echo "Depinstall of $MODLIST under $HARNESS_OPTIONS parallel testing $errlog"
a9fc70ee 186 )"
579472df 187
188 HARNESS_OPTIONS=""
47749813 189 run_or_err "Retrying same $# modules without parallel testing" "_dep_inst_with_test $MODLIST"
579472df 190 fi
191
192 INSTALLDEPS_OUT="${INSTALLDEPS_OUT}${LASTOUT}"
193}
194
e6b373aa 195_dep_inst_with_test() {
196 if [[ "$DEVREL_DEPS" == "true" ]] ; then
197 # --dev is already part of CPANM_OPT
47749813 198 LASTCMD="$TIMEOUT_CMD cpanm $@"
cdda207b 199 $LASTCMD 2>&1 || return 1
e6b373aa 200 else
47749813 201 LASTCMD="$TIMEOUT_CMD cpan $@"
cdda207b 202 $LASTCMD 2>&1 || return 1
579472df 203
e6b373aa 204 # older perls do not have a CPAN which can exit with error on failed install
205 for m in "$@"; do
206 if ! perl -e '
579472df 207
cdda207b 208$ARGV[0] =~ s/-TRIAL\.//;
209
647da28e 210my $mod = (
cdda207b 211 # abuse backtrack
212 $ARGV[0] =~ m{ / .*? ( [^/]+ ) $ }x
579472df 213 ? do { my @p = split (/\-/, $1); pop @p; join "::", @p }
214 : $ARGV[0]
647da28e 215);
216
cdda207b 217# map some install-names to a module/version combo
218# serves both as a grandfathered title-less tarball, and
219# as a minimum version check for upgraded core modules
220my $eval_map = {
221
222 # this is temporary, will need something more robust down the road
223 # (perhaps by then Module::CoreList will be dep-free)
224 "Module::Build" => { ver => "0.4214" },
225 "podlators" => { mod => "Pod::Man", ver => "2.17" },
226
227 "File::Spec" => { ver => "3.47" },
228 "Cwd" => { ver => "3.47" },
229
230 "List::Util" => { ver => "1.42" },
231 "Scalar::Util" => { ver => "1.42" },
232 "Scalar::List::Utils" => { mod => "List::Util", ver => "1.42" },
233};
234
235my $m = $eval_map->{$mod}{mod} || $mod;
236
237eval(
238 "require $m"
239
240 .
241
242 ($eval_map->{$mod}{ver}
243 ? "; $m->VERSION(\$eval_map->{\$mod}{ver}) "
244 : ""
245 )
647da28e 246
cdda207b 247 .
248
249 "; 1"
250)
251 or
252( print $@ and exit 1)
579472df 253
e6b373aa 254 ' "$m" 2> /dev/null ; then
255 echo -e "$m installation seems to have failed"
256 return 1
257 fi
258 done
259 fi
579472df 260}
261
cdda207b 262# Idea stolen from
263# https://github.com/kentfredric/Dist-Zilla-Plugin-Prereqs-MatchInstalled-All/blob/master/maint-travis-ci/sterilize_env.pl
264# Only works on 5.12+ (where sitelib was finally properly fixed)
265purge_sitelib() {
266 echo_err "$(tstamp) Sterilizing the Perl installation (cleaning up sitelib)"
267
268 if perl -M5.012 -e1 &>/dev/null ; then
269
270 perl -M5.012 -MConfig -MFile::Find -e '
271 my $sitedirs = {
272 map { $Config{$_} => 1 }
273 grep { $_ =~ /site(lib|arch)exp$/ }
274 keys %Config
275 };
276 find({ bydepth => 1, no_chdir => 1, follow_fast => 1, wanted => sub {
277 ! $sitedirs->{$_} and ( -d _ ? rmdir : unlink )
278 } }, keys %$sitedirs )
279 '
280 else
281
282 cl_fn="/tmp/${TRAVIS_BUILD_ID}_Module_CoreList.pm";
283
284 [[ -s "$cl_fn" ]] || run_or_err \
285 "Downloading latest Module::CoreList" \
286 "curl -s --compress -o '$cl_fn' https://api.metacpan.org/source/Module::CoreList"
287
288 perl -0777 -Ilib -MDBIx::Class::Optional::Dependencies -e '
289
290 # this is horrible, but really all we want is "has this ever been used"
291 # so a grep without a load is quite legit (and horrible)
292 my $mcl_source = <>;
293
294 my %reqs_for_group = %{DBIx::Class::Optional::Dependencies->req_group_list};
295
296 my @all_possible_never_been_core_modpaths = map
297 { (my $mp = $_ . ".pm" ) =~ s|::|/|g; $mp }
298 grep
299 { $mcl_source !~ / ^ \s+ \x27 $_ \x27 \s* \=\> /mx }
300 (
301 qw(
302 Module::Build::Tiny
303 ),
304 (map
305 { keys %{$reqs_for_group{$_}} }
306 grep
307 { !/^rdbms_|^dist_/ }
308 keys %reqs_for_group
309 ),
310 )
311 ;
312
313 # now that we have the list we can go ahead and destroy every single one
314 # of these modules without being concerned about breaking the base ability
315 # to install things
316 for my $mp ( sort { lc($a) cmp lc($b) } @all_possible_never_been_core_modpaths ) {
317 for my $incdir (@INC) {
318 -e "$incdir/$mp"
319 and
320 unlink "$incdir/$mp"
321 and
322 print "Nuking $incdir/$mp\n"
323 }
324 }
325 ' "$cl_fn"
326
327 fi
328}
329
330
b58ecb01 331CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }
f207111d 332
333CPAN_supports_BUILDPL() { perl -MCPAN\ 1.9205 -e1 &>/dev/null; }
cdda207b 334
335have_sudo() { sudo /bin/true &>/dev/null ; }