Bug 17610 - Allow the number of plack workers and max connections to be set in koha...
[koha.git] / debian / scripts / koha-rebuild-zebra
1 #!/bin/sh
2 #
3 # koha-rebuild-zebra - Rebuild the Zebra database for Koha instances.
4 # Copyright 2010  Catalyst IT, Ltd
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
20 set -e
21
22 # Read configuration variable file if it is present
23 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
24
25 # include helper functions
26 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
27     . "/usr/share/koha/bin/koha-functions.sh"
28 else
29     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
30     exit 1
31 fi
32
33 toggle_biblios_only()
34 {
35     biblios_only="yes"
36     biblios="yes"
37     if [ "$authorities_only" != "yes" ]; then
38         authorities="no"
39     fi
40 }
41
42 toggle_authorities_only()
43 {
44     authorities_only="yes"
45     authorities="yes"
46     if [ "$biblios_only" != "yes" ]; then
47         biblios="no"
48     fi
49 }
50
51 run_rebuild_zebra()
52 {
53     local instancename=$1; shift
54
55     if [ "$USE_INDEXER_DAEMON" = "no"  ] ||
56        [     "${full_reindex}" = "yes" ] ||
57        [            "${force}" = "yes" ] ; then
58
59         if [ "$DEV_INSTALL" = "" ]; then
60             KOHA_BINDIR=$KOHA_HOME/bin
61         else
62             KOHA_BINDIR=$KOHA_HOME/misc
63         fi
64
65         # TODO: This comment is here to remind us that we should make
66         # rebuild_zebra.pl return error codes on failure
67         if sudo -u "$instancename-koha" -H \
68             env PERL5LIB=$PERL5LIB \
69             KOHA_CONF="/etc/koha/sites/$instancename/koha-conf.xml" \
70             $KOHA_BINDIR/migration_tools/rebuild_zebra.pl $@ ; then
71             return 0
72         else
73             return 1
74         fi
75     fi
76 }
77
78 usage()
79 {
80     local scriptname=$0
81     cat <<EOF
82 Rebuild the Zebra indexes for Koha instances. The default behaviour
83 is to do an incremental rebuild.
84
85 Usage: $scriptname [options] instancename1 instancename2...
86 Options:
87     --usmarc|-u       Runs the process as USMARC rather than
88                       the default of MARCXML.
89     --authorities|-a  Only run process for authorities.
90     --biblios|-b      Only run process for biblios.
91     --full|-f         Does a reindex of the whole collection.
92     --force           Run incremental indexing even if USE_INDEXER_DAEMON="yes"
93     --quiet|-q        Sometimes be a bit quieter for scripts/cronjobs.
94     --verbose|-v      Be verbose.
95     --help|-h         Print this help.
96
97
98 Note: Any other options are passed directly to rebuild_zebra.pl.
99 EOF
100 }
101
102 # Default parameters
103 opt_idx="-z"
104 opt_xml="-x"
105 opt_verbose=""
106 opts_other=""
107 biblios_only="no"
108 authorities_only="no"
109 biblios="yes"
110 authorities="yes"
111 force="no"
112 full_reindex="no"
113
114 # The '-q' option is intended to prevent the cronjob causing this to output
115 # help information if there are no instances defined.
116 quiet="no"
117
118 # Read parameters
119 while [ -n "$*" ]; do
120     case "$1" in
121         -h|--help)
122             usage ; exit 0
123             ;;
124         -b|--biblios)
125             toggle_biblios_only
126             ;;
127         -a|--authorities)
128             toggle_authorities_only
129             ;;
130         -u|--usmarc)
131             opt_xml=""
132             ;;
133         -f|--full)
134             full_reindex="yes"
135             opt_idx="-r"
136             ;;
137         --force)
138             force="yes"
139             ;;
140         -v|--verbose)
141             opt_verbose="-v"
142             ;;
143         -q|--quiet)
144             quiet="yes"
145             ;;
146         -*)
147             opts_other="$opts_other $1";
148             ;;
149         *)
150             break
151             ;;
152     esac
153
154     shift
155 done
156
157 # Optionally use alternative paths for a dev install
158 adjust_paths_dev_install $1
159
160 # Parse command line.
161 if [ $# -lt 1 ]; then
162     if [ "$quiet" = "no" ]; then
163         usage
164         die "Missing instance name."
165     else
166         exit
167     fi
168 fi
169
170 # Loop over instance names
171 for name in "$@"
172 do
173     if is_instance $name; then
174         if [ "$biblios" = "yes" ]; then
175             if ! run_rebuild_zebra $name \
176                 -b $opt_verbose $opt_idx $opt_xml $opts_other; then
177                 warn "Something went wrong rebuilding biblio indexes for $name"
178             fi
179         fi
180         if [ "$authorities" = "yes" ]; then
181             if ! run_rebuild_zebra $name \
182                 -a $opt_verbose $opt_idx $opts_other ; then
183                 warn "Something went wrong rebuilding authority indexes for $name"
184             fi
185         fi
186     else
187         warn "Unknown instance $name."
188     fi
189 done
190
191 exit 0