Bug 34653: Make koha-foreach return the correct status code
[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
158         MAX_ITERATION=10
159         while is_sip_running ${name}; do
160             i=$((i+1))
161             if [ $MAX_ITERATION -lt $i ]; then
162                 break
163             fi
164             sleep 1;
165
166         done
167         echo $noLF `start_sip ${name}`
168     else
169         if [ "$verbose" != "no" ]; then
170             log_warning_msg "Warning: SIP server not running for ${name}."
171             start_sip ${name}
172         else
173             start_sip ${name}
174             return 0
175         fi
176     fi
177 }
178
179 sip_status()
180 {
181     local name=$1
182
183     if is_sip_running ${name}; then
184         log_daemon_msg "SIP server running for ${name}"
185         log_end_msg 0
186     else
187         log_daemon_msg "SIP server not running for ${name}"
188         log_end_msg 3
189     fi
190 }
191
192 enable_sip()
193 {
194     local name=$1
195     local libdir=/var/lib/koha/${name}
196
197     sipfile=/etc/koha/sites/${name}/SIPconfig.xml
198
199     if is_sip_enabled ${name}; then
200         echo "Warning: SIP server already enabled for ${name}"
201     else
202         echo "Enabling SIP server for ${name} - edit ${sipfile} to configure"
203         touch $libdir/sip.enabled
204         if [[ ! -f "/etc/koha/sites/${name}/SIPconfig.xml" ]]; then
205             cp -v /etc/koha/SIPconfig.xml ${sipfile}
206             chown ${name}-koha:${name}-koha ${sipfile}
207             chmod 600 ${sipfile}
208             echo "This is the first time SIP has been enabled. Please check the configurations in /etc/koha/sites/${name}/SIPconfig.xml"
209         fi
210     fi
211 }
212
213 disable_sip()
214 {
215     local name=$1
216     local libdir=/var/lib/koha/${name}
217
218     if is_sip_enabled ${name}; then
219         # SIP is enabled, we should disable it
220         echo "Stopping running SIP"
221         stop_sip ${name}
222         rm ${libdir}/sip.enabled
223         echo "Information: SIP server disabled for ${name}"
224     else
225         echo " SIP server for ${name} not enabled - use koha-sip --enable <instance> to enable."
226     fi
227 }
228
229
230 _check_and_fix_perms()
231 {
232     local name=$1
233
234     local files="/var/log/koha/${name}/sip-error.log \
235                  /var/log/koha/${name}/sip.log \
236                  /var/log/koha/$name/sip-output.log"
237
238     for file in ${files}
239     do
240         if [ ! -e "${file}" ]; then
241             touch ${file}
242         fi
243         chown "${name}-koha":"${name}-koha" ${file}
244     done
245 }
246
247 set_action()
248 {
249     if [ "$op" = "" ]; then
250         op=$1
251     else
252         die "Error: only one action can be specified."
253     fi
254 }
255
256 op=""
257 verbose="no"
258
259 # Backwards compatible with old koha-*-sip scripts
260 # TODO: Remove once there's consensus to remove the legacy scripts
261 used_script_name=$(basename $0)
262
263 if [ "$used_script_name" != "koha-sip" ]; then
264     warn "Deprecated script used (${used_script_name})"
265
266     case "$used_script_name" in
267         koha-start-sip)
268             set_action "start" ;;
269         koha-stop-sip)
270             set_action "stop" ;;
271         koha-enable-sip)
272             set_action "enable" ;;
273         *)
274             break ;;
275     esac
276 fi
277 # / Backwards compatible handling code
278
279 # Read command line parameters
280 while [ $# -gt 0 ]; do
281
282     case "$1" in
283         -h|--help)
284             usage ; exit 0 ;;
285         -v|--verbose)
286             verbose="yes"
287             shift ;;
288         --start)
289             set_action "start"
290             shift ;;
291         --stop)
292             set_action "stop"
293             shift ;;
294         --restart)
295             set_action "restart"
296             shift ;;
297         --status)
298             set_action "status"
299             shift ;;
300         --enable)
301             set_action "enable"
302             shift ;;
303         --disable)
304             set_action "disable"
305             shift ;;
306         -*)
307             die "Error: invalid option switch ($1)" ;;
308         *)
309             # We expect the remaining stuff are the instance names
310             break ;;
311     esac
312
313 done
314
315 if [ $# -gt 0 ]; then
316     # We have at least one instance name
317     for name in "$@"; do
318
319         if is_instance $name; then
320
321             export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
322
323             case $op in
324                 "start")
325                     start_sip $name
326                     ;;
327                 "stop")
328                     stop_sip $name
329                     ;;
330                 "restart")
331                     restart_sip $name
332                     ;;
333                 "status")
334                     sip_status $name
335                     ;;
336                 "enable")
337                     enable_sip $name
338                     ;;
339                 "disable")
340                     disable_sip $name
341             esac
342
343         else
344             if [ "$verbose" != "no" ]; then
345                 log_daemon_msg "Error: Invalid instance name $name"
346                 log_end_msg 1
347             fi
348         fi
349
350     done
351 else
352     if [ "$verbose" != "no" ]; then
353         warn "Error: you must provide at least one instance name"
354     fi
355 fi
356
357 exit 0