Bug 19546: Run starman from the instance's home dir
[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 [ "$debug_mode" = "yes" ]; then
97         environment="development"
98         daemonize=""
99         logging="" # remote debugger takes care
100         max_requests_and_workers="--workers 1"
101         STARMAN="/usr/bin/perl -d ${STARMAN}"
102     fi
103
104     STARMANOPTS="-M FindBin ${max_requests_and_workers} \
105                  --user=${instance_user} --group ${instancename}-koha \
106                  --pid ${PIDFILE} ${daemonize} ${logging} \
107                  -E ${environment} --socket ${PLACKSOCKET} ${PSGIFILE}"
108
109     if ! is_plack_running ${instancename}; then
110         export KOHA_CONF="/etc/koha/sites/${instancename}/koha-conf.xml"
111
112         log_daemon_msg "Starting Plack daemon for ${instancename}"
113
114         # Change to the instance's user dir
115         current_dir=$(pwd)
116         eval cd ~$instance_user
117
118         if ${STARMAN} ${STARMANOPTS}; then
119             log_end_msg 0
120         else
121             log_end_msg 1
122         fi
123         # Go back to the original dir
124         cd $current_dir
125
126     else
127         log_daemon_msg "Error: Plack already running for ${instancename}"
128         log_end_msg 1
129     fi
130 }
131
132 stop_plack()
133 {
134     local instancename=$1
135
136     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
137
138     if is_plack_running ${instancename}; then
139
140         log_daemon_msg "Stopping Plack daemon for ${instancename}"
141
142         if start-stop-daemon --pidfile ${PIDFILE} --stop --retry=TERM/30/KILL/5; then
143             log_end_msg 0
144         else
145             log_end_msg 1
146         fi
147     else
148         log_daemon_msg "Error: Plack not running for ${instancename}"
149         log_end_msg 1
150     fi
151 }
152
153 restart_plack()
154 {
155     local instancename=$1
156
157     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
158
159     if is_plack_running ${instancename}; then
160
161         log_daemon_msg "Restarting Plack daemon for ${instancename}"
162
163         if stop_plack $instancename && start_plack $instancename; then
164             log_end_msg 0
165         else
166             log_end_msg 1
167         fi
168     else
169         log_daemon_msg "Error: Plack not running for ${instancename}"
170         log_end_msg 1
171     fi
172 }
173
174 enable_plack()
175 {
176     local instancename=$1
177     local instancefile=$(get_apache_config_for "$instancename")
178
179     if ! is_plack_enabled $instancename; then
180         # Uncomment the plack related lines for OPAC and intranet
181         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:\1:' "$instancefile"
182         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:\1:' "$instancefile"
183         [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename}"
184         return 0
185     else
186         [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename}"
187         return 1
188     fi
189 }
190
191 disable_plack()
192 {
193     local instancename=$1
194     local instancefile=$(get_apache_config_for "$instancename")
195
196     if is_plack_enabled $instancename; then
197         # Comment out the plack related lines for OPAC and intranet
198         sed -i 's:^\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:#\1:' "$instancefile"
199         sed -i 's:^\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:#\1:' "$instancefile"
200         [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename}"
201         return 0
202     else
203         [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename}"
204         return 1
205     fi
206 }
207
208 check_env_and_warn()
209 {
210     local apache_version_ok="no"
211     local required_modules="headers proxy_http"
212     local missing_modules=""
213
214     if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
215         apache_version_ok="yes"
216     fi
217
218     for module in ${required_modules}; do
219         if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q ${module}; then
220             missing_modules="${missing_modules}${module} "
221         fi
222     done
223
224     if [ "${apache_version_ok}" != "yes" ]; then
225         warn "WARNING: koha-plack requires Apache 2.4.x and you don't have that."
226     fi
227
228     if [ "${missing_modules}" != "" ]; then
229         cat 1>&2 <<EOM
230 WARNING: koha-plack requires some Apache modules that you are missing.
231 You can install them with:
232
233     sudo a2enmod ${missing_modules}
234
235 EOM
236
237     fi
238 }
239
240 _check_and_fix_perms()
241 {
242     local instance=$1
243
244     local files="/var/log/koha/${instance}/plack.log \
245                  /var/log/koha/${instance}/plack-error.log"
246
247     for file in ${files}
248     do
249         if [ ! -e "${file}" ]; then
250             touch ${file}
251         fi
252         chown "${instance}-koha":"${instance}-koha" ${file}
253     done
254 }
255
256 set_action()
257 {
258     if [ "$op" = "" ]; then
259         op=$1
260     else
261         die "Error: only one action can be specified."
262     fi
263 }
264
265 STARMAN=$(which starman)
266 op=""
267 quiet="no"
268 debug_mode="no"
269 debugger_key=""
270 debugger_location="localhost:9000"
271 debugger_path=""
272
273 # Read command line parameters
274 while [ $# -gt 0 ]; do
275
276     case "$1" in
277         -h|--help)
278             usage ; exit 0 ;;
279         -q|--quiet)
280             quiet="yes"
281             shift ;;
282         --start)
283             set_action "start"
284             shift ;;
285         --stop)
286             set_action "stop"
287             shift ;;
288         --restart)
289             set_action "restart"
290             shift ;;
291         --enable)
292             set_action "enable"
293             shift ;;
294         --disable)
295             set_action "disable"
296             shift ;;
297         --debugger)
298             debug_mode="yes"
299             shift ;;
300         --debugger-key)
301             debugger_key="$2"
302             shift 2 ;;
303         --debugger-location)
304             debugger_location="$2"
305             shift 2 ;;
306         --debugger-path)
307             debugger_path="$2"
308             shift 2 ;;
309         -*)
310             die "Error: invalid option switch ($1)" ;;
311         *)
312             # We expect the remaining stuff are the instance names
313             break ;;
314     esac
315
316 done
317
318 [ "${quiet}" != "yes" ] && check_env_and_warn
319
320 if [ $# -gt 0 ]; then
321     # We have at least one instance name
322     for name in "$@"; do
323
324         if is_instance $name; then
325
326             adjust_paths_dev_install $name
327             export DEV_INSTALL
328             export KOHA_HOME
329             PERL5LIB=$PERL5LIB:$KOHA_HOME/installer:$KOHA_HOME/lib/installer
330             # If debug mode is enabled, add the debugger lib path
331             # to PERL5LIB if appropriate
332             if [ "$debug_mode" = "yes" ]; then
333                 if [ "$debugger_path" != "" ]; then
334                     PERL5LIB="${debugger_path}":$PERL5LIB
335                 fi
336                 export PERL5DB="BEGIN { require q(${debugger_path}/perl5db.pl) }"
337                 export PERLDB_OPTS="RemotePort=${debugger_location} async=1 LogFile=/var/log/koha/${name}/plack-debug.log"
338                 export DBGP_IDEKEY=${debugger_key}
339                 export PLACK_DEBUG=1
340                 export PERL5OPT="-d"
341             fi
342
343             export PERL5LIB
344
345             case $op in
346                 "start")
347                     start_plack $name
348                     ;;
349                 "stop")
350                     stop_plack $name
351                     ;;
352                 "restart")
353                     restart_plack $name
354                     ;;
355                 "enable")
356                     enable_plack $name
357                     ;;
358                 "disable")
359                     disable_plack $name
360                     ;;
361                 *)
362                     usage
363                     ;;
364             esac
365
366         else
367             if [ "$quiet" = "no" ]; then
368                 log_daemon_msg "Error: Invalid instance name $name"
369                 log_end_msg 1
370             fi
371         fi
372
373     done
374 else
375     if [ "$quiet" = "no" ]; then
376         warn "Error: you must provide at least one instance name"
377     fi
378 fi
379
380 exit 0