Bug 20692: Extreme toggle checking for plack
[koha-equinox.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
166         log_daemon_msg "Restarting Plack daemon for ${instancename}"
167
168         if stop_plack $instancename && start_plack $instancename; then
169             log_end_msg 0
170         else
171             log_end_msg 1
172         fi
173     else
174         log_daemon_msg "Error: Plack not running for ${instancename}"
175         log_end_msg 1
176     fi
177 }
178
179 enable_plack()
180 {
181     local instancename=$1
182     local instancefile=$(get_apache_config_for "$instancename")
183
184     alreadyopac=0
185     alreadyintra=0
186     failopac=0
187     failintra=0
188     if ! is_plack_enabled_opac $instancefile; then
189         # Uncomment the plack related lines for OPAC
190         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:\1:' "$instancefile"
191         if ! is_plack_enabled_opac $instancefile; then
192             [ "${quiet}" != "yes" ] && warn "Plack not enabled for ${instancename} OPAC"
193             failopac=1
194         else
195             [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename} OPAC"
196         fi
197     else
198         [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename} OPAC"
199         alreadyopac=1
200     fi
201     if ! is_plack_enabled_intranet $instancefile; then
202         # Uncomment the plack related lines for intranet
203         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:\1:' "$instancefile"
204         if ! is_plack_enabled_intranet $instancefile; then
205             [ "${quiet}" != "yes" ] && warn "Plack not enabled for ${instancename} Intranet"
206             failintra=1
207         else
208             [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename} Intranet"
209         fi
210     else
211         [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename} Intranet"
212         alreadyintra=1
213     fi
214
215     # Fail if it was already plack enabled.
216     if [ $alreadyopac -eq 1 ] && [ $alreadyintra -eq 1 ] ; then
217         return 1
218     elif [ "$alreadyopac" != "$alreadyintra" ]; then
219         [ "${quiet}" != "yes" ] && warn "$instancename had a plack configuration error. Please confirm it is corrected."
220     fi
221
222     # Succeed if both or any plack were turned on.
223     if [ $failopac -eq 0 ] ||  [ $failintra -eq 0 ] ; then
224         return 0
225     else
226         return 1
227     fi
228 }
229
230 disable_plack()
231 {
232     local instancename=$1
233     local instancefile=$(get_apache_config_for "$instancename")
234
235     alreadyopac=0
236     alreadyintra=0
237     failopac=0
238     failintra=0
239     if is_plack_enabled_opac $instancefile ; then
240         # Comment the plack related lines for OPAC
241         sed -i 's:^\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:#\1:' "$instancefile"
242         if is_plack_enabled_opac $instancefile ; then
243             [ "${quiet}" != "yes" ] && warn "Plack not disabled for ${instancename} OPAC"
244             failopac=1
245         else
246             [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename} OPAC"
247         fi
248     else
249         [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename} OPAC"
250         alreadyopac=1
251     fi
252     if is_plack_enabled_intranet $instancefile; then
253         # Comment the plack related lines for intranet
254         sed -i 's:^\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:#\1:' "$instancefile"
255         if is_plack_enabled_intranet $instancefile; then
256             [ "${quiet}" != "yes" ] && warn "Plack not disabled for ${instancename} Intranet"
257             failintra=1
258         else
259             [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename} Intranet"
260         fi
261     else
262         [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename} Intranet"
263         alreadyintra=1
264     fi
265
266     # Fail if it was already plack disabled.
267     if [ $alreadyopac -eq 1 ] &&  [ $alreadyintra -eq 1 ] ; then
268         return 1
269     elif [ "$alreadyopac" != "$alreadyintra" ]; then
270         [ "${quiet}" != "yes" ] && warn "$instancename had a plack configuration error. Please confirm it is corrected."
271     fi
272
273     # Succeed if both or any plack were turned off.
274     if  [ $failopac -eq 0 ] || [ $failintra -eq 0 ] ; then
275         return 0
276     else
277         return 1
278     fi
279 }
280
281 check_env_and_warn()
282 {
283     local apache_version_ok="no"
284     local required_modules="headers proxy_http"
285     local missing_modules=""
286
287     if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
288         apache_version_ok="yes"
289     fi
290
291     for module in ${required_modules}; do
292         if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q ${module}; then
293             missing_modules="${missing_modules}${module} "
294         fi
295     done
296
297     if [ "${apache_version_ok}" != "yes" ]; then
298         warn "WARNING: koha-plack requires Apache 2.4.x and you don't have that."
299     fi
300
301     if [ "${missing_modules}" != "" ]; then
302         cat 1>&2 <<EOM
303 WARNING: koha-plack requires some Apache modules that you are missing.
304 You can install them with:
305
306     sudo a2enmod ${missing_modules}
307
308 EOM
309
310     fi
311 }
312
313 _check_and_fix_perms()
314 {
315     local instance=$1
316
317     local files="/var/log/koha/${instance}/plack.log \
318                  /var/log/koha/${instance}/plack-error.log"
319
320     for file in ${files}
321     do
322         if [ ! -e "${file}" ]; then
323             touch ${file}
324         fi
325         chown "${instance}-koha":"${instance}-koha" ${file}
326     done
327 }
328
329 set_action()
330 {
331     if [ "$op" = "" ]; then
332         op=$1
333     else
334         die "Error: only one action can be specified."
335     fi
336 }
337
338 STARMAN=$(which starman)
339 op=""
340 quiet="no"
341 debug_mode="no"
342 debugger_key=""
343 debugger_location="localhost:9000"
344 debugger_path=""
345
346 # Read command line parameters
347 while [ $# -gt 0 ]; do
348
349     case "$1" in
350         -h|--help)
351             usage ; exit 0 ;;
352         -q|--quiet)
353             quiet="yes"
354             shift ;;
355         --start)
356             set_action "start"
357             shift ;;
358         --stop)
359             set_action "stop"
360             shift ;;
361         --restart)
362             set_action "restart"
363             shift ;;
364         --enable)
365             set_action "enable"
366             shift ;;
367         --disable)
368             set_action "disable"
369             shift ;;
370         --debugger)
371             debug_mode="yes"
372             shift ;;
373         --debugger-key)
374             debugger_key="$2"
375             shift 2 ;;
376         --debugger-location)
377             debugger_location="$2"
378             shift 2 ;;
379         --debugger-path)
380             debugger_path="$2"
381             shift 2 ;;
382         -*)
383             die "Error: invalid option switch ($1)" ;;
384         *)
385             # We expect the remaining stuff are the instance names
386             break ;;
387     esac
388
389 done
390
391 [ "${quiet}" != "yes" ] && check_env_and_warn
392
393 if [ $# -gt 0 ]; then
394     # We have at least one instance name
395     for name in "$@"; do
396
397         if is_instance $name; then
398
399             adjust_paths_dev_install $name
400             export DEV_INSTALL
401             export KOHA_HOME
402             PERL5LIB=$PERL5LIB:$KOHA_HOME/installer:$KOHA_HOME/lib/installer
403             # If debug mode is enabled, add the debugger lib path
404             # to PERL5LIB if appropriate
405             if [ "$debug_mode" = "yes" ]; then
406                 if [ "$debugger_path" != "" ]; then
407                     PERL5LIB="${debugger_path}":$PERL5LIB
408                 fi
409                 export PERL5DB="BEGIN { require q(${debugger_path}/perl5db.pl) }"
410                 export PERLDB_OPTS="RemotePort=${debugger_location} async=1 LogFile=/var/log/koha/${name}/plack-debug.log"
411                 export DBGP_IDEKEY=${debugger_key}
412                 export PLACK_DEBUG=1
413                 export PERL5OPT="-d"
414             fi
415
416             export PERL5LIB
417
418             case $op in
419                 "start")
420                     start_plack $name
421                     ;;
422                 "stop")
423                     stop_plack $name
424                     ;;
425                 "restart")
426                     restart_plack $name
427                     ;;
428                 "enable")
429                     enable_plack $name
430                     ;;
431                 "disable")
432                     disable_plack $name
433                     ;;
434                 *)
435                     usage
436                     ;;
437             esac
438
439         else
440             if [ "$quiet" = "no" ]; then
441                 log_daemon_msg "Error: Invalid instance name $name"
442                 log_end_msg 1
443             fi
444         fi
445
446     done
447 else
448     if [ "$quiet" = "no" ]; then
449         warn "Error: you must provide at least one instance name"
450     fi
451 fi
452
453 exit 0