Bug 24993: Make sure SIP server is stopped before restarting it
[koha.git] / debian / scripts / koha-sip
1 #!/bin/bash
2
3 # koha-sip - Manage SIP server for Koha instances
4 #              Copyright 2019 Theke Solutions
5 #              Copyright 2012 Catalyst IT, Ltd
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 set -e
21
22 . /lib/lsb/init-functions
23
24 # Read configuration variable file if it is present
25 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
26
27 # include helper functions
28 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
29     . "/usr/share/koha/bin/koha-functions.sh"
30 else
31     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
32     exit 1
33 fi
34
35 usage()
36 {
37     local scriptname=$(basename $0)
38
39     cat <<EOF
40 $scriptname
41
42 This script lets you manage the SIP server for your Koha instances.
43
44 Usage:
45 $scriptname [--start|--stop|--restart] instancename1 [instancename2...]
46 $scriptname -h|--help
47
48     --enable              Enable the Koha SIP server.
49     --disable             Disable and stop the Koha SIP server.
50     --start               Start the SIP server for the specified instance(s)
51     --stop                Stop the SIP server for the specified instance(s)
52     --restart             Restart the SIP server for the specified instance(s)
53     --status              Show the status of the SIP server for the specified instance(s)
54     --verbose|-v          Display progress and actions messages
55     --help|-h             Display this help message
56
57 EOF
58 }
59
60 start_sip()
61 {
62     local name=$1
63
64     _check_and_fix_perms $name
65
66     if ! is_sip_running $name; then
67         if [ ! -f "/etc/koha/sites/${name}/SIPconfig.xml" ] || [ ! -f "/var/lib/koha/${name}/sip.enabled" ] ; then
68             echo "SIP is disabled, or you do not have a SIPconfig.xml file."
69         else
70             adjust_paths_dev_install $name
71             export KOHA_HOME PERL5LIB
72
73             if [ "$DEV_INSTALL" = "" ]; then
74                 LIBDIR=$KOHA_HOME/lib
75             else
76                 LIBDIR=$KOHA_HOME
77             fi
78
79             DAEMONOPTS="--name=${name}-koha-sip \
80                     --errlog=/var/log/koha/${name}/sip-error.log \
81                     --stdout=/var/log/koha/${name}/sip.log \
82                     --output=/var/log/koha/${name}/sip-output.log \
83                     --verbose=1 \
84                     --respawn \
85                     --delay=30 \
86                     --pidfiles=/var/run/koha/${name} \
87                     --user=${name}-koha.${name}-koha"
88
89             SIP_PARAMS="$LIBDIR/C4/SIP/SIPServer.pm \
90                     /etc/koha/sites/${name}/SIPconfig.xml"
91
92             [ "$verbose" != "no" ] && \
93                 log_daemon_msg "Starting SIP server for ${name}"
94
95             if daemon $DAEMONOPTS -- perl $SIP_PARAMS; then
96                 ([ "$verbose" != "no" ] && \
97                     log_end_msg 0) || return 0
98             else
99                 ([ "$verbose" != "no" ] && \
100                     log_end_msg 1) || return 1
101             fi
102         fi
103     else
104         if [ "$verbose" != "no" ]; then
105             log_daemon_msg "Warning: SIP server already running for ${name}"
106             log_end_msg 0
107         else
108             return 0
109         fi
110     fi
111 }
112
113 stop_sip()
114 {
115     local name=$1
116
117     if is_sip_running $name; then
118
119         DAEMONOPTS="--name=${name}-koha-sip \
120                     --errlog=/var/log/koha/${name}/sip-error.log \
121                     --stdout=/var/log/koha/${name}/sip.log \
122                     --output=/var/log/koha/${name}/sip-output.log \
123                     --verbose=1 \
124                     --respawn \
125                     --delay=30 \
126                     --pidfiles=/var/run/koha/${name} \
127                     --user=${name}-koha.${name}-koha"
128
129         [ "$verbose" != "no" ] && \
130             log_daemon_msg "Stopping SIP server for ${name}"
131
132         if daemon $DAEMONOPTS --stop; then
133             ([ "$verbose" != "no" ] && \
134                 log_end_msg 0) || return 0
135         else
136             ([ "$verbose" != "no" ] && \
137                 log_end_msg 1) || return 1
138         fi
139     else
140         if [ "$verbose" != "no" ]; then
141             log_daemon_msg "Warning: SIP server not running for ${name}"
142             log_end_msg 0
143         else
144             return 0
145         fi
146     fi
147 }
148
149 restart_sip()
150 {
151     local name=$1
152
153     if is_sip_running ${name}; then
154         local noLF="-n"
155         [ "$verbose" != "no" ] && noLF=""
156         echo $noLF `stop_sip ${name}`
157         while is_sip_running ${name}; do
158             sleep 1;
159         done
160         echo $noLF `start_sip ${name}`
161     else
162         if [ "$verbose" != "no" ]; then
163             log_daemon_msg "Warning: SIP server not running for ${name}"
164             log_end_msg 0
165         else
166             return 0
167         fi
168     fi
169 }
170
171 sip_status()
172 {
173     local name=$1
174
175     if is_sip_running ${name}; then
176         log_daemon_msg "SIP server running for ${name}"
177         log_end_msg 0
178     else
179         log_daemon_msg "SIP server not running for ${name}"
180         log_end_msg 3
181     fi
182 }
183
184 enable_sip()
185 {
186     local name=$1
187     local libdir=/var/lib/koha/${name}
188
189     sipfile=/etc/koha/sites/${name}/SIPconfig.xml
190
191     if is_sip_enabled ${name}; then
192         echo "Warning: SIP server already enabled for ${name}"
193     else
194         echo "Enabling SIP server for ${name} - edit ${sipfile} to configure"
195         touch $libdir/sip.enabled
196         if [[ ! -f "/etc/koha/sites/${name}/SIPconfig.xml" ]]; then
197             cp -v /etc/koha/SIPconfig.xml ${sipfile}
198             chown ${name}-koha:${name}-koha ${sipfile}
199             chmod 600 ${sipfile}
200             echo "This is the first time SIP has been enabled. Please check the configurations in /etc/koha/sites/${name}/SIPconfig.xml"
201         fi
202     fi
203 }
204
205 disable_sip()
206 {
207     local name=$1
208     local libdir=/var/lib/koha/${name}
209
210     if is_sip_enabled ${name}; then
211         # SIP is enabled, we should disable it
212         echo "Stopping running SIP"
213         stop_sip ${name}
214         rm ${libdir}/sip.enabled
215         echo "Information: SIP server disabled for ${name}"
216     else
217         echo " SIP server for ${name} not enabled - use koha-sip --enable <instance> to enable."
218     fi
219 }
220
221
222 _check_and_fix_perms()
223 {
224     local name=$1
225
226     local files="/var/log/koha/${name}/sip-error.log \
227                  /var/log/koha/${name}/sip.log \
228                  /var/log/koha/$name/sip-output.log"
229
230     for file in ${files}
231     do
232         if [ ! -e "${file}" ]; then
233             touch ${file}
234         fi
235         chown "${name}-koha":"${name}-koha" ${file}
236     done
237 }
238
239 set_action()
240 {
241     if [ "$op" = "" ]; then
242         op=$1
243     else
244         die "Error: only one action can be specified."
245     fi
246 }
247
248 op=""
249 verbose="no"
250
251 # Backwards compatible with old koha-*-sip scripts
252 # TODO: Remove once there's consensus to remove the legacy scripts
253 used_script_name=$(basename $0)
254
255 if [ "$used_script_name" != "koha-sip" ]; then
256     warn "Deprecated script used (${used_script_name})"
257
258     case "$used_script_name" in
259         koha-start-sip)
260             set_action "start" ;;
261         koha-stop-sip)
262             set_action "stop" ;;
263         koha-enable-sip)
264             set_action "enable" ;;
265         *)
266             break ;;
267     esac
268 fi
269 # / Backwards compatible handling code
270
271 # Read command line parameters
272 while [ $# -gt 0 ]; do
273
274     case "$1" in
275         -h|--help)
276             usage ; exit 0 ;;
277         -v|--verbose)
278             verbose="yes"
279             shift ;;
280         --start)
281             set_action "start"
282             shift ;;
283         --stop)
284             set_action "stop"
285             shift ;;
286         --restart)
287             set_action "restart"
288             shift ;;
289         --status)
290             set_action "status"
291             shift ;;
292         --enable)
293             set_action "enable"
294             shift ;;
295         --disable)
296             set_action "disable"
297             shift ;;
298         -*)
299             die "Error: invalid option switch ($1)" ;;
300         *)
301             # We expect the remaining stuff are the instance names
302             break ;;
303     esac
304
305 done
306
307 if [ $# -gt 0 ]; then
308     # We have at least one instance name
309     for name in "$@"; do
310
311         if is_instance $name; then
312
313             export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
314
315             case $op in
316                 "start")
317                     start_sip $name
318                     ;;
319                 "stop")
320                     stop_sip $name
321                     ;;
322                 "restart")
323                     restart_sip $name
324                     ;;
325                 "status")
326                     sip_status $name
327                     ;;
328                 "enable")
329                     enable_sip $name
330                     ;;
331                 "disable")
332                     disable_sip $name
333             esac
334
335         else
336             if [ "$verbose" != "no" ]; then
337                 log_daemon_msg "Error: Invalid instance name $name"
338                 log_end_msg 1
339             fi
340         fi
341
342     done
343 else
344     if [ "$verbose" != "no" ]; then
345         warn "Error: you must provide at least one instance name"
346     fi
347 fi
348
349 exit 0