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