Bug 36009: Document koha-worker --queue elastic_index
[koha.git] / debian / scripts / koha-z3950-responder
1 #!/bin/bash
2 #
3 # Copyright 2015 Theke Solutions
4 # Copyright 2016 Koha-Suomi
5 # Copyright 2018 The National Library of Finland, University of Helsinki
6 #
7 # This file is part of Koha.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 set -e
23
24 . /lib/lsb/init-functions
25
26 # Read configuration variable file if it is present
27 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
28
29 # include helper functions
30 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
31     . "/usr/share/koha/bin/koha-functions.sh"
32 else
33     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
34     exit 1
35 fi
36
37 usage()
38 {
39     local scriptname=$(basename $0)
40
41     cat <<EOF
42 $scriptname
43
44 This script lets you manage the Z39.50/SRU daemons for your Koha instances.
45
46 Usage:
47 $scriptname --start|--stop|--restart [--quiet|-q] instancename1 [instancename2...]
48 $scriptname --enable|--disable instancename1 [instancename2]
49 $scriptname -h|--help
50
51     --start               Start the Z39.50/SRU daemon for the specified instances
52     --stop                Stop the Z39.50/SRU daemon for the specified instances
53     --restart             Restart the Z39.50/SRU daemon for the specified instances
54     --enable              Enable Z39.50/SRU for the specified instances
55     --disable             Disable Z39.50/SRU for the specified instances
56     --debugger            Enable running Z39.50/SRU in debug mode
57     --debugger-key        Specify the key the IDE is expecting
58     --debugger-location   Specify the host:port for your debugger tool (defaults
59                           to localhost:9000)
60     --debugger-path       Specify the path for the debugger library
61     --quiet|-q            Make the script quiet about non existent instance names
62                           (useful for calling from another scripts).
63     --help|-h             Display this help message
64
65 EOF
66 }
67
68 start_z3950()
69 {
70     local instancename=$1
71
72     local PIDFILE="/var/run/koha/${instancename}/z3950-responder.pid"
73     local NAME="${instancename}-koha-z3950-responder"
74     local CONFIGDIR="/etc/koha/z3950"
75
76     if [ -e "/etc/koha/sites/${instancename}/z3950/config.xml" ]; then
77         # pick instance-specific config dir
78         CONFIGDIR="/etc/koha/sites/${instancename}/z3950"
79     fi # else stick with the default one
80
81     _check_and_fix_perms $instancename
82
83     instance_user="${instancename}-koha"
84
85     daemonize="-D -d ${instancename}-koha-z3950"
86     logging="-l /var/log/koha/${instancename}/z3950.log"
87
88     if [ "$DEV_INSTALL" = "1" ]; then
89         MISCDIR=$KOHA_HOME/misc
90     else
91         MISCDIR=/usr/share/koha/bin
92     fi
93
94     Z3950RESPONDER="/usr/bin/perl $MISCDIR/z3950_responder.pl"
95     if [ "$debug_mode" = "yes" ]; then
96         if [ "$DEV_INSTALL" = "1" ]; then
97             warn "Not a dev install, disabling debug mode"
98         else
99             environment="development"
100             daemonize=""
101             logging="" # remote debugger takes care
102             Z3950RESPONDER="/usr/bin/perl -d ${MISCDIR}/z3950_responder.pl"
103         fi
104     fi
105
106     Z3950OPTS="-c ${CONFIGDIR} \
107                -u ${instance_user} \
108                -p ${PIDFILE} ${daemonize} ${logging}"
109
110     if ! is_z3950_running ${instancename}; then
111         export KOHA_CONF="/etc/koha/sites/${instancename}/koha-conf.xml"
112
113         if [[ ! $Z3950_ADDITIONAL_OPTS ]]; then
114             Z3950_ADDITIONAL_OPTS="$( xmlstarlet sel -t -v 'yazgfs/config/z3950_responder_options' "$CONFIGDIR/config.xml" || true )"
115         fi
116
117         log_daemon_msg "Starting Z39.50/SRU daemon for ${instancename}"
118
119         # Change to the instance's user dir
120         current_dir=$(pwd)
121         eval cd ~$instance_user
122
123         if ${Z3950RESPONDER} ${Z3950_ADDITIONAL_OPTS} ${Z3950OPTS}; then
124             log_end_msg 0
125         else
126             log_end_msg 1
127         fi
128         # Go back to the original dir
129         cd "$current_dir"
130
131     else
132         log_daemon_msg "Error: Z39.50/SRU already running for ${instancename}"
133         log_end_msg 1
134     fi
135 }
136
137 stop_z3950()
138 {
139     local instancename=$1
140
141     local PIDFILE="/var/run/koha/${instancename}/z3950-responder.pid"
142
143     if is_z3950_running ${instancename}; then
144
145         log_daemon_msg "Stopping Z39.50/SRU daemon for ${instancename}"
146
147         if start-stop-daemon --pidfile ${PIDFILE} --stop --retry=TERM/30/KILL/5; then
148             log_end_msg 0
149         else
150             log_end_msg 1
151         fi
152     else
153         log_daemon_msg "Error: Z39.50/SRU not running for ${instancename}"
154         log_end_msg 1
155     fi
156 }
157
158 restart_z3950()
159 {
160     local instancename=$1
161
162     local PIDFILE="/var/run/koha/${instancename}/z3950.pid"
163
164     if is_z3950_running ${instancename}; then
165
166         log_daemon_msg "Restarting Z39.50/SRU daemon for ${instancename}"
167
168         if stop_z3950 $instancename && start_z3950 $instancename; then
169             log_end_msg 0
170         else
171             log_end_msg 1
172         fi
173     else
174         log_warning_msg "Z39.50/SRU not running for ${instancename}."
175         start_z3950 $instancename
176     fi
177 }
178
179 enable_z3950()
180 {
181     local instancename=$1
182
183     if [ ! -e /etc/koha/sites/${instancename}/koha-conf.xml ] ;
184     then
185         echo "No such instance: ${instancename}" > /dev/stderr
186         return 1
187     fi
188
189     local configdir=/etc/koha/sites/${instancename}/z3950
190     if [ -e ${configdir}/config.xml ]
191     then
192         [ "${quiet}" != "yes" ] && warn "Z39.50/SRU already enabled for $name"
193         return 1
194     fi
195
196     if [ ! -e ${configdir} ]
197     then
198         mkdir ${configdir}
199     fi
200     cp /etc/koha/z3950/* ${configdir}/
201     chown ${name}-koha:${name}-koha ${configdir}/*
202     chmod 600 ${configdir}/*
203
204     [ "${quiet}" != "yes" ] && warn "Z39.50/SRU enabled for $name - edit files in ${configdir} to configure"
205     return 0
206 }
207
208 disable_z3950()
209 {
210     local instancename=$1
211
212     if is_z3950_enabled $instancename; then
213         local configdir=/etc/koha/sites/${instancename}/z3950
214         mv ${configdir} ${configdir}.`date +%F_%T`
215         [ "${quiet}" != "yes" ] && warn "Z39.50/SRU disabled for ${instancename}"
216         return 0
217     else
218         [ "${quiet}" != "yes" ] && warn "Z39.50/SRU already disabled for ${instancename}"
219         return 1
220     fi
221 }
222
223 _check_and_fix_perms()
224 {
225     local instance=$1
226
227     local files="/var/log/koha/${instance}/z3950.log"
228
229     for file in ${files}
230     do
231         if [ ! -e "${file}" ]; then
232             touch ${file}
233         fi
234         chown "${instance}-koha":"${instance}-koha" ${file}
235     done
236 }
237
238 set_action()
239 {
240     if [ "$op" = "" ]; then
241         op=$1
242     else
243         die "Error: only one action can be specified."
244     fi
245 }
246
247 op=""
248 quiet="no"
249 debug_mode="no"
250 debugger_key=""
251 debugger_location="localhost:9000"
252 debugger_path=""
253
254 # Read command line parameters
255 while [ $# -gt 0 ]; do
256
257     case "$1" in
258         -h|--help)
259             usage ; exit 0 ;;
260         -q|--quiet)
261             quiet="yes"
262             shift ;;
263         --start)
264             set_action "start"
265             shift ;;
266         --stop)
267             set_action "stop"
268             shift ;;
269         --restart)
270             set_action "restart"
271             shift ;;
272         --enable)
273             set_action "enable"
274             shift ;;
275         --disable)
276             set_action "disable"
277             shift ;;
278         --debugger)
279             debug_mode="yes"
280             shift ;;
281         --debugger-key)
282             debugger_key="$2"
283             shift 2 ;;
284         --debugger-location)
285             debugger_location="$2"
286             shift 2 ;;
287         --debugger-path)
288             debugger_path="$2"
289             shift 2 ;;
290         -*)
291             die "Error: invalid option switch ($1)" ;;
292         *)
293             # We expect the remaining stuff are the instance names
294             break ;;
295     esac
296
297 done
298
299 if [ $# -gt 0 ]; then
300     # We have at least one instance name
301     for name in "$@"; do
302
303         if is_instance $name; then
304
305             adjust_paths_dev_install $name
306             export DEV_INSTALL
307             export KOHA_HOME
308             PERL5LIB=$PERL5LIB:$KOHA_HOME/installer:$KOHA_HOME/lib/installer
309             # If debug mode is enabled, add the debugger lib path
310             # to PERL5LIB if appropriate
311             if [ "$debug_mode" = "yes" ]; then
312                 if [ "$debugger_path" != "" ]; then
313                     PERL5LIB="${debugger_path}":$PERL5LIB
314                 fi
315                 export PERL5DB="BEGIN { require q(${debugger_path}/perl5db.pl) }"
316                 export PERLDB_OPTS="RemotePort=${debugger_location} async=1 LogFile=/var/log/koha/${name}/z3950-debug.log"
317                 export DBGP_IDEKEY=${debugger_key}
318                 export PERL5OPT="-d"
319             fi
320
321             export PERL5LIB
322
323             case $op in
324                 "start")
325                     start_z3950 $name
326                     ;;
327                 "stop")
328                     stop_z3950 $name
329                     ;;
330                 "restart")
331                     restart_z3950 $name
332                     ;;
333                 "enable")
334                     enable_z3950 $name
335                     ;;
336                 "disable")
337                     disable_z3950 $name
338                     ;;
339                 *)
340                     usage
341                     ;;
342             esac
343
344         else
345             if [ "$quiet" = "no" ]; then
346                 log_daemon_msg "Error: Invalid instance name $name"
347                 log_end_msg 1
348             fi
349         fi
350
351     done
352 else
353     if [ "$quiet" = "no" ]; then
354         warn "Error: you must provide at least one instance name"
355     fi
356 fi
357
358 exit 0