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