install-eg.sh: install script for generating popularity badge scores
[sitka/sitka-tools.git] / deployment / install-osrf.sh
1 #!/bin/bash
2 DEFAULT_USER="sitkastaff"
3 HOMEDIR="/home/${DEFAULT_USER}"
4 BUILDDIR="${HOMEDIR}/opensrf"
5 INSTALLDIR="/srv/openils"
6 BRANCH="rel_2_5"
7 HOSTNAME="nonprod"
8 SITKACONF="${HOMEDIR}/sysadmin"
9 SITKACONF_BRANCH="master"
10
11 THISHOST=`hostname -s`
12
13 # test for ejabberd passwords
14 if [ -z "$PRIVATE_ROUTER_PASSWD" -o -z "$PRIVATE_OPENSRF_PASSWD" -o -z "$PUBLIC_ROUTER_PASSWD" -o -z "$PUBLIC_OPENSRF_PASSWD" ]
15 then
16   echo "Missing ejabberd password(s), no OpenSRF install!"
17   exit
18 fi
19
20 while getopts ":pw" opt ; do
21   case $opt in
22     p ) PROD="prod";;
23     w ) WEBSOCKETS="websockets";;
24   esac
25 done
26 shift $(($OPTIND - 1))
27
28 # always use a proxy for websockets traffic
29 if [[ -n "$WEBSOCKETS" ]] ; then
30     WSPORT="--with-websockets-port=443"
31 else
32     WSPORT=""
33 fi
34
35 if [[ -n "$PROD" ]] ; then
36   echo 'this is a production install'
37 else
38   echo 'this is NOT a production install'
39 fi
40 # as root
41 usermod -a -G sudo ${DEFAULT_USER}
42 apt-get install git-core build-essential translate-toolkit python-polib bsdmainutils
43  
44 # as DEFAULT_USER
45 if [ ! -d "${SITKACONF}" ]
46 then
47   # this will fail unless DEFAULT_USER has read access to the repo
48   su -c "cd ${HOMEDIR} && git clone git+ssh://git@git.sitka.bclibraries.ca/sitka-infra/sysadmin" ${DEFAULT_USER}
49 fi
50 su -c "cd ${HOMEDIR}/sysadmin && git fetch origin && git checkout ${SITKACONF_BRANCH} && git pull" ${DEFAULT_USER}
51 if [ ! -d "${BUILDDIR}/.git" ] ; then
52   su -c "cd ${HOMEDIR} && git clone git://git.evergreen-ils.org/OpenSRF.git opensrf" ${DEFAULT_USER}
53 fi
54 su -c "cd ${BUILDDIR} && git fetch origin && git checkout ${BRANCH} && git pull" ${DEFAULT_USER}
55  
56 # as root
57 echo "need a placeholder apache2.conf, installing from config repo..."
58 if [ -f '/etc/apache2/apache2.conf' ]
59 then
60   mv /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak
61 fi
62 cp ${SITKACONF}/files/etc/apache2/apache2.conf/${HOSTNAME} /etc/apache2/apache2.conf
63 sed -i "s|__HOSTNAME__|${THISHOST}|g" /etc/apache2/apache2.conf
64
65 # as root
66 echo "Installing OpenSRF prerequisites for Ubuntu..."
67 cd ${BUILDDIR} && make -f src/extras/Makefile.install ubuntu-trusty
68  
69 # as DEFAULT_USER
70 echo "Configuring and building OpenSRF..."
71 su -c "cd ${BUILDDIR} && autoreconf -i" ${DEFAULT_USER}
72 su -c "cd ${BUILDDIR} && ./configure --prefix=${INSTALLDIR} --sysconfdir=${INSTALLDIR}/conf ${WSPORT}" ${DEFAULT_USER}
73 su -c "cd ${BUILDDIR} && make" ${DEFAULT_USER}
74  
75 # as root
76 echo "Installing OpenSRF..."
77 cd ${BUILDDIR} && make install
78 useradd -m -s /bin/bash opensrf
79 PATHFILES="/etc/profile /root/.bashrc /home/${DEFAULT_USER}/.bashrc /home/opensrf/.bashrc"
80 for PATHFILE in ${PATHFILES} ; do
81   if [[ ! -e "${PATHFILE}" || ! `egrep "PATH=.*${INSTALLDIR}/bin" "${PATHFILE}"` ]] ; then 
82     echo "export PATH=${INSTALLDIR}/bin:\$PATH" >> ${PATHFILE}
83   fi
84 done
85 chown ${DEFAULT_USER}:${DEFAULT_USER} /home/${DEFAULT_USER}/.bashrc
86 chown -R opensrf:opensrf ${INSTALLDIR}
87  
88 # as root, add the following two lines to /etc/hosts:
89 #127.0.1.2      public.localhost        public
90 #127.0.1.3      private.localhost       private
91 if [[ ! -n "$PROD" ]] ; then
92   if [[ ! `grep public.localhost /etc/hosts` ]] ; then
93     echo "Updating /etc/hosts..."
94     echo -e "127.0.1.2\tpublic.localhost\tpublic\n127.0.1.3\tprivate.localhost\tprivate\n" | \
95       cat - /etc/hosts > /etc/hosts.new && mv /etc/hosts.new /etc/hosts
96   fi
97 else
98   echo 'WARNING: If this is a new production server, you will need to manually update /etc/hosts!'
99 fi
100  
101 # as root
102 echo "ldconfig stuff..."
103 echo "${INSTALLDIR}/lib" > /etc/ld.so.conf.d/opensrf.conf
104 ldconfig
105  
106 # as root
107 echo "Setting up ejabberd..."
108 cp ${SITKACONF}/files/etc/ejabberd/ejabberd.cfg/${HOSTNAME} /etc/ejabberd/ejabberd.cfg
109 sed -i "s|__HOSTNAME__|${THISHOST}|g" /etc/ejabberd/ejabberd.cfg
110 chgrp ejabberd /etc/ejabberd/ejabberd.cfg
111 chmod 640 /etc/ejabberd/ejabberd.cfg
112 /etc/init.d/ejabberd restart && sleep 10 && \
113   ejabberdctl register router private.localhost ${PRIVATE_ROUTER_PASSWD} && \
114   ejabberdctl register opensrf private.localhost ${PRIVATE_OPENSRF_PASSWD} && \
115   ejabberdctl register router public.localhost ${PUBLIC_ROUTER_PASSWD} && \
116   ejabberdctl register opensrf public.localhost ${PUBLIC_OPENSRF_PASSWD}
117
118 echo "OpenSRF is installed!"
119
120 # websockets install (REQUIRED for web client)
121 # XXX a valid SSL cert is required!
122 if [[ -n "$WEBSOCKETS" ]] ; then
123   rm -r /tmp/apache-websocket
124   cd /tmp && git clone https://github.com/disconnect/apache-websocket
125   cd apache-websocket && apxs2 -i -a -c mod_websocket.c
126   sh /usr/share/doc/apache2/examples/setup-instance websockets
127   a2dismod websocket
128   cp ${SITKACONF}/files/etc/apache2-websockets/apache2.conf/${HOSTNAME} /etc/apache2-websockets/apache2.conf
129   sed -i "s|__HOSTNAME__|${THISHOST}|g" /etc/apache2-websockets/apache2.conf
130   cp ${SITKACONF}/files/etc/apache2-websockets/envvars/${HOSTNAME} /etc/apache2-websockets/envvars
131
132   #/etc/init.d/apache2-websockets start
133
134   #echo "websockets is installed!"
135 fi