Bug 21548: ILLLIBS patron category / partner_code seems extraneous
[koha.git] / debian / scripts / koha-indexer
1 #!/bin/bash
2 #
3 # koha-indexer - Manage Indexer Daemons for Koha instances
4 # Copyright 2014 Tomás Cohen Arazi @ Universidad Nacional de Córdoba
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 set -e
20
21 . /lib/lsb/init-functions
22
23 # Read configuration variable file if it is present
24 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
25
26 # include helper functions
27 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
28     . "/usr/share/koha/bin/koha-functions.sh"
29 else
30     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
31     exit 1
32 fi
33
34 usage()
35 {
36     local scriptname=$(basename $0)
37
38     cat <<EOF
39 $scriptname
40
41 This script lets you manage the indexer daemon for your Koha instances.
42
43 Usage:
44 $scriptname [--start|--stop|--restart] [--quiet|-q] instancename1 [instancename2...]
45 $scriptname -h|--help
46
47     --start               Start the indexer daemon for the specified instances
48     --stop                Stop the indexer daemon for the specified instances
49     --restart             Restart the indexer daemon for the specified instances
50     --status              Show the status of the indexer for the specified instances
51     --quiet|-q            Make the script quiet about non existent instance names
52                           (useful for calling from another scripts).
53     --help|-h             Display this help message
54
55 EOF
56 }
57
58 start_indexer()
59 {
60     local name=$1
61
62     if ! is_indexer_running $name; then
63         export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
64
65         DAEMONOPTS="--name=$name-koha-indexer \
66             --errlog=/var/log/koha/$name/indexer-error.log \
67             --output=/var/log/koha/$name/indexer-output.log \
68             --pidfiles=/var/run/koha/$name/ \
69             --verbose=1 --respawn --delay=30 \
70             --user=$name-koha.$name-koha"
71
72         log_daemon_msg "Starting Koha indexing daemon for $name"
73
74         if daemon $DAEMONOPTS -- $INDEXER_DAEMON $INDEXER_PARAMS; then
75             log_end_msg 0
76         else
77             log_end_msg 1
78         fi
79     else
80         log_daemon_msg "Error: Indexer already running for $name"
81         log_end_msg 1
82     fi
83 }
84
85 stop_indexer()
86 {
87     local name=$1
88
89     if is_indexer_running $name; then
90         export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
91
92         DAEMONOPTS="--name=$name-koha-indexer \
93             --errlog=/var/log/koha/$name/indexer-error.log \
94             --output=/var/log/koha/$name/indexer-output.log \
95             --pidfiles=/var/run/koha/$name/ \
96             --verbose=1 --respawn --delay=30 \
97             --user=$name-koha.$name-koha"
98
99         log_daemon_msg "Stopping Koha indexing daemon for $name"
100
101         if daemon $DAEMONOPTS --stop -- $INDEXER_DAEMON $INDEXER_PARAMS; then
102             log_end_msg 0
103         else
104             log_end_msg 1
105         fi
106     else
107         log_daemon_msg "Error: Indexer not running for $name"
108         log_end_msg 1
109     fi
110 }
111
112 restart_indexer()
113 {
114     local name=$1
115
116     if is_indexer_running $name; then
117         export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
118
119         DAEMONOPTS="--name=$name-koha-indexer \
120             --errlog=/var/log/koha/$name/indexer-error.log \
121             --output=/var/log/koha/$name/indexer-output.log \
122             --pidfiles=/var/run/koha/$name/ \
123             --verbose=1 --respawn --delay=30 \
124             --user=$name-koha.$name-koha"
125
126         log_daemon_msg "Restarting Koha indexing daemon for $name"
127
128         if daemon $DAEMONOPTS --restart -- $INDEXER_DAEMON $INDEXER_PARAMS; then
129             log_end_msg 0
130         else
131             log_end_msg 1
132         fi
133     else
134         log_warning_msg "Indexer not running for $name."
135         start_indexer $name
136     fi
137 }
138
139 indexer_status()
140 {
141     local name=$1
142
143     if is_indexer_running ${name}; then
144         log_daemon_msg "Indexer running for ${name}"
145         log_end_msg 0
146     else
147         log_daemon_msg "Indexer not running for ${name}"
148         log_end_msg 3
149     fi
150 }
151
152 set_action()
153 {
154     if [ "$op" = "" ]; then
155         op=$1
156     else
157         die "Error: only one action can be specified."
158     fi
159 }
160
161 op=""
162 quiet="no"
163
164 # Read command line parameters
165 while [ $# -gt 0 ]; do
166
167     case "$1" in
168         -h|--help)
169             usage ; exit 0 ;;
170         -q|--quiet)
171             quiet="yes"
172             shift ;;
173         --start)
174             set_action "start"
175             shift ;;
176         --stop)
177             set_action "stop"
178             shift ;;
179         --restart)
180             set_action "restart"
181             shift ;;
182         --status)
183             set_action "status"
184             shift ;;
185         --debug)
186             DEBUG="-v -v"
187             shift ;;
188         -*)
189             die "Error: invalid option switch ($1)" ;;
190         *)
191             # We expect the remaining stuff are the instance names
192             break ;;
193     esac
194
195 done
196
197 # Optionally use alternative paths for a dev install
198 adjust_paths_dev_install $1
199
200 # Check if an alternate indexer has been set
201 if [ ! -z "$ALTERNATE_INDEXER_DAEMON" ]; then
202     INDEXER_DAEMON="$ALTERNATE_INDEXER_DAEMON"
203 else
204     # We default to rebuild_zebra.pl if no alternate indexer set
205     if [ "$DEV_INSTALL" = "" ]; then
206         INDEXER_DAEMON="${KOHA_HOME}/bin/migration_tools/rebuild_zebra.pl"
207     else
208         INDEXER_DAEMON="${KOHA_HOME}/misc/migration_tools/rebuild_zebra.pl"
209     fi
210 fi
211
212 if [ $INDEXER_TIMEOUT -lt 1 ]; then
213     # Something's wrong, default to 5 seconds
214     INDEXER_TIMEOUT=5
215 fi
216
217 if [ -z "$INDEXER_PARAMS" ]; then
218     # Default to the parameters required by rebuild_zebra.pl
219     INDEXER_PARAMS="-daemon -sleep $INDEXER_TIMEOUT $DEBUG"
220 fi
221
222 # PERL5LIB has been read from etc/default
223 export PERL5LIB
224
225 if [ $# -gt 0 ]; then
226     # We have at least one instance name
227     for name in "$@"; do
228
229         if is_instance $name; then
230
231             case $op in
232                 "start")
233                     start_indexer $name
234                     ;;
235                 "stop")
236                     stop_indexer $name
237                     ;;
238                 "restart")
239                     restart_indexer $name
240                     ;;
241                 "status")
242                     indexer_status $name
243             esac
244
245         else
246             if [ "$quiet" = "no" ]; then
247                 log_daemon_msg "Error: Invalid instance name $name"
248                 log_end_msg 1
249             fi
250         fi
251
252     done
253 else
254     if [ "$quiet" = "no" ]; then
255         warn "Error: you must provide at least one instance name"
256     fi
257 fi
258
259 exit 0