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