3 # Copyright 2015 Theke Solutions
4 # Copyright 2016 Koha-Suomi
6 # This file is part of Koha.
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.
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.
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/>.
23 . /lib/lsb/init-functions
25 # Read configuration variable file if it is present
26 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
28 # include helper functions
29 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
30 . "/usr/share/koha/bin/koha-functions.sh"
32 echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
38 local scriptname=$(basename $0)
43 This script lets you manage the plack daemons for your Koha instances.
46 $scriptname --start|--stop|--restart|--reload [--quiet|-q] instancename1 [instancename2...]
47 $scriptname --enable|--disable instancename1 [instancename2]
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
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
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"
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
84 _check_and_fix_perms $instancename
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"
91 instance_user="${instancename}-koha"
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}"
99 if [ "$DEV_INSTALL" = "1" ]; then
100 # Maybe we should switch off debug_mode if DEV_INSTALL is not set?
101 environment="development"
104 if [ "$debug_mode" = "yes" ]; then
105 environment="development"
107 logging="" # remote debugger takes care
108 max_requests_and_workers="--workers 1"
109 STARMAN="/usr/bin/perl -d ${STARMAN}"
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}"
117 if ! is_plack_running ${instancename}; then
118 export KOHA_CONF="/etc/koha/sites/${instancename}/koha-conf.xml"
120 log_daemon_msg "Starting Plack daemon for ${instancename}"
122 # Change to the instance's user dir
124 eval cd ~$instance_user
126 if ${STARMAN} ${STARMANOPTS}; then
131 # Go back to the original dir
135 log_daemon_msg "Error: Plack already running for ${instancename}"
142 local instancename=$1
144 local PIDFILE="/var/run/koha/${instancename}/plack.pid"
146 if is_plack_running ${instancename}; then
148 log_daemon_msg "Stopping Plack daemon for ${instancename}"
150 if start-stop-daemon --pidfile ${PIDFILE} --user="${instancename}-koha" --stop --retry=QUIT/30/KILL/5; then
156 log_daemon_msg "Error: Plack not running for ${instancename}"
163 local instancename=$1
165 local PIDFILE="/var/run/koha/${instancename}/plack.pid"
167 if is_plack_running ${instancename}; then
168 stop_plack $instancename && start_plack $instancename
170 log_warning_msg "Plack not running for ${instancename}."
171 start_plack $instancename
177 local instancename=$1
179 local PIDFILE="/var/run/koha/${instancename}/plack.pid"
181 if is_plack_running ${instancename}; then
182 log_daemon_msg "Reloading Plack daemon for ${instancename}"
184 if start-stop-daemon --pidfile ${PIDFILE} --user="${instancename}-koha" --stop --signal HUP; then
190 log_daemon_msg "Error: Plack not running for ${instancename}"
197 local instancename=$1
198 local instancefile=$(get_apache_config_for "$instancename")
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"
211 [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename} OPAC"
214 [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename} OPAC"
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"
224 [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename} Intranet"
227 [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename} Intranet"
231 # Fail if it was already plack enabled.
232 if [ $alreadyopac -eq 1 ] && [ $alreadyintra -eq 1 ] ; then
234 elif [ "$alreadyopac" != "$alreadyintra" ]; then
235 [ "${quiet}" != "yes" ] && warn "$instancename had a plack configuration error. Please confirm it is corrected."
238 # Succeed if both or any plack were turned on.
239 if [ $failopac -eq 0 ] || [ $failintra -eq 0 ] ; then
248 local instancename=$1
249 local instancefile=$(get_apache_config_for "$instancename")
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"
262 [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename} OPAC"
265 [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename} OPAC"
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"
275 [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename} Intranet"
278 [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename} Intranet"
282 # Fail if it was already plack disabled.
283 if [ $alreadyopac -eq 1 ] && [ $alreadyintra -eq 1 ] ; then
285 elif [ "$alreadyopac" != "$alreadyintra" ]; then
286 [ "${quiet}" != "yes" ] && warn "$instancename had a plack configuration error. Please confirm it is corrected."
289 # Succeed if both or any plack were turned off.
290 if [ $failopac -eq 0 ] || [ $failintra -eq 0 ] ; then
299 local apache_version_ok="no"
300 local required_modules="headers proxy_http"
301 local missing_modules=""
303 if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
304 apache_version_ok="yes"
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} "
313 if [ "${apache_version_ok}" != "yes" ]; then
314 warn "WARNING: koha-plack requires Apache 2.4.x and you don't have that."
317 if [ "${missing_modules}" != "" ]; then
319 WARNING: koha-plack requires some Apache modules that you are missing.
320 You can install them with:
322 sudo a2enmod ${missing_modules}
329 _check_and_fix_perms()
333 local files="/var/log/koha/${instance}/plack.log \
334 /var/log/koha/${instance}/plack-error.log"
338 if [ ! -e "${file}" ]; then
341 chown "${instance}-koha":"${instance}-koha" ${file}
347 if [ "$op" = "" ]; then
350 die "Error: only one action can be specified."
356 local PERL5LIB=$PERL5LIB
357 local KOHA_HOME=$KOHA_HOME
358 local DEV_INSTALL=$DEV_INSTALL
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
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}
401 STARMAN=$(which starman)
406 debugger_location="localhost:9000"
409 # Read command line parameters
410 while [ $# -gt 0 ]; do
443 debugger_location="$2"
449 die "Error: invalid option switch ($1)" ;;
451 # We expect the remaining stuff are the instance names
457 [ "${quiet}" != "yes" ] && check_env_and_warn
463 if [ $# -gt 0 ]; then
464 # We have at least one instance name
467 if is_instance $name; then
470 if [ "$quiet" = "no" ]; then
471 log_daemon_msg "Error: Invalid instance name $name"
478 if [ "$quiet" = "no" ]; then
479 warn "Error: you must provide at least one instance name"