Bug 20581: Unit tests for status_alias
[koha.git] / debian / scripts / koha-plack
blob68f899113b751d7b2ae24fb7011e7697e1a07a01
1 #!/bin/bash
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/>.
21 set -e
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"
31 else
32 echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
33 exit 1
36 usage()
38 local scriptname=$(basename $0)
40 cat <<EOF
41 $scriptname
43 This script lets you manage the plack daemons for your Koha instances.
45 Usage:
46 $scriptname --start|--stop|--restart [--quiet|-q] instancename1 [instancename2...]
47 $scriptname --enable|--disable instancename1 [instancename2]
48 $scriptname -h|--help
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
64 EOF
67 start_plack()
69 local instancename=$1
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"
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
81 _check_and_fix_perms $instancename
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"
88 instance_user="${instancename}-koha"
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}"
96 if [ "$DEV_INSTALL" = "1" ]; then
97 # Maybe we should switch off debug_mode if DEV_INSTALL is not set?
98 environment="development"
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}"
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}"
114 if ! is_plack_running ${instancename}; then
115 export KOHA_CONF="/etc/koha/sites/${instancename}/koha-conf.xml"
117 log_daemon_msg "Starting Plack daemon for ${instancename}"
119 # Change to the instance's user dir
120 current_dir=$(pwd)
121 eval cd ~$instance_user
123 if ${STARMAN} ${STARMANOPTS}; then
124 log_end_msg 0
125 else
126 log_end_msg 1
128 # Go back to the original dir
129 cd "$current_dir"
131 else
132 log_daemon_msg "Error: Plack already running for ${instancename}"
133 log_end_msg 1
137 stop_plack()
139 local instancename=$1
141 local PIDFILE="/var/run/koha/${instancename}/plack.pid"
143 if is_plack_running ${instancename}; then
145 log_daemon_msg "Stopping Plack daemon for ${instancename}"
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
152 else
153 log_daemon_msg "Error: Plack not running for ${instancename}"
154 log_end_msg 1
158 restart_plack()
160 local instancename=$1
162 local PIDFILE="/var/run/koha/${instancename}/plack.pid"
164 if is_plack_running ${instancename}; then
166 log_daemon_msg "Restarting Plack daemon for ${instancename}"
168 if stop_plack $instancename && start_plack $instancename; then
169 log_end_msg 0
170 else
171 log_end_msg 1
173 else
174 log_daemon_msg "Error: Plack not running for ${instancename}"
175 log_end_msg 1
179 enable_plack()
181 local instancename=$1
182 local instancefile=$(get_apache_config_for "$instancename")
184 if ! is_plack_enabled $instancename; then
185 # Uncomment the plack related lines for OPAC and intranet
186 sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:\1:' "$instancefile"
187 sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:\1:' "$instancefile"
188 [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename}"
189 return 0
190 else
191 [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename}"
192 return 1
196 disable_plack()
198 local instancename=$1
199 local instancefile=$(get_apache_config_for "$instancename")
201 if is_plack_enabled $instancename; then
202 # Comment out the plack related lines for OPAC and intranet
203 sed -i 's:^\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:#\1:' "$instancefile"
204 sed -i 's:^\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:#\1:' "$instancefile"
205 [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename}"
206 return 0
207 else
208 [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename}"
209 return 1
213 check_env_and_warn()
215 local apache_version_ok="no"
216 local required_modules="headers proxy_http"
217 local missing_modules=""
219 if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
220 apache_version_ok="yes"
223 for module in ${required_modules}; do
224 if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q ${module}; then
225 missing_modules="${missing_modules}${module} "
227 done
229 if [ "${apache_version_ok}" != "yes" ]; then
230 warn "WARNING: koha-plack requires Apache 2.4.x and you don't have that."
233 if [ "${missing_modules}" != "" ]; then
234 cat 1>&2 <<EOM
235 WARNING: koha-plack requires some Apache modules that you are missing.
236 You can install them with:
238 sudo a2enmod ${missing_modules}
245 _check_and_fix_perms()
247 local instance=$1
249 local files="/var/log/koha/${instance}/plack.log \
250 /var/log/koha/${instance}/plack-error.log"
252 for file in ${files}
254 if [ ! -e "${file}" ]; then
255 touch ${file}
257 chown "${instance}-koha":"${instance}-koha" ${file}
258 done
261 set_action()
263 if [ "$op" = "" ]; then
264 op=$1
265 else
266 die "Error: only one action can be specified."
270 STARMAN=$(which starman)
271 op=""
272 quiet="no"
273 debug_mode="no"
274 debugger_key=""
275 debugger_location="localhost:9000"
276 debugger_path=""
278 # Read command line parameters
279 while [ $# -gt 0 ]; do
281 case "$1" in
282 -h|--help)
283 usage ; exit 0 ;;
284 -q|--quiet)
285 quiet="yes"
286 shift ;;
287 --start)
288 set_action "start"
289 shift ;;
290 --stop)
291 set_action "stop"
292 shift ;;
293 --restart)
294 set_action "restart"
295 shift ;;
296 --enable)
297 set_action "enable"
298 shift ;;
299 --disable)
300 set_action "disable"
301 shift ;;
302 --debugger)
303 debug_mode="yes"
304 shift ;;
305 --debugger-key)
306 debugger_key="$2"
307 shift 2 ;;
308 --debugger-location)
309 debugger_location="$2"
310 shift 2 ;;
311 --debugger-path)
312 debugger_path="$2"
313 shift 2 ;;
315 die "Error: invalid option switch ($1)" ;;
317 # We expect the remaining stuff are the instance names
318 break ;;
319 esac
321 done
323 [ "${quiet}" != "yes" ] && check_env_and_warn
325 if [ $# -gt 0 ]; then
326 # We have at least one instance name
327 for name in "$@"; do
329 if is_instance $name; then
331 adjust_paths_dev_install $name
332 export DEV_INSTALL
333 export KOHA_HOME
334 PERL5LIB=$PERL5LIB:$KOHA_HOME/installer:$KOHA_HOME/lib/installer
335 # If debug mode is enabled, add the debugger lib path
336 # to PERL5LIB if appropriate
337 if [ "$debug_mode" = "yes" ]; then
338 if [ "$debugger_path" != "" ]; then
339 PERL5LIB="${debugger_path}":$PERL5LIB
341 export PERL5DB="BEGIN { require q(${debugger_path}/perl5db.pl) }"
342 export PERLDB_OPTS="RemotePort=${debugger_location} async=1 LogFile=/var/log/koha/${name}/plack-debug.log"
343 export DBGP_IDEKEY=${debugger_key}
344 export PLACK_DEBUG=1
345 export PERL5OPT="-d"
348 export PERL5LIB
350 case $op in
351 "start")
352 start_plack $name
354 "stop")
355 stop_plack $name
357 "restart")
358 restart_plack $name
360 "enable")
361 enable_plack $name
363 "disable")
364 disable_plack $name
367 usage
369 esac
371 else
372 if [ "$quiet" = "no" ]; then
373 log_daemon_msg "Error: Invalid instance name $name"
374 log_end_msg 1
378 done
379 else
380 if [ "$quiet" = "no" ]; then
381 warn "Error: you must provide at least one instance name"
385 exit 0