Bug 25596: DBRev 20.06.00.054
[koha.git] / debian / scripts / koha-sip
blob402e9fa2e3efa565bf225c611881d449d19782e7
1 #!/bin/bash
3 # koha-sip - Manage SIP server for Koha instances
4 # Copyright 2019 Theke Solutions
5 # Copyright 2012 Catalyst IT, Ltd
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.
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.
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/>.
20 set -e
22 . /lib/lsb/init-functions
24 # Read configuration variable file if it is present
25 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
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
35 usage()
37 local scriptname=$(basename $0)
39 cat <<EOF
40 $scriptname
42 This script lets you manage the SIP server for your Koha instances.
44 Usage:
45 $scriptname [--start|--stop|--restart] instancename1 [instancename2...]
46 $scriptname -h|--help
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
57 EOF
60 start_sip()
62 local name=$1
64 _check_and_fix_perms $name
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
73 if [ "$DEV_INSTALL" = "" ]; then
74 LIBDIR=$KOHA_HOME/lib
75 else
76 LIBDIR=$KOHA_HOME
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"
89 SIP_PARAMS="$LIBDIR/C4/SIP/SIPServer.pm \
90 /etc/koha/sites/${name}/SIPconfig.xml"
92 [ "$verbose" != "no" ] && \
93 log_daemon_msg "Starting SIP server for ${name}"
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
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
113 stop_sip()
115 local name=$1
117 if is_sip_running $name; then
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"
129 [ "$verbose" != "no" ] && \
130 log_daemon_msg "Stopping SIP server for ${name}"
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
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
149 restart_sip()
151 local name=$1
153 if is_sip_running ${name}; then
154 local noLF="-n"
155 [ "$verbose" != "no" ] && noLF=""
156 echo $noLF `stop_sip ${name}`
158 MAX_ITERATION=10
159 while is_sip_running ${name}; do
160 i=$((i+1))
161 if [ $MAX_ITERATION -lt $i ]; then
162 break
164 sleep 1;
166 done
167 echo $noLF `start_sip ${name}`
168 else
169 if [ "$verbose" != "no" ]; then
170 log_daemon_msg "Warning: SIP server not running for ${name}"
171 log_end_msg 0
172 else
173 return 0
178 sip_status()
180 local name=$1
182 if is_sip_running ${name}; then
183 log_daemon_msg "SIP server running for ${name}"
184 log_end_msg 0
185 else
186 log_daemon_msg "SIP server not running for ${name}"
187 log_end_msg 3
191 enable_sip()
193 local name=$1
194 local libdir=/var/lib/koha/${name}
196 sipfile=/etc/koha/sites/${name}/SIPconfig.xml
198 if is_sip_enabled ${name}; then
199 echo "Warning: SIP server already enabled for ${name}"
200 else
201 echo "Enabling SIP server for ${name} - edit ${sipfile} to configure"
202 touch $libdir/sip.enabled
203 if [[ ! -f "/etc/koha/sites/${name}/SIPconfig.xml" ]]; then
204 cp -v /etc/koha/SIPconfig.xml ${sipfile}
205 chown ${name}-koha:${name}-koha ${sipfile}
206 chmod 600 ${sipfile}
207 echo "This is the first time SIP has been enabled. Please check the configurations in /etc/koha/sites/${name}/SIPconfig.xml"
212 disable_sip()
214 local name=$1
215 local libdir=/var/lib/koha/${name}
217 if is_sip_enabled ${name}; then
218 # SIP is enabled, we should disable it
219 echo "Stopping running SIP"
220 stop_sip ${name}
221 rm ${libdir}/sip.enabled
222 echo "Information: SIP server disabled for ${name}"
223 else
224 echo " SIP server for ${name} not enabled - use koha-sip --enable <instance> to enable."
229 _check_and_fix_perms()
231 local name=$1
233 local files="/var/log/koha/${name}/sip-error.log \
234 /var/log/koha/${name}/sip.log \
235 /var/log/koha/$name/sip-output.log"
237 for file in ${files}
239 if [ ! -e "${file}" ]; then
240 touch ${file}
242 chown "${name}-koha":"${name}-koha" ${file}
243 done
246 set_action()
248 if [ "$op" = "" ]; then
249 op=$1
250 else
251 die "Error: only one action can be specified."
255 op=""
256 verbose="no"
258 # Backwards compatible with old koha-*-sip scripts
259 # TODO: Remove once there's consensus to remove the legacy scripts
260 used_script_name=$(basename $0)
262 if [ "$used_script_name" != "koha-sip" ]; then
263 warn "Deprecated script used (${used_script_name})"
265 case "$used_script_name" in
266 koha-start-sip)
267 set_action "start" ;;
268 koha-stop-sip)
269 set_action "stop" ;;
270 koha-enable-sip)
271 set_action "enable" ;;
273 break ;;
274 esac
276 # / Backwards compatible handling code
278 # Read command line parameters
279 while [ $# -gt 0 ]; do
281 case "$1" in
282 -h|--help)
283 usage ; exit 0 ;;
284 -v|--verbose)
285 verbose="yes"
286 shift ;;
287 --start)
288 set_action "start"
289 shift ;;
290 --stop)
291 set_action "stop"
292 shift ;;
293 --restart)
294 set_action "restart"
295 shift ;;
296 --status)
297 set_action "status"
298 shift ;;
299 --enable)
300 set_action "enable"
301 shift ;;
302 --disable)
303 set_action "disable"
304 shift ;;
306 die "Error: invalid option switch ($1)" ;;
308 # We expect the remaining stuff are the instance names
309 break ;;
310 esac
312 done
314 if [ $# -gt 0 ]; then
315 # We have at least one instance name
316 for name in "$@"; do
318 if is_instance $name; then
320 export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
322 case $op in
323 "start")
324 start_sip $name
326 "stop")
327 stop_sip $name
329 "restart")
330 restart_sip $name
332 "status")
333 sip_status $name
335 "enable")
336 enable_sip $name
338 "disable")
339 disable_sip $name
340 esac
342 else
343 if [ "$verbose" != "no" ]; then
344 log_daemon_msg "Error: Invalid instance name $name"
345 log_end_msg 1
349 done
350 else
351 if [ "$verbose" != "no" ]; then
352 warn "Error: you must provide at least one instance name"
356 exit 0