245d4a52e418e6e75427e9d06dc6b3e7b55e2e8b
[koha-ffzg.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     PLACK_MAX_REQUESTS="50"
78     PLACK_WORKERS="2"
79
80     if [[ $(get_plack_max_requests $instancename) ]]; then
81         PLACK_MAX_REQUESTS="$(get_plack_max_requests $instancename)"
82     fi
83
84     if [[ $(get_plack_workers $instancename) ]]; then
85         PLACK_WORKERS="$(get_plack_workers $instancename)"
86     fi
87
88     STARMANOPTS="-M FindBin --max-requests ${PLACK_MAX_REQUESTS} --workers ${PLACK_WORKERS} \
89                  --user=${instancename}-koha --group ${instancename}-koha \
90                  --pid ${PIDFILE} \
91                  --daemonize \
92                  --access-log /var/log/koha/${instancename}/plack.log \
93                  --error-log /var/log/koha/${instancename}/plack-error.log \
94                  -E deployment --socket ${PLACKSOCKET} ${PSGIFILE}"
95
96     if ! is_plack_running ${instancename}; then
97         export KOHA_CONF="/etc/koha/sites/${instancename}/koha-conf.xml"
98
99         log_daemon_msg "Starting Plack daemon for ${instancename}"
100
101         if ${STARMAN} ${STARMANOPTS}; then
102             log_end_msg 0
103         else
104             log_end_msg 1
105         fi
106     else
107         log_daemon_msg "Error: Plack already running for ${instancename}"
108         log_end_msg 1
109     fi
110 }
111
112 stop_plack()
113 {
114     local instancename=$1
115
116     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
117
118     if is_plack_running ${instancename}; then
119
120         log_daemon_msg "Stopping Plack daemon for ${instancename}"
121
122         if start-stop-daemon --pidfile ${PIDFILE} --stop; then
123             log_end_msg 0
124         else
125             log_end_msg 1
126         fi
127     else
128         log_daemon_msg "Error: Plack not running for ${instancename}"
129         log_end_msg 1
130     fi
131 }
132
133 restart_plack()
134 {
135     local instancename=$1
136
137     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
138
139     if is_plack_running ${instancename}; then
140
141         log_daemon_msg "Restarting Plack daemon for ${instancename}"
142
143         if stop_plack $instancename && start_plack $instancename; then
144             log_end_msg 0
145         else
146             log_end_msg 1
147         fi
148     else
149         log_daemon_msg "Error: Plack not running for ${instancename}"
150         log_end_msg 1
151     fi
152 }
153
154 enable_plack()
155 {
156     local instancename=$1
157     local instancefile=$(get_apache_config_for "$instancename")
158
159     if ! is_plack_enabled $instancename; then
160         # Uncomment the plack related lines for OPAC and intranet
161         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:\1:' "$instancefile"
162         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:\1:' "$instancefile"
163         [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename}"
164         return 0
165     else
166         [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename}"
167         return 1
168     fi
169 }
170
171 disable_plack()
172 {
173     local instancename=$1
174     local instancefile=$(get_apache_config_for "$instancename")
175
176     if is_plack_enabled $instancename; then
177         # Comment out the plack related lines for OPAC and intranet
178         sed -i 's:^\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:#\1:' "$instancefile"
179         sed -i 's:^\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:#\1:' "$instancefile"
180         [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename}"
181         return 0
182     else
183         [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename}"
184         return 1
185     fi
186 }
187
188 check_env_and_warn()
189 {
190     local apache_version_ok="no"
191     local required_modules="headers proxy_http"
192     local missing_modules=""
193
194     if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
195         apache_version_ok="yes"
196     fi
197
198     for module in ${required_modules}; do
199         if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q ${module}; then
200             missing_modules="${missing_modules}${module} "
201         fi
202     done
203
204     if [ "${apache_version_ok}" != "yes" ]; then
205         warn "WARNING: koha-plack requires Apache 2.4.x and you don't have that."
206     fi
207
208     if [ "${missing_modules}" != "" ]; then
209         cat 1>&2 <<EOM
210 WARNING: koha-plack requires some Apache modules that you are missing.
211 You can install them with:
212
213     sudo a2enmod ${missing_modules}
214
215 EOM
216
217     fi
218 }
219
220 _check_and_fix_perms()
221 {
222     local instance=$1
223
224     local files="/var/log/koha/${instance}/plack.log \
225                  /var/log/koha/${instance}/plack-error.log"
226
227     for file in ${files}
228     do
229         if [ ! -e "${file}" ]; then
230             touch ${file}
231         fi
232         chown "${instance}-koha":"${instance}-koha" ${file}
233     done
234 }
235
236 set_action()
237 {
238     if [ "$op" = "" ]; then
239         op=$1
240     else
241         die "Error: only one action can be specified."
242     fi
243 }
244
245 adjust_paths()
246 {
247     # Optionally use alternative paths for a dev install
248     adjust_paths_dev_install $1
249
250     # PERL5LIB has been read from etc/default, add lib/installer
251     # export some vars (for plack.psgi)
252     export KOHA_HOME
253     if [ "$DEV_INSTALL" = "" ]; then
254         export PERL5LIB=$PERL5LIB:$KOHA_HOME/lib/installer
255     else
256         export DEV_INSTALL
257         export PERL5LIB=$PERL5LIB:$KOHA_HOME/installer
258     fi
259 }
260
261 get_plack_max_requests() {
262     xmlstarlet sel -t -v 'yazgfs/config/plack_max_requests' "/etc/koha/sites/$1/koha-conf.xml"
263 }
264
265 get_plack_workers() {
266     xmlstarlet sel -t -v 'yazgfs/config/plack_workers' "/etc/koha/sites/$1/koha-conf.xml"
267 }
268
269 STARMAN=$(which starman)
270 op=""
271 quiet="no"
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         -*)
298             die "Error: invalid option switch ($1)" ;;
299         *)
300             # We expect the remaining stuff are the instance names
301             break ;;
302     esac
303
304 done
305
306 [ "${quiet}" != "yes" ] && check_env_and_warn
307
308 if [ $# -gt 0 ]; then
309     # We have at least one instance name
310     for name in "$@"; do
311
312         if is_instance $name; then
313
314             adjust_paths $name
315             case $op in
316                 "start")
317                     start_plack $name
318                     ;;
319                 "stop")
320                     stop_plack $name
321                     ;;
322                 "restart")
323                     restart_plack $name
324                     ;;
325                 "enable")
326                     enable_plack $name
327                     ;;
328                 "disable")
329                     disable_plack $name
330                     ;;
331                 *)
332                     usage
333                     ;;
334             esac
335
336         else
337             if [ "$quiet" = "no" ]; then
338                 log_daemon_msg "Error: Invalid instance name $name"
339                 log_end_msg 1
340             fi
341         fi
342
343     done
344 else
345     if [ "$quiet" = "no" ]; then
346         warn "Error: you must provide at least one instance name"
347     fi
348 fi
349
350 exit 0