Bug 8773 - Start per-instance koha-index-daemon in .deb setup
[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     --quiet|-q            Make the script quiet about non existent instance names
51                           (useful for calling from another scripts).
52     --help|-h             Display this help message
53
54 EOF
55 }
56
57 start_indexer()
58 {
59     local name=$1
60
61     if ! is_indexer_running $name; then
62         export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
63
64         DAEMONOPTS="--name=$name-koha-indexer \
65             --errlog=/var/log/koha/$name/indexer-error.log \
66             --stdout=/var/log/koha/$name/indexer.log \
67             --output=/var/log/koha/$name/indexer-output.log \
68             --verbose=1 --respawn --delay=30 \
69             --user=$name-koha.$name-koha"
70
71         log_daemon_msg "Starting Koha indexing daemon for $name"
72
73         if daemon $DAEMONOPTS -- $INDEXER_DAEMON $INDEXER_PARAMS; then
74             log_end_msg 0
75         else
76             log_end_msg 1
77         fi
78     else
79         log_daemon_msg "Error: Indexer already running for $name"
80         log_end_msg 1
81     fi
82 }
83
84 stop_indexer()
85 {
86     local name=$1
87
88     if is_indexer_running $name; then
89         export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
90
91         DAEMONOPTS="--name=$name-koha-indexer \
92             --errlog=/var/log/koha/$name/indexer-error.log \
93             --stdout=/var/log/koha/$name/indexer.log \
94             --output=/var/log/koha/$name/indexer-output.log \
95             --verbose=1 --respawn --delay=30 \
96             --user=$name-koha.$name-koha"
97
98         log_daemon_msg "Stopping Koha indexing daemon for $name"
99
100         if daemon $DAEMONOPTS --stop -- $INDEXER_DAEMON $INDEXER_PARAMS; then
101             log_end_msg 0
102         else
103             log_end_msg 1
104         fi
105     else
106         log_daemon_msg "Error: Indexer not running for $name"
107         log_end_msg 1
108     fi
109 }
110
111 restart_indexer()
112 {
113     local name=$1
114
115     if is_indexer_running $name; then
116         export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
117
118         DAEMONOPTS="--name=$name-koha-indexer \
119             --errlog=/var/log/koha/$name/indexer-error.log \
120             --stdout=/var/log/koha/$name/indexer.log \
121             --output=/var/log/koha/$name/indexer-output.log \
122             --verbose=1 --respawn --delay=30 \
123             --user=$name-koha.$name-koha"
124
125         log_daemon_msg "Stopping Koha indexing daemon for $name"
126
127         if daemon $DAEMONOPTS --restart -- $INDEXER_DAEMON $INDEXER_PARAMS; then
128             log_end_msg 0
129         else
130             log_end_msg 1
131         fi
132     else
133         log_daemon_msg "Error: Indexer not running for $name"
134         log_end_msg 1
135     fi
136 }
137
138 set_action()
139 {
140     if [ "$op" = "" ]; then
141         op=$1
142     else
143         die "Error: only one action can be specified."
144     fi
145 }
146
147 op=""
148 quiet="no"
149
150 # Read command line parameters
151 while [ $# -gt 0 ]; do
152
153     case "$1" in
154         -h|--help)
155             usage ; exit 0 ;;
156         -q|--quiet)
157             quiet="yes"
158             shift ;;
159         --start)
160             set_action "start"
161             shift ;;
162         --stop)
163             set_action "stop"
164             shift ;;
165         --restart)
166             set_action "restart"
167             shift ;;
168         -*)
169             die "Error: invalid option switch ($1)" ;;
170         *)
171             # We expect the remaining stuff are the instance names
172             break ;;
173     esac
174
175 done
176
177 # Check if an alternate indexer has been set
178 if [ -z $ALTERNATE_INDEXER_DAEMON ]; then
179     INDEXER_DAEMON="$ALTERNATE_INDEXER_DAEMON"
180 else
181     # We default to rebuild_zebra.pl if no alternate indexer set
182     INDEXER_DAEMON="${KOHA_HOME}/bin/migration_tools/rebuild_zebra.pl"
183 fi
184
185 if [ $INDEXER_TIMEOUT -lt 1 ]; then
186     # Something's wrong, default to 5 seconds
187     INDEXER_TIMEOUT=5
188 fi
189
190 if [ -z $INDEXER_PARAMS ]; then
191     # Default to the parameters required by rebuild_zebra.pl
192     INDEXER_PARAMS="-daemon -x -sleep $INDEXER_TIMEOUT"
193 fi
194
195 if [ -z $PERL5LIB ]; then
196     PERL5LIB="/usr/share/koha/lib"
197 fi
198
199 export PERL5LIB
200
201 if [ $# -gt 0 ]; then
202     # We have at least one instance name
203     for name in "$@"; do
204
205         if is_instance $name; then
206
207             case $op in
208                 "start")
209                     start_indexer $name
210                     ;;
211                 "stop")
212                     stop_indexer $name
213                     ;;
214                 "restart")
215                     restart_indexer $name
216                     ;;
217             esac
218
219         else
220             if [ "$quiet" = "no" ]; then
221                 log_daemon_msg "Error: Invalid instance name $name"
222                 log_end_msg 1
223             fi
224         fi
225
226     done
227 else
228     if [ "$quiet" = "no" ]; then
229         warn "Error: you must provide at least one instance name"
230     fi
231 fi
232
233 exit 0