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