Bug 36009: Document koha-worker --queue elastic_index
[koha.git] / debian / scripts / koha-plack
1 #!/bin/bash
2 #
3 # Copyright 2015 Theke Solutions
4 # Copyright 2016 Koha-Suomi
5 #
6 # This file is part of Koha.
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 set -e
22
23 . /lib/lsb/init-functions
24
25 # Read configuration variable file if it is present
26 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
27
28 # include helper functions
29 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
30     . "/usr/share/koha/bin/koha-functions.sh"
31 else
32     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
33     exit 1
34 fi
35
36 usage()
37 {
38     local scriptname=$(basename $0)
39
40     cat <<EOF
41 $scriptname
42
43 This script lets you manage the plack daemons for your Koha instances.
44
45 Usage:
46 $scriptname --start|--stop|--restart|--reload [--quiet|-q] instancename1 [instancename2...]
47 $scriptname --enable|--disable instancename1 [instancename2]
48 $scriptname -h|--help
49
50     --start               Start the plack daemon for the specified instances
51     --stop                Stop the plack daemon for the specified instances
52     --restart             Restart the plack daemon for the specified instances
53     --reload              Reload the plack daemon for the specified instances,
54                           letting the busy workers finish processing their
55                           requests before restarting them
56     --enable              Enable plack for the specified instances
57     --disable             Disable plack for the specified instances
58     --debugger            Enable running Plack in debug mode
59     --debugger-key        Specify the key the IDE is expecting
60     --debugger-location   Specify the host:port for your debugger tool (defaults
61                           to localhost:9000)
62     --debugger-path       Specify the path for the debugger library
63     --quiet|-q            Make the script quiet about non existent instance names
64                           (useful for calling from another scripts).
65     --help|-h             Display this help message
66
67 EOF
68 }
69
70 start_plack()
71 {
72     local instancename=$1
73
74     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
75     local PLACKSOCKET="/var/run/koha/${instancename}/plack.sock"
76     local PSGIFILE="/etc/koha/plack.psgi"
77     local NAME="${instancename}-koha-plack"
78
79     if [ -e "/etc/koha/sites/${instancename}/plack.psgi" ]; then
80         # pick instance-specific psgi file
81         PSGIFILE="/etc/koha/sites/${instancename}/plack.psgi"
82     fi # else stick with the default one
83
84     _check_and_fix_perms $instancename
85
86     PLACK_MAX_REQUESTS=$(run_safe_xmlstarlet $instancename plack_max_requests)
87     [ -z $PLACK_MAX_REQUESTS ] && PLACK_MAX_REQUESTS="50"
88     PLACK_WORKERS=$(run_safe_xmlstarlet $instancename plack_workers)
89     [ -z $PLACK_WORKERS ] && PLACK_WORKERS="2"
90
91     instance_user="${instancename}-koha"
92
93     environment="deployment"
94     daemonize="--daemonize"
95     logging="--access-log /var/log/koha/${instancename}/plack.log \
96              --error-log /var/log/koha/${instancename}/plack-error.log"
97     max_requests_and_workers="--max-requests ${PLACK_MAX_REQUESTS} --workers ${PLACK_WORKERS}"
98
99     if [ "$DEV_INSTALL" = "1" ]; then
100         # Maybe we should switch off debug_mode if DEV_INSTALL is not set?
101         environment="development"
102     fi
103
104     if [ "$debug_mode" = "yes" ]; then
105         environment="development"
106         daemonize=""
107         logging="" # remote debugger takes care
108         max_requests_and_workers="--workers 1"
109         STARMAN="/usr/bin/perl -d ${STARMAN}"
110     fi
111
112     STARMANOPTS="-M FindBin ${max_requests_and_workers} \
113                  --user=${instance_user} --group ${instancename}-koha \
114                  --pid ${PIDFILE} ${daemonize} ${logging} \
115                  -E ${environment} --socket ${PLACKSOCKET} ${PSGIFILE}"
116
117     if ! is_plack_running ${instancename}; then
118         export KOHA_CONF="/etc/koha/sites/${instancename}/koha-conf.xml"
119
120         log_daemon_msg "Starting Plack daemon for ${instancename}"
121
122         # Change to the instance's user dir
123         current_dir=$(pwd)
124         eval cd ~$instance_user
125
126         if ${STARMAN} ${STARMANOPTS}; then
127             log_end_msg 0
128         else
129             log_end_msg 1
130         fi
131         # Go back to the original dir
132         cd "$current_dir"
133
134     else
135         log_daemon_msg "Error: Plack already running for ${instancename}"
136         log_end_msg 1
137     fi
138 }
139
140 stop_plack()
141 {
142     local instancename=$1
143
144     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
145
146     if is_plack_running ${instancename}; then
147
148         log_daemon_msg "Stopping Plack daemon for ${instancename}"
149
150         if start-stop-daemon --pidfile ${PIDFILE} --user="${instancename}-koha" --stop --retry=QUIT/30/KILL/5; then
151             log_end_msg 0
152         else
153             log_end_msg 1
154         fi
155     else
156         log_daemon_msg "Error: Plack not running for ${instancename}"
157         log_end_msg 1
158     fi
159 }
160
161 restart_plack()
162 {
163     local instancename=$1
164
165     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
166
167     if is_plack_running ${instancename}; then
168         stop_plack $instancename && start_plack $instancename
169     else
170         log_warning_msg "Plack not running for ${instancename}."
171         start_plack $instancename
172     fi
173 }
174
175 reload_plack()
176 {
177     local instancename=$1
178
179     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
180
181     if is_plack_running ${instancename}; then
182         log_daemon_msg "Reloading Plack daemon for ${instancename}"
183
184         if start-stop-daemon --pidfile ${PIDFILE} --user="${instancename}-koha" --stop --signal HUP; then
185             log_end_msg 0
186         else
187             log_end_msg 1
188         fi
189     else
190         log_daemon_msg "Error: Plack not running for ${instancename}"
191         log_end_msg 1
192     fi
193 }
194
195 enable_plack()
196 {
197     local instancename=$1
198     local instancefile=$(get_apache_config_for "$instancename")
199
200     alreadyopac=0
201     alreadyintra=0
202     failopac=0
203     failintra=0
204     if ! is_plack_enabled_opac $instancefile; then
205         # Uncomment the plack related lines for OPAC
206         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:\1:' "$instancefile"
207         if ! is_plack_enabled_opac $instancefile; then
208             [ "${quiet}" != "yes" ] && warn "Plack not enabled for ${instancename} OPAC"
209             failopac=1
210         else
211             [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename} OPAC"
212         fi
213     else
214         [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename} OPAC"
215         alreadyopac=1
216     fi
217     if ! is_plack_enabled_intranet $instancefile; then
218         # Uncomment the plack related lines for intranet
219         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:\1:' "$instancefile"
220         if ! is_plack_enabled_intranet $instancefile; then
221             [ "${quiet}" != "yes" ] && warn "Plack not enabled for ${instancename} Intranet"
222             failintra=1
223         else
224             [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename} Intranet"
225         fi
226     else
227         [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename} Intranet"
228         alreadyintra=1
229     fi
230
231     # Fail if it was already plack enabled.
232     if [ $alreadyopac -eq 1 ] && [ $alreadyintra -eq 1 ] ; then
233         return 1
234     elif [ "$alreadyopac" != "$alreadyintra" ]; then
235         [ "${quiet}" != "yes" ] && warn "$instancename had a plack configuration error. Please confirm it is corrected."
236     fi
237
238     # Succeed if both or any plack were turned on.
239     if [ $failopac -eq 0 ] ||  [ $failintra -eq 0 ] ; then
240         return 0
241     else
242         return 1
243     fi
244 }
245
246 disable_plack()
247 {
248     local instancename=$1
249     local instancefile=$(get_apache_config_for "$instancename")
250
251     alreadyopac=0
252     alreadyintra=0
253     failopac=0
254     failintra=0
255     if is_plack_enabled_opac $instancefile ; then
256         # Comment the plack related lines for OPAC
257         sed -i 's:^\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:#\1:' "$instancefile"
258         if is_plack_enabled_opac $instancefile ; then
259             [ "${quiet}" != "yes" ] && warn "Plack not disabled for ${instancename} OPAC"
260             failopac=1
261         else
262             [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename} OPAC"
263         fi
264     else
265         [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename} OPAC"
266         alreadyopac=1
267     fi
268     if is_plack_enabled_intranet $instancefile; then
269         # Comment the plack related lines for intranet
270         sed -i 's:^\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:#\1:' "$instancefile"
271         if is_plack_enabled_intranet $instancefile; then
272             [ "${quiet}" != "yes" ] && warn "Plack not disabled for ${instancename} Intranet"
273             failintra=1
274         else
275             [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename} Intranet"
276         fi
277     else
278         [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename} Intranet"
279         alreadyintra=1
280     fi
281
282     # Fail if it was already plack disabled.
283     if [ $alreadyopac -eq 1 ] &&  [ $alreadyintra -eq 1 ] ; then
284         return 1
285     elif [ "$alreadyopac" != "$alreadyintra" ]; then
286         [ "${quiet}" != "yes" ] && warn "$instancename had a plack configuration error. Please confirm it is corrected."
287     fi
288
289     # Succeed if both or any plack were turned off.
290     if  [ $failopac -eq 0 ] || [ $failintra -eq 0 ] ; then
291         return 0
292     else
293         return 1
294     fi
295 }
296
297 check_env_and_warn()
298 {
299     local apache_version_ok="no"
300     local required_modules="headers proxy_http"
301     local missing_modules=""
302
303     if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
304         apache_version_ok="yes"
305     fi
306
307     for module in ${required_modules}; do
308         if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q ${module}; then
309             missing_modules="${missing_modules}${module} "
310         fi
311     done
312
313     if [ "${apache_version_ok}" != "yes" ]; then
314         warn "WARNING: koha-plack requires Apache 2.4.x and you don't have that."
315     fi
316
317     if [ "${missing_modules}" != "" ]; then
318         cat 1>&2 <<EOM
319 WARNING: koha-plack requires some Apache modules that you are missing.
320 You can install them with:
321
322     sudo a2enmod ${missing_modules}
323
324 EOM
325
326     fi
327 }
328
329 _check_and_fix_perms()
330 {
331     local instance=$1
332
333     local files="/var/log/koha/${instance}/plack.log \
334                  /var/log/koha/${instance}/plack-error.log"
335
336     for file in ${files}
337     do
338         if [ ! -e "${file}" ]; then
339             touch ${file}
340         fi
341         chown "${instance}-koha":"${instance}-koha" ${file}
342     done
343 }
344
345 set_action()
346 {
347     if [ "$op" = "" ]; then
348         op=$1
349     else
350         die "Error: only one action can be specified."
351     fi
352 }
353
354 _do_instance() {
355     local name=$1
356     local PERL5LIB=$PERL5LIB
357     local KOHA_HOME=$KOHA_HOME
358     local DEV_INSTALL=$DEV_INSTALL
359
360     adjust_paths_dev_install $name
361     PERL5LIB=$PERL5LIB:$KOHA_HOME/installer:$KOHA_HOME/lib/installer
362     # If debug mode is enabled, add the debugger lib path
363     # to PERL5LIB if appropriate
364     #FIXME: many of these variables should be set in a higher scope
365     if [ "$debug_mode" = "yes" ]; then
366         if [ "$debugger_path" != "" ]; then
367             PERL5LIB="${debugger_path}":$PERL5LIB
368         fi
369         export PERL5DB="BEGIN { require q(${debugger_path}/perl5db.pl) }"
370         export PERLDB_OPTS="RemotePort=${debugger_location} async=1 LogFile=/var/log/koha/${name}/plack-debug.log"
371         export DBGP_IDEKEY=${debugger_key}
372         export PLACK_DEBUG=1
373         export PERL5OPT="-d"
374     fi
375
376     case $op in
377         "start")
378             start_plack $name
379             ;;
380         "stop")
381             stop_plack $name
382             ;;
383         "restart")
384             restart_plack $name
385             ;;
386         "reload")
387             reload_plack $name
388             ;;
389         "enable")
390             enable_plack $name
391             ;;
392         "disable")
393             disable_plack $name
394             ;;
395         *)
396             usage
397             ;;
398     esac
399 }
400
401 STARMAN=$(which starman)
402 op=""
403 quiet="no"
404 debug_mode="no"
405 debugger_key=""
406 debugger_location="localhost:9000"
407 debugger_path=""
408
409 # Read command line parameters
410 while [ $# -gt 0 ]; do
411
412     case "$1" in
413         -h|--help)
414             usage ; exit 0 ;;
415         -q|--quiet)
416             quiet="yes"
417             shift ;;
418         --start)
419             set_action "start"
420             shift ;;
421         --stop)
422             set_action "stop"
423             shift ;;
424         --restart)
425             set_action "restart"
426             shift ;;
427         --reload)
428             set_action "reload"
429             shift ;;
430         --enable)
431             set_action "enable"
432             shift ;;
433         --disable)
434             set_action "disable"
435             shift ;;
436         --debugger)
437             debug_mode="yes"
438             shift ;;
439         --debugger-key)
440             debugger_key="$2"
441             shift 2 ;;
442         --debugger-location)
443             debugger_location="$2"
444             shift 2 ;;
445         --debugger-path)
446             debugger_path="$2"
447             shift 2 ;;
448         -*)
449             die "Error: invalid option switch ($1)" ;;
450         *)
451             # We expect the remaining stuff are the instance names
452             break ;;
453     esac
454
455 done
456
457 [ "${quiet}" != "yes" ] && check_env_and_warn
458
459 export PERL5LIB
460 export DEV_INSTALL
461 export KOHA_HOME
462
463 if [ $# -gt 0 ]; then
464     # We have at least one instance name
465     for name in "$@"; do
466
467         if is_instance $name; then
468             _do_instance $name
469         else
470             if [ "$quiet" = "no" ]; then
471                 log_daemon_msg "Error: Invalid instance name $name"
472                 log_end_msg 1
473             fi
474         fi
475
476     done
477 else
478     if [ "$quiet" = "no" ]; then
479         warn "Error: you must provide at least one instance name"
480     fi
481 fi
482
483 exit 0