Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / debian / scripts / koha-zebra
1 #!/bin/bash
2
3 # koha-zebra - Manage Zebra daemons for Koha instances
4 #              Copyright 2016 Theke Solutions
5 #              Copyright 2010 Catalyst IT, Ltd
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 Zebra daemon for your Koha instances.
43
44 Usage:
45 $scriptname [--start|--stop|--restart] instancename1 [instancename2...]
46 $scriptname -h|--help
47
48     --start               Start the Zebra daemon for the specified instance(s)
49     --stop                Stop the Zebra daemon for the specified instance(s)
50     --restart             Restart the Zebra daemon for the specified instance(s)
51     --status              Show the status of the Zebra daemon for the specified instance(s)
52     --verbose|-v          Display progress and actions messages
53     --help|-h             Display this help message
54
55 EOF
56 }
57
58 start_zebra()
59 {
60     local name=$1
61
62     # get zebra log levels from koha-conf.xml
63     local loglevels=$(get_loglevels ${name})
64     local max_record_size=$(get_max_record_size ${name})
65
66     if ! is_zebra_running $name; then
67
68         _check_and_fix_perms ${name}
69
70         DAEMONOPTS="--name=${name}-koha-zebra \
71                     --pidfiles=/var/run/koha/${name}/ \
72                     --errlog=/var/log/koha/${name}/zebra-error.log \
73                     --output=/var/log/koha/${name}/zebra-output.log \
74                     --verbose=1 \
75                     --respawn \
76                     --delay=30 \
77                     --user=${name}-koha.${name}-koha"
78
79         ZEBRA_PARAMS="-v $loglevels \
80                       -k $max_record_size \
81                       -f /etc/koha/sites/${name}/koha-conf.xml"
82
83         [ "$verbose" != "no" ] && \
84             log_daemon_msg "Starting Koha Zebra daemon for ${name}"
85
86         if daemon $DAEMONOPTS -- $ZEBRA_DAEMON $ZEBRA_PARAMS; then
87             ([ "$verbose" != "no" ] && \
88                 log_end_msg 0) || return 0
89         else
90             ([ "$verbose" != "no" ] && \
91                 log_end_msg 1) || return 1
92         fi
93     else
94         if [ "$verbose" != "no" ]; then
95             log_daemon_msg "Error: Zebra already running for ${name}"
96             log_end_msg 1
97         else
98             return 1
99         fi
100     fi
101 }
102
103 stop_zebra()
104 {
105     local name=$1
106
107     if is_zebra_running $name; then
108
109         DAEMONOPTS="--name=${name}-koha-zebra \
110                     --pidfiles=/var/run/koha/${name}/ \
111                     --errlog=/var/log/koha/${name}/zebra-error.log \
112                     --output=/var/log/koha/${name}/zebra-output.log \
113                     --verbose=1 \
114                     --respawn \
115                     --delay=30 \
116                     --user=${name}-koha.${name}-koha"
117
118         [ "$verbose" != "no" ] && \
119             log_daemon_msg "Stopping Koha Zebra daemon for ${name}"
120
121         if daemon $DAEMONOPTS --stop -- $ZEBRA_DAEMON $ZEBRA_PARAMS; then
122             ([ "$verbose" != "no" ] && \
123                 log_end_msg 0) || return 0
124         else
125             ([ "$verbose" != "no" ] && \
126                 log_end_msg 1) || return 1
127         fi
128     else
129         if [ "$verbose" != "no" ]; then
130             log_daemon_msg "Error: Zebra not running for ${name}"
131             log_end_msg 1
132         else
133             return 1
134         fi
135     fi
136 }
137
138 restart_zebra()
139 {
140     local name=$1
141
142     if is_zebra_running ${name}; then
143         local noLF="-n"
144         [ "$verbose" != "no" ] && noLF=""
145         echo $noLF `stop_zebra ${name}`
146         echo $noLF `start_zebra ${name}`
147     else
148         if [ "$verbose" != "no" ]; then
149             log_daemon_msg "Error: Zebra not running for ${name}"
150             log_end_msg 1
151         else
152             return 1
153         fi
154     fi
155 }
156
157 zebra_status()
158 {
159     local name=$1
160
161     if is_zebra_running ${name}; then
162         log_daemon_msg "Zebra running for ${name}"
163         log_end_msg 0
164     else
165         log_daemon_msg "Zebra not running for ${name}"
166         log_end_msg 3
167     fi
168 }
169
170 _check_and_fix_perms()
171 {
172     local name=$1
173
174     local files="/var/log/koha/${name}/zebra-output.log \
175                  /var/log/koha/${name}/zebra-error.log"
176
177     for file in ${files}
178     do
179         if [ ! -e "${file}" ]; then
180             touch ${file}
181         fi
182         chown "${name}-koha":"${name}-koha" ${file}
183     done
184 }
185
186 set_action()
187 {
188     if [ "$op" = "" ]; then
189         op=$1
190     else
191         die "Error: only one action can be specified."
192     fi
193 }
194
195 op=""
196 verbose="no"
197
198 # Backwards compatible with old koha-*-zebra scripts
199 # TODO: Remove once there's consensus to remove the legacy scripts
200 used_script_name=$(basename $0)
201
202 if [ "$used_script_name" != "koha-zebra" ]; then
203     warn "Deprecated script used (${used_script_name})"
204
205     case "$used_script_name" in
206         koha-start-zebra)
207             set_action "start" ;;
208         koha-stop-zebra)
209             set_action "stop" ;;
210         koha-restart-zebra)
211             set_action "restart" ;;
212         *)
213             break ;;
214     esac
215 fi
216 # / Backwards compatible handling code
217
218 # Read command line parameters
219 while [ $# -gt 0 ]; do
220
221     case "$1" in
222         -h|--help)
223             usage ; exit 0 ;;
224         -v|--verbose)
225             verbose="yes"
226             shift ;;
227         --start)
228             set_action "start"
229             shift ;;
230         --stop)
231             set_action "stop"
232             shift ;;
233         --restart)
234             set_action "restart"
235             shift ;;
236         --status)
237             set_action "status"
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 ZEBRA_DAEMON=$(which zebrasrv)
249
250 if [ $# -gt 0 ]; then
251     # We have at least one instance name
252     for name in "$@"; do
253
254         if is_instance $name; then
255
256             case $op in
257                 "start")
258                     start_zebra $name
259                     ;;
260                 "stop")
261                     stop_zebra $name
262                     ;;
263                 "restart")
264                     restart_zebra $name
265                     ;;
266                 "status")
267                     zebra_status $name
268             esac
269
270         else
271             if [ "$verbose" != "no" ]; then
272                 log_daemon_msg "Error: Invalid instance name $name"
273                 log_end_msg 1
274             fi
275         fi
276
277     done
278 else
279     if [ "$verbose" != "no" ]; then
280         warn "Error: you must provide at least one instance name"
281     fi
282 fi
283
284 exit 0