]> git.evergreen-ils.org Git - working/random.git/blob - installer/jessie/eg_jessie_installer.sh
multi-host support for live tester
[working/random.git] / installer / jessie / eg_jessie_installer.sh
1 #!/bin/bash
2 # -----------------------------------------------------------------------
3 # Copyright (C) 2009-2012  Equinox Software Inc.
4 # Bill Erickson <berick@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # -----------------------------------------------------------------------
16
17 while getopts ayst option
18 do
19         case "${option}"
20         in
21                 a) AUTOSTART=1;;
22                 y) YES=1;;
23                 s) SAMPLEDATA=--load-all-sample;;
24                 t) LIVETEST=1;;
25         esac
26 done
27
28 function my_init {
29     date
30     init_variables
31     configure_cpan
32     build_essentials
33     setting_up_opensrf_env
34     cloning_git_repos
35     opensrf_prereqs
36     evergreen_prereqs
37     evergreen_db_prereqs
38     setting_ldconfig_and_rsyslog_and_hosts_and_ejabberd
39     build_opensrf
40     test_opensrf_build
41     install_opensrf
42     build_evergreen
43     test_evergreen_build
44     test_and_build_eg_browser_client
45     install_evergreen
46     configure_database
47     configure_apache
48     if [ $AUTOSTART ]; then
49         start_evergreen
50         if [ $LIVETEST ]; then
51             test_evergreen_live
52         fi
53     fi
54 }
55
56 function init_variables {
57     echo _.-~= initializing installer
58     date
59     # -----------------------------------------------------------------------
60     # Handling passed arguments to the script
61     # -----------------------------------------------------------------------
62     export DOJO_VERSION='1.3.3';
63     export PATH=/openils/bin:$PATH
64     export LD_LIBRARY_PATH=/openils/lib:/usr/local/lib:/usr/local/lib/dbd:$LD_LIBRARY_PATH
65     export BASE_DIR=$PWD
66     echo AUTOSTART=${AUTOSTART}
67     echo YES=${YES}
68     echo SAMPLEDATA=${SAMPLEDATA}
69     echo LIVETEST=${LIVETEST}
70     echo DOJO_VERSION=${DOJO_VERSION}
71     echo PATH=${PATH}
72     echo LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
73     echo BASE_DIR=${BASE_DIR}
74     # -----------------------------------------------------------------------
75     # Change to suit...
76     # -----------------------------------------------------------------------
77     # If you change the jabber password, you will need to 
78     # edit opensrf_core.xml and srfsh.xml accordingly
79     export JABBER_PASSWORD='password'
80     export ADMIN_USER='admin';
81     export ADMIN_PASS='demo123';
82     # -----------------------------------------------------------------------
83     # Database variables
84     # -----------------------------------------------------------------------
85     export PGDATABASE=evergreen
86     export PGUSER=evergreen
87     export PGPASSWORD=evergreen
88     export PGHOST=localhost
89     # -----------------------------------------------------------------------
90     # You can override these like so:
91     #       EVERGREEN_BRANCH='master' ./eg_jessie_installer.sh
92     # -----------------------------------------------------------------------
93     OPENSRF_REPO='git://git.evergreen-ils.org/OpenSRF.git'
94     OPENSRF_BRANCH='master'
95     EVERGREEN_REPO='git://git.evergreen-ils.org/Evergreen.git'
96     EVERGREEN_BRANCH='master'
97     echo OPENSRF_REPO=${OPENSRF_REPO}
98     echo OPENSRF_BRANCH=${OPENSRF_BRANCH}
99     echo EVERGREEN_REPO=${EVERGREEN_REPO}
100     echo EVERGREEN_BRANCH=${EVERGREEN_BRANCH}
101     OPENSRF_PREREQ_TARGET=debian-jessie
102     EVERGREEN_PREREQ_TARGET=debian-jessie
103     EVERGREEN_DB_PREREQ_TARGET=postgres-server-debian-jessie
104     echo OPENSRF_PREREQ_TARGET=${OPENSRF_PREREQ_TARGET}
105     echo EVERGREEN_PREREQ_TARGET=${EVERGREEN_PREREQ_TARGET}
106     echo EVERGREEN_DB_PREREQ_TARGET=${EVERGREEN_DB_PREREQ_TARGET}
107
108     export NODEJS_VERSION='v6.10.1'
109     export NODEJS_BINDIR="node-$NODEJS_VERSION-linux-x64"
110     export NODEJS_TARBIN="$NODEJS_BINDIR.tar.xz"
111     export NODEJS_BINARY="https://nodejs.org/dist/$NODEJS_VERSION/$NODEJS_TARBIN"
112     echo "NODEJS_BINARY=$NODEJS_BINARY"
113     echo "NODEJS_VERSION=$NODEJS_VERSION"
114
115     echo End of intializing installer =~-._
116 }
117
118
119 function configure_cpan {
120     echo _.-~= configure CPAN
121     date
122     # -----------------------------------------------------------------------
123     # force CPAN to load by installing something that should already be installed
124     if [ $YES ]; then
125         yes | cpan Fcntl
126     else
127         cpan Fcntl
128     fi
129     echo Return Value = $?
130     # CPAN follow pre-reqs?
131     if [ ! "$(echo 'o conf prerequisites_policy' | cpan | grep follow)" ]; then
132     if [ $YES ]; then
133
134         echo "setting cpan prerequisites_policy to follow"
135         echo -e "o conf prerequisites_policy follow\\n o conf commit" | cpan
136
137     else
138
139         echo '
140
141 -----------------------------------------------------------------------
142 The install will go faster if CPAN is configured to automatically install 
143 prerequisites.  You can revert the action later with:
144
145 echo -e "o conf prerequisites_policy ask\n o conf commit" | cpan 
146 '
147         while true; do
148             echo -n 'Automatically install prereqs? [Y/n] ';
149             read X;
150             [ "$X" == 'n' -o "$X" == "N" ] && break;
151             if [ "$X" == 'y' -o "$X" == 'Y' ]; then
152                 echo -e "o conf prerequisites_policy follow\\n o conf commit" | cpan
153                 break;
154             fi;
155         done;
156     fi;
157     fi;
158     echo End of configure CPAN =~-._
159 }
160
161 function build_essentials {
162     echo _.-~= Installing some build essentials
163     date
164     # Install some essential tools
165     apt-get update \
166     && apt-get -yq dist-upgrade \
167     && apt-get -yq install build-essential automake git psmisc ntp rsyslog lsb-release unzip;
168     echo Return Value = $?
169
170     if [ $LIVETEST ]; then
171         cpan TAP::Parser::SourceHandler::pgTAP
172         echo Return Value = $?
173     fi;
174     echo End of Installing some build essentials  =~-._
175 }
176
177 function setting_up_opensrf_env {
178     echo _.-~= creating opensrf user and environment
179     date
180     # Create opensrf user and set up environment
181     if [ ! "$(grep ^opensrf: /etc/passwd)" ]; then
182         useradd -m -s /bin/bash opensrf
183         echo Return Value = $?
184         echo 'export PATH=/openils/bin:$PATH' >> /home/opensrf/.bashrc
185         echo 'export LD_LIBRARY_PATH=/openils/lib:/usr/local/lib:/usr/local/lib/dbd:$LD_LIBRARY_PATH' >> /home/opensrf/.bashrc
186     fi;
187     echo end of creating opensrf user and environment =~-._
188 }
189
190 function cloning_git_repos {
191     echo _.-~= cloning git repositories
192     date
193     OSRF_COMMAND="
194     cd /home/opensrf;
195     git clone --depth 1 --branch $OPENSRF_BRANCH $OPENSRF_REPO OpenSRF;
196     git clone --depth 1 --branch $EVERGREEN_BRANCH $EVERGREEN_REPO Evergreen;
197     "
198     rm -rf /home/opensrf/Evergreen /home/opensrf/OpenSRF
199     su - opensrf sh -c "$OSRF_COMMAND";
200     echo Return Value = $?
201
202     # Show tips
203     cd /home/opensrf/OpenSRF/
204     echo 'Tip of OpenSRF:' `git log --format=oneline | head -1`
205     cd /home/opensrf/Evergreen/
206     echo 'Tip of Evergreen:' `git log --format=oneline | head -1`
207
208     echo End of cloning git repositories =~-._
209 }
210
211 function opensrf_prereqs {
212     echo _.-~= Installing OpenSRF pre-requisites
213     date
214     # Install pre-reqs
215     mkdir -p /usr/src/evergreen; 
216     cd /usr/src/evergreen;
217     if [ $YES ]; then
218         yes | make -f /home/opensrf/OpenSRF/src/extras/Makefile.install ${OPENSRF_PREREQ_TARGET}
219     else
220         make -f /home/opensrf/OpenSRF/src/extras/Makefile.install ${OPENSRF_PREREQ_TARGET}
221     fi;
222     echo Return Value = $?
223     echo End of Installing OpenSRF pre-requisites =~-._
224 }
225
226 function evergreen_prereqs {
227     echo _.-~= Installing Evergreen pre-requisites
228     date
229     if [ $YES ]; then
230         yes | make -f /home/opensrf/Evergreen/Open-ILS/src/extras/Makefile.install ${EVERGREEN_PREREQ_TARGET}
231     else
232         make -f /home/opensrf/Evergreen/Open-ILS/src/extras/Makefile.install ${EVERGREEN_PREREQ_TARGET}
233     fi;
234     echo Return Value = $?
235     echo End of Installing Evergreen pre-requisites =~-._
236 }
237
238 function evergreen_db_prereqs {
239     echo _.-~= Installing Evergreen database pre-requisites
240     date
241     if [ $YES ]; then
242         yes | make -f /home/opensrf/Evergreen/Open-ILS/src/extras/Makefile.install ${EVERGREEN_DB_PREREQ_TARGET}
243     else
244         make -f /home/opensrf/Evergreen/Open-ILS/src/extras/Makefile.install ${EVERGREEN_DB_PREREQ_TARGET}
245     fi;
246     echo Return Value = $?
247     echo End of Installing Evergreen database pre-requisites =~-._
248 }
249
250 function setting_ldconfig_and_rsyslog_and_hosts_and_ejabberd {
251     echo _.-~= setting ld.so.conf and rsyslog and /etc/hosts and ejabberd
252     date
253     cp $BASE_DIR/evergreen.ld.conf /etc/ld.so.conf.d/
254     ldconfig;
255     echo Return Value = $?
256     # Configure rsyslog and restart
257     cp /home/opensrf/Evergreen/Open-ILS/examples/evergreen-rsyslog.conf /etc/rsyslog.d/evergreen.conf
258     /etc/init.d/rsyslog restart
259     echo Return Value = $?
260     if [ ! "$(grep 'public.localhost' /etc/hosts)" ]; then
261
262         if [ $YES ]; then
263             echo 'Adding public.localhost and private.localhost to /etc/hosts'
264             echo '127.0.1.2    public.localhost     public' >> /etc/hosts
265             echo '127.0.1.3    private.localhost    private' >> /etc/hosts
266         else
267
268         cat <<EOF
269
270     * Add these lines to /etc/hosts.
271
272     127.0.1.2   public.localhost    public
273     127.0.1.3   private.localhost   private
274
275 EOF
276         fi;
277
278     else
279         echo "INFO: /etc/hosts already has public.localhost line";
280     fi
281     echo Return Value = $?
282     # Patch Ejabberd and register users
283     if [ ! "$(grep 'public.localhost' /etc/ejabberd/ejabberd.yml)" ]; then
284         cd /etc/ejabberd/
285         /etc/init.d/ejabberd stop;
286         killall beam epmd; # just in case
287         cp ejabberd.yml /root/ejabberd.yml.orig
288         patch -p0 < $BASE_DIR/ejabberd.EG.patch
289         chown ejabberd:ejabberd ejabberd.yml
290         echo starting ejabberd after patching
291         date
292         /etc/init.d/ejabberd start
293         echo Return Value = $?
294         sleep 10;
295         ejabberdctl register router  private.localhost $JABBER_PASSWORD
296         echo Return Value = $?
297         ejabberdctl register opensrf private.localhost $JABBER_PASSWORD
298         echo Return Value = $?
299         ejabberdctl register router  public.localhost  $JABBER_PASSWORD
300         echo Return Value = $?
301         ejabberdctl register opensrf public.localhost  $JABBER_PASSWORD
302         echo Return Value = $?
303     fi;
304     echo End of setting ld.so.conf and rsyslog and /etc/hosts and ejabberd =~-._
305 }
306
307 function build_opensrf {
308     # Build and install OpenSRF
309     echo _.-~= Building OpenSRF
310     date
311     OSRF_COMMAND='
312     cd /home/opensrf/OpenSRF;
313     autoreconf -i;
314     ./configure --prefix=/openils --sysconfdir=/openils/conf;
315     make;'
316     su - opensrf sh -c "$OSRF_COMMAND"
317     echo Return Value = $?
318     echo End of Building OpenSRF =~-._
319 }
320
321 function test_opensrf_build {
322     echo _.-~= Running OpenSRF build tests
323     date
324     cd /home/opensrf/OpenSRF;
325     make check
326     echo Return Value = $?
327     echo End of OpenSRF build tests =~-._
328 }
329
330 function install_opensrf {
331     echo _.-~= Installing OpenSRF
332     date
333     make install
334     echo Return Value = $?
335     echo End of Installing OpenSRF =~-._
336 }
337
338 function build_evergreen {
339     echo _.-~= Building Evergreen
340     date
341     OSRF_COMMAND='
342     export PATH=/openils/bin:$PATH
343     cd /home/opensrf/Evergreen;
344     autoreconf -i;
345     ./configure --prefix=/openils --sysconfdir=/openils/conf;
346     make;
347     '
348     su - opensrf sh -c "$OSRF_COMMAND"
349     echo Return Value = $?
350     echo End of Building Evergreen =~-._
351 }
352
353 function test_evergreen_build {
354     echo _.-~= Running Evergreen build tests
355     date
356     cd /home/opensrf/Evergreen
357     make check
358     echo Return Value = $?
359     echo End of Evergreen build tests =~-._
360 }
361
362
363 # The evergreen browser client prereq installation and 
364 # build process is not baked into Evergreen proper (yet).  
365 # Run it as a standalone operation for now.
366 function test_and_build_eg_browser_client {
367     echo _.-~= Running Evergreen browser client build/test
368     cd /home/opensrf
369     wget -N $NODEJS_BINARY
370     tar xf $NODEJS_TARBIN
371     echo Return Value = $?
372     export PATH=$PATH:/home/opensrf/$NODEJS_BINDIR/bin
373     npm update
374     echo Return Value = $?
375     npm install -g grunt-cli    # install grunt
376     echo Return Value = $?
377     cd /home/opensrf/Evergreen/Open-ILS/web/js/ui/default/staff
378     npm install                 # fetch build depencies
379     echo Return Value = $?
380     grunt build                 # concatentate, minify, copy into place
381     echo Return Value = $?
382     grunt test                  # run JS unit tests
383     echo Return Value = $?
384     echo End of Evergreen browser client build/test =~-._
385 }
386
387 function install_evergreen {
388     echo _.-~= Installing Evergreen
389     date
390     cd /home/opensrf/Evergreen
391     make install
392     echo Return Value = $?
393
394     cp /openils/conf/opensrf.xml.example      /openils/conf/opensrf.xml
395     cp /openils/conf/opensrf_core.xml.example /openils/conf/opensrf_core.xml
396
397     # fetch and install Dojo
398     cd /tmp;
399     wget -N "http://download.dojotoolkit.org/release-$DOJO_VERSION/dojo-release-$DOJO_VERSION.tar.gz" \
400     || wget -N "http://evergreen-ils.org/~phasefx/download.dojotoolkit.org/dojo-release-$DOJO_VERSION.tar.gz"
401     tar -zxf dojo-release-$DOJO_VERSION.tar.gz;
402     cp -r dojo-release-$DOJO_VERSION/* /openils/var/web/js/dojo/;
403
404     # give it all to opensrf
405     chown -R opensrf:opensrf /openils
406
407     # copy srfsh config into place
408     cp /openils/conf/srfsh.xml.example /home/opensrf/.srfsh.xml;
409     chown opensrf:opensrf /home/opensrf/.srfsh.xml;
410     echo End of Installing Evergreen =~-._
411 }
412
413 function configure_database {
414     echo _.-~= configure database
415     date
416     if [ $YES ]; then
417         echo "Using password $PGPASSWORD for the $PGUSER database user."
418         echo -e "$PGPASSWORD\n$PGPASSWORD\n" | su - postgres sh -c "createuser -P -s $PGUSER;"
419     else
420         echo -e "\n\nPlease enter a password for the $PGUSER database user.\n  If you do not want to edit configs, use \"$PGPASSWORD\"\n"
421         su - postgres sh -c "createuser -P -s $PGUSER;"
422     fi;
423
424     # Apply the DB schema
425     cd /home/opensrf/Evergreen
426     perl Open-ILS/src/support-scripts/eg_db_config \
427         --create-database       \
428         --create-schema         \
429         --create-offline        \
430         --update-config $SAMPLEDATA \
431         --service all           \
432         --user $PGUSER          \
433         --password $PGPASSWORD  \
434         --hostname $PGHOST      \
435         --database $PGDATABASE  \
436         --admin-user $ADMIN_USER \
437         --admin-pass $ADMIN_PASS;
438     echo Return Value = $?
439
440     if [ $LIVETEST ]; then
441         PG_TAP_VER="0.97.0"
442         wget -N http://api.pgxn.org/dist/pgtap/$PG_TAP_VER/pgtap-$PG_TAP_VER.zip \
443         && unzip pgtap-$PG_TAP_VER.zip \
444         && cd pgtap-$PG_TAP_VER \
445         && make \
446         && make installcheck \
447         && make install
448         echo 'CREATE EXTENSION pgtap;' | su - postgres -c "psql evergreen"
449     fi;
450     echo configure database =~-._
451 }
452
453 function configure_apache {
454     echo _.-~= configure apache
455     date
456     /etc/init.d/apache2 stop
457     # Copy apache configs into place and create SSL cert
458     cd /home/opensrf/Evergreen/
459     cp Open-ILS/examples/apache_24/eg_24.conf       /etc/apache2/sites-available/eg.conf
460     cp Open-ILS/examples/apache_24/eg_vhost_24.conf /etc/apache2/eg_vhost.conf
461     cp Open-ILS/examples/apache/eg_startup    /etc/apache2/
462
463     mkdir -p /etc/apache2/ssl;
464     if [ ! -f /etc/apache2/ssl/server.key ] ; then
465         echo -e "\n\nConfiguring a new temporary SSL certificate....\n";
466         if [ $YES ]; then
467            yes "" | openssl req -new -x509 -days 365 -nodes -out /etc/apache2/ssl/server.crt -keyout /etc/apache2/ssl/server.key
468         else
469            openssl req -new -x509 -days 365 -nodes -out /etc/apache2/ssl/server.crt -keyout /etc/apache2/ssl/server.key
470         fi;
471     else
472         echo -e "\nkeeping existing ssl/server.key file\n";
473     fi
474
475     a2enmod ssl  
476     echo Return Value = $?
477     a2enmod rewrite
478     echo Return Value = $?
479     a2enmod expires 
480     echo Return Value = $?
481     a2dissite 000-default
482     echo Return Value = $?
483     a2ensite eg.conf
484     echo Return Value = $?
485
486     echo Modifying APACHE_RUN_USER/APACHE_RUN_GROUP in /etc/apache2/envvars
487     sed -i 's/www-data/opensrf/g' /etc/apache2/envvars
488
489     echo Making sure /var/lock/apache2 is owned by opensrf
490     chown opensrf:opensrf /var/lock/apache2
491
492     echo Modifying KeepAliveTimeout in /etc/apache2/apache2.conf
493     sed -i 's/KeepAliveTimeout .*/KeepAliveTimeout 1/' /etc/apache2/apache2.conf
494
495     echo End of configure apache =~-._
496 }
497
498 function start_evergreen {
499     echo _.-~= Starting Evergreen
500     date
501     if [ $LIVETEST ]; then
502         rm /openils/var/log/*.log
503     fi
504
505     OSRF_COMMAND='
506     export LD_LIBRARY_PATH=/openils/lib:/usr/local/lib:/usr/local/lib/dbd:$LD_LIBRARY_PATH \
507     && export PATH=/openils/bin:$PATH \
508     && OSRF_HOSTNAME="dns_hack" /openils/bin/osrf_control --localhost --restart-all  && sleep 3 \
509     && /openils/bin/autogen.sh /openils/conf/opensrf_core.xml \
510     && echo Finis;
511     '
512     echo Starting services...
513     su - opensrf sh -c "$OSRF_COMMAND";
514     echo Return Value = $?
515
516     echo Restarting Apache
517     /etc/init.d/apache2 restart
518     echo Return Value = $?
519
520     echo End of Starting Evergreen =~-._
521 }
522
523 function test_evergreen_live {
524     # TODO: Eventually move these tests into a Make target within Evergreen
525     cd /home/opensrf/Evergreen
526     echo _.-~= Running pgTAP tests
527     date
528     su - postgres -c 'cd /home/opensrf/Evergreen ; pg_prove -vr -d evergreen Open-ILS/src/sql/Pg/t/ ; echo Return Value = $?'
529     echo End of pgTAP tests =~-._
530     echo _.-~= Running pgTAP live tests
531     date
532     su - postgres -c 'cd /home/opensrf/Evergreen ; pg_prove -vr -d evergreen Open-ILS/src/sql/Pg/live_t/ ; echo Return Value = $?'
533     echo End of pgTAP live tests =~-._
534     echo _.-~= Running settings-tester.pl
535     date
536     su - opensrf sh -c 'export PATH=/openils/bin:$PATH ; cd /home/opensrf/Evergreen/Open-ILS/src/support-scripts/ ; ./settings-tester.pl ; echo Return Value = $?'
537     echo End of settings-tester.pl output =~-._
538     echo _.-~= Running perl live tests
539     date
540     su - opensrf sh -c 'export PATH=/openils/bin:$PATH ; cd /home/opensrf/Evergreen/Open-ILS/src/perlmods/ ; make livecheck; echo Return Value = $?'
541     echo End of perl live tests =~-._
542     echo _.-~= Gathering log summary
543     date
544     echo ''
545     echo 'wc -l *.log:'
546     su - opensrf sh -c 'cd /openils/var/log/ ; wc -l *.log'
547     echo ''
548     echo 'du -sh *.log:'
549     su - opensrf sh -c 'cd /openils/var/log/ ; du -sh *.log'
550     echo ''
551     echo 'perl -ne ''if (/^\[.*?\] (.*?) \[/) { print "$1\n"; }'' osrfsys.log | sort | uniq -c | sort -k2:'
552     (cd /openils/var/log/ ; perl -ne 'if (/^\[.*?\] (.*?) \[/) { print "$1\n"; }' osrfsys.log | sort | uniq -c | sort -k2)
553     echo ''
554     echo End of log summary =~-._
555     cd /openils/var/log/
556     for x in *.log; do
557         echo _.-~= Log Output: $x
558         date
559         cat $x
560         echo End of $x =~-._
561     done
562     echo _.-~= Gathering system information
563     echo ''
564     date
565     echo ''
566     uname -a
567     echo 'select version();' | su - postgres -c 'psql -At'
568     echo ''
569     echo '/proc/meminfo:'
570     cat /proc/meminfo
571     echo ''
572     echo '/proc/cpuinfo:'
573     cat /proc/cpuinfo
574     echo ''
575     echo 'dpkg --list:'
576     dpkg --list
577     echo ''
578     echo 'cpan -l:'
579     cpan -l 2> /dev/null | sort
580     echo End of system information =~-._
581     date
582 }
583
584 my_init
585
586