Line 0
Link Here
|
|
|
1 |
#!/bin/bash |
2 |
# |
3 |
# koha-sip - Manage SIP Daemons for Koha instances |
4 |
# |
5 |
# Copyright 2015 Theke Solutions |
6 |
# Copyright 2018 Punsarn Asia |
7 |
# |
8 |
# This file is part of Koha. |
9 |
# |
10 |
# This program is free software: you can redistribute it and/or modify |
11 |
# it under the terms of the GNU General Public License as published by |
12 |
# the Free Software Foundation, either version 3 of the License, or |
13 |
# (at your option) any later version. |
14 |
# |
15 |
# This program is distributed in the hope that it will be useful, |
16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
# GNU General Public License for more details. |
19 |
# |
20 |
# You should have received a copy of the GNU General Public License |
21 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
22 |
|
23 |
set -e |
24 |
|
25 |
. /lib/lsb/init-functions |
26 |
|
27 |
# Read configuration variable file if it is present |
28 |
[ -r /etc/default/koha-common ] && . /etc/default/koha-common |
29 |
|
30 |
# include helper functions |
31 |
if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then |
32 |
. "/usr/share/koha/bin/koha-functions.sh" |
33 |
else |
34 |
echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2 |
35 |
exit 1 |
36 |
fi |
37 |
|
38 |
usage() |
39 |
{ |
40 |
local scriptname=$(basename $0) |
41 |
|
42 |
cat <<EOF |
43 |
$scriptname |
44 |
|
45 |
This script lets you manage the SIP daemon for your Koha instances. |
46 |
|
47 |
Usage: |
48 |
$scriptname [--start|--stop|--restart] [--quiet|-q] instancename1 [instancename2...] |
49 |
$scriptname --enable|--disable instancename1 [instancename2] |
50 |
$scriptname -h|--help |
51 |
|
52 |
--start Start the SIP daemon for the specified instances |
53 |
--stop Stop the SIP daemon for the specified instances |
54 |
--restart Restart the SIP daemon for the specified instances |
55 |
--enable Enable SIP for the specified instances |
56 |
--disable Disable SIP For the specified instances |
57 |
--quiet|-q Make the script quiet about non existent instance names |
58 |
(useful for calling from another scripts). |
59 |
--help|-h Display this help message |
60 |
|
61 |
EOF |
62 |
} |
63 |
|
64 |
start_sip() |
65 |
{ |
66 |
local name=$1 |
67 |
|
68 |
if ! is_sip_running $instancename; then |
69 |
export KOHA_CONF="/etc/koha/sites/$instancename/koha-conf.xml" |
70 |
|
71 |
DAEMONOPTS="--name=$instancename-koha-sip \ |
72 |
--errlog=/var/log/koha/$instancename/sip-error.log \ |
73 |
--stdout=/var/log/koha/$instancename/sip.log \ |
74 |
--output=/var/log/koha/$instancename/sip-output.log \ |
75 |
--pidfiles=/var/run/koha/$instancename/ \ |
76 |
--verbose=1 --respawn --delay=30 \ |
77 |
--user=$instancename-koha.$instancename-koha" |
78 |
|
79 |
log_daemon_msg "Starting Koha SIP daemon for $instancename" |
80 |
|
81 |
if daemon $DAEMONOPTS -- $SIP_DAEMON "/etc/koha/sites/${name}/SIPconfig.xml"; then |
82 |
log_end_msg 0 |
83 |
else |
84 |
log_end_msg 1 |
85 |
fi |
86 |
else |
87 |
log_daemon_msg "Error: SIP daemon already running for $instancename" |
88 |
log_end_msg 1 |
89 |
fi |
90 |
} |
91 |
|
92 |
stop_sip() |
93 |
{ |
94 |
local name=$1 |
95 |
|
96 |
if is_sip_running $instancename; then |
97 |
local PIDFILE="/var/run/koha/${instancename}/${instancename}-koha-sip.pid" |
98 |
|
99 |
log_daemon_msg "Stopping Koha SIP daemon for $instancename" |
100 |
|
101 |
if start-stop-daemon --pidfile ${PIDFILE} --stop --retry=TERM/30/KILL/5; then |
102 |
log_end_msg 0 |
103 |
else |
104 |
log_end_msg 1 |
105 |
fi |
106 |
else |
107 |
log_daemon_msg "Error: SIP daemon not running for $instancename" |
108 |
log_end_msg 1 |
109 |
fi |
110 |
} |
111 |
|
112 |
restart_sip() |
113 |
{ |
114 |
local name=$1 |
115 |
|
116 |
if is_sip_running $instancename; then |
117 |
|
118 |
log_daemon_msg "Restarting Koha SIP daemon for $instancename" |
119 |
|
120 |
if stop_sip $instancename && start_sip $instancename; then |
121 |
log_end_msg 0 |
122 |
else |
123 |
log_end_msg 1 |
124 |
fi |
125 |
else |
126 |
log_daemon_msg "Error: SIP daemon not running for $instancename" |
127 |
log_end_msg 1 |
128 |
fi |
129 |
} |
130 |
|
131 |
enable_sip() |
132 |
{ |
133 |
local instancename=$1 |
134 |
|
135 |
local sipfile=/etc/koha/sites/${instancename}/SIPconfig.xml |
136 |
|
137 |
if ! is_sip_enabled $instancename; then |
138 |
if [ -e ${sipfile}.disabled ]; then |
139 |
mv ${sipfile}.disabled ${sipfile} |
140 |
else |
141 |
cp /etc/koha/SIPconfig.xml ${sipfile} |
142 |
fi |
143 |
|
144 |
chown ${instancename}-koha:${instancename}-koha ${sipfile} |
145 |
chmod 600 ${sipfile} |
146 |
|
147 |
[ "${quiet}" != "yes" ] && warn "SIP enabled for ${instancename} - edit ${sipfile} to configure" |
148 |
# return 0 |
149 |
else |
150 |
[ "${quiet}" != "yes" ] && warn "SIP already enabled for ${instancename}" |
151 |
# return 1 |
152 |
fi |
153 |
} |
154 |
|
155 |
disable_sip() |
156 |
{ |
157 |
local instancename=$1 |
158 |
|
159 |
local sipfile=/etc/koha/sites/${instancename}/SIPconfig.xml |
160 |
|
161 |
if is_sip_enabled $instancename; then |
162 |
mv ${sipfile} ${sipfile}.disabled |
163 |
|
164 |
[ "${quiet}" != "yes" ] && warn "SIP disabled for ${instancename}" |
165 |
# return 0 |
166 |
else |
167 |
[ "${quiet}" != "yes" ] && warn "SIP already disabled for ${instancename}" |
168 |
# return 1 |
169 |
fi |
170 |
} |
171 |
|
172 |
is_sip_running() |
173 |
{ |
174 |
local instancename=$1 |
175 |
local PIDFILE="/var/run/koha/${instancename}/${instancename}-koha-sip.pid" |
176 |
|
177 |
if start-stop-daemon --pidfile ${PIDFILE} --status ; then |
178 |
return 0 |
179 |
else |
180 |
return 1 |
181 |
fi |
182 |
} |
183 |
|
184 |
set_action() |
185 |
{ |
186 |
if [ "$op" = "" ]; then |
187 |
op=$1 |
188 |
else |
189 |
die "Error: only one action can be specified." |
190 |
fi |
191 |
} |
192 |
|
193 |
op="" |
194 |
quiet="no" |
195 |
|
196 |
# Read command line parameters |
197 |
while [ $# -gt 0 ]; do |
198 |
|
199 |
case "$1" in |
200 |
-h|--help) |
201 |
usage ; exit 0 ;; |
202 |
-q|--quiet) |
203 |
quiet="yes" |
204 |
shift ;; |
205 |
--start) |
206 |
set_action "start" |
207 |
shift ;; |
208 |
--stop) |
209 |
set_action "stop" |
210 |
shift ;; |
211 |
--restart) |
212 |
set_action "restart" |
213 |
shift ;; |
214 |
--enable) |
215 |
set_action "enable" |
216 |
shift ;; |
217 |
--disable) |
218 |
set_action "disable" |
219 |
shift ;; |
220 |
-*) |
221 |
die "Error: invalid option switch ($1)" ;; |
222 |
*) |
223 |
# We expect the remaining stuff are the instance names |
224 |
break ;; |
225 |
esac |
226 |
|
227 |
done |
228 |
|
229 |
|
230 |
if [ $# -gt 0 ]; then |
231 |
# We have at least one instance name |
232 |
for instancename in "$@"; do |
233 |
|
234 |
if is_instance $instancename; then |
235 |
|
236 |
adjust_paths_dev_install $instancename |
237 |
export PERL5LIB |
238 |
|
239 |
if [ "$DEV_INSTALL" = "" ]; then |
240 |
SIP_DAEMON="perl ${KOHA_HOME}/lib/C4/SIP/SIPServer.pm" |
241 |
else |
242 |
SIP_DAEMON="perl ${KOHA_HOME}/C4/SIP/SIPServer.pm" |
243 |
fi |
244 |
|
245 |
case $op in |
246 |
"start") |
247 |
start_sip $instancename |
248 |
;; |
249 |
"stop") |
250 |
stop_sip $instancename |
251 |
;; |
252 |
"restart") |
253 |
restart_sip $instancename |
254 |
;; |
255 |
"enable") |
256 |
enable_sip $instancename |
257 |
;; |
258 |
"disable") |
259 |
disable_sip $instancename |
260 |
;; |
261 |
*) |
262 |
usage |
263 |
;; |
264 |
esac |
265 |
|
266 |
else |
267 |
if [ "$quiet" = "no" ]; then |
268 |
log_daemon_msg "Error: Invalid instance name $instancename" |
269 |
log_end_msg 1 |
270 |
fi |
271 |
fi |
272 |
|
273 |
done |
274 |
else |
275 |
if [ "$quiet" = "no" ]; then |
276 |
warn "Error: you must provide at least one instance name" |
277 |
fi |
278 |
fi |
279 |
|
280 |
exit 0 |