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