Bug 29881: libdbd-sqlite2-perl is unavailable on deb12 (koha-common wont install)
[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     --delete|-d       Delete elasticsearch index before reindexing.
42     --reset|-r        Reset elasticsearch mappings from file.
43     --authorities|-a  Only run process for authorities.
44     --biblios|-b      Only run process for biblios.
45     -c|--commit n     Specify how many records will be batched up before
46                       they're added to Elasticsearch (default: 5000).
47     -p|--processes n  Specify the number of parallel processes to use
48                       for indexing.
49     --verbose|-v      Be verbose.
50     --help|-h         Print this help.
51
52 EOF
53 }
54
55 toggle_biblios_only()
56 {
57     biblios_only="yes"
58     biblios="yes"
59     if [ "${authorities_only}" != "yes" ]; then
60         authorities="no"
61     fi
62 }
63
64 toggle_authorities_only()
65 {
66     authorities_only="yes"
67     authorities="yes"
68     if [ "${biblios_only}" != "yes" ]; then
69         biblios="no"
70     fi
71 }
72
73 set_action()
74 {
75     if [ "${op}" = "" ]; then
76         op=$1
77     else
78         die "Error: only one action can be specified."
79     fi
80 }
81
82 run_rebuild_elasticsearch()
83 {
84     # read instance name and eliminate instance name from params
85     local name=$1; shift
86
87     if [ "$DEV_INSTALL" = "" ]; then
88         KOHA_BINDIR=${KOHA_HOME}/bin
89     else
90         KOHA_BINDIR=${KOHA_HOME}/misc
91     fi
92
93     if [ "${clo_commit_size}" > 0 ]; then
94         commit_size=${clo_commit_size}
95     fi
96     rebuild_opts="--commit ${commit_size}"
97
98     if [ "${clo_processes}" > 0 ]; then
99         processes=${clo_processes}
100     fi
101     rebuild_opts="${rebuild_opts} --processes ${processes}"
102
103     if [ "${delete}" = "yes" ]; then
104         rebuild_opts="${rebuild_opts} -d"
105     fi
106
107     if [ "${reset}" = "yes" ]; then
108         rebuild_opts="${rebuild_opts} -r"
109     fi
110
111     if [ "${biblios}" = "yes" ]; then
112         rebuild_opts="${rebuild_opts} -b"
113     fi
114
115     if [ "${authorities}" = "yes" ]; then
116         rebuild_opts="${rebuild_opts} -a"
117     fi
118
119     if [ "${verbose}" = "yes" ]; then
120         rebuild_opts="${rebuild_opts} -v"
121     fi
122
123     if koha-shell \
124           -c "${KOHA_BINDIR}/search_tools/rebuild_elasticsearch.pl ${rebuild_opts}" \
125           ${name}; then
126         return 0
127     else
128         return 1
129     fi
130 }
131
132 # Default parameters
133 biblios="yes"
134 authorities="yes"
135 biblios_only="no"
136 authorities_only="no"
137 commit_size=5000
138 processes=1
139 verbose="no"
140 op=""
141
142 # Read parameters
143 while [ -n "$*" ]; do
144     case "$1" in
145         -h|--help)
146             usage ; exit 0
147             ;;
148         -d|--delete)
149             delete="yes"
150             ;;
151         -r|--reset)
152             reset="yes"
153             ;;
154         -b|--biblios)
155             toggle_biblios_only
156             ;;
157         -a|--authorities)
158             toggle_authorities_only
159             ;;
160         -c|--commit)
161             clo_commit_size="$2" ; shift
162             ;;
163         -p|--processes)
164             clo_processes="$2" ; shift
165             ;;
166         --rebuild)
167             set_action "rebuild"
168             ;;
169         -v|--verbose)
170             verbose="yes"
171             ;;
172         *)
173             break
174             ;;
175     esac
176
177     shift
178 done
179
180 # Optionally use alternative paths for a dev install
181 adjust_paths_dev_install $1
182
183 # Parse command line.
184 if [ $# -lt 1 ]; then
185     usage
186     die "Missing instance name."
187 fi
188
189 # Loop over instance names
190 for name in "$@"
191 do
192     if is_instance $name; then
193         if [ "${op}" = "rebuild" ]; then
194             if ! run_rebuild_elasticsearch $name; then
195                 warn "Something went wrong rebuilding indexes for ${name}"
196             fi
197         else
198             # TODO: Add other actions, status? etc
199             usage
200             die "Error: no action passed"
201         fi
202     else
203         warn "Unknown instance ${name}"
204     fi
205 done
206
207 exit 0