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