Bug 22238: Remove koha-*-sip scripts in favor of koha-sip
[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     --start               Start the SIP server for the specified instance(s)
49     --stop                Stop the SIP server for the specified instance(s)
50     --restart             Restart the SIP server for the specified instance(s)
51     --status              Show the status of the SIP server for the specified instance(s)
52     --verbose|-v          Display progress and actions messages
53     --help|-h             Display this help message
54
55 EOF
56 }
57
58 start_sip()
59 {
60     local name=$1
61
62     _check_and_fix_perms $name
63
64     if ! is_sip_running $name; then
65
66         adjust_paths_dev_install $name
67         export KOHA_CONF PERL5LIB
68         if [ "$DEV_INSTALL" = "" ]; then
69             LIBDIR=$KOHA_HOME/lib
70         else
71             LIBDIR=$KOHA_HOME
72         fi
73
74         DAEMONOPTS="--name=${name}-koha-sip \
75                     --errlog=/var/log/koha/${name}/sip-error.log \
76                     --stdout=/var/log/koha/${name}/sip.log \
77                     --output=/var/log/koha/${name}/sip-output.log \
78                     --verbose=1 \
79                     --respawn \
80                     --delay=30 \
81                     --pidfiles=/var/run/koha/${name} \
82                     --user=${name}-koha.${name}-koha"
83
84         SIP_PARAMS="$LIBDIR/C4/SIP/SIPServer.pm \
85                     /etc/koha/sites/${name}/SIPconfig.xml"
86
87         [ "$verbose" != "no" ] && \
88             log_daemon_msg "Starting Koha Zebra daemon for ${name}"
89
90         if daemon $DAEMONOPTS -- perl $SIP_PARAMS; then
91             ([ "$verbose" != "no" ] && \
92                 log_end_msg 0) || return 0
93         else
94             ([ "$verbose" != "no" ] && \
95                 log_end_msg 1) || return 1
96         fi
97     else
98         if [ "$verbose" != "no" ]; then
99             log_daemon_msg "Warning: SIP server already running for ${name}"
100             log_end_msg 0
101         else
102             return 0
103         fi
104     fi
105 }
106
107 stop_sip()
108 {
109     local name=$1
110
111     if is_sip_running $name; then
112
113         DAEMONOPTS="--name=${name}-koha-sip \
114                     --errlog=/var/log/koha/${name}/sip-error.log \
115                     --stdout=/var/log/koha/${name}/sip.log \
116                     --output=/var/log/koha/${name}/sip-output.log \
117                     --verbose=1 \
118                     --respawn \
119                     --delay=30 \
120                     --pidfiles=/var/run/koha/${name} \
121                     --user=${name}-koha.${name}-koha"
122
123         [ "$verbose" != "no" ] && \
124             log_daemon_msg "Stopping SIP server for ${name}"
125
126         if daemon $DAEMONOPTS --stop; then
127             ([ "$verbose" != "no" ] && \
128                 log_end_msg 0) || return 0
129         else
130             ([ "$verbose" != "no" ] && \
131                 log_end_msg 1) || return 1
132         fi
133     else
134         if [ "$verbose" != "no" ]; then
135             log_daemon_msg "Warning: SIP server not running for ${name}"
136             log_end_msg 0
137         else
138             return 0
139         fi
140     fi
141 }
142
143 restart_sip()
144 {
145     local name=$1
146
147     if is_sip_running ${name}; then
148         local noLF="-n"
149         [ "$verbose" != "no" ] && noLF=""
150         echo $noLF `stop_sip ${name}`
151         echo $noLF `start_sip ${name}`
152     else
153         if [ "$verbose" != "no" ]; then
154             log_daemon_msg "Warning: SIP server not running for ${name}"
155             log_end_msg 0
156         else
157             return 0
158         fi
159     fi
160 }
161
162 sip_status()
163 {
164     local name=$1
165
166     if is_sip_running ${name}; then
167         log_daemon_msg "SIP server running for ${name}"
168         log_end_msg 0
169     else
170         log_daemon_msg "SIP server not running for ${name}"
171         log_end_msg 3
172     fi
173 }
174
175 enable_sip()
176 {
177     local name=$1
178
179     sipfile=/etc/koha/sites/${name}/SIPconfig.xml
180
181     if is_sip_enabled ${name}; then
182         echo "Warning: SIP server already enabled for ${name}"
183     else
184         echo "Enabling SIP server for ${name} - edit ${sipfile} to configure"
185         cp -v /etc/koha/SIPconfig.xml ${sipfile}
186         chown ${name}-koha:${name}-koha ${sipfile}
187         chmod 600 ${sipfile}
188     fi
189 }
190
191 _check_and_fix_perms()
192 {
193     local name=$1
194
195     local files="/var/log/koha/${name}/sip-error.log \
196                  /var/log/koha/${name}/sip.log \
197                  /var/log/koha/$name/sip-output.log"
198
199     for file in ${files}
200     do
201         if [ ! -e "${file}" ]; then
202             touch ${file}
203         fi
204         chown "${name}-koha":"${name}-koha" ${file}
205     done
206 }
207
208 set_action()
209 {
210     if [ "$op" = "" ]; then
211         op=$1
212     else
213         die "Error: only one action can be specified."
214     fi
215 }
216
217 op=""
218 verbose="no"
219
220 # Backwards compatible with old koha-*-sip scripts
221 # TODO: Remove once there's consensus to remove the legacy scripts
222 used_script_name=$(basename $0)
223
224 if [ "$used_script_name" != "koha-sip" ]; then
225     warn "Deprecated script used (${used_script_name})"
226
227     case "$used_script_name" in
228         koha-start-sip)
229             set_action "start" ;;
230         koha-stop-sip)
231             set_action "stop" ;;
232         koha-enable-sip)
233             set_action "enable" ;;
234         *)
235             break ;;
236     esac
237 fi
238 # / Backwards compatible handling code
239
240 # Read command line parameters
241 while [ $# -gt 0 ]; do
242
243     case "$1" in
244         -h|--help)
245             usage ; exit 0 ;;
246         -v|--verbose)
247             verbose="yes"
248             shift ;;
249         --start)
250             set_action "start"
251             shift ;;
252         --stop)
253             set_action "stop"
254             shift ;;
255         --restart)
256             set_action "restart"
257             shift ;;
258         --status)
259             set_action "status"
260             shift ;;
261         --enable)
262             set_action "enable"
263             shift ;;
264         -*)
265             die "Error: invalid option switch ($1)" ;;
266         *)
267             # We expect the remaining stuff are the instance names
268             break ;;
269     esac
270
271 done
272
273 if [ $# -gt 0 ]; then
274     # We have at least one instance name
275     for name in "$@"; do
276
277         if is_instance $name; then
278
279             case $op in
280                 "start")
281                     start_sip $name
282                     ;;
283                 "stop")
284                     stop_sip $name
285                     ;;
286                 "restart")
287                     restart_sip $name
288                     ;;
289                 "status")
290                     sip_status $name
291                     ;;
292                 "enable")
293                     enable_sip $name
294             esac
295
296         else
297             if [ "$verbose" != "no" ]; then
298                 log_daemon_msg "Error: Invalid instance name $name"
299                 log_end_msg 1
300             fi
301         fi
302
303     done
304 else
305     if [ "$verbose" != "no" ]; then
306         warn "Error: you must provide at least one instance name"
307     fi
308 fi
309
310 exit 0