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