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