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