updated release notes for 3.14.0 beta
[koha.git] / debian / scripts / koha-create
1 #!/bin/bash
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|es|fr|nb|ru|uk] \
25     [--auth-idx dom|grs1] [--biblio-idx dom|grs1] \
26     [--use-memcached] \
27     [--memcached-servers server:port] [--memcached-prefix prefix] \
28     [--defaultsql /path/to/some.sql] \
29     [--configfile /path/to/config] [--passwdfile /path/to/passwd] \
30     [--database database] [--adminuser n] instancename"
31
32 die() {
33     echo "$@" 1>&2
34     exit 1
35 }
36
37 # UPPER CASE VARIABLES - from configfile or default value
38 # lower case variables - generated within this script
39 generate_config_file() {
40     touch "$2"
41     chown "root:$username" "$2"
42     chmod 0640 "$2"
43     sed -e "s/__KOHASITE__/$name/g" \
44         -e "s/__OPACPORT__/$OPACPORT/g" \
45         -e "s/__INTRAPORT__/$INTRAPORT/g" \
46         -e "s/__OPACSERVER__/$opacdomain/g" \
47         -e "s/__INTRASERVER__/$intradomain/g" \
48         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
49         -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
50         -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
51         -e "s/__BIBLIOS_INDEXING_MODE__/$BIBLIOS_INDEXING_MODE/g" \
52         -e "s/__AUTHORITIES_INDEXING_MODE__/$AUTHORITIES_INDEXING_MODE/g" \
53         -e "s/__ZEBRA_BIBLIOS_CFG__/$ZEBRA_BIBLIOS_CFG/g" \
54         -e "s/__ZEBRA_AUTHORITIES_CFG__/$ZEBRA_AUTHORITIES_CFG/g" \
55         -e "s/__START_BIBLIOS_RETRIEVAL_INFO__/`echo $START_BIBLIOS_RETRIEVAL_INFO`/g" \
56         -e "s/__END_BIBLIOS_RETRIEVAL_INFO__/`echo $END_BIBLIOS_RETRIEVAL_INFO`/g" \
57         -e "s/__START_AUTHORITIES_RETRIEVAL_INFO__/`echo $START_AUTHORITIES_RETRIEVAL_INFO`/g" \
58         -e "s/__END_AUTHORITIES_RETRIEVAL_INFO__/`echo $END_AUTHORITIES_RETRIEVAL_INFO`/g" \
59         -e "s/__DB_NAME__/$mysqldb/g" \
60         -e "s/__DB_HOST__/$mysqlhost/g" \
61         -e "s/__DB_USER__/$mysqluser/g" \
62         -e "s/__DB_PASS__/$mysqlpwd/g" \
63         -e "s/__UNIXUSER__/$username/g" \
64         -e "s/__UNIXGROUP__/$username/g" \
65         -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
66         -e "s/__MEMCACHED_NAMESPACE__/$MEMCACHED_NAMESPACE/g" \
67         -e "s/__MEMCACHED_SERVERS__/$MEMCACHED_SERVERS/g" \
68         "/etc/koha/$1" > "$2"
69
70 }
71
72 getmysqlhost() {
73     awk '
74         /^\[/ { inclient = 0 }
75         /^\[client\]/ { inclient = 1 }
76         inclient && /^ *host *=/ { print $3 }' \
77         /etc/mysql/koha-common.cnf
78 }
79
80 getinstancemysqlpassword() {
81     xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
82 }
83
84 getinstancemysqluser() {
85     xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
86 }
87
88 getinstancemysqldatabase() {
89     xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
90 }
91
92 set_biblios_indexing_mode()
93 {
94     local indexing_mode=$1
95     local marc_format=$2
96
97     case $indexing_mode in
98         "dom")
99             START_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
100     <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-bib-dom.xml"\n
101                 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
102      <xi:fallback>\n
103       <retrievalinfo>
104 EOF`
105
106             END_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
107       <\/retrievalinfo>\n
108      <\/xi:fallback>\n
109     <\/xi:include>
110 EOF`
111             BIBLIOS_INDEXING_MODE="dom"
112             ZEBRA_BIBLIOS_CFG="zebra-biblios-dom.cfg"
113             ;;
114         "grs1")
115             START_BIBLIOS_RETRIEVAL_INFO="    <retrievalinfo>"
116             END_BIBLIOS_RETRIEVAL_INFO="    <\/retrievalinfo>"
117             BIBLIOS_INDEXING_MODE="grs1"
118             ZEBRA_BIBLIOS_CFG="zebra-biblios.cfg"
119             ;;
120         *)
121             die "Error: '$indexing_mode' is not a valid indexing mode for bibliographic records."
122             ;;
123     esac
124 }
125
126
127 set_authorities_indexing_mode()
128 {
129     local indexing_mode=$1
130     local marc_format=$2
131
132     case $indexing_mode in
133         "dom")
134             START_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
135     <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-auth-dom.xml"\n
136                 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
137      <xi:fallback>\n
138       <retrievalinfo>
139 EOF`
140
141             END_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
142       <\/retrievalinfo>\n
143      <\/xi:fallback>\n
144     <\/xi:include>\n
145 EOF`
146             AUTHORITIES_INDEXING_MODE="dom"
147             ZEBRA_AUTHORITIES_CFG="zebra-authorities-dom.cfg"
148             ;;
149         "grs1")
150             START_AUTHORITIES_RETRIEVAL_INFO="    <retrievalinfo>"
151             END_AUTHORITIES_RETRIEVAL_INFO="    <\/retrievalinfo>"
152             AUTHORITIES_INDEXING_MODE="grs1"
153             ZEBRA_AUTHORITIES_CFG="zebra-authorities.cfg"
154             ;;
155         *)
156             die "Error: '$indexing_mode' is not a valid indexing mode for authority records."
157             ;;
158     esac
159 }
160
161
162 set_memcached()
163 {
164     local instance="$1"
165
166     if [ "$CLO_MEMCACHED_SERVERS" != "" ]; then
167         MEMCACHED_SERVERS=$CLO_MEMCACHED_SERVERS
168     else
169         if [ "$MEMCACHED_SERVERS" = "" ]; then
170             MEMCACHED_SERVERS=$DEFAULT_MEMCACHED_SERVERS
171         # else: was set by the koha-sites.conf file
172         fi
173     fi
174
175     if [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
176         MEMCACHED_NAMESPACE="$CLO_MEMCACHED_PREFIX$instance"
177     else
178         if [ "$MEMCACHED_PREFIX" != "" ]; then
179             MEMCACHED_NAMESPACE="$MEMCACHED_PREFIX$instance"
180         else
181             MEMCACHED_NAMESPACE="$DEFAULT_MEMCACHED_PREFIX$instance"
182         fi
183     fi
184
185 }
186
187 # Set defaults and read config file, if it exists.
188 DOMAIN=""
189 OPACPORT="80"
190 OPACPREFIX=""
191 OPACSUFFIX=""
192 INTRAPORT="8080"
193 INTRAPREFIX=""
194 INTRASUFFIX=""
195 DEFAULTSQL=""
196 ZEBRA_MARC_FORMAT="marc21"
197 ZEBRA_LANGUAGE="en"
198 ADMINUSER="1"
199 PASSWDFILE="/etc/koha/passwd"
200
201 # memcached variables
202 USE_MEMCACHED="no"
203 MEMCACHED_SERVERS=""
204 MEMCACHED_PREFIX=""
205 # hardcoded memcached defaults
206 DEFAULT_MEMCACHED_SERVERS="127.0.0.1:11211"
207 DEFAULT_MEMCACHED_PREFIX="koha_"
208
209 # Indexing mode variables (default is DOM)
210 BIBLIOS_INDEXING_MODE="dom"
211 AUTHORITIES_INDEXING_MODE="dom"
212
213 START_BIBLIOS_RETRIEVAL_INFO=""
214 END_BIBLIOS_RETRIEVAL_INFO=""
215 START_AUTHORITIES_RETRIEVAL_INFO=""
216 END_AUTHORITIES_RETRIEVAL_INFO=""
217
218 if [ -e /etc/koha/koha-sites.conf ]
219 then
220     . /etc/koha/koha-sites.conf
221 fi
222
223 [ $# -ge 2 ] && [ $# -le 16 ] || die $usage
224
225 TEMP=`getopt -o crpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,use-memcached,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser:,memcached-servers:,memcached-prefix:, \
226      -n "$0" -- "$@"`
227
228 # Note the quotes around `$TEMP': they are essential!
229 eval set -- "$TEMP"
230
231 # Temporary variables for the command line options
232 CLO_ZEBRA_MARC_FORMAT=""
233 CLO_ZEBRA_LANGUAGE=""
234 CLO_DEFAULTSQL=""
235 CLO_ADMINUSER=""
236 CLO_BIBLIOS_INDEXING_MODE=""
237 CLO_AUTHORITIES_INDEXING_MODE=""
238 CLO_MEMCACHED_SERVERS=""
239 CLO_MEMCACHED_PREFIX=""
240
241
242 while true ; do
243     case "$1" in
244         -c|--create-db)
245             op=create ; shift ;;
246         -r|--request-db)
247             op=request ; shift ;;
248         -p|--populate-db)
249             op=populate ; shift ;;
250         -u|--use-db)
251             op=use ; shift ;;
252         --use-memcached)
253             USE_MEMCACHED="yes" ; shift ;;
254         --memcached-servers)
255             CLO_MEMCACHED_SERVERS="$2" ; shift 2 ;;
256         --memcached-prefix)
257             CLO_MEMCACHED_PREFIX="$2" ; shift 2;;
258         -m|--marcflavor)
259             CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
260     -l|--zebralang)
261             CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
262     --auth-idx)
263             CLO_AUTHORITIES_INDEXING_MODE="$2" ; shift 2 ;;
264     --biblio-idx)
265             CLO_BIBLIOS_INDEXING_MODE="$2" ; shift 2 ;;
266         -d|--defaultsql)
267             CLO_DEFAULTSQL="$2" ; shift 2 ;;
268         -f|--configfile)
269             configfile="$2" ; shift 2 ;;
270         -s|--passwdfile)
271             CLO_PASSWDFILE="$2" ; shift 2 ;;
272         -b|--database)
273             CLO_DATABASE="$2" ; shift 2 ;;
274         -a|--adminuser)
275             CLO_ADMINUSER="$2" ; shift 2 ;;
276         --)
277             shift ; break ;;
278         *)
279             die "Internal error processing command line arguments" ;;
280     esac
281 done
282
283 # Load the configfile given on the command line
284 if [ "$configfile" != "" ]
285 then
286     if [ -e "$configfile" ]
287     then
288         . "$configfile"
289     else
290         die "$configfile does not exist.";
291     fi
292 fi
293
294 # Make sure options from the command line get the highest precedence
295 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
296 then
297     ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
298 fi
299 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
300 then
301     ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
302 fi
303 if [ "$CLO_DEFAULTSQL" != "" ]
304 then
305     DEFAULTSQL="$CLO_DEFAULTSQL"
306 fi
307 if [ "$CLO_ADMINUSER" != "" ]
308 then
309     ADMINUSER="$CLO_ADMINUSER"
310 fi
311 if [ "$CLO_PASSWDFILE" != "" ]
312 then
313     PASSWDFILE="$CLO_PASSWDFILE"
314 fi
315
316 if [ "$CLO_BIBLIOS_INDEXING_MODE" !=  "" ]; then
317     BIBLIOS_INDEXING_MODE=$CLO_BIBLIOS_INDEXING_MODE
318 fi
319
320 set_biblios_indexing_mode $BIBLIOS_INDEXING_MODE $ZEBRA_MARC_FORMAT
321
322
323 if [ "$CLO_AUTHORITIES_INDEXING_MODE" !=  "" ]; then
324     AUTHORITIES_INDEXING_MODE=$CLO_AUTHORITIES_INDEXING_MODE
325 fi
326
327 set_authorities_indexing_mode $AUTHORITIES_INDEXING_MODE $ZEBRA_MARC_FORMAT
328
329 name="$1"
330
331 if [ "$USE_MEMCACHED" = "yes" ]; then
332     set_memcached $name
333 elif [ "$CLO_MEMCACHED_SERVERS" != "" ] || \
334      [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
335
336     MSG=`cat <<EOF
337
338 Error: you provided memcached configuration switches but memcached is not enabled.
339 Please set USE_MEMCACHED="yes" on /etc/koha/koha-sites.conf or use the
340 --use-memcached optio switch to enable it.
341
342 EOF`
343
344     echo $usage
345     die $MSG
346 else
347     # Unset memcached-related variables
348     MEMCACHED_SERVERS=""
349     MEMCACHED_PREFIX=""
350 fi
351
352 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
353 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
354
355
356 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
357 then
358     passwdline=`cat $PASSWDFILE | grep "^$name:"`
359     mysqluser=`echo $passwdline | cut -d ":" -f 2`
360     mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
361     mysqldb=`echo $passwdline | cut -d ":" -f 4`
362 fi
363
364 # The order of precedence for MySQL database name is:
365 # default < passwd file < command line
366 if [ "$mysqldb" = "" ]
367 then
368     mysqldb="koha_$name"
369 fi
370 if [ "$CLO_DATABASE" != "" ]
371 then
372     mysqldb="$CLO_DATABASE"
373 fi
374
375 if [ "$mysqluser" = "" ]
376 then
377     mysqluser="koha_$name"
378 fi
379 mysqlhost="$(getmysqlhost)"
380
381 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
382 then
383     if [ "$mysqlpwd" = "" ]
384     then
385         mysqlpwd="$(pwgen -s 16 1)"
386     fi
387 else
388     mysqlpwd="$(getinstancemysqlpassword $name)"
389 fi
390
391
392 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
393 then
394     # Create new user and group.
395     username="$name-koha"
396     if getent passwd "$username" > /dev/null
397     then
398         die "User $username already exists."
399     fi
400     if getent group "$username" > /dev/null
401     then
402         die "Group $username already exists."
403     fi
404     adduser --no-create-home --disabled-login \
405         --gecos "Koha instance $username" \
406         --home "/var/lib/koha/$name" \
407         --quiet "$username"
408
409     # Create the site-specific directories.
410     koha-create-dirs "$name"
411
412     # Generate Zebra database password.
413     zebrapwd="$(pwgen -s 16 1)"
414     # Future enhancement: make this configurable for when your db is on
415     # another server.
416     mysql_hostname="localhost"
417     # Set up MySQL database for this instance.
418     if [ "$op" = create ]
419     then
420         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
421 CREATE DATABASE \`$mysqldb\`;
422 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
423 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
424 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
425 FLUSH PRIVILEGES;
426 eof
427     fi #`
428
429     if [ "$op" = use ]
430     then
431         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
432 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
433 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
434 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
435 FLUSH PRIVILEGES;
436 eof
437     fi #`
438
439     # Generate and install Apache site-available file and log dir.
440     generate_config_file apache-site.conf.in \
441         "/etc/apache2/sites-available/$name"
442     mkdir "/var/log/koha/$name"
443     chown "$username:$username" "/var/log/koha/$name"
444
445
446     # Generate and install main Koha config file.
447     generate_config_file koha-conf-site.xml.in \
448         "/etc/koha/sites/$name/koha-conf.xml"
449
450     # Generate and install Zebra config files.
451     generate_config_file zebra-biblios-site.cfg.in \
452         "/etc/koha/sites/$name/zebra-biblios.cfg"
453     generate_config_file zebra-biblios-dom-site.cfg.in \
454         "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
455     generate_config_file zebra-authorities-site.cfg.in \
456         "/etc/koha/sites/$name/zebra-authorities.cfg"
457     generate_config_file zebra-authorities-dom-site.cfg.in \
458         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
459     generate_config_file zebra.passwd.in \
460         "/etc/koha/sites/$name/zebra.passwd"
461
462
463     # Create a GPG-encrypted file for requesting a DB to be set up.
464     if [ "$op" = request ]
465     then
466         touch "$name-db-request.txt"
467         chmod 0600 "$name-db-request.txt"
468         cat > "$name-db-request.txt" << eof
469 Please create a MySQL database and user on $mysqlhost as follows:
470
471 database name: $mysqldb
472 database user: $mysqluser
473      password: $mysqlpwd
474
475 Thank you.
476 eof
477
478         echo "See $name-db-request.txt for database creation request."
479         echo "Please forward it to the right person, and then run"
480         echo "$0 --populate-db $name"
481         echo "Thanks."
482     fi
483 fi
484
485
486 if [ "$op" = create ] || [ "$op" = populate ]
487 then
488     # Re-fetch the passwords from the config we've generated, allows it
489     # to be different from what we set, in case the user had to change
490     # something.
491     mysqluser=$(getinstancemysqluser $name)
492     mysqldb=$(getinstancemysqldatabase $name)
493     # Use the default database content if that exists.
494     if [ -e "$DEFAULTSQL" ]
495     then
496         # Populate the database with default content.
497         zcat "$DEFAULTSQL" |
498         sed "s/__KOHASITE__/$name/g" |
499         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
500
501
502         # Change the default user's password.
503         staffpass="$(pwgen 12 1)"
504         staffdigest=$(echo -n "$staffpass" |
505                       perl -e '
506                             use Digest::MD5 qw(md5_base64); 
507                             while (<>) { print md5_base64($_), "\n"; }')
508         mysql --host="$mysqlhost" --user="$mysqluser" \
509 --password="$mysqlpwd" <<eof
510 USE \`$mysqldb\`;
511 UPDATE borrowers 
512 SET password = '$staffdigest' 
513 WHERE borrowernumber = $ADMINUSER;
514 eof
515         #`
516         echo "staff user password is '$staffpass' but keep that secret"
517
518         # Upgrade the database schema, just in case the dump was from an 
519         # old version.
520         koha-upgrade-schema "$name"
521     else
522         echo "Koha instance is empty, no staff user created."
523     fi
524 fi
525
526
527 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
528 then
529     # Reconfigure Apache.
530     a2ensite "$name"
531     service apache2 restart
532
533     # Start Zebra.
534     koha-start-zebra "$name"
535 fi
536
537
538 if [ "$op" = request ]
539 then
540     koha-disable "$name"
541 fi
542
543 echo <<eoh
544
545 Email for this instance is disabled. When you're ready to enable it, use:
546 koha-email-enable $name
547 eoh