5d2799a3d4490d565e7eca8612eda52186609a1d
[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
88         log_daemon_msg "Starting Plack daemon for ${instancename}"
89
90         if ${STARMAN} ${STARMANOPTS}; then
91             log_end_msg 0
92         else
93             log_end_msg 1
94         fi
95     else
96         log_daemon_msg "Error: Plack already running for ${instancename}"
97         log_end_msg 1
98     fi
99 }
100
101 stop_plack()
102 {
103     local instancename=$1
104
105     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
106
107     if is_plack_running ${instancename}; then
108
109         log_daemon_msg "Stopping Plack daemon for ${instancename}"
110
111         if start-stop-daemon --pidfile ${PIDFILE} --stop --retry=TERM/30/KILL/5; then
112             log_end_msg 0
113         else
114             log_end_msg 1
115         fi
116     else
117         log_daemon_msg "Error: Plack not running for ${instancename}"
118         log_end_msg 1
119     fi
120 }
121
122 restart_plack()
123 {
124     local instancename=$1
125
126     local PIDFILE="/var/run/koha/${instancename}/plack.pid"
127
128     if is_plack_running ${instancename}; then
129
130         log_daemon_msg "Restarting Plack daemon for ${instancename}"
131
132         if stop_plack $instancename && start_plack $instancename; then
133             log_end_msg 0
134         else
135             log_end_msg 1
136         fi
137     else
138         log_daemon_msg "Error: Plack not running for ${instancename}"
139         log_end_msg 1
140     fi
141 }
142
143 enable_plack()
144 {
145     local instancename=$1
146     local instancefile=$(get_apache_config_for "$instancename")
147
148     if ! is_plack_enabled $instancename; then
149         # Uncomment the plack related lines for OPAC and intranet
150         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:\1:' "$instancefile"
151         sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:\1:' "$instancefile"
152         [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename}"
153         return 0
154     else
155         [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename}"
156         return 1
157     fi
158 }
159
160 disable_plack()
161 {
162     local instancename=$1
163     local instancefile=$(get_apache_config_for "$instancename")
164
165     if is_plack_enabled $instancename; then
166         # Comment out the plack related lines for OPAC and intranet
167         sed -i 's:^\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:#\1:' "$instancefile"
168         sed -i 's:^\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:#\1:' "$instancefile"
169         [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename}"
170         return 0
171     else
172         [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename}"
173         return 1
174     fi
175 }
176
177 check_env_and_warn()
178 {
179     local apache_version_ok="no"
180     local required_modules="headers proxy_http"
181     local missing_modules=""
182
183     if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
184         apache_version_ok="yes"
185     fi
186
187     for module in ${required_modules}; do
188         if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q ${module}; then
189             missing_modules="${missing_modules}${module} "
190         fi
191     done
192
193     if [ "${apache_version_ok}" != "yes" ]; then
194         warn "WARNING: koha-plack requires Apache 2.4.x and you don't have that."
195     fi
196
197     if [ "${missing_modules}" != "" ]; then
198         cat 1>&2 <<EOM
199 WARNING: koha-plack requires some Apache modules that you are missing.
200 You can install them with:
201
202     sudo a2enmod ${missing_modules}
203
204 EOM
205
206     fi
207 }
208
209 _check_and_fix_perms()
210 {
211     local instance=$1
212
213     local files="/var/log/koha/${instance}/plack.log \
214                  /var/log/koha/${instance}/plack-error.log"
215
216     for file in ${files}
217     do
218         if [ ! -e "${file}" ]; then
219             touch ${file}
220         fi
221         chown "${instance}-koha":"${instance}-koha" ${file}
222     done
223 }
224
225 set_action()
226 {
227     if [ "$op" = "" ]; then
228         op=$1
229     else
230         die "Error: only one action can be specified."
231     fi
232 }
233
234 STARMAN=$(which starman)
235 op=""
236 quiet="no"
237
238 # Read command line parameters
239 while [ $# -gt 0 ]; do
240
241     case "$1" in
242         -h|--help)
243             usage ; exit 0 ;;
244         -q|--quiet)
245             quiet="yes"
246             shift ;;
247         --start)
248             set_action "start"
249             shift ;;
250         --stop)
251             set_action "stop"
252             shift ;;
253         --restart)
254             set_action "restart"
255             shift ;;
256         --enable)
257             set_action "enable"
258             shift ;;
259         --disable)
260             set_action "disable"
261             shift ;;
262         -*)
263             die "Error: invalid option switch ($1)" ;;
264         *)
265             # We expect the remaining stuff are the instance names
266             break ;;
267     esac
268
269 done
270
271 [ "${quiet}" != "yes" ] && check_env_and_warn
272
273 if [ $# -gt 0 ]; then
274     # We have at least one instance name
275     for name in "$@"; do
276
277         if is_instance $name; then
278
279             adjust_paths_dev_install $name
280             export DEV_INSTALL KOHA_HOME PERL5LIB=$PERL5LIB:$KOHA_HOME/installer:$KOHA_HOME/lib/installer
281
282             case $op in
283                 "start")
284                     start_plack $name
285                     ;;
286                 "stop")
287                     stop_plack $name
288                     ;;
289                 "restart")
290                     restart_plack $name
291                     ;;
292                 "enable")
293                     enable_plack $name
294                     ;;
295                 "disable")
296                     disable_plack $name
297                     ;;
298                 *)
299                     usage
300                     ;;
301             esac
302
303         else
304             if [ "$quiet" = "no" ]; then
305                 log_daemon_msg "Error: Invalid instance name $name"
306                 log_end_msg 1
307             fi
308         fi
309
310     done
311 else
312     if [ "$quiet" = "no" ]; then
313         warn "Error: you must provide at least one instance name"
314     fi
315 fi
316
317 exit 0