Bug 34204: Fix koha-shell under debian 12
[koha.git] / debian / scripts / koha-functions.sh
1 #!/bin/sh
2 #
3 # koha-functions.sh -- shared library of helper functions for koha-* scripts
4 # Copyright 2014 - Tomas Cohen Arazi
5 #                  Universidad Nacional de Cordoba
6 #
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.
11 #
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.
16 #
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/>.
19
20
21 die()
22 {
23     echo "$@" 1>&2
24     exit 1
25 }
26
27 warn()
28 {
29     echo "$@" 1>&2
30 }
31
32 get_apache_config_for()
33 {
34     local site=$1
35     local sitefile="/etc/apache2/sites-available/$site"
36
37     if is_instance $site; then
38         if [ -f "$sitefile.conf" ]; then
39             echo "$sitefile.conf"
40         elif [ -f "$sitefile" ]; then
41             echo "$sitefile"
42         fi
43     fi
44 }
45
46 get_opacdomain_for()
47 {
48     local site=$1
49
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
55     fi
56     local opacdomain="$OPACPREFIX$site$OPACSUFFIX$DOMAIN"
57     echo "$opacdomain"
58 }
59
60 get_intradomain_for()
61 {
62     local site=$1
63
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
69     fi
70     local intradomain="$INTRAPREFIX$site$INTRASUFFIX$DOMAIN"
71     echo "$intradomain"
72 }
73
74 letsencrypt_get_opacdomain_for()
75 {
76     local site=$1
77
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)
82     fi
83     echo "$opacdomain"
84 }
85
86 is_enabled()
87 {
88     local site=$1
89     local instancefile=$(get_apache_config_for $site)
90
91     if [ "$instancefile" = "" ]; then
92         return 1
93     fi
94
95     if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-disable.conf' \
96             "$instancefile" ; then
97         return 1
98     else
99         return 0
100     fi
101 }
102
103 is_instance()
104 {
105     local instancename=$1
106
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
113     fi
114 }
115
116 is_email_enabled()
117 {
118     local instancename=$1
119
120     if [ -e /var/lib/koha/$instancename/email.enabled ]; then
121         return 0
122     else
123         return 1
124     fi
125 }
126
127 is_letsencrypt_enabled()
128 {
129     local instancename=$1
130
131     if [ -e /var/lib/koha/$instancename/letsencrypt.enabled ]; then
132         return 0
133     else
134         return 1
135     fi
136 }
137
138 is_sip_enabled()
139 {
140     local instancename=$1
141
142     if [ -e /var/lib/koha/$instancename/sip.enabled ]; then
143         return 0
144     else
145         return 1
146     fi
147 }
148
149 is_sitemap_enabled()
150 {
151     local instancename=$1
152
153     if [ -e /var/lib/koha/$instancename/sitemap.enabled ]; then
154         return 0
155     else
156         return 1
157     fi
158 }
159
160 is_sip_running()
161 {
162     local instancename=$1
163
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
171     fi
172 }
173
174 is_zebra_running()
175 {
176     local instancename=$1
177
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
185     fi
186 }
187
188 is_indexer_running()
189 {
190     local instancename=$1
191
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
199     fi
200 }
201
202 is_es_indexer_running()
203 {
204     local instancename=$1
205
206     if daemon --name="$instancename-koha-es-indexer" \
207             --pidfiles="/var/run/koha/$instancename/" \
208             --user="$instancename-koha.$instancename-koha" \
209             --running ; then
210         return 0
211     else
212         return 1
213     fi
214 }
215
216 is_worker_running()
217 {
218     local instancename=$1
219     local queue=$2
220
221     local name=`get_worker_name $instancename $queue`
222
223     if daemon --name="$name" \
224             --pidfiles="/var/run/koha/$instancename/" \
225             --user="$instancename-koha.$instancename-koha" \
226             --running ; then
227         return 0
228     else
229         return 1
230     fi
231 }
232
233 get_worker_name()
234 {
235     local name=$1
236     local queue=$2
237
238     if [ "$queue" = "" ] || [ "$queue" = "default" ]; then
239         echo "${name}-koha-worker"
240     else
241         echo "${name}-koha-worker-${queue}"
242     fi
243 }
244
245 is_plack_enabled_opac()
246 {
247     local instancefile=$1
248
249     if [ "$instancefile" = "" ]; then
250         return 1
251     fi
252
253     # remember 0 means success/true in bash.
254     if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-opac-plack.conf' \
255             "$instancefile" ; then
256         return 0
257     else
258         return 1
259     fi
260 }
261
262 is_plack_enabled_intranet()
263 {
264     local instancefile=$1
265
266     if [ "$instancefile" = "" ]; then
267         return 1
268     fi
269
270     # remember 0 means success/true in bash.
271     if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-intranet-plack.conf' \
272             "$instancefile" ; then
273         return 0
274     else
275         return 1
276     fi
277 }
278
279 is_plack_enabled()
280 {
281     local site=$1
282     local instancefile=$(get_apache_config_for $site)
283
284     if [ "$instancefile" = "" ]; then
285         return 1
286     fi
287
288     if is_plack_enabled_opac $instancefile ; then
289         enabledopac=1
290     else
291         enabledopac=0
292     fi
293     if is_plack_enabled_intranet $instancefile ; then
294         enabledintra=1
295     else
296         enabledintra=0
297     fi
298
299     # remember 0 means success/true in bash.
300     if [ "$enabledopac" != "$enabledintra" ] ; then
301         echo "$site has a plack configuration error. Enable or disable plack to correct this."
302         return 0
303     elif [ "$enabledopac" = "1" ] ; then
304         return 0
305     else
306         return 1
307     fi
308 }
309
310 is_plack_running()
311 {
312     local instancename=$1
313
314     if start-stop-daemon --pidfile "/var/run/koha/${instancename}/plack.pid" \
315             --user="$instancename-koha" \
316             --status ; then
317         return 0
318     else
319         return 1
320     fi
321 }
322
323 is_z3950_enabled()
324 {
325     local instancename=$1
326
327     if [ -e /etc/koha/sites/$instancename/z3950/config.xml ]; then
328         return 0
329     else
330         return 1
331     fi
332 }
333
334 is_z3950_running()
335 {
336     local instancename=$1
337
338     if start-stop-daemon --pidfile "/var/run/koha/${instancename}/z3950-responder.pid" \
339             --user="$instancename-koha" \
340             --status ; then
341         return 0
342     else
343         return 1
344     fi
345 }
346
347 is_elasticsearch_enabled()
348 {
349     local instancename=$1
350
351     # is querying the DB fails then $searching won't match 'Elasticsearch'. Ditching STDERR
352     search_engine=$(koha-shell $instancename -c "/usr/share/koha/bin/admin/koha-preferences get SearchEngine 2> /dev/null")
353
354     if [ "$search_engine" = "Elasticsearch" ]; then
355         return 0
356     else
357         return 1
358     fi
359 }
360
361 adjust_paths_dev_install()
362 {
363 # Adjust KOHA_HOME, PERL5LIB for dev installs, as indicated by
364 # corresponding tag in koha-conf.xml
365
366     local instancename=$1
367     local dev_install=""
368
369     if [ "$instancename" != "" ] && is_instance $instancename; then
370         dev_install=$(run_safe_xmlstarlet $instancename dev_install)
371     fi
372
373     if [ "$dev_install" != "" ] && [ "$dev_install" != "0" ]; then
374         DEV_INSTALL=1
375         KOHA_HOME=$(run_safe_xmlstarlet $instancename intranetdir)
376         PERL5LIB="$KOHA_HOME:$KOHA_HOME/lib"
377     else
378         DEV_INSTALL=""
379     fi
380 }
381
382 get_instances()
383 {
384     find -L /etc/koha/sites -mindepth 1 -maxdepth 1\
385                          -type d -printf '%f\n' | sort
386 }
387
388 get_loglevels()
389 {
390     local instancename=$1
391     local retval=$(run_safe_xmlstarlet $instancename zebra_loglevels)
392     if [ "$retval" != "" ]; then
393         echo "$retval"
394     else
395         echo "none,fatal,warn"
396     fi
397 }
398
399 get_max_record_size()
400 {
401     local instancename=$1
402     local retval=$(xmlstarlet sel -t -v 'yazgfs/config/zebra_max_record_size' /etc/koha/sites/$instancename/koha-conf.xml)
403     if [ "$retval" != "" ]; then
404         echo "$retval"
405     else
406         echo "1024"
407     fi
408 }
409
410 get_tmp_path()
411 {
412     local instancename=$1
413     local retval=$(run_safe_xmlstarlet $instancename tmp_path)
414     if [ "$retval" != "" ]; then
415         echo "$retval"
416         return 0
417     fi
418 }
419
420 get_upload_path()
421 {
422     local instancename=$1
423     local retval=$(run_safe_xmlstarlet $instancename upload_path)
424     if [ "$retval" != "" ]; then
425         echo "$retval"
426         return 0
427     fi
428 }
429
430 get_tmpdir()
431 {
432     if [ "$TMPDIR" != "" ]; then
433         if [ -d "$TMPDIR" ]; then
434             echo $TMPDIR
435             return 0
436         fi
437         # We will not unset TMPDIR but just default to /tmp here
438         # Note that mktemp (used later) would look at TMPDIR
439         echo "/tmp"
440         return 0
441     fi
442     local retval=$(mktemp -u)
443     if [ "$retval" = "" ]; then
444         echo "/tmp"
445         return 0
446     fi
447     echo $(dirname $retval)
448 }
449
450 run_safe_xmlstarlet()
451 {
452     # When a bash script sets -e (errexit), calling xmlstarlet on an
453     # unexisting key would halt the script. This is resolved by calling
454     # this function in a subshell. It will always returns true, while not
455     # affecting the exec env of the caller. (Otherwise, errexit is cleared.)
456     local instancename=$1
457     local myexpr=$2
458     set +e; # stay on the safe side
459     echo $(xmlstarlet sel -t -v "yazgfs/config/$myexpr" /etc/koha/sites/$instancename/koha-conf.xml)
460     return 0
461 }