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