(travis) Re-consolidate sysinfo handling (reverts part of 83da25f0)
[dbsrgits/DBIx-Class-Historic.git] / maint / travis-ci_scripts / common.bash
CommitLineData
b58ecb01 1#!/bin/bash
2
3set -e
4
b9970637 5TEST_STDERR_LOG=/tmp/dbictest.stderr
851b6c2d 6TIMEOUT_CMD="/usr/bin/timeout --kill-after=9.5m --signal=TERM 9m"
b9970637 7
b58ecb01 8echo_err() { echo "$@" 1>&2 ; }
9
10if [[ "$TRAVIS" != "true" ]] ; then
11 echo_err "Running this script makes no sense outside of travis-ci"
12 exit 1
13fi
14
15tstamp() { echo -n "[$(date '+%H:%M:%S')]" ; }
16
5c0b7a18 17ci_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
b58ecb01 52run_or_err() {
53 echo_err -n "$(tstamp) $1 ... "
54
47749813 55 LASTCMD="$2"
b58ecb01 56 LASTEXIT=0
57 START_TIME=$SECONDS
8f1a96a2 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
47749813 63 LASTOUT=$( eval "$2" 2>&1 ) || LASTEXIT=$?
8f1a96a2 64
65 # stop progress meter
66 for p in $(cat "$PRMETER_PIDFILE"); do kill $p ; done
67
b58ecb01 68 DELTA_TIME=$(( $SECONDS - $START_TIME ))
69
70 if [[ "$LASTEXIT" != "0" ]] ; then
47749813 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
4242985f 78
b58ecb01 79 return $LASTEXIT
80 else
81 echo_err "done (took ${DELTA_TIME}s)"
82 fi
83}
84
003e97c5 85apt_install() {
86 # flatten
87 pkgs="$@"
88
89 # Need to do this at every step, the sources list may very well have changed
90 run_or_err "Updating APT available package list" "sudo apt-get update"
91
92 run_or_err "Installing Debian APT packages: $pkgs" "sudo apt-get install --allow-unauthenticated --no-install-recommends -y $pkgs"
93}
94
b58ecb01 95extract_prereqs() {
71dd2ecc 96 # once --verbose is set, --no-verbose can't disable it
97 # do this by hand
b2435630 98 local PERL_CPANM_OPT="$( echo $PERL_CPANM_OPT | sed 's/--verbose\s*//' )"
71dd2ecc 99
b58ecb01 100 # hack-hack-hack
101 LASTEXIT=0
102 COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
103 || LASTEXIT=$?
104
105 OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
777447e9 106 ERR=${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}
b58ecb01 107
50e644e5 108 if [[ "$LASTEXIT" != "0" ]] ; then
4242985f 109 echo_err "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:"
110 echo_err "$ERR"
111 echo_err "$OUT"
b58ecb01 112 exit 1
113 fi
114
777447e9 115 # throw away warnings, up-to-date diag, ascii art, convert to modnames
116 PQ=$(perl -p -e '
117 s/^.*?is up to date.*$//;
118 s/^\!.*//;
119 s/^[^a-z]+//i;
120 s/\-[^\-]+$/ /; # strip version part
121 s/\-/::/g
122 ' <<< "$OUT")
f207111d 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"
b58ecb01 131}
132
133parallel_installdeps_notest() {
134 if [[ -z "$@" ]] ; then return; fi
135
4242985f 136 # one module spec per line
137 MODLIST="$(printf '%s\n' "$@")"
b58ecb01 138
23905f5f 139 # We want to trap the output of each process and serially append them to
140 # each other as opposed to just dumping a jumbled up mass-log that would
141 # need careful unpicking by a human
142 #
143 # While cpanm does maintain individual buildlogs in more recent versions,
144 # we are not terribly interested in trying to figure out which log is which
145 # dist. The verbose-output + trap STDIO technique is vastly superior in this
146 # particular case
10e248bf 147 #
148 # Explanation of inline args:
149 #
150 # [09:38] <T> you need a $0
151 # [09:38] <G> hence the _
152 # [09:38] <G> bash -c '...' _
153 # [09:39] <T> I like -- because it's the magic that gnu getopts uses for somethign else
154 # [09:39] <G> or --, yes
155 # [09:39] <T> ribasushi: you could put "giant space monkey penises" instead of "--" and it would work just as well
156 #
4242985f 157 run_or_err "Installing (without testing) $(echo $MODLIST)" \
158 "echo \\
159\"$MODLIST\" \\
160 | xargs -d '\\n' -n 1 -P $NUMTHREADS bash -c \\
83da25f0 161 'OUT=\$(maint/getstatus $TIMEOUT_CMD cpanm --notest \"\$@\" 2>&1 ) || (LASTEXIT=\$?; echo \"\$OUT\"; exit \$LASTEXIT)' \\
4242985f 162 'giant space monkey penises'
10e248bf 163 "
b58ecb01 164}
165
579472df 166installdeps() {
167 if [[ -z "$@" ]] ; then return; fi
168
47749813 169 MODLIST=$(printf "%q " "$@" | perl -pe 's/^\s+|\s+$//g')
579472df 170
171 local -x HARNESS_OPTIONS
172
173 HARNESS_OPTIONS="j$NUMTHREADS"
174
47749813 175 if ! run_or_err "Attempting install of $# modules under parallel ($HARNESS_OPTIONS) testing ($MODLIST)" "_dep_inst_with_test $MODLIST" quiet_fail ; then
176 local errlog="failed after ${DELTA_TIME}s Exit:$LASTEXIT Log:$(/usr/bin/nopaste -q -s Shadowcat -d "Parallel testfail" <<< "$LASTOUT")"
177 echo "$errlog"
579472df 178
a9fc70ee 179 POSTMORTEM="$POSTMORTEM$(
180 echo
47749813 181 echo "Depinstall of $MODLIST under $HARNESS_OPTIONS parallel testing $errlog"
a9fc70ee 182 )"
579472df 183
184 HARNESS_OPTIONS=""
47749813 185 run_or_err "Retrying same $# modules without parallel testing" "_dep_inst_with_test $MODLIST"
579472df 186 fi
187
188 INSTALLDEPS_OUT="${INSTALLDEPS_OUT}${LASTOUT}"
189}
190
e6b373aa 191_dep_inst_with_test() {
192 if [[ "$DEVREL_DEPS" == "true" ]] ; then
193 # --dev is already part of CPANM_OPT
47749813 194 LASTCMD="$TIMEOUT_CMD cpanm $@"
195 $LASTCMD 2>&1
e6b373aa 196 else
47749813 197 LASTCMD="$TIMEOUT_CMD cpan $@"
198 $LASTCMD 2>&1
579472df 199
e6b373aa 200 # older perls do not have a CPAN which can exit with error on failed install
201 for m in "$@"; do
202 if ! perl -e '
579472df 203
647da28e 204my $mod = (
579472df 205 $ARGV[0] =~ m{ \/ .*? ([^\/]+) $ }x
206 ? do { my @p = split (/\-/, $1); pop @p; join "::", @p }
207 : $ARGV[0]
647da28e 208);
209
210$mod = q{List::Util} if $mod eq q{Scalar::List::Utils};
211
212eval qq{require($mod)} or ( print $@ and exit 1)
579472df 213
e6b373aa 214 ' "$m" 2> /dev/null ; then
215 echo -e "$m installation seems to have failed"
216 return 1
217 fi
218 done
219 fi
579472df 220}
221
b58ecb01 222CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }
f207111d 223
224CPAN_supports_BUILDPL() { perl -MCPAN\ 1.9205 -e1 &>/dev/null; }