Merge branch 'new/bug11185' into 3.14.x
[koha.git] / debian / scripts / koha-start-zebra
1 #!/bin/sh
2 #
3 # koha-start-zebra - Start Zebra for named Koha instances
4 # Copyright 2010  Catalyst IT, Ltd
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 set -e
20
21 die()
22 {
23     echo "$@" 1>&2
24     exit 1
25 }
26
27 warn()
28 {
29     echo "$@" 1>&2
30 }
31
32 is_enabled()
33 {
34     local instancename=$1
35
36     if ! is_instance $instancename; then
37         return 1
38     fi
39
40     if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-disable.conf' \
41             "/etc/apache2/sites-available/$instancename" ; then
42         return 1
43     else
44         return 0
45     fi
46 }
47
48 is_instance()
49 {
50     local instancename=$1
51
52     if find /etc/koha/sites -mindepth 1 -maxdepth 1 \
53                          -type d -printf '%f\n'\
54           | grep -q -x $instancename ; then
55         return 0
56     else
57         return 1
58     fi
59 }
60
61 is_zebra_running()
62 {
63     local instancename=$1
64
65     if daemon --name="$instancename-koha-zebra" \
66             --user="$instancename-koha.$instancename-koha" \
67             --running ; then
68         return 0
69     else
70         return 1
71     fi
72 }
73
74 start_zebra_instance()
75 {
76     local instancename=$1
77
78     if is_enabled $instancename; then
79         echo "Starting Zebra server for $instancename"
80         touch "/var/log/koha/$instancename/zebra-error.log" \
81             "/var/log/koha/$instancename/zebra.log" \
82             "/var/log/koha/$instancename/zebra-output.log"
83         chown "$instancename-koha:$instancename-koha" \
84             "/var/log/koha/$instancename/zebra-error.log" \
85             "/var/log/koha/$instancename/zebra.log" \
86             "/var/log/koha/$instancename/zebra-output.log"
87         daemon \
88             --name="$instancename-koha-zebra" \
89             --errlog="/var/log/koha/$instancename/zebra-error.log" \
90             --stdout="/var/log/koha/$instancename/zebra.log" \
91             --output="/var/log/koha/$instancename/zebra-output.log" \
92             --verbose=1 \
93             --respawn \
94             --delay=30 \
95             --user="$instancename-koha.$instancename-koha" \
96             -- \
97             zebrasrv \
98             -v none,fatal,warn \
99             -f "/etc/koha/sites/$instancename/koha-conf.xml" && \
100         return 0
101     else
102         return 1
103     fi
104 }
105
106 usage()
107 {
108     local scriptname=$0
109     cat <<EOF
110 Starts Zebra for Koha instances.
111
112 Usage: $scriptname instancename1 instancename2...
113
114 EOF
115 }
116
117 # Parse command line.
118 #[ $# -ge 1 ] || ( usage ; die "Missing instance name..." )
119
120 # Loop through the instance names
121 for name in "$@"
122 do
123     if is_instance $name ; then
124         if is_enabled $name ; then
125             if ! is_zebra_running $name; then
126                 if ! start_zebra_instance $name; then
127                     warn "Something went wrong starting Zebra for $name."
128                 fi
129             else
130                 warn "Zebra already running for instance $name."
131             fi
132         else
133             warn "Instance $name disabled. No action taken."
134         fi
135     else
136         warn "Unknown instance $name."
137     fi
138 done
139
140 exit 0