Bug 9890: fix plugin handling by koha-create and koha-create-dirs
[koha.git] / debian / scripts / koha-create
1 #!/bin/sh
2 #
3 # koha-create -- Create a new Koha instance.
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
20 set -e
21
22 usage="Usage: $0 [--create-db|--request-db|--populate-db|--use-db] \
23     [--marcflavor marc21|normarc|unimarc] \
24     [--zebralang en|nb|fr] \
25     [--defaultsql /path/to/some.sql] \
26     [--configfile /path/to/config] [--passwdfile /path/to/passwd] \
27     [--database database] [--adminuser n] instancename"
28
29 die() {
30     echo "$@" 1>&2
31     exit 1
32 }
33
34 # UPPER CASE VARIABLES - from configfile or default value
35 # lower case variables - generated within this script
36 generate_config_file() {
37     touch "$2"
38     chown "root:$username" "$2"
39     chmod 0640 "$2"
40     sed -e "s/__KOHASITE__/$name/g" \
41         -e "s/__OPACPORT__/$OPACPORT/g" \
42         -e "s/__INTRAPORT__/$INTRAPORT/g" \
43         -e "s/__OPACSERVER__/$opacdomain/g" \
44         -e "s/__INTRASERVER__/$intradomain/g" \
45         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
46         -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
47         -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
48         -e "s/__DB_NAME__/$mysqldb/g" \
49         -e "s/__DB_HOST__/$mysqlhost/g" \
50         -e "s/__DB_USER__/$mysqluser/g" \
51         -e "s/__DB_PASS__/$mysqlpwd/g" \
52         -e "s/__UNIXUSER__/$username/g" \
53         -e "s/__UNIXGROUP__/$username/g" \
54         -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
55         "/etc/koha/$1" > "$2"
56 }
57
58 getmysqlhost() {
59     awk '
60         /^\[/ { inclient = 0 }
61         /^\[client\]/ { inclient = 1 }
62         inclient && /^ *host *=/ { print $3 }' \
63         /etc/mysql/koha-common.cnf
64 }
65
66 getinstancemysqlpassword() {
67     xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
68 }
69
70 getinstancemysqluser() {
71     xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
72 }
73
74 getinstancemysqldatabase() {
75     xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
76 }
77
78 # Set defaults and read config file, if it exists.
79 DOMAIN=""
80 OPACPORT="80"
81 OPACPREFIX=""
82 OPACSUFFIX=""
83 INTRAPORT="8080"
84 INTRAPREFIX=""
85 INTRASUFFIX=""
86 DEFAULTSQL=""
87 ZEBRA_MARC_FORMAT="marc21"
88 ZEBRA_LANGUAGE="en"
89 ADMINUSER="1"
90 PASSWDFILE="/etc/koha/passwd"
91 if [ -e /etc/koha/koha-sites.conf ]
92 then
93     . /etc/koha/koha-sites.conf
94 fi
95
96 [ $# -ge 2 ] && [ $# -le 16 ] || die $usage
97
98 TEMP=`getopt -o crpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,marcflavor:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser: \
99      -n "$0" -- "$@"`
100
101 # Note the quotes around `$TEMP': they are essential!
102 eval set -- "$TEMP"
103
104 # Temporary variables for the command line options
105 CLO_ZEBRA_MARC_FORMAT=""
106 CLO_ZEBRA_LANGUAGE=""
107 CLO_DEFAULTSQL=""
108 CLO_ADMINUSER=""
109
110 while true ; do
111         case "$1" in
112                 -c|--create-db) op=create ; shift ;;
113                 -r|--request-db) op=request ; shift ;;
114                 -p|--populate-db) op=populate ; shift ;;
115         -u|--use-db) op=use ; shift ;;
116                 -m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
117                 -l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
118                 -d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;;
119                 -f|--configfile) configfile="$2" ; shift 2 ;;
120         -s|--passwdfile) CLO_PASSWDFILE="$2" ; shift 2 ;;
121         -b|--database) CLO_DATABASE="$2" ; shift 2 ;;
122                 -a|--adminuser) CLO_ADMINUSER="$2" ; shift 2 ;;
123                 --) shift ; break ;;
124                 *) die "Internal error processing command line arguments" ;;
125         esac
126 done
127
128 # Load the configfile given on the command line
129 if [ "$configfile" != "" ]
130 then
131     if [ -e "$configfile" ]
132     then
133         . "$configfile"
134     else
135         die "$configfile does not exist.";
136     fi
137 fi
138
139 # Make sure options from the command line get the highest precedence
140 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
141 then
142     ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
143 fi
144 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
145 then
146     ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
147 fi
148 if [ "$CLO_DEFAULTSQL" != "" ]
149 then
150     DEFAULTSQL="$CLO_DEFAULTSQL"
151 fi
152 if [ "$CLO_ADMINUSER" != "" ]
153 then
154     ADMINUSER="$CLO_ADMINUSER"
155 fi
156 if [ "$CLO_PASSWDFILE" != "" ]
157 then
158     PASSWDFILE="$CLO_PASSWDFILE"
159 fi
160
161 name="$1"
162
163 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
164 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
165
166
167 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
168 then
169     passwdline=`cat $PASSWDFILE | grep "^$name:"`
170     mysqluser=`echo $passwdline | cut -d ":" -f 2`
171     mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
172     mysqldb=`echo $passwdline | cut -d ":" -f 4`
173 fi
174
175 # The order of precedence for MySQL database name is:
176 # default < passwd file < command line
177 if [ "$mysqldb" = "" ]
178 then
179     mysqldb="koha_$name"
180 fi
181 if [ "$CLO_DATABASE" != "" ]
182 then
183     mysqldb="$CLO_DATABASE"
184 fi
185
186 if [ "$mysqluser" = "" ]
187 then
188     mysqluser="koha_$name"
189 fi
190 mysqlhost="$(getmysqlhost)"
191
192 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
193 then
194     if [ "$mysqlpwd" = "" ]
195     then
196         mysqlpwd="$(pwgen -s 16 1)"
197     fi
198 else
199     mysqlpwd="$(getinstancemysqlpassword $name)"
200 fi
201
202
203 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
204 then
205     # Create new user and group.
206     username="$name-koha"
207     if getent passwd "$username" > /dev/null
208     then
209         die "User $username already exists."
210     fi
211     if getent group "$username" > /dev/null
212     then
213         die "Group $username already exists."
214     fi
215     adduser --no-create-home --disabled-login \
216         --gecos "Koha instance $username" \
217         --home "/var/lib/koha/$name" \
218         --quiet "$username"
219
220     # Create the site-specific directories.
221     koha-create-dirs "$name"
222
223     # Generate Zebra database password.
224     zebrapwd="$(pwgen -s 16 1)"
225     # Future enhancement: make this configurable for when your db is on
226     # another server.
227     mysql_hostname="localhost"
228     # Set up MySQL database for this instance.
229     if [ "$op" = create ]
230     then
231         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
232 CREATE DATABASE \`$mysqldb\`;
233 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
234 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
235 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
236 FLUSH PRIVILEGES;
237 eof
238     fi #`
239
240     if [ "$op" = use ]
241     then
242         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
243 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
244 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
245 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
246 FLUSH PRIVILEGES;
247 eof
248     fi #`
249
250     # Generate and install Apache site-available file and log dir.
251     generate_config_file apache-site.conf.in \
252         "/etc/apache2/sites-available/$name"
253     mkdir "/var/log/koha/$name"
254     chown "$username:$username" "/var/log/koha/$name"
255
256
257     # Generate and install main Koha config file.
258     generate_config_file koha-conf-site.xml.in \
259         "/etc/koha/sites/$name/koha-conf.xml"
260
261     # Generate and install Zebra config files.
262     generate_config_file zebra-biblios-site.cfg.in \
263         "/etc/koha/sites/$name/zebra-biblios.cfg"
264     generate_config_file zebra-authorities-site.cfg.in \
265         "/etc/koha/sites/$name/zebra-authorities.cfg"
266     generate_config_file zebra-authorities-dom-site.cfg.in \
267         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
268     generate_config_file zebra.passwd.in \
269         "/etc/koha/sites/$name/zebra.passwd"
270
271
272     # Create a GPG-encrypted file for requesting a DB to be set up.
273     if [ "$op" = request ]
274     then
275         touch "$name-db-request.txt"
276         chmod 0600 "$name-db-request.txt"
277         cat > "$name-db-request.txt" << eof
278 Please create a MySQL database and user on $mysqlhost as follows:
279
280 database name: $mysqldb
281 database user: $mysqluser
282      password: $mysqlpwd
283
284 Thank you.
285 eof
286
287         echo "See $name-db-request.txt for database creation request."
288         echo "Please forward it to the right person, and then run"
289         echo "$0 --populate-db $name"
290         echo "Thanks."
291     fi
292 fi
293
294
295 if [ "$op" = create ] || [ "$op" = populate ]
296 then
297     # Re-fetch the passwords from the config we've generated, allows it
298     # to be different from what we set, in case the user had to change
299     # something.
300     mysqluser=$(getinstancemysqluser $name)
301     mysqldb=$(getinstancemysqldatabase $name)
302     # Use the default database content if that exists.
303     if [ -e "$DEFAULTSQL" ]
304     then
305         # Populate the database with default content.
306         zcat "$DEFAULTSQL" |
307         sed "s/__KOHASITE__/$name/g" |
308         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
309
310
311         # Change the default user's password.
312         staffpass="$(pwgen 12 1)"
313         staffdigest=$(echo -n "$staffpass" |
314                       perl -e '
315                             use Digest::MD5 qw(md5_base64); 
316                             while (<>) { print md5_base64($_), "\n"; }')
317         mysql --host="$mysqlhost" --user="$mysqluser" \
318 --password="$mysqlpwd" <<eof
319 USE \`$mysqldb\`;
320 UPDATE borrowers 
321 SET password = '$staffdigest' 
322 WHERE borrowernumber = $ADMINUSER;
323 eof
324         #`
325         echo "staff user password is '$staffpass' but keep that secret"
326
327         # Upgrade the database schema, just in case the dump was from an 
328         # old version.
329         koha-upgrade-schema "$name"
330     else
331         echo "Koha instance is empty, no staff user created."
332     fi
333 fi
334
335
336 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
337 then
338     # Reconfigure Apache.
339     a2ensite "$name"
340     service apache2 restart
341
342     # Start Zebra.
343     koha-start-zebra "$name"
344 fi
345
346
347 if [ "$op" = request ]
348 then
349     koha-disable "$name"
350 fi
351
352 echo <<eoh
353
354 Email for this instance is disabled. When you're ready to enable it, use:
355 koha-email-enable $name
356 eoh