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