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