Bug 17494: (QA followup) Fix exception name
[koha.git] / debian / scripts / koha-plack
1 #!/bin/bash
2 #
3 # Copyright 2015 Theke Solutions
4 #
5 # This file is part of Koha.
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 set -e
21
22 . /lib/lsb/init-functions
23
24 # Read configuration variable file if it is present
25 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
26
27 # include helper functions
28 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
29     . "/usr/share/koha/bin/koha-functions.sh"
30 else
31     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
32     exit 1
33 fi
34
35 usage()
36 {
37     local scriptname=$(basename $0)
38
39     cat <<EOF
40 $scriptname
41
42 This script lets you manage the plack daemons for your Koha instances.
43
44 Usage:
45 $scriptname --start|--stop|--restart [--quiet|-q] instancename1 [instancename2...]
46 $scriptname --enable|--disable instancename1 [instancename2]
47 $scriptname -h|--help
48
49     --start               Start the plack daemon for the specified instances
50     --stop                Stop the plack daemon for the specified instances
51     --restart             Restart the plack daemon for the specified instances
52     --enable              Enable plack for the specified instances
53     --disable             Disable plack for the specified instances
54     --quiet|-q            Make the script quiet about non existent instance names
55                           (useful for calling from another scripts).
56     --help|-h             Display this help message
57
58 EOF
59 }
60
61 start_plack()
62 {
63     local instancename=$1
64
65     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
66     local PLACKSOCKET="/var/run/koha/${instancename}/plack.sock"
67     local PSGIFILE="/etc/koha/plack.psgi"
68     local NAME="${instancename}-koha-plack"
69
70     if [ -e "/etc/koha/sites/${instancename}/plack.psgi" ]; then
71         # pick instance-specific psgi file
72         PSGIFILE="/etc/koha/sites/${instancename}/plack.psgi"
73     fi # else stick with the default one
74
75     _check_and_fix_perms $instancename
76
77     STARMANOPTS="-M FindBin --max-requests 50 --workers 2 \
78                  --user=${instancename}-koha --group ${instancename}-koha \
79                  --pid ${PIDFILE} \
80                  --daemonize \
81                  --access-log /var/log/koha/${instancename}/plack.log \
82                  --error-log /var/log/koha/${instancename}/plack-error.log \
83                  -E deployment --socket ${PLACKSOCKET} ${PSGIFILE}"
84
85     if ! is_plack_running ${instancename}; then
86         export KOHA_CONF="/etc/koha/sites/${instancename}/koha-conf.xml"
87         export MEMCACHED_SERVERS=$(get_memcached_servers_for $instancename)
88         export MEMCACHED_NAMESPACE=$(get_memcached_namespace_for $instancename)
89
90         log_daemon_msg "Starting Plack daemon for ${instancename}"
91
92         if ${STARMAN} ${STARMANOPTS}; then
93             log_end_msg 0
94         else
95             log_end_msg 1
96         fi
97     else
98         log_daemon_msg "Error: Plack already running for ${instancename}"
99         log_end_msg 1
100     fi
101 }
102
103 stop_plack()
104 {
105     local instancename=$1
106
107     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
108
109     if is_plack_running ${instancename}; then
110
111         log_daemon_msg "Stopping Plack daemon for ${instancename}"
112
113         if start-stop-daemon --pidfile ${PIDFILE} --stop; then
114             log_end_msg 0
115         else
116             log_end_msg 1
117         fi
118     else
119         log_daemon_msg "Error: Plack not running for ${instancename}"
120         log_end_msg 1
121     fi
122 }
123
124 restart_plack()
125 {
126     local instancename=$1
127
128     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
129
130     if is_plack_running ${instancename}; then
131
132         log_daemon_msg "Restarting Plack daemon for ${instancename}"
133
134         if stop_plack $instancename && start_plack $instancename; then
135             log_end_msg 0
136         else
137             log_end_msg 1
138         fi
139     else
140         log_daemon_msg "Error: Plack not running for ${instancename}"
141         log_end_msg 1
142     fi
143 }
144
145 enable_plack()
146 {
147     local instancename=$1
148     local instancefile=$(get_apache_config_for "$instancename")
149
150     if ! is_plack_enabled $instancename; then
151         # Uncomment the plack related lines for OPAC and intranet
152         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:\1:' "$instancefile"
153         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:\1:' "$instancefile"
154         [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename}"
155         return 0
156     else
157         [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename}"
158         return 1
159     fi
160 }
161
162 disable_plack()
163 {
164     local instancename=$1
165     local instancefile=$(get_apache_config_for "$instancename")
166
167     if is_plack_enabled $instancename; then
168         # Comment out the plack related lines for OPAC and intranet
169         sed -i 's:^\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:#\1:' "$instancefile"
170         sed -i 's:^\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:#\1:' "$instancefile"
171         [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename}"
172         return 0
173     else
174         [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename}"
175         return 1
176     fi
177 }
178
179 check_env_and_warn()
180 {
181     local apache_version_ok="no"
182     local required_modules="headers proxy_http"
183     local missing_modules=""
184
185     if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
186         apache_version_ok="yes"
187     fi
188
189     for module in ${required_modules}; do
190         if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q ${module}; then
191             missing_modules="${missing_modules}${module} "
192         fi
193     done
194
195     if [ "${apache_version_ok}" != "yes" ]; then
196         warn "WARNING: koha-plack requires Apache 2.4.x and you don't have that."
197     fi
198
199     if [ "${missing_modules}" != "" ]; then
200         cat 1>&2 <<EOM
201 WARNING: koha-plack requires some Apache modules that you are missing.
202 You can install them with:
203
204     sudo a2enmod ${missing_modules}
205
206 EOM
207
208     fi
209 }
210
211 _check_and_fix_perms()
212 {
213     local instance=$1
214
215     local files="/var/log/koha/${instance}/plack.log \
216                  /var/log/koha/${instance}/plack-error.log"
217
218     for file in ${files}
219     do
220         if [ ! -e "${file}" ]; then
221             touch ${file}
222         fi
223         chown "${instance}-koha":"${instance}-koha" ${file}
224     done
225 }
226
227 set_action()
228 {
229     if [ "$op" = "" ]; then
230         op=$1
231     else
232         die "Error: only one action can be specified."
233     fi
234 }
235
236 STARMAN=$(which starman)
237 op=""
238 quiet="no"
239
240 # Read command line parameters
241 while [ $# -gt 0 ]; do
242
243     case "$1" in
244         -h|--help)
245             usage ; exit 0 ;;
246         -q|--quiet)
247             quiet="yes"
248             shift ;;
249         --start)
250             set_action "start"
251             shift ;;
252         --stop)
253             set_action "stop"
254             shift ;;
255         --restart)
256             set_action "restart"
257             shift ;;
258         --enable)
259             set_action "enable"
260             shift ;;
261         --disable)
262             set_action "disable"
263             shift ;;
264         -*)
265             die "Error: invalid option switch ($1)" ;;
266         *)
267             # We expect the remaining stuff are the instance names
268             break ;;
269     esac
270
271 done
272
273 if [ -z $PERL5LIB ]; then
274     PERL5LIB="/usr/share/koha/lib"
275 fi
276
277 export PERL5LIB
278
279 [ "${quiet}" != "yes" ] && check_env_and_warn
280
281 if [ $# -gt 0 ]; then
282     # We have at least one instance name
283     for name in "$@"; do
284
285         if is_instance $name; then
286
287             case $op in
288                 "start")
289                     start_plack $name
290                     ;;
291                 "stop")
292                     stop_plack $name
293                     ;;
294                 "restart")
295                     restart_plack $name
296                     ;;
297                 "enable")
298                     enable_plack $name
299                     ;;
300                 "disable")
301                     disable_plack $name
302                     ;;
303                 *)
304                     usage
305                     ;;
306             esac
307
308         else
309             if [ "$quiet" = "no" ]; then
310                 log_daemon_msg "Error: Invalid instance name $name"
311                 log_end_msg 1
312             fi
313         fi
314
315     done
316 else
317     if [ "$quiet" = "no" ]; then
318         warn "Error: you must provide at least one instance name"
319     fi
320 fi
321
322 exit 0