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