Bug 17467: Add missing --status switch handling
[koha.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
65     if ! is_zebra_running $name; then
66
67         _check_and_fix_perms ${name}
68
69         DAEMONOPTS="--name=${name}-koha-zebra \
70                     --pidfiles=/var/run/koha/${name}/ \
71                     --errlog=/var/log/koha/${name}/zebra-error.log \
72                     --output=/var/log/koha/${name}/zebra-output.log \
73                     --verbose=1 \
74                     --respawn \
75                     --delay=30 \
76                     --user=${name}-koha.${name}-koha"
77
78         ZEBRA_PARAMS="-v $loglevels \
79                       -f /etc/koha/sites/${name}/koha-conf.xml"
80
81         [ "$verbose" != "no" ] && \
82             log_daemon_msg "Starting Koha Zebra daemon for ${name}"
83
84         if daemon $DAEMONOPTS -- $ZEBRA_DAEMON $ZEBRA_PARAMS; then
85             [ "$verbose" != "no" ] && \
86                 log_end_msg 0
87         else
88             [ "$verbose" != "no" ] && \
89                 log_end_msg 1
90         fi
91     else
92         if [ "$verbose" != "no" ]; then
93             log_daemon_msg "Error: Zebra already running for ${name}"
94             log_end_msg 1
95         fi
96     fi
97 }
98
99 stop_zebra()
100 {
101     local name=$1
102
103     if is_zebra_running $name; then
104
105         DAEMONOPTS="--name=${name}-koha-zebra \
106                     --pidfiles=/var/run/koha/${name}/ \
107                     --errlog=/var/log/koha/${name}/zebra-error.log \
108                     --output=/var/log/koha/${name}/zebra-output.log \
109                     --verbose=1 \
110                     --respawn \
111                     --delay=30 \
112                     --user=${name}-koha.${name}-koha"
113
114         [ "$verbose" != "no" ] && \
115             log_daemon_msg "Stopping Koha Zebra daemon for ${name}"
116
117         if daemon $DAEMONOPTS --stop -- $ZEBRA_DAEMON $ZEBRA_PARAMS; then
118             [ "$verbose" != "no" ] && \
119                 log_end_msg 0
120         else
121             [ "$verbose" != "no" ] && \
122                 log_end_msg 1
123         fi
124     else
125         if [ "$verbose" != "no" ]; then
126             log_daemon_msg "Error: Zebra not running for ${name}"
127             log_end_msg 1
128         fi
129     fi
130 }
131
132 restart_zebra()
133 {
134     local name=$1
135
136     if is_zebra_running ${name}; then
137         local noLF="-n"
138         [ "$verbose" != "no" ] && noLF=""
139         echo $noLF `stop_zebra ${name}`
140         echo $noLF `start_zebra ${name}`
141     else
142         if [ "$verbose" != "no" ]; then
143             log_daemon_msg "Error: Zebra not running for ${name}"
144             log_end_msg 1
145         fi
146     fi
147 }
148
149 zebra_status()
150 {
151     local name=$1
152
153     if is_zebra_running ${name}; then
154         log_daemon_msg "Zebra running for ${name}"
155         log_end_msg 0
156     else
157         log_daemon_msg "Zebra not running for ${name}"
158         log_end_msg 3
159     fi
160 }
161
162 _check_and_fix_perms()
163 {
164     local name=$1
165
166     local files="/var/log/koha/${name}/zebra-output.log \
167                  /var/log/koha/${name}/zebra-error.log"
168
169     for file in ${files}
170     do
171         if [ ! -e "${file}" ]; then
172             touch ${file}
173         fi
174         chown "${name}-koha":"${name}-koha" ${file}
175     done
176 }
177
178 set_action()
179 {
180     if [ "$op" = "" ]; then
181         op=$1
182     else
183         die "Error: only one action can be specified."
184     fi
185 }
186
187 op=""
188 verbose="no"
189
190 # Backwards compatible with old koha-*-zebra scripts
191 # TODO: Remove once there's consensus to remove the legacy scripts
192 used_script_name=$(basename $0)
193
194 if [ "$used_script_name" != "koha-zebra" ]; then
195     warn "Deprecated script used (${used_script_name})"
196
197     case "$used_script_name" in
198         koha-start-zebra)
199             set_action "start" ;;
200         koha-stop-zebra)
201             set_action "stop" ;;
202         koha-restart-zebra)
203             set_action "restart" ;;
204         *)
205             break ;;
206     esac
207 fi
208 # / Backwards compatible handling code
209
210 # Read command line parameters
211 while [ $# -gt 0 ]; do
212
213     case "$1" in
214         -h|--help)
215             usage ; exit 0 ;;
216         -v|--verbose)
217             verbose="yes"
218             shift ;;
219         --start)
220             set_action "start"
221             shift ;;
222         --stop)
223             set_action "stop"
224             shift ;;
225         --restart)
226             set_action "restart"
227             shift ;;
228         --status)
229             set_action "status"
230             shift ;;
231         -*)
232             die "Error: invalid option switch ($1)" ;;
233         *)
234             # We expect the remaining stuff are the instance names
235             break ;;
236     esac
237
238 done
239
240 ZEBRA_DAEMON=$(which zebrasrv)
241
242 if [ $# -gt 0 ]; then
243     # We have at least one instance name
244     for name in "$@"; do
245
246         if is_instance $name; then
247
248             case $op in
249                 "start")
250                     start_zebra $name
251                     ;;
252                 "stop")
253                     stop_zebra $name
254                     ;;
255                 "restart")
256                     restart_zebra $name
257                     ;;
258                 "status")
259                     zebra_status $name
260             esac
261
262         else
263             if [ "$verbose" != "no" ]; then
264                 log_daemon_msg "Error: Invalid instance name $name"
265                 log_end_msg 1
266             fi
267         fi
268
269     done
270 else
271     if [ "$verbose" != "no" ]; then
272         warn "Error: you must provide at least one instance name"
273     fi
274 fi
275
276 exit 0