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] \
26 [--defaultsql /path/to/some.sql] \
27 [--configfile /path/to/config] [--passwdfile /path/to/passwd] \
28 [--database database] [--adminuser n] instancename"
35 # UPPER CASE VARIABLES - from configfile or default value
36 # lower case variables - generated within this script
37 generate_config_file() {
39 chown "root:$username" "$2"
41 sed -e "s/__KOHASITE__/$name/g" \
42 -e "s/__OPACPORT__/$OPACPORT/g" \
43 -e "s/__INTRAPORT__/$INTRAPORT/g" \
44 -e "s/__OPACSERVER__/$opacdomain/g" \
45 -e "s/__INTRASERVER__/$intradomain/g" \
46 -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
47 -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
48 -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
49 -e "s/__BIBLIOS_INDEXING_MODE__/$BIBLIOS_INDEXING_MODE/g" \
50 -e "s/__AUTHORITIES_INDEXING_MODE__/$AUTHORITIES_INDEXING_MODE/g" \
51 -e "s/__ZEBRA_BIBLIOS_CFG__/$ZEBRA_BIBLIOS_CFG/g" \
52 -e "s/__ZEBRA_AUTHORITIES_CFG__/$ZEBRA_AUTHORITIES_CFG/g" \
53 -e "s/__START_BIBLIOS_RETRIEVAL_INFO__/`echo $START_BIBLIOS_RETRIEVAL_INFO`/g" \
54 -e "s/__END_BIBLIOS_RETRIEVAL_INFO__/`echo $END_BIBLIOS_RETRIEVAL_INFO`/g" \
55 -e "s/__START_AUTHORITIES_RETRIEVAL_INFO__/`echo $START_AUTHORITIES_RETRIEVAL_INFO`/g" \
56 -e "s/__END_AUTHORITIES_RETRIEVAL_INFO__/`echo $END_AUTHORITIES_RETRIEVAL_INFO`/g" \
57 -e "s/__DB_NAME__/$mysqldb/g" \
58 -e "s/__DB_HOST__/$mysqlhost/g" \
59 -e "s/__DB_USER__/$mysqluser/g" \
60 -e "s/__DB_PASS__/$mysqlpwd/g" \
61 -e "s/__UNIXUSER__/$username/g" \
62 -e "s/__UNIXGROUP__/$username/g" \
63 -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
70 /^\[/ { inclient = 0 }
71 /^\[client\]/ { inclient = 1 }
72 inclient && /^ *host *=/ { print $3 }' \
73 /etc/mysql/koha-common.cnf
76 getinstancemysqlpassword() {
77 xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
80 getinstancemysqluser() {
81 xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
84 getinstancemysqldatabase() {
85 xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
88 set_biblios_indexing_mode()
90 local indexing_mode=$1
93 case $indexing_mode in
95 START_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
96 <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-bib-dom.xml"\n
97 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
102 END_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
107 BIBLIOS_INDEXING_MODE="dom"
108 ZEBRA_BIBLIOS_CFG="zebra-biblios-dom.cfg"
111 START_BIBLIOS_RETRIEVAL_INFO=" <retrievalinfo>"
112 END_BIBLIOS_RETRIEVAL_INFO=" <\/retrievalinfo>"
113 BIBLIOS_INDEXING_MODE="grs1"
114 ZEBRA_BIBLIOS_CFG="zebra-biblios.cfg"
117 die "Error: '$indexing_mode' is not a valid indexing mode for bibliographic records."
123 set_authorities_indexing_mode()
125 local indexing_mode=$1
128 case $indexing_mode in
130 START_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
131 <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-auth-dom.xml"\n
132 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
137 END_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
142 AUTHORITIES_INDEXING_MODE="dom"
143 ZEBRA_AUTHORITIES_CFG="zebra-authorities-dom.cfg"
146 START_AUTHORITIES_RETRIEVAL_INFO=" <retrievalinfo>"
147 END_AUTHORITIES_RETRIEVAL_INFO=" <\/retrievalinfo>"
148 AUTHORITIES_INDEXING_MODE="grs1"
149 ZEBRA_AUTHORITIES_CFG="zebra-authorities.cfg"
152 die "Error: '$indexing_mode' is not a valid indexing mode for authority records."
157 # Set defaults and read config file, if it exists.
166 ZEBRA_MARC_FORMAT="marc21"
169 PASSWDFILE="/etc/koha/passwd"
171 # Indexing mode variables (default is DOM)
172 BIBLIOS_INDEXING_MODE="dom"
173 AUTHORITIES_INDEXING_MODE="dom"
175 START_BIBLIOS_RETRIEVAL_INFO=""
176 END_BIBLIOS_RETRIEVAL_INFO=""
177 START_AUTHORITIES_RETRIEVAL_INFO=""
178 END_AUTHORITIES_RETRIEVAL_INFO=""
180 if [ -e /etc/koha/koha-sites.conf ]
182 . /etc/koha/koha-sites.conf
185 [ $# -ge 2 ] && [ $# -le 16 ] || die $usage
187 TEMP=`getopt -o crpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser: \
190 # Note the quotes around `$TEMP': they are essential!
193 # Temporary variables for the command line options
194 CLO_ZEBRA_MARC_FORMAT=""
195 CLO_ZEBRA_LANGUAGE=""
198 CLO_BIBLIOS_INDEXING_MODE=""
199 CLO_AUTHORITIES_INDEXING_MODE=""
204 -c|--create-db) op=create ; shift ;;
205 -r|--request-db) op=request ; shift ;;
206 -p|--populate-db) op=populate ; shift ;;
207 -u|--use-db) op=use ; shift ;;
208 -m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
209 -l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
210 --auth-idx) CLO_AUTHORITIES_INDEXING_MODE="$2" ; shift 2 ;;
211 --biblio-idx) CLO_BIBLIOS_INDEXING_MODE="$2" ; shift 2 ;;
212 -d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;;
213 -f|--configfile) configfile="$2" ; shift 2 ;;
214 -s|--passwdfile) CLO_PASSWDFILE="$2" ; shift 2 ;;
215 -b|--database) CLO_DATABASE="$2" ; shift 2 ;;
216 -a|--adminuser) CLO_ADMINUSER="$2" ; shift 2 ;;
218 *) die "Internal error processing command line arguments" ;;
222 # Load the configfile given on the command line
223 if [ "$configfile" != "" ]
225 if [ -e "$configfile" ]
229 die "$configfile does not exist.";
233 # Make sure options from the command line get the highest precedence
234 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
236 ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
238 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
240 ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
242 if [ "$CLO_DEFAULTSQL" != "" ]
244 DEFAULTSQL="$CLO_DEFAULTSQL"
246 if [ "$CLO_ADMINUSER" != "" ]
248 ADMINUSER="$CLO_ADMINUSER"
250 if [ "$CLO_PASSWDFILE" != "" ]
252 PASSWDFILE="$CLO_PASSWDFILE"
255 if [ "$CLO_BIBLIOS_INDEXING_MODE" != "" ]; then
256 BIBLIOS_INDEXING_MODE=$CLO_BIBLIOS_INDEXING_MODE
259 set_biblios_indexing_mode $BIBLIOS_INDEXING_MODE $ZEBRA_MARC_FORMAT
262 if [ "$CLO_AUTHORITIES_INDEXING_MODE" != "" ]; then
263 AUTHORITIES_INDEXING_MODE=$CLO_AUTHORITIES_INDEXING_MODE
266 set_authorities_indexing_mode $AUTHORITIES_INDEXING_MODE $ZEBRA_MARC_FORMAT
270 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
271 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
274 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
276 passwdline=`cat $PASSWDFILE | grep "^$name:"`
277 mysqluser=`echo $passwdline | cut -d ":" -f 2`
278 mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
279 mysqldb=`echo $passwdline | cut -d ":" -f 4`
282 # The order of precedence for MySQL database name is:
283 # default < passwd file < command line
284 if [ "$mysqldb" = "" ]
288 if [ "$CLO_DATABASE" != "" ]
290 mysqldb="$CLO_DATABASE"
293 if [ "$mysqluser" = "" ]
295 mysqluser="koha_$name"
297 mysqlhost="$(getmysqlhost)"
299 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
301 if [ "$mysqlpwd" = "" ]
303 mysqlpwd="$(pwgen -s 16 1)"
306 mysqlpwd="$(getinstancemysqlpassword $name)"
310 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
312 # Create new user and group.
313 username="$name-koha"
314 if getent passwd "$username" > /dev/null
316 die "User $username already exists."
318 if getent group "$username" > /dev/null
320 die "Group $username already exists."
322 adduser --no-create-home --disabled-login \
323 --gecos "Koha instance $username" \
324 --home "/var/lib/koha/$name" \
327 # Create the site-specific directories.
328 koha-create-dirs "$name"
330 # Generate Zebra database password.
331 zebrapwd="$(pwgen -s 16 1)"
332 # Future enhancement: make this configurable for when your db is on
334 mysql_hostname="localhost"
335 # Set up MySQL database for this instance.
336 if [ "$op" = create ]
338 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
339 CREATE DATABASE \`$mysqldb\`;
340 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
341 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
342 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
349 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
350 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
351 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
352 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
357 # Generate and install Apache site-available file and log dir.
358 generate_config_file apache-site.conf.in \
359 "/etc/apache2/sites-available/$name"
360 mkdir "/var/log/koha/$name"
361 chown "$username:$username" "/var/log/koha/$name"
364 # Generate and install main Koha config file.
365 generate_config_file koha-conf-site.xml.in \
366 "/etc/koha/sites/$name/koha-conf.xml"
368 # Generate and install Zebra config files.
369 generate_config_file zebra-biblios-site.cfg.in \
370 "/etc/koha/sites/$name/zebra-biblios.cfg"
371 generate_config_file zebra-biblios-dom-site.cfg.in \
372 "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
373 generate_config_file zebra-authorities-site.cfg.in \
374 "/etc/koha/sites/$name/zebra-authorities.cfg"
375 generate_config_file zebra-authorities-dom-site.cfg.in \
376 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
377 generate_config_file zebra.passwd.in \
378 "/etc/koha/sites/$name/zebra.passwd"
381 # Create a GPG-encrypted file for requesting a DB to be set up.
382 if [ "$op" = request ]
384 touch "$name-db-request.txt"
385 chmod 0600 "$name-db-request.txt"
386 cat > "$name-db-request.txt" << eof
387 Please create a MySQL database and user on $mysqlhost as follows:
389 database name: $mysqldb
390 database user: $mysqluser
396 echo "See $name-db-request.txt for database creation request."
397 echo "Please forward it to the right person, and then run"
398 echo "$0 --populate-db $name"
404 if [ "$op" = create ] || [ "$op" = populate ]
406 # Re-fetch the passwords from the config we've generated, allows it
407 # to be different from what we set, in case the user had to change
409 mysqluser=$(getinstancemysqluser $name)
410 mysqldb=$(getinstancemysqldatabase $name)
411 # Use the default database content if that exists.
412 if [ -e "$DEFAULTSQL" ]
414 # Populate the database with default content.
416 sed "s/__KOHASITE__/$name/g" |
417 mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
420 # Change the default user's password.
421 staffpass="$(pwgen 12 1)"
422 staffdigest=$(echo -n "$staffpass" |
424 use Digest::MD5 qw(md5_base64);
425 while (<>) { print md5_base64($_), "\n"; }')
426 mysql --host="$mysqlhost" --user="$mysqluser" \
427 --password="$mysqlpwd" <<eof
430 SET password = '$staffdigest'
431 WHERE borrowernumber = $ADMINUSER;
434 echo "staff user password is '$staffpass' but keep that secret"
436 # Upgrade the database schema, just in case the dump was from an
438 koha-upgrade-schema "$name"
440 echo "Koha instance is empty, no staff user created."
445 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
447 # Reconfigure Apache.
449 service apache2 restart
452 koha-start-zebra "$name"
456 if [ "$op" = request ]
463 Email for this instance is disabled. When you're ready to enable it, use:
464 koha-email-enable $name