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