3 # koha-create -- Create a new Koha instance.
4 # Copyright 2010 Catalyst IT, Ltd
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.
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.
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/>.
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] \
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"
37 # UPPER CASE VARIABLES - from configfile or default value
38 # lower case variables - generated within this script
39 generate_config_file() {
41 chown "root:$username" "$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" \
74 /^\[/ { inclient = 0 }
75 /^\[client\]/ { inclient = 1 }
76 inclient && /^ *host *=/ { print $3 }' \
77 /etc/mysql/koha-common.cnf
80 getinstancemysqlpassword() {
81 xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
84 getinstancemysqluser() {
85 xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
88 getinstancemysqldatabase() {
89 xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
92 set_biblios_indexing_mode()
94 local indexing_mode=$1
97 case $indexing_mode in
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
106 END_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
111 BIBLIOS_INDEXING_MODE="dom"
112 ZEBRA_BIBLIOS_CFG="zebra-biblios-dom.cfg"
115 START_BIBLIOS_RETRIEVAL_INFO=" <retrievalinfo>"
116 END_BIBLIOS_RETRIEVAL_INFO=" <\/retrievalinfo>"
117 BIBLIOS_INDEXING_MODE="grs1"
118 ZEBRA_BIBLIOS_CFG="zebra-biblios.cfg"
121 die "Error: '$indexing_mode' is not a valid indexing mode for bibliographic records."
127 set_authorities_indexing_mode()
129 local indexing_mode=$1
132 case $indexing_mode in
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
141 END_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
146 AUTHORITIES_INDEXING_MODE="dom"
147 ZEBRA_AUTHORITIES_CFG="zebra-authorities-dom.cfg"
150 START_AUTHORITIES_RETRIEVAL_INFO=" <retrievalinfo>"
151 END_AUTHORITIES_RETRIEVAL_INFO=" <\/retrievalinfo>"
152 AUTHORITIES_INDEXING_MODE="grs1"
153 ZEBRA_AUTHORITIES_CFG="zebra-authorities.cfg"
156 die "Error: '$indexing_mode' is not a valid indexing mode for authority records."
166 if [ "$CLO_MEMCACHED_SERVERS" != "" ]; then
167 MEMCACHED_SERVERS=$CLO_MEMCACHED_SERVERS
169 if [ "$MEMCACHED_SERVERS" = "" ]; then
170 MEMCACHED_SERVERS=$DEFAULT_MEMCACHED_SERVERS
171 # else: was set by the koha-sites.conf file
175 if [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
176 MEMCACHED_NAMESPACE="$CLO_MEMCACHED_PREFIX$instance"
178 if [ "$MEMCACHED_PREFIX" != "" ]; then
179 MEMCACHED_NAMESPACE="$MEMCACHED_PREFIX$instance"
181 MEMCACHED_NAMESPACE="$DEFAULT_MEMCACHED_PREFIX$instance"
187 # Set defaults and read config file, if it exists.
196 ZEBRA_MARC_FORMAT="marc21"
199 PASSWDFILE="/etc/koha/passwd"
201 # memcached variables
205 # hardcoded memcached defaults
206 DEFAULT_MEMCACHED_SERVERS="127.0.0.1:11211"
207 DEFAULT_MEMCACHED_PREFIX="koha_"
209 # Indexing mode variables (default is DOM)
210 BIBLIOS_INDEXING_MODE="dom"
211 AUTHORITIES_INDEXING_MODE="dom"
213 START_BIBLIOS_RETRIEVAL_INFO=""
214 END_BIBLIOS_RETRIEVAL_INFO=""
215 START_AUTHORITIES_RETRIEVAL_INFO=""
216 END_AUTHORITIES_RETRIEVAL_INFO=""
218 if [ -e /etc/koha/koha-sites.conf ]
220 . /etc/koha/koha-sites.conf
223 [ $# -ge 2 ] && [ $# -le 16 ] || die $usage
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:, \
228 # Note the quotes around `$TEMP': they are essential!
231 # Temporary variables for the command line options
232 CLO_ZEBRA_MARC_FORMAT=""
233 CLO_ZEBRA_LANGUAGE=""
236 CLO_BIBLIOS_INDEXING_MODE=""
237 CLO_AUTHORITIES_INDEXING_MODE=""
238 CLO_MEMCACHED_SERVERS=""
239 CLO_MEMCACHED_PREFIX=""
247 op=request ; shift ;;
249 op=populate ; shift ;;
253 USE_MEMCACHED="yes" ; shift ;;
255 CLO_MEMCACHED_SERVERS="$2" ; shift 2 ;;
257 CLO_MEMCACHED_PREFIX="$2" ; shift 2;;
259 CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
261 CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
263 CLO_AUTHORITIES_INDEXING_MODE="$2" ; shift 2 ;;
265 CLO_BIBLIOS_INDEXING_MODE="$2" ; shift 2 ;;
267 CLO_DEFAULTSQL="$2" ; shift 2 ;;
269 configfile="$2" ; shift 2 ;;
271 CLO_PASSWDFILE="$2" ; shift 2 ;;
273 CLO_DATABASE="$2" ; shift 2 ;;
275 CLO_ADMINUSER="$2" ; shift 2 ;;
279 die "Internal error processing command line arguments" ;;
283 # Load the configfile given on the command line
284 if [ "$configfile" != "" ]
286 if [ -e "$configfile" ]
290 die "$configfile does not exist.";
294 # Make sure options from the command line get the highest precedence
295 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
297 ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
299 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
301 ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
303 if [ "$CLO_DEFAULTSQL" != "" ]
305 DEFAULTSQL="$CLO_DEFAULTSQL"
307 if [ "$CLO_ADMINUSER" != "" ]
309 ADMINUSER="$CLO_ADMINUSER"
311 if [ "$CLO_PASSWDFILE" != "" ]
313 PASSWDFILE="$CLO_PASSWDFILE"
316 if [ "$CLO_BIBLIOS_INDEXING_MODE" != "" ]; then
317 BIBLIOS_INDEXING_MODE=$CLO_BIBLIOS_INDEXING_MODE
320 set_biblios_indexing_mode $BIBLIOS_INDEXING_MODE $ZEBRA_MARC_FORMAT
323 if [ "$CLO_AUTHORITIES_INDEXING_MODE" != "" ]; then
324 AUTHORITIES_INDEXING_MODE=$CLO_AUTHORITIES_INDEXING_MODE
327 set_authorities_indexing_mode $AUTHORITIES_INDEXING_MODE $ZEBRA_MARC_FORMAT
331 if [ "$USE_MEMCACHED" = "yes" ]; then
333 elif [ "$CLO_MEMCACHED_SERVERS" != "" ] || \
334 [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
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.
347 # Unset memcached-related variables
352 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
353 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
356 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
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`
364 # The order of precedence for MySQL database name is:
365 # default < passwd file < command line
366 if [ "$mysqldb" = "" ]
370 if [ "$CLO_DATABASE" != "" ]
372 mysqldb="$CLO_DATABASE"
375 if [ "$mysqluser" = "" ]
377 mysqluser="koha_$name"
379 mysqlhost="$(getmysqlhost)"
381 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
383 if [ "$mysqlpwd" = "" ]
385 mysqlpwd="$(pwgen -s 16 1)"
388 mysqlpwd="$(getinstancemysqlpassword $name)"
392 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
394 # Create new user and group.
395 username="$name-koha"
396 if getent passwd "$username" > /dev/null
398 die "User $username already exists."
400 if getent group "$username" > /dev/null
402 die "Group $username already exists."
404 adduser --no-create-home --disabled-login \
405 --gecos "Koha instance $username" \
406 --home "/var/lib/koha/$name" \
409 # Create the site-specific directories.
410 koha-create-dirs "$name"
412 # Generate Zebra database password.
413 zebrapwd="$(pwgen -s 16 1)"
414 # Future enhancement: make this configurable for when your db is on
416 mysql_hostname="localhost"
417 # Set up MySQL database for this instance.
418 if [ "$op" = create ]
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\`;
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\`;
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"
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"
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"
463 # Create a GPG-encrypted file for requesting a DB to be set up.
464 if [ "$op" = request ]
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:
471 database name: $mysqldb
472 database user: $mysqluser
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"
486 if [ "$op" = create ] || [ "$op" = populate ]
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
491 mysqluser=$(getinstancemysqluser $name)
492 mysqldb=$(getinstancemysqldatabase $name)
493 # Use the default database content if that exists.
494 if [ -e "$DEFAULTSQL" ]
496 # Populate the database with default content.
498 sed "s/__KOHASITE__/$name/g" |
499 mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
502 # Change the default user's password.
503 staffpass="$(pwgen 12 1)"
504 staffdigest=$(echo -n "$staffpass" |
506 use Digest::MD5 qw(md5_base64);
507 while (<>) { print md5_base64($_), "\n"; }')
508 mysql --host="$mysqlhost" --user="$mysqluser" \
509 --password="$mysqlpwd" <<eof
512 SET password = '$staffdigest'
513 WHERE borrowernumber = $ADMINUSER;
516 echo "staff user password is '$staffpass' but keep that secret"
518 # Upgrade the database schema, just in case the dump was from an
520 koha-upgrade-schema "$name"
522 echo "Koha instance is empty, no staff user created."
527 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
529 # Reconfigure Apache.
531 service apache2 restart
534 koha-start-zebra "$name"
538 if [ "$op" = request ]
545 Email for this instance is disabled. When you're ready to enable it, use:
546 koha-email-enable $name