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