install-osrf.sh: testing fix for LP#1774703
[sitka/sitka-tools.git] / deployment / install-osrf.sh
index 3c1feb9..988eeb3 100755 (executable)
@@ -1,20 +1,49 @@
 #!/bin/bash
-DEFAULT_USER="sitkastaff"
+DEFAULT_USER="opensrf"
 HOMEDIR="/home/${DEFAULT_USER}"
 BUILDDIR="${HOMEDIR}/opensrf"
 INSTALLDIR="/srv/openils"
-BRANCH="rel_2_4"
-HOSTNAME="nonprod-28"
+#BRANCH="rel_3_0"
+BRANCH="user/berick/lp1774703-websocket-thread-lock"
+HOSTNAME="nonprod"
 SITKACONF="${HOMEDIR}/sysadmin"
-SITKACONF_BRANCH="eg28"
+SITKACONF_BRANCH="master"
+
+THISHOST=`hostname -s`
+
+while getopts ":pWj" opt ; do
+  case $opt in
+    p ) PROD="prod";;
+    W ) SKIP_WEBSOCKETS="websockets";;
+    j ) SKIP_EJABBERD_REG="skip_ejabberd_reg";;
+  esac
+done
+shift $(($OPTIND - 1))
 
 # test for ejabberd passwords
 if [ -z "$PRIVATE_ROUTER_PASSWD" -o -z "$PRIVATE_OPENSRF_PASSWD" -o -z "$PUBLIC_ROUTER_PASSWD" -o -z "$PUBLIC_OPENSRF_PASSWD" ]
 then
-  echo "Missing ejabberd password(s), no OpenSRF install!"
-  exit
+  if [[ ! -n "$SKIP_EJABBERD_REG" ]] ; then
+    echo "Missing ejabberd password(s), no OpenSRF install!"
+    exit
+  fi
 fi
 
+# always use a proxy for websockets traffic
+if [[ ! -n "$SKIP_WEBSOCKETS" ]] ; then
+    WSPORT="--with-websockets-port=443"
+    # load-balanced installs require mod_rpaf - see LP#1684970
+    apt-get install libapache2-mod-rpaf
+else
+    WSPORT=""
+fi
+
+if [[ -n "$PROD" ]] ; then
+  echo 'this is a production install'
+  [ "$HOSTNAME" == "nonprod" ] && HOSTNAME="prod"
+else
+  echo 'this is NOT a production install'
+fi
 # as root
 usermod -a -G sudo ${DEFAULT_USER}
 apt-get install git-core build-essential translate-toolkit python-polib bsdmainutils
@@ -23,13 +52,13 @@ apt-get install git-core build-essential translate-toolkit python-polib bsdmainu
 if [ ! -d "${SITKACONF}" ]
 then
   # this will fail unless DEFAULT_USER has read access to the repo
-  su -l -c "cd ${HOMEDIR} && git clone git+ssh://git@git.sitka.bclibraries.ca/sitka-infra/sysadmin" ${DEFAULT_USER}
+  su -c "cd ${HOMEDIR} && git clone git+ssh://git@git.sitka.bclibraries.ca/sitka-infra/sysadmin" ${DEFAULT_USER}
 fi
-su -l -c "cd ${HOMEDIR}/sysadmin && git fetch origin && git checkout ${SITKACONF_BRANCH} && git pull" ${DEFAULT_USER}
+su -c "cd ${HOMEDIR}/sysadmin && git fetch origin && git checkout ${SITKACONF_BRANCH} && git pull" ${DEFAULT_USER}
 if [ ! -d "${BUILDDIR}/.git" ] ; then
-  su -l -c "cd ${HOMEDIR} && git clone git://git.evergreen-ils.org/OpenSRF.git opensrf" ${DEFAULT_USER}
+  su -c "cd ${HOMEDIR} && git clone git://git.evergreen-ils.org/working/OpenSRF.git opensrf" ${DEFAULT_USER}
 fi
-su -l -c "cd ${BUILDDIR} && git fetch origin && git checkout ${BRANCH} && git pull" ${DEFAULT_USER}
+su -c "cd ${BUILDDIR} && git fetch origin && git checkout ${BRANCH} && git pull" ${DEFAULT_USER}
  
 # as root
 echo "need a placeholder apache2.conf, installing from config repo..."
@@ -38,6 +67,7 @@ then
   mv /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak
 fi
 cp ${SITKACONF}/files/etc/apache2/apache2.conf/${HOSTNAME} /etc/apache2/apache2.conf
+sed -i "s|__HOSTNAME__|${THISHOST}|g" /etc/apache2/apache2.conf
 
 # as root
 echo "Installing OpenSRF prerequisites for Ubuntu..."
@@ -45,28 +75,37 @@ cd ${BUILDDIR} && make -f src/extras/Makefile.install ubuntu-trusty
  
 # as DEFAULT_USER
 echo "Configuring and building OpenSRF..."
-su -l -c "cd ${BUILDDIR} && autoreconf -i" ${DEFAULT_USER}
-su -l -c "cd ${BUILDDIR} && ./configure --prefix=${INSTALLDIR} --sysconfdir=${INSTALLDIR}/conf" ${DEFAULT_USER}
-su -l -c "cd ${BUILDDIR} && make" ${DEFAULT_USER}
+su -c "cd ${BUILDDIR} && autoreconf -i" ${DEFAULT_USER}
+su -c "cd ${BUILDDIR} && ./configure --prefix=${INSTALLDIR} --sysconfdir=${INSTALLDIR}/conf ${WSPORT}" ${DEFAULT_USER}
+su -c "cd ${BUILDDIR} && make" ${DEFAULT_USER}
  
 # as root
 echo "Installing OpenSRF..."
 cd ${BUILDDIR} && make install
 useradd -m -s /bin/bash opensrf
-PATHFILES="/etc/profile /home/opensrf/.bashrc"
+PATHFILES="/etc/profile /root/.bashrc /home/${DEFAULT_USER}/.bashrc /home/opensrf/.bashrc"
 for PATHFILE in ${PATHFILES} ; do
-  if [[ -e "${PATHFILE}" && ! `egrep "PATH=.*${INSTALLDIR}/bin" "${PATHFILE}"` ]] ; then 
+  if [[ ! -e "${PATHFILE}" || ! `egrep "PATH=.*${INSTALLDIR}/bin" "${PATHFILE}"` ]] ; then 
     echo "export PATH=${INSTALLDIR}/bin:\$PATH" >> ${PATHFILE}
   fi
 done
-chown -R opensrf:opensrf ${INSTALLDIR}
+chown ${DEFAULT_USER}:${DEFAULT_USER} /home/${DEFAULT_USER}/.bashrc
+if [[ ! -n "$PROD" ]] ; then
+  chown -R opensrf:opensrf ${INSTALLDIR}
+fi
  
 # as root, add the following two lines to /etc/hosts:
 #127.0.1.2     public.localhost        public
 #127.0.1.3     private.localhost       private
-echo "Updating /etc/hosts..."
-echo -e "127.0.1.2\tpublic.localhost\tpublic\n127.0.1.3\tprivate.localhost\tprivate\n" | \
-  cat - /etc/hosts > /etc/hosts.new && mv /etc/hosts.new /etc/hosts
+if [[ ! -n "$PROD" ]] ; then
+  if [[ ! `grep public.localhost /etc/hosts` ]] ; then
+    echo "Updating /etc/hosts..."
+    echo -e "127.0.1.2\tpublic.localhost\tpublic\n127.0.1.3\tprivate.localhost\tprivate\n" | \
+      cat - /etc/hosts > /etc/hosts.new && mv /etc/hosts.new /etc/hosts
+  fi
+else
+  echo 'WARNING: If this is a new production server, you will need to manually update /etc/hosts!'
+fi
  
 # as root
 echo "ldconfig stuff..."
@@ -76,27 +115,35 @@ ldconfig
 # as root
 echo "Setting up ejabberd..."
 cp ${SITKACONF}/files/etc/ejabberd/ejabberd.cfg/${HOSTNAME} /etc/ejabberd/ejabberd.cfg
+sed -i "s|__HOSTNAME__|${THISHOST}|g" /etc/ejabberd/ejabberd.cfg
 chgrp ejabberd /etc/ejabberd/ejabberd.cfg
 chmod 640 /etc/ejabberd/ejabberd.cfg
-/etc/init.d/ejabberd restart && sleep 10 && \
+/etc/init.d/ejabberd stop && /etc/init.d/ejabberd start
+if [[ ! -n "$SKIP_EJABBERD_REG" ]] ; then
+  sleep 5
   ejabberdctl register router private.localhost ${PRIVATE_ROUTER_PASSWD} && \
   ejabberdctl register opensrf private.localhost ${PRIVATE_OPENSRF_PASSWD} && \
   ejabberdctl register router public.localhost ${PUBLIC_ROUTER_PASSWD} && \
   ejabberdctl register opensrf public.localhost ${PUBLIC_OPENSRF_PASSWD}
+fi
 
 echo "OpenSRF is installed!"
 
 # websockets install (REQUIRED for web client)
 # XXX a valid SSL cert is required!
-#rm -r /tmp/apache-websocket
-#cd /tmp && git clone https://github.com/disconnect/apache-websocket
-#cd apache-websocket && apxs2 -i -a -c mod_websocket.c
-#sh /usr/share/doc/apache2/examples/setup-instance websockets
-#a2dismod websocket
-#cp ${SITKACONF}/files/etc/apache2-websockets/apache2.conf/${HOSTNAME} /etc/apache2-websockets/apache2.conf
-#cp ${SITKACONF}/files/etc/apache2-websockets/envvars/${HOSTNAME} /etc/apache2-websockets/envvars
-
-#/etc/init.d/apache2-websockets start
+if [[ ! -n "$SKIP_WEBSOCKETS" ]] ; then
+  rm -r /tmp/apache-websocket
+  rm -r /etc/apache2-websockets /etc/init.d/apache2-websockets /usr/local/sbin/*-websockets /var/log/apache2-websockets/ /etc/logrotate.d/apache2-websockets
+  #cd /tmp && git clone https://github.com/disconnect/apache-websocket
+  cd /tmp && git clone https://github.com/jchampio/apache-websocket
+  cd apache-websocket && apxs2 -i -a -c mod_websocket.c
+  sh /usr/share/doc/apache2/examples/setup-instance websockets
+  a2dismod websocket
+  cp ${SITKACONF}/files/etc/apache2-websockets/apache2.conf/${HOSTNAME} /etc/apache2-websockets/apache2.conf
+  sed -i "s|__THISHOST__|${THISHOST}|g" /etc/apache2-websockets/apache2.conf
+  cp ${SITKACONF}/files/etc/apache2-websockets/envvars/${HOSTNAME} /etc/apache2-websockets/envvars
 
-echo "websockets is installed!"
+  #/etc/init.d/apache2-websockets start
 
+  #echo "websockets is installed!"
+fi