Bug 26180: Add descending option to rebuild_elasticsearch.pl
[koha.git] / debian / scripts / koha-functions.sh
blobf82ebfcc80b9235eea1474e8d4c45e08d44ab9cf
1 #!/bin/sh
3 # koha-functions.sh -- shared library of helper functions for koha-* scripts
4 # Copyright 2014 - Tomas Cohen Arazi
5 # Universidad Nacional de Cordoba
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 die()
23 echo "$@" 1>&2
24 exit 1
27 warn()
29 echo "$@" 1>&2
32 get_apache_config_for()
34 local site=$1
35 local sitefile="/etc/apache2/sites-available/$site"
37 if is_instance $site; then
38 if [ -f "$sitefile.conf" ]; then
39 echo "$sitefile.conf"
40 elif [ -f "$sitefile" ]; then
41 echo "$sitefile"
46 get_opacdomain_for()
48 local site=$1
50 if [ -e /etc/koha/koha-sites.conf ]; then
51 . /etc/koha/koha-sites.conf
52 else
53 echo "Error: /etc/koha/koha-sites.conf not present." 1>&2
54 exit 1
56 local opacdomain="$OPACPREFIX$site$OPACSUFFIX$DOMAIN"
57 echo "$opacdomain"
60 get_intradomain_for()
62 local site=$1
64 if [ -e /etc/koha/koha-sites.conf ]; then
65 . /etc/koha/koha-sites.conf
66 else
67 echo "Error: /etc/koha/koha-sites.conf not present." 1>&2
68 exit 1
70 local intradomain="$INTRAPREFIX$site$INTRASUFFIX$DOMAIN"
71 echo "$intradomain"
74 letsencrypt_get_opacdomain_for()
76 local site=$1
78 if [ -e /var/lib/koha/$site/letsencrypt.enabled ]; then
79 . /var/lib/koha/$site/letsencrypt.enabled
80 else
81 local opacdomain=$(get_opacdomain_for $site)
83 echo "$opacdomain"
86 is_enabled()
88 local site=$1
89 local instancefile=$(get_apache_config_for $site)
91 if [ "$instancefile" = "" ]; then
92 return 1
95 if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-disable.conf' \
96 "$instancefile" ; then
97 return 1
98 else
99 return 0
103 is_instance()
105 local instancename=$1
107 if find -L /etc/koha/sites -mindepth 1 -maxdepth 1 \
108 -type d -printf '%f\n'\
109 | grep -q -x "$instancename" ; then
110 return 0
111 else
112 return 1
116 is_email_enabled()
118 local instancename=$1
120 if [ -e /var/lib/koha/$instancename/email.enabled ]; then
121 return 0
122 else
123 return 1
127 is_letsencrypt_enabled()
129 local instancename=$1
131 if [ -e /var/lib/koha/$instancename/letsencrypt.enabled ]; then
132 return 0
133 else
134 return 1
138 is_sip_enabled()
140 local instancename=$1
142 if [ -e /var/lib/koha/$instancename/sip.enabled ]; then
143 return 0
144 else
145 return 1
149 is_sitemap_enabled()
151 local instancename=$1
153 if [ -e /var/lib/koha/$instancename/sitemap.enabled ]; then
154 return 0
155 else
156 return 1
160 is_sip_running()
162 local instancename=$1
164 if daemon --name="$instancename-koha-sip" \
165 --pidfiles="/var/run/koha/$instancename/" \
166 --user="$instancename-koha.$instancename-koha" \
167 --running ; then
168 return 0
169 else
170 return 1
174 is_zebra_running()
176 local instancename=$1
178 if daemon --name="$instancename-koha-zebra" \
179 --pidfiles="/var/run/koha/$instancename/" \
180 --user="$instancename-koha.$instancename-koha" \
181 --running ; then
182 return 0
183 else
184 return 1
188 is_indexer_running()
190 local instancename=$1
192 if daemon --name="$instancename-koha-indexer" \
193 --pidfiles="/var/run/koha/$instancename/" \
194 --user="$instancename-koha.$instancename-koha" \
195 --running ; then
196 return 0
197 else
198 return 1
202 is_worker_running()
204 local instancename=$1
206 if daemon --name="$instancename-koha-worker" \
207 --pidfiles="/var/run/koha/$instancename/" \
208 --user="$instancename-koha.$instancename-koha" \
209 --running ; then
210 return 0
211 else
212 return 1
216 is_plack_enabled_opac()
218 local instancefile=$1
220 if [ "$instancefile" = "" ]; then
221 return 1
224 # remember 0 means success/true in bash.
225 if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-opac-plack.conf' \
226 "$instancefile" ; then
227 return 0
228 else
229 return 1
233 is_plack_enabled_intranet()
235 local instancefile=$1
237 if [ "$instancefile" = "" ]; then
238 return 1
241 # remember 0 means success/true in bash.
242 if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-intranet-plack.conf' \
243 "$instancefile" ; then
244 return 0
245 else
246 return 1
250 is_plack_enabled()
252 local site=$1
253 local instancefile=$(get_apache_config_for $site)
255 if [ "$instancefile" = "" ]; then
256 return 1
259 if is_plack_enabled_opac $instancefile ; then
260 enabledopac=1
261 else
262 enabledopac=0
264 if is_plack_enabled_intranet $instancefile ; then
265 enabledintra=1
266 else
267 enabledintra=0
270 # remember 0 means success/true in bash.
271 if [ "$enabledopac" != "$enabledintra" ] ; then
272 echo "$site has a plack configuration error. Enable or disable plack to correct this."
273 return 0
274 elif [ "$enabledopac" = "1" ] ; then
275 return 0
276 else
277 return 1
281 is_plack_running()
283 local instancename=$1
285 if start-stop-daemon --pidfile "/var/run/koha/${instancename}/plack.pid" \
286 --user="$instancename-koha" \
287 --status ; then
288 return 0
289 else
290 return 1
294 is_z3950_enabled()
296 local instancename=$1
298 if [ -e /etc/koha/sites/$instancename/z3950/config.xml ]; then
299 return 0
300 else
301 return 1
305 is_z3950_running()
307 local instancename=$1
309 if start-stop-daemon --pidfile "/var/run/koha/${instancename}/z3950-responder.pid" \
310 --user="$instancename-koha" \
311 --status ; then
312 return 0
313 else
314 return 1
318 adjust_paths_dev_install()
320 # Adjust KOHA_HOME, PERL5LIB for dev installs, as indicated by
321 # corresponding tag in koha-conf.xml
323 local instancename=$1
324 local dev_install=""
326 if [ "$instancename" != "" ] && is_instance $instancename; then
327 dev_install=$(run_safe_xmlstarlet $instancename dev_install)
330 if [ "$dev_install" != "" ] && [ "$dev_install" != "0" ]; then
331 DEV_INSTALL=1
332 KOHA_HOME=$(run_safe_xmlstarlet $instancename intranetdir)
333 PERL5LIB=$KOHA_HOME
334 else
335 DEV_INSTALL=""
339 get_instances()
341 find -L /etc/koha/sites -mindepth 1 -maxdepth 1\
342 -type d -printf '%f\n' | sort
345 get_loglevels()
347 local instancename=$1
348 local retval=$(run_safe_xmlstarlet $instancename zebra_loglevels)
349 if [ "$retval" != "" ]; then
350 echo "$retval"
351 else
352 echo "none,fatal,warn"
356 get_max_record_size()
358 local instancename=$1
359 local retval=$(xmlstarlet sel -t -v 'yazgfs/config/zebra_max_record_size' /etc/koha/sites/$instancename/koha-conf.xml)
360 if [ "$retval" != "" ]; then
361 echo "$retval"
362 else
363 echo "1024"
367 get_tmpdir()
369 if [ "$TMPDIR" != "" ]; then
370 if [ -d "$TMPDIR" ]; then
371 echo $TMPDIR
372 return 0
374 # We will not unset TMPDIR but just default to /tmp here
375 # Note that mktemp (used later) would look at TMPDIR
376 echo "/tmp"
377 return 0
379 local retval=$(mktemp -u)
380 if [ "$retval" = "" ]; then
381 echo "/tmp"
382 return 0
384 echo $(dirname $retval)
387 run_safe_xmlstarlet()
389 # When a bash script sets -e (errexit), calling xmlstarlet on an
390 # unexisting key would halt the script. This is resolved by calling
391 # this function in a subshell. It will always returns true, while not
392 # affecting the exec env of the caller. (Otherwise, errexit is cleared.)
393 local instancename=$1
394 local myexpr=$2
395 set +e; # stay on the safe side
396 echo $(xmlstarlet sel -t -v "yazgfs/config/$myexpr" /etc/koha/sites/$instancename/koha-conf.xml)
397 return 0