6dea9a78a8c2a345bc10d8f5f681232bf5347fc5
[dbsrgits/DBIx-Class.git] / maint / travis-ci_prepare_env
1 #!/bin/bash
2
3 # * The way .travis.yml is fed to the command controller is idiotic - it
4 # makes using multiline `bash -c` statements impossible. Therefore to
5 # aid readability (our travis logic is rather complex), the bulk of
6 # functionality is moved to a script. More about the problem here:
7 # https://github.com/travis-ci/travis-ci/issues/497
8
9 set -e
10
11 echo_err() { echo "$@" 1>&2 ; }
12
13 tstamp() { echo -n "[$(date '+%H:%M:%S')]" ; }
14
15 run_or_err() {
16   echo_err -n "$(tstamp) $1 ... "
17
18   LASTEXIT=0
19   LASTOUT=$( bash -c "$2" 2>&1 ) || LASTEXIT=$?
20
21   if [[ "$LASTEXIT" != "0" ]] ; then
22     echo_err -e "FAILED !!!\nCommand executed:\n$2\nSTDOUT+STDERR:\n$LASTOUT"
23     return $LASTEXIT
24   else
25     echo_err "done."
26   fi
27 }
28
29 if [[ "$TRAVIS" != "true" ]] ; then
30   echo_err "Running this script makes no sense outside of travis-ci"
31   exit 1
32 fi
33
34 #
35 # Current dir is the root of the DBIC checkout (make sure to
36 # come back there if moving around before end of this script)
37 #
38 # envvars available for us:
39 #
40 # NUMTHREADS = { number}
41 #   dynamically determined amount of threads we want to run on this
42 #   smoker concurrently
43 #
44 # CLEANTEST = [ true | false ]
45 #   controls whether we simulate a "user-side" install experience
46 #   that is - no author deps and no M::I installation
47 #
48 # BREWVER = { tripple dotted perl version, e.g. 5.8.3 }
49 #   brew a custom perl version such and such
50 #
51 # BREWOPTS = { string to be fed unquoted to perlbrew, e.g. -Duseithreads }
52 #   build options for perlbrew
53 #
54
55 echo_err "$(tstamp) Smoke preconfiguration starting $(date)"
56
57 TRAVIS_CPAN_MIRROR=$(echo "$PERL_CPANM_OPT" | grep -oP -- '--mirror\s+\S+' | head -n 1 | cut -d ' ' -f 2)
58 if ! [[ "$TRAVIS_CPAN_MIRROR" =~ "http://" ]] ; then
59   echo_err "Unable to extract primary cpan mirror from PERL_CPANM_OPT - something is wrong"
60   echo_err "PERL_CPANM_OPT: $PERL_CPANM_OPT"
61   exit 1
62 fi
63
64 export PERL_MM_USE_DEFAULT=1 PERL_MM_NONINTERACTIVE=1 PERL_AUTOINSTALL_PREFER_CPAN=1 PERLBREW_CPAN_MIRROR="$TRAVIS_CPAN_MIRROR"
65
66 # Fixup CPANM_OPT to behave more like a traditional cpan client
67 export PERL_CPANM_OPT="$( echo $PERL_CPANM_OPT | sed 's/--skip-satisfied//' )"
68
69 if [[ -n "$BREWVER" ]] ; then
70   # .travis.yml already restricts branches to master, topic/* and smoke/*
71   # do some extra short-circuiting here
72
73   # when smoking master do not attempt bleadperl (not release-critical)
74   if [[ "$TRAVIS_BRANCH" = "master" ]] && [[ "$BREWVER" = "blead" ]]; then
75     export SHORT_CIRCUIT_SMOKE=1
76   # on topic/ branches test only with travis perls
77   elif [[ "$TRAVIS_BRANCH" =~ "topic/" ]]; then
78     export SHORT_CIRCUIT_SMOKE=1
79   fi
80
81   if [[ -n "$SHORT_CIRCUIT_SMOKE" ]]; then
82     echo_err "$(tstamp) non-smoke branch and custom perl compilation requested - bailing out"
83     sleep 20  # give the console time to attach, otherwise it hangs
84     return  # this is like an `exit 0` in sourcing
85   fi
86
87   run_or_err "Compiling/installing Perl $BREWVER (without testing, may take up to 5 minutes)" \
88     "perlbrew install --as $BREWVER --notest --verbose $BREWOPTS -j $NUMTHREADS $BREWVER"
89
90   sync
91   perlbrew use $BREWVER
92 fi
93
94 # configure CPAN.pm - older versions go into an endless loop
95 # when trying to autoconf themselves
96 CPAN_CFG_SCRIPT="
97   require CPAN;
98   require CPAN::FirstTime;
99   *CPAN::FirstTime::conf_sites = sub {};
100   CPAN::Config->load;
101   \$CPAN::Config->{urllist} = [qw{ $TRAVIS_CPAN_MIRROR }];
102   \$CPAN::Config->{halt_on_failure} = 1;
103   CPAN::Config->commit;
104 "
105 run_or_err "Configuring CPAN.pm" "perl -e '$CPAN_CFG_SCRIPT'"
106
107 extract_prereqs() {
108   # hack-hack-hack
109   LASTEXIT=0
110   COMBINED_OUT="$( { stdout="$(cpanm --quiet --scandeps --format tree "$@")" ; } 2>&1; echo "!!!STDERRSTDOUTSEPARATOR!!!$stdout")" \
111     || LASTEXIT=$?
112
113   OUT=${COMBINED_OUT#*!!!STDERRSTDOUTSEPARATOR!!!}
114   ERR=$(grep -v " is up to date." <<< "${COMBINED_OUT%!!!STDERRSTDOUTSEPARATOR!!!*}")
115
116   if [[ "$LASTEXIT" != "0" ]] || [[ -n "$ERR" ]] ; then
117     echo_err "$(echo -e "Error occured (exit code $LASTEXIT) retrieving dependencies of $@:\n$ERR\n$OUT")"
118     exit 1
119   fi
120
121   # throw away non-children (what was in $@), throw away ascii art, convert to modnames
122   perl -p -e 's/^[a-z].+//i; s/^[^a-z]+//i; s/\-[^\-]+$/ /; s/\-/::/g' <<< "$OUT"
123 }
124
125 parallel_installdeps_notest() {
126   if [[ -z "$@" ]] ; then return; fi
127
128   # flatten list into one string
129   MODLIST=$(echo "$@")
130
131   # The reason we do things so "non-interactively" is that xargs -P will have the
132   # latest cpanm instance overwrite the buildlog. There seems to be no way to
133   # specify a custom buildlog, hence we just collect the verbose output
134   # and display it in case of failure
135   run_or_err "Installing (without testing) $MODLIST" \
136     "echo $MODLIST | xargs -n 1 -P $NUMTHREADS cpanm --verbose --no-interactive --notest --no-man-pages"
137 }
138
139 if [[ "$CLEANTEST" = "true" ]]; then
140   # get the last inc/ off cpan - we will get rid of MI
141   # soon enough, but till then this will do
142   # the point is to have a *really* clean perl (the ones
143   # we build are guaranteed to be clean, without side
144   # effects from travis preinstalls)
145
146   # trick cpanm into executing true as shell - we just need the find+unpack
147   run_or_err "Downloading DBIC inc/ from CPAN" \
148     "SHELL=/bin/true cpanm --look DBIx::Class"
149
150   mv ~/.cpanm/latest-build/DBIx-Class-*/inc .
151 else
152   # we will be running all dbic tests - preinstall lots of stuff, run basic tests
153   # using SQLT and set up whatever databases necessary
154
155   # do the preinstall in several passes to minimize amount of cross-deps installing
156   # multiple times, and to avoid module re-architecture breaking another install
157   # (e.g. once Carp is upgraded there's no more Carp::Heavy)
158   #
159   parallel_installdeps_notest ExtUtils::MakeMaker
160   parallel_installdeps_notest Carp
161   parallel_installdeps_notest Module::Build
162   parallel_installdeps_notest Module::Runtime ExtUtils::Depends File::Spec
163   parallel_installdeps_notest Test::Exception Data::Dumper LWP
164   parallel_installdeps_notest Test::Fatal Test::Warn bareword::filehandles
165   parallel_installdeps_notest namespace::clean Class::XSAccessor MRO::Compat
166   parallel_installdeps_notest DBD::SQLite Moo Class::Accessor::Grouped
167   parallel_installdeps_notest Module::Install DateTime::Format::Strptime
168   parallel_installdeps_notest JSON::DWIW JSON JSON::XS Test::Pod::Coverage Test::EOL
169   parallel_installdeps_notest MooseX::Types JSON::Any Class::DBI
170
171   export DBICTEST_SQLT_DEPLOY=1
172
173 ### apt-get invocation - faster to grab everything at once
174   #
175   # FIXME these debconf lines should automate the firebird config but do not :(((
176   sudo bash -c 'echo -e "firebird2.5-super\tshared/firebird/enabled\tboolean\ttrue" | debconf-set-selections'
177   sudo bash -c 'echo -e "firebird2.5-super\tshared/firebird/sysdba_password/new_password\tpassword\t123" | debconf-set-selections'
178
179   APT_PACKAGES="memcached firebird2.5-super firebird2.5-dev expect"
180   run_or_err "Installing packages ($APT_PACKAGES)" "sudo apt-get install -y $APT_PACKAGES"
181
182 ### memcached
183   export DBICTEST_MEMCACHED=127.0.0.1:11211
184
185 ### mysql
186   run_or_err "Creating MySQL TestDB" "mysql -e 'create database dbic_test;'"
187   export DBICTEST_MYSQL_DSN='dbi:mysql:database=dbic_test;host=127.0.0.1'
188   export DBICTEST_MYSQL_USER=root
189
190 ### pg
191   run_or_err "Creating PostgreSQL TestDB" "psql -c 'create database dbic_test;' -U postgres"
192   export DBICTEST_PG_DSN='dbi:Pg:database=dbic_test;host=127.0.0.1'
193   export DBICTEST_PG_USER=postgres
194
195 ### firebird
196   # poor man's deb config
197   EXPECT_FB_SCRIPT='
198     spawn dpkg-reconfigure --frontend=text firebird2.5-super
199     expect "Enable Firebird server?"
200     send "\177\177\177\177yes\r"
201     expect "Password for SYSDBA"
202     send "123\r"
203     sleep 1
204     wait
205     sleep 1
206   '
207   run_or_err "Re-configuring Firebird" "
208     sync
209     DEBIAN_FRONTEND=text sudo expect -c '$EXPECT_FB_SCRIPT'
210     sleep 1
211     sync
212     # restart the server for good measure
213     sudo /etc/init.d/firebird2.5-super stop || true
214     sleep 1
215     sync
216     sudo /etc/init.d/firebird2.5-super start
217     sleep 1
218     sync
219   "
220
221   # creating testdb
222   # FIXME - this step still fails from time to time >:(((
223   # has to do with the FB reconfiguration I suppose
224   # for now if it fails - simply skip FB testing
225   if run_or_err "Creating Firebird TestDB" \
226     "echo \"CREATE DATABASE '/var/lib/firebird/2.5/data/dbic_test.fdb';\" | sudo isql-fb -u sysdba -p 123"
227   then
228     # the official version is full of 5.10-isms, but works perfectly fine on 5.8
229     # pull in our patched copy
230     run_or_err "Fetching patched DBD::Firebird" \
231       "git clone https://github.com/dbsrgits/perl-dbd-firebird-5.8.git ~/dbd-firebird"
232
233     # the official version is very much outdated and does not compile on 5.14+
234     # use this rather updated source tree (needs to go to PAUSE):
235     # https://github.com/pilcrow/perl-dbd-interbase
236     run_or_err "Fetching patched DBD::InterBase" \
237       "git clone https://github.com/dbsrgits/perl-dbd-interbase ~/dbd-interbase"
238
239     parallel_installdeps_notest ~/dbd-interbase/ ~/dbd-firebird/
240
241     export DBICTEST_FIREBIRD_DSN=dbi:Firebird:dbname=/var/lib/firebird/2.5/data/dbic_test.fdb
242     export DBICTEST_FIREBIRD_USER=SYSDBA
243     export DBICTEST_FIREBIRD_PASS=123
244
245     export DBICTEST_FIREBIRD_INTERBASE_DSN=dbi:InterBase:dbname=/var/lib/firebird/2.5/data/dbic_test.fdb
246     export DBICTEST_FIREBIRD_INTERBASE_USER=SYSDBA
247     export DBICTEST_FIREBIRD_INTERBASE_PASS=123
248   fi
249
250 ### oracle
251   # FIXME: todo
252   #DBICTEST_ORA_DSN=dbi:Oracle:host=localhost;sid=XE
253   #DBICTEST_ORA_USER=dbic_test
254   #DBICTEST_ORA_PASS=123
255   #DBICTEST_ORA_EXTRAUSER_DSN=dbi:Oracle:host=localhost;sid=XE
256   #DBICTEST_ORA_EXTRAUSER_USER=dbic_test_extra
257   #DBICTEST_ORA_EXTRAUSER_PASS=123
258   #ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/client
259 fi
260
261 CPAN_is_sane() { perl -MCPAN\ 1.94_56 -e 1 &>/dev/null ; }
262
263 # install the rest
264 if [[ "$CLEANTEST" = "true" ]]; then
265   # older perls do not have a CPAN which understands configure_requires
266   # properly and what is worse a `cpan Foo` run exits with 0 even if some
267   # modules failed to install
268   # The first CPAN which is somewhat sane is around 1.94_56 (perl 5.12)
269   # The problem is that the first sane version also brings a *lot* of
270   # deps with it, notably things like YAML and HTTP::Tiny
271   # The goal of CLEANTEST is to have as little extra stuff installed as
272   # possible, mainly to catch "but X is perl core" mistakes
273   # So instead we still use our stock (possibly old) CPAN, and add some
274   # handholding
275
276   CPAN_is_sane || \
277     run_or_err "Pre-installing ExtUtils::MakeMaker and Module::Build" \
278       "cpan ExtUtils::MakeMaker Module::Build"
279
280   if ! perl -MModule::Build -e 1 &> /dev/null ; then
281     echo_err -e "Module::Build installation failed\n$LASTOUT"
282     exit 1
283   fi
284
285   # DBI has by far the longest test runtime - run less tests
286   # FIXME horrible horrible hack, need to implement in DBI itself
287   run_or_err "Downloading latest DBI distdir from CPAN" \
288     "SHELL=/bin/true cpanm --look DBI"
289   cd ~/.cpanm/latest-build/DBI-*/
290   perl -p -i -e 's/(create_.+?_tests) => 1/$1 => 0/' Makefile.PL
291   run_or_err "Pre-installing DBI, but running less tests" "perl Makefile.PL && make && make test && make install"
292   cd - &>/dev/null
293
294   # generate the makefile which will have different deps depending on
295   # the runmode and envvars set above
296   run_or_err "Configure on current branch" "perl Makefile.PL"
297   HARD_DEPS="$(echo $(make listdeps))"
298
299   # this is a fucked CPAN - won't understand configure_requires of
300   # various pieces we may run into
301   CPAN_is_sane || HARD_DEPS="ExtUtils::Depends B::Hooks::OP::Check $HARD_DEPS"
302
303 ##### TEMPORARY WORKAROUNDS
304
305   # List::Util got broken rather badly (RT#80646)
306   # install without tests for the time being
307   perl -M5.014 -e 1 &>/dev/null || \
308     parallel_installdeps_notest List::Util
309
310   # not sure what's going on here yet
311   perl -M5.008008 -e 1 &> /dev/null || \
312     parallel_installdeps_notest multidimensional bareword::filehandles
313
314   # work around Params::Validate not having a Makefile.PL so really old
315   # toolchains can not figure out what the prereqs are ;(
316   # Need to do more research before filing a bug requesting Makefile inclusion
317   perl -M5.008008 -e 1 &> /dev/null || \
318     HARD_DEPS="$(extract_prereqs Params::Validate) $HARD_DEPS"
319
320 ##### END TEMPORARY WORKAROUNDS
321
322   run_or_err "Installing/testing dependencies (may take up to 10 minutes): $HARD_DEPS" "cpan $HARD_DEPS"
323
324   # this is a fucked CPAN - save the log as we may need it
325   CPAN_is_sane || INSTALLDEPS_OUT="$LASTOUT"
326
327 else
328   # generate the makefile which will have different deps depending on
329   # the runmode and envvars set above
330   run_or_err "Configure on current branch" "perl Makefile.PL"
331
332   # listalldeps is deliberate - will upgrade everything it can find
333   parallel_installdeps_notest $(make listalldeps)
334 fi
335
336 echo_err "$(tstamp) Module configuration finished"
337 # this will display list of available versions
338 perl Makefile.PL
339
340 # make sure we got everything we need
341 if [[ -n "$(make listdeps)" ]] ; then
342   echo_err "$(tstamp) Not all deps installed - something went wrong :("
343   sleep 1 # without this the echo below confuses the console listener >.<
344   CPAN_is_sane || echo_err -e "Outdated CPAN.pm used - full logs follows\n$INSTALLDEPS_OUT\n\nSearch for 'NOT OK' in the text above\n\nDeps still missing:"
345   sleep 3 # without this the above echo confuses the console listener >.<
346   make listdeps
347   exit 1
348 fi
349
350 run_or_err "Prepare blib" "make pure_all"
351
352 # announce what are we running
353 echo_err "
354 ========================= CONFIGURATION COMPLETE ===========================
355 $(tstamp) Configuration phase seems to have taken $(date -ud "@$SECONDS" '+%H:%M:%S') (@$SECONDS)
356
357 = CPUinfo
358 $(perl -0777 -p -e 's/.+\n\n(?!\z)//s' < /proc/cpuinfo)
359
360 = Meminfo
361 $(free -m -t)
362
363 = Environment
364 $(env | grep -P 'TEST|TRAVIS|PERL|DBIC' | LC_ALL=C sort | cat -v)
365
366 = Perl in use
367 $(perl -V)
368 ============================================================================
369
370 $(tstamp) Starting tests using $NUMTHREADS concurrent processes"