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