Bug 18913: Allow symbolic link in /etc/koha/sites
[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             --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             --stdout=/var/log/koha/$name/indexer.log \
95             --output=/var/log/koha/$name/indexer-output.log \
96             --pidfiles=/var/run/koha/$name/ \
97             --verbose=1 --respawn --delay=30 \
98             --user=$name-koha.$name-koha"
99
100         log_daemon_msg "Stopping Koha indexing daemon for $name"
101
102         if daemon $DAEMONOPTS --stop -- $INDEXER_DAEMON $INDEXER_PARAMS; then
103             log_end_msg 0
104         else
105             log_end_msg 1
106         fi
107     else
108         log_daemon_msg "Error: Indexer not running for $name"
109         log_end_msg 1
110     fi
111 }
112
113 restart_indexer()
114 {
115     local name=$1
116
117     if is_indexer_running $name; then
118         export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
119
120         DAEMONOPTS="--name=$name-koha-indexer \
121             --errlog=/var/log/koha/$name/indexer-error.log \
122             --stdout=/var/log/koha/$name/indexer.log \
123             --output=/var/log/koha/$name/indexer-output.log \
124             --pidfiles=/var/run/koha/$name/ \
125             --verbose=1 --respawn --delay=30 \
126             --user=$name-koha.$name-koha"
127
128         log_daemon_msg "Restarting Koha indexing daemon for $name"
129
130         if daemon $DAEMONOPTS --restart -- $INDEXER_DAEMON $INDEXER_PARAMS; then
131             log_end_msg 0
132         else
133             log_end_msg 1
134         fi
135     else
136         log_daemon_msg "Error: Indexer not running for $name"
137         log_end_msg 1
138     fi
139 }
140
141 set_action()
142 {
143     if [ "$op" = "" ]; then
144         op=$1
145     else
146         die "Error: only one action can be specified."
147     fi
148 }
149
150 op=""
151 quiet="no"
152
153 # Read command line parameters
154 while [ $# -gt 0 ]; do
155
156     case "$1" in
157         -h|--help)
158             usage ; exit 0 ;;
159         -q|--quiet)
160             quiet="yes"
161             shift ;;
162         --start)
163             set_action "start"
164             shift ;;
165         --stop)
166             set_action "stop"
167             shift ;;
168         --restart)
169             set_action "restart"
170             shift ;;
171         -*)
172             die "Error: invalid option switch ($1)" ;;
173         *)
174             # We expect the remaining stuff are the instance names
175             break ;;
176     esac
177
178 done
179
180 # Optionally use alternative paths for a dev install
181 adjust_paths_dev_install $1
182
183 # Check if an alternate indexer has been set
184 if [ ! -z $ALTERNATE_INDEXER_DAEMON ]; then
185     INDEXER_DAEMON="$ALTERNATE_INDEXER_DAEMON"
186 else
187     # We default to rebuild_zebra.pl if no alternate indexer set
188     if [ "$DEV_INSTALL" = "" ]; then
189         INDEXER_DAEMON="${KOHA_HOME}/bin/migration_tools/rebuild_zebra.pl"
190     else
191         INDEXER_DAEMON="${KOHA_HOME}/misc/migration_tools/rebuild_zebra.pl"
192     fi
193 fi
194
195 if [ $INDEXER_TIMEOUT -lt 1 ]; then
196     # Something's wrong, default to 5 seconds
197     INDEXER_TIMEOUT=5
198 fi
199
200 if [ -z $INDEXER_PARAMS ]; then
201     # Default to the parameters required by rebuild_zebra.pl
202     INDEXER_PARAMS="-daemon -sleep $INDEXER_TIMEOUT"
203 fi
204
205 # PERL5LIB has been read from etc/default
206 export PERL5LIB
207
208 if [ $# -gt 0 ]; then
209     # We have at least one instance name
210     for name in "$@"; do
211
212         if is_instance $name; then
213
214             case $op in
215                 "start")
216                     start_indexer $name
217                     ;;
218                 "stop")
219                     stop_indexer $name
220                     ;;
221                 "restart")
222                     restart_indexer $name
223                     ;;
224             esac
225
226         else
227             if [ "$quiet" = "no" ]; then
228                 log_daemon_msg "Error: Invalid instance name $name"
229                 log_end_msg 1
230             fi
231         fi
232
233     done
234 else
235     if [ "$quiet" = "no" ]; then
236         warn "Error: you must provide at least one instance name"
237     fi
238 fi
239
240 exit 0