Bug 22516: Remove remaining calls to lastincrement
[koha.git] / debian / scripts / koha-elasticsearch
1 #!/bin/sh
2
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 set -e
17
18 # Read configuration variable file if it is present
19 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
20
21 # include helper functions
22 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
23     . "/usr/share/koha/bin/koha-functions.sh"
24 else
25     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
26     exit 1
27 fi
28
29 usage()
30 {
31     local scriptname=$0
32     cat <<EOF
33 Manage Elasticsearch-related tasks for Koha instances
34
35 Usage: $scriptname [actions] [options] instancename1 instancename2...
36
37 Actions:
38     --rebuild         Trigger a rebuild process
39
40 Options:
41     --authorities|-a  Only run process for authorities.
42     --biblios|-b      Only run process for biblios.
43     -c|--commit n     Specify how many records will be batched up before
44                       they're added to Elasticsearch (default: 5000).
45     --verbose|-v      Be verbose.
46     --help|-h         Print this help.
47
48 EOF
49 }
50
51 toggle_biblios_only()
52 {
53     biblios_only="yes"
54     biblios="yes"
55     if [ "${authorities_only}" != "yes" ]; then
56         authorities="no"
57     fi
58 }
59
60 toggle_authorities_only()
61 {
62     authorities_only="yes"
63     authorities="yes"
64     if [ "${biblios_only}" != "yes" ]; then
65         biblios="no"
66     fi
67 }
68
69 set_action()
70 {
71     if [ "${op}" = "" ]; then
72         op=$1
73     else
74         die "Error: only one action can be specified."
75     fi
76 }
77
78 run_rebuild_elasticsearch()
79 {
80     # read instance name and eliminate instance name from params
81     local name=$1; shift
82
83     if [ "$DEV_INSTALL" = "" ]; then
84         KOHA_BINDIR=${KOHA_HOME}/bin
85     else
86         KOHA_BINDIR=${KOHA_HOME}/misc
87     fi
88
89     if [ "${clo_commit_size}" > 0 ]; then
90         commit_size=${clo_commit_size}
91     fi
92     rebuild_opts="--commit ${commit_size}"
93
94     if [ "${biblios}" = "yes" ]; then
95         rebuild_opts="${rebuild_opts} -b"
96     fi
97
98     if [ "${authorities}" = "yes" ]; then
99         rebuild_opts="${rebuild_opts} -a"
100     fi
101
102     if [ "${verbose}" = "yes" ]; then
103         rebuild_opts="${rebuild_opts} -v"
104     fi
105
106     if koha-shell \
107           -c "${KOHA_BINDIR}/search_tools/rebuild_elastic_search.pl ${rebuild_opts}" \
108           ${name}; then
109         return 0
110     else
111         return 1
112     fi
113 }
114
115 # Default parameters
116 biblios="yes"
117 authorities="yes"
118 biblios_only="no"
119 authorities_only="no"
120 commit_size=5000
121 verbose="no"
122 op=""
123
124 # Read parameters
125 while [ -n "$*" ]; do
126     case "$1" in
127         -h|--help)
128             usage ; exit 0
129             ;;
130         -b|--biblios)
131             toggle_biblios_only
132             ;;
133         -a|--authorities)
134             toggle_authorities_only
135             ;;
136         -c|--commit)
137             clo_commit_size="$2" ; shift
138             ;;
139         --rebuild)
140             set_action "rebuild"
141             ;;
142         -v|--verbose)
143             verbose="yes"
144             ;;
145         *)
146             break
147             ;;
148     esac
149
150     shift
151 done
152
153 # Optionally use alternative paths for a dev install
154 adjust_paths_dev_install $1
155
156 # Parse command line.
157 if [ $# -lt 1 ]; then
158     usage
159     die "Missing instance name."
160 fi
161
162 # Loop over instance names
163 for name in "$@"
164 do
165     if is_instance $name; then
166         if [ "${op}" = "rebuild" ]; then
167             if ! run_rebuild_elasticsearch $name; then
168                 warn "Something went wrong rebuilding indexes for ${name}"
169             fi
170         else
171             # TODO: Add other actions, status? etc
172             usage
173             die "Error: no action passed"
174         fi
175     else
176         warn "Unknown instance ${name}"
177     fi
178 done
179
180 exit 0