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/>.
27 Creates new Koha instances.
31 $scriptname [DB usage mode] [options] instancename
34 --create-db Create a new database on localhost. (default).
35 --request-db Creates a instancename-db-request.txt file where
36 you adjust your DB settings and re-run with --populate-db.
37 --populate-db Finish the installation you started with --request-db after
38 you adjusted the instancename-db-request.txt file.
39 --use-db Use this option if you already created and populated your DB.
42 --marcflavor flavor Set the MARC flavor. Valid values are marc21 (default),
44 --zebralang lang Choose the primary language for Zebra indexing. Valid
45 values are en (default), es, fr, nb, ru and uk.
46 --auth-idx idx_mode Set the indexing mode for authority records. Valid
47 values are dom (default) and grs1.
48 --biblio-idx idx_mode Set the indexing mode for bibliographic records.
49 Valid values are dom (default) and grs1.
50 --use-memcached Set the instance to make use of memcache.
51 --memcached-servers str Set a comma-separated list of host:port memcached servers.
52 --memcached-prefix str Set the desired prefix for the instance memcached namespace.
53 --defaultsql some.sql Specify a default SQL file to be loaded on the DB.
54 --configfile cfg_file Specify an alternate config file for reading default values.
55 --passwdfile passwd Specify an alternate passwd file.
56 --database dbname Enforce the use of the specified DB name (64 char limit)
57 --adminuser n Explicit the admin user ID in the DB. Relevant in
58 conjunction with --defaultsql and --populate-db.
59 --help,-h Show this help.
61 Note: the instance name cannot be longer that 11 chars.
71 # UPPER CASE VARIABLES - from configfile or default value
72 # lower case variables - generated within this script
73 generate_config_file
() {
75 chown
"root:$username" "$2"
77 sed -e "s/__KOHASITE__/$name/g" \
78 -e "s/__OPACPORT__/$OPACPORT/g" \
79 -e "s/__INTRAPORT__/$INTRAPORT/g" \
80 -e "s/__OPACSERVER__/$opacdomain/g" \
81 -e "s/__INTRASERVER__/$intradomain/g" \
82 -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
83 -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
84 -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
85 -e "s/__BIBLIOS_INDEXING_MODE__/$BIBLIOS_INDEXING_MODE/g" \
86 -e "s/__AUTHORITIES_INDEXING_MODE__/$AUTHORITIES_INDEXING_MODE/g" \
87 -e "s/__ZEBRA_BIBLIOS_CFG__/$ZEBRA_BIBLIOS_CFG/g" \
88 -e "s/__ZEBRA_AUTHORITIES_CFG__/$ZEBRA_AUTHORITIES_CFG/g" \
89 -e "s/__START_BIBLIOS_RETRIEVAL_INFO__/`echo $START_BIBLIOS_RETRIEVAL_INFO`/g" \
90 -e "s/__END_BIBLIOS_RETRIEVAL_INFO__/`echo $END_BIBLIOS_RETRIEVAL_INFO`/g" \
91 -e "s/__START_AUTHORITIES_RETRIEVAL_INFO__/`echo $START_AUTHORITIES_RETRIEVAL_INFO`/g" \
92 -e "s/__END_AUTHORITIES_RETRIEVAL_INFO__/`echo $END_AUTHORITIES_RETRIEVAL_INFO`/g" \
93 -e "s/__DB_NAME__/$mysqldb/g" \
94 -e "s/__DB_HOST__/$mysqlhost/g" \
95 -e "s/__DB_USER__/$mysqluser/g" \
96 -e "s/__DB_PASS__/$mysqlpwd/g" \
97 -e "s/__UNIXUSER__/$username/g" \
98 -e "s/__UNIXGROUP__/$username/g" \
99 -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
100 -e "s/__MEMCACHED_NAMESPACE__/$MEMCACHED_NAMESPACE/g" \
101 -e "s/__MEMCACHED_SERVERS__/$MEMCACHED_SERVERS/g" \
102 "/etc/koha/$1" > "$2"
108 /^\[/ { inclient = 0 }
109 /^\[client\]/ { inclient = 1 }
110 inclient && /^ *host *=/ { print $3 }' \
111 /etc
/mysql
/koha-common.cnf
114 getinstancemysqlpassword
() {
115 xmlstarlet sel
-t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
118 getinstancemysqluser
() {
119 xmlstarlet sel
-t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
122 getinstancemysqldatabase
() {
123 xmlstarlet sel
-t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
126 set_biblios_indexing_mode
()
128 local indexing_mode
=$1
131 case $indexing_mode in
133 START_BIBLIOS_RETRIEVAL_INFO
=`cat <<EOF
134 <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-bib-dom.xml"\n
135 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
140 END_BIBLIOS_RETRIEVAL_INFO
=`cat <<EOF
145 BIBLIOS_INDEXING_MODE
="dom"
146 ZEBRA_BIBLIOS_CFG
="zebra-biblios-dom.cfg"
149 START_BIBLIOS_RETRIEVAL_INFO
=" <retrievalinfo>"
150 END_BIBLIOS_RETRIEVAL_INFO
=" <\/retrievalinfo>"
151 BIBLIOS_INDEXING_MODE
="grs1"
152 ZEBRA_BIBLIOS_CFG
="zebra-biblios.cfg"
155 die
"Error: '$indexing_mode' is not a valid indexing mode for bibliographic records."
161 set_authorities_indexing_mode
()
163 local indexing_mode
=$1
166 case $indexing_mode in
168 START_AUTHORITIES_RETRIEVAL_INFO
=`cat <<EOF
169 <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-auth-dom.xml"\n
170 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
175 END_AUTHORITIES_RETRIEVAL_INFO
=`cat <<EOF
180 AUTHORITIES_INDEXING_MODE
="dom"
181 ZEBRA_AUTHORITIES_CFG
="zebra-authorities-dom.cfg"
184 START_AUTHORITIES_RETRIEVAL_INFO
=" <retrievalinfo>"
185 END_AUTHORITIES_RETRIEVAL_INFO
=" <\/retrievalinfo>"
186 AUTHORITIES_INDEXING_MODE
="grs1"
187 ZEBRA_AUTHORITIES_CFG
="zebra-authorities.cfg"
190 die
"Error: '$indexing_mode' is not a valid indexing mode for authority records."
200 if [ "$CLO_MEMCACHED_SERVERS" != "" ]; then
201 MEMCACHED_SERVERS
=$CLO_MEMCACHED_SERVERS
203 if [ "$MEMCACHED_SERVERS" = "" ]; then
204 MEMCACHED_SERVERS
=$DEFAULT_MEMCACHED_SERVERS
205 # else: was set by the koha-sites.conf file
209 if [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
210 MEMCACHED_NAMESPACE
="$CLO_MEMCACHED_PREFIX$instance"
212 if [ "$MEMCACHED_PREFIX" != "" ]; then
213 MEMCACHED_NAMESPACE
="$MEMCACHED_PREFIX$instance"
215 MEMCACHED_NAMESPACE
="$DEFAULT_MEMCACHED_PREFIX$instance"
221 # Set defaults and read config file, if it exists.
230 ZEBRA_MARC_FORMAT
="marc21"
233 PASSWDFILE
="/etc/koha/passwd"
235 # memcached variables
239 # hardcoded memcached defaults
240 DEFAULT_MEMCACHED_SERVERS
="127.0.0.1:11211"
241 DEFAULT_MEMCACHED_PREFIX
="koha_"
243 # Indexing mode variables (default is DOM)
244 BIBLIOS_INDEXING_MODE
="dom"
245 AUTHORITIES_INDEXING_MODE
="dom"
247 START_BIBLIOS_RETRIEVAL_INFO
=""
248 END_BIBLIOS_RETRIEVAL_INFO
=""
249 START_AUTHORITIES_RETRIEVAL_INFO
=""
250 END_AUTHORITIES_RETRIEVAL_INFO
=""
252 if [ -e /etc
/koha
/koha-sites.conf
]
254 .
/etc
/koha
/koha-sites.conf
257 [ $# -ge 1 ] && [ $# -le 16 ] ||
( usage
; die
"Error: wrong parameters" )
259 TEMP
=`getopt -o chrpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,use-memcached,help,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser:,memcached-servers:,memcached-prefix:, \
262 # Note the quotes around `$TEMP': they are essential!
265 # Temporary variables for the command line options
266 CLO_ZEBRA_MARC_FORMAT
=""
267 CLO_ZEBRA_LANGUAGE
=""
270 CLO_BIBLIOS_INDEXING_MODE
=""
271 CLO_AUTHORITIES_INDEXING_MODE
=""
272 CLO_MEMCACHED_SERVERS
=""
273 CLO_MEMCACHED_PREFIX
=""
281 op
=request
; shift ;;
283 op
=populate
; shift ;;
287 USE_MEMCACHED
="yes" ; shift ;;
289 CLO_MEMCACHED_SERVERS
="$2" ; shift 2 ;;
291 CLO_MEMCACHED_PREFIX
="$2" ; shift 2;;
293 CLO_ZEBRA_MARC_FORMAT
="$2" ; shift 2 ;;
295 CLO_ZEBRA_LANGUAGE
="$2" ; shift 2 ;;
297 CLO_AUTHORITIES_INDEXING_MODE
="$2" ; shift 2 ;;
299 CLO_BIBLIOS_INDEXING_MODE
="$2" ; shift 2 ;;
301 CLO_DEFAULTSQL
="$2" ; shift 2 ;;
303 configfile
="$2" ; shift 2 ;;
305 CLO_PASSWDFILE
="$2" ; shift 2 ;;
307 CLO_DATABASE
="$2" ; shift 2 ;;
309 CLO_ADMINUSER
="$2" ; shift 2 ;;
315 die
"Internal error processing command line arguments" ;;
319 # Load the configfile given on the command line
320 if [ "$configfile" != "" ]
322 if [ -e "$configfile" ]
326 die
"$configfile does not exist.";
330 # Make sure options from the command line get the highest precedence
331 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
333 ZEBRA_MARC_FORMAT
="$CLO_ZEBRA_MARC_FORMAT"
335 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
337 ZEBRA_LANGUAGE
="$CLO_ZEBRA_LANGUAGE"
339 if [ "$CLO_DEFAULTSQL" != "" ]
341 DEFAULTSQL
="$CLO_DEFAULTSQL"
343 if [ "$CLO_ADMINUSER" != "" ]
345 ADMINUSER
="$CLO_ADMINUSER"
347 if [ "$CLO_PASSWDFILE" != "" ]
349 PASSWDFILE
="$CLO_PASSWDFILE"
352 if [ "$CLO_BIBLIOS_INDEXING_MODE" != "" ]; then
353 BIBLIOS_INDEXING_MODE
=$CLO_BIBLIOS_INDEXING_MODE
356 set_biblios_indexing_mode
$BIBLIOS_INDEXING_MODE $ZEBRA_MARC_FORMAT
359 if [ "$CLO_AUTHORITIES_INDEXING_MODE" != "" ]; then
360 AUTHORITIES_INDEXING_MODE
=$CLO_AUTHORITIES_INDEXING_MODE
363 set_authorities_indexing_mode
$AUTHORITIES_INDEXING_MODE $ZEBRA_MARC_FORMAT
367 if [ "$USE_MEMCACHED" = "yes" ]; then
369 elif [ "$CLO_MEMCACHED_SERVERS" != "" ] || \
370 [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
374 Error: you provided memcached configuration switches but memcached is not enabled.
375 Please set USE_MEMCACHED="yes" on /etc/koha/koha-sites.conf or use the
376 --use-memcached optio switch to enable it.
382 # Unset memcached-related variables
387 # Are we root? If not, the mod_rewrite check will fail and be confusing, so
388 # we look into this first.
391 die
"This script must be run with root privileges."
394 # Check that mod_rewrite is installed so we can bail out if it's not.
395 if ! /usr
/sbin
/apachectl
-M 2> /dev
/null |
grep -q 'rewrite_module'
399 Koha requires mod_rewrite to be enabled within Apache in order to run.
400 Typically this can be enabled with:
407 opacdomain
="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
408 intradomain
="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
411 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
413 passwdline
=`cat $PASSWDFILE | grep "^$name:"`
414 mysqluser
=`echo $passwdline | cut -d ":" -f 2`
415 mysqlpwd
=`echo $passwdline | cut -d ":" -f 3`
416 mysqldb
=`echo $passwdline | cut -d ":" -f 4`
419 # The order of precedence for MySQL database name is:
420 # default < passwd file < command line
421 if [ "$mysqldb" = "" ]
425 if [ "$CLO_DATABASE" != "" ]
427 mysqldb
="$CLO_DATABASE"
430 if [ "$mysqluser" = "" ]
432 mysqluser
="koha_$name"
434 mysqlhost
="$(getmysqlhost)"
436 if [ "$op" = create
] ||
[ "$op" = request
] ||
[ "$op" = use
]
438 if [ "$mysqlpwd" = "" ]
440 mysqlpwd
="$(pwgen -s 16 1)"
443 mysqlpwd
="$(getinstancemysqlpassword $name)"
447 if [ "$op" = create
] ||
[ "$op" = request
] ||
[ "$op" = use
]
449 # Create new user and group.
450 username
="$name-koha"
451 if getent passwd
"$username" > /dev
/null
453 die
"User $username already exists."
455 if getent group
"$username" > /dev
/null
457 die
"Group $username already exists."
459 adduser
--no-create-home --disabled-login \
460 --gecos "Koha instance $username" \
461 --home "/var/lib/koha/$name" \
464 # Create the site-specific directories.
465 koha-create-dirs
"$name"
467 # Generate Zebra database password.
468 zebrapwd
="$(pwgen -s 16 1)"
469 # Future enhancement: make this configurable for when your db is on
471 mysql_hostname
="localhost"
472 # Set up MySQL database for this instance.
473 if [ "$op" = create
]
475 mysql
--defaults-extra-file=/etc
/mysql
/koha-common.cnf
<<eof
476 CREATE DATABASE \`$mysqldb\`;
477 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
478 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
479 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
486 mysql
--defaults-extra-file=/etc
/mysql
/koha-common.cnf
--force <<eof
487 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
488 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
489 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
494 # Generate and install Apache site-available file and log dir.
495 generate_config_file apache-site.conf.
in \
496 "/etc/apache2/sites-available/$name"
497 mkdir
"/var/log/koha/$name"
498 chown
"$username:$username" "/var/log/koha/$name"
501 # Generate and install main Koha config file.
502 generate_config_file koha-conf-site.xml.
in \
503 "/etc/koha/sites/$name/koha-conf.xml"
505 # Generate and install Zebra config files.
506 generate_config_file zebra-biblios-site.cfg.
in \
507 "/etc/koha/sites/$name/zebra-biblios.cfg"
508 generate_config_file zebra-biblios-dom-site.cfg.
in \
509 "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
510 generate_config_file zebra-authorities-site.cfg.
in \
511 "/etc/koha/sites/$name/zebra-authorities.cfg"
512 generate_config_file zebra-authorities-dom-site.cfg.
in \
513 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
514 generate_config_file zebra.passwd.
in \
515 "/etc/koha/sites/$name/zebra.passwd"
518 # Create a GPG-encrypted file for requesting a DB to be set up.
519 if [ "$op" = request
]
521 touch "$name-db-request.txt"
522 chmod 0600 "$name-db-request.txt"
523 cat > "$name-db-request.txt" << eof
524 Please create a MySQL database and user on $mysqlhost as follows:
526 database name: $mysqldb
527 database user: $mysqluser
533 echo "See $name-db-request.txt for database creation request."
534 echo "Please forward it to the right person, and then run"
535 echo "$0 --populate-db $name"
541 if [ "$op" = create
] ||
[ "$op" = populate
]
543 # Re-fetch the passwords from the config we've generated, allows it
544 # to be different from what we set, in case the user had to change
546 mysqluser
=$
(getinstancemysqluser
$name)
547 mysqldb
=$
(getinstancemysqldatabase
$name)
548 # Use the default database content if that exists.
549 if [ -e "$DEFAULTSQL" ]
551 # Populate the database with default content.
553 sed "s/__KOHASITE__/$name/g" |
554 mysql
--host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
557 # Change the default user's password.
558 staffpass
="$(pwgen 12 1)"
559 staffdigest
=$
(echo -n "$staffpass" |
561 use Digest::MD5 qw(md5_base64);
562 while (<>) { print md5_base64($_), "\n"; }')
563 mysql
--host="$mysqlhost" --user="$mysqluser" \
564 --password="$mysqlpwd" <<eof
567 SET password = '$staffdigest'
568 WHERE borrowernumber = $ADMINUSER;
571 echo "staff user password is '$staffpass' but keep that secret"
573 # Upgrade the database schema, just in case the dump was from an
575 koha-upgrade-schema
"$name"
577 echo "Koha instance is empty, no staff user created."
582 if [ "$op" = create
] ||
[ "$op" = populate
] ||
[ "$op" = use
]
584 # Reconfigure Apache.
586 service apache2 restart
589 koha-start-zebra
"$name"
593 if [ "$op" = request
]
600 Email for this instance is disabled. When you're ready to enable it, use:
601 koha-email-enable $name