Bug 11509: (trivial rmaint followup) wrong parameter count
[koha.git] / debian / scripts / koha-create
blob826163c573ef78dcf88102c8b1cf63f32aa3624d
1 #!/bin/bash
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.
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/>.
20 set -e
22 usage()
24 local scriptname=$0
25 cat <<EOF
27 Creates new Koha instances.
29 Usage:
31 $scriptname [DB usage mode] [options] instancename
33 DB usage mode:
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.
41 Options:
42 --marcflavor flavor Set the MARC flavor. Valid values are marc21 (default),
43 normarc and unimarc.
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.
63 EOF
66 die() {
67 echo "$@" 1>&2
68 exit 1
71 # UPPER CASE VARIABLES - from configfile or default value
72 # lower case variables - generated within this script
73 generate_config_file() {
74 touch "$2"
75 chown "root:$username" "$2"
76 chmod 0640 "$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"
106 getmysqlhost() {
107 awk '
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
129 local marc_format=$2
131 case $indexing_mode in
132 "dom")
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
136 <xi:fallback>\n
137 <retrievalinfo>
138 EOF`
140 END_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
141 <\/retrievalinfo>\n
142 <\/xi:fallback>\n
143 <\/xi:include>
144 EOF`
145 BIBLIOS_INDEXING_MODE="dom"
146 ZEBRA_BIBLIOS_CFG="zebra-biblios-dom.cfg"
148 "grs1")
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."
157 esac
161 set_authorities_indexing_mode()
163 local indexing_mode=$1
164 local marc_format=$2
166 case $indexing_mode in
167 "dom")
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
171 <xi:fallback>\n
172 <retrievalinfo>
173 EOF`
175 END_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
176 <\/retrievalinfo>\n
177 <\/xi:fallback>\n
178 <\/xi:include>\n
179 EOF`
180 AUTHORITIES_INDEXING_MODE="dom"
181 ZEBRA_AUTHORITIES_CFG="zebra-authorities-dom.cfg"
183 "grs1")
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."
192 esac
196 set_memcached()
198 local instance="$1"
200 if [ "$CLO_MEMCACHED_SERVERS" != "" ]; then
201 MEMCACHED_SERVERS=$CLO_MEMCACHED_SERVERS
202 else
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"
211 else
212 if [ "$MEMCACHED_PREFIX" != "" ]; then
213 MEMCACHED_NAMESPACE="$MEMCACHED_PREFIX$instance"
214 else
215 MEMCACHED_NAMESPACE="$DEFAULT_MEMCACHED_PREFIX$instance"
221 # Set defaults and read config file, if it exists.
222 DOMAIN=""
223 OPACPORT="80"
224 OPACPREFIX=""
225 OPACSUFFIX=""
226 INTRAPORT="8080"
227 INTRAPREFIX=""
228 INTRASUFFIX=""
229 DEFAULTSQL=""
230 ZEBRA_MARC_FORMAT="marc21"
231 ZEBRA_LANGUAGE="en"
232 ADMINUSER="1"
233 PASSWDFILE="/etc/koha/passwd"
235 # memcached variables
236 USE_MEMCACHED="no"
237 MEMCACHED_SERVERS=""
238 MEMCACHED_PREFIX=""
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 ]
253 then
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:, \
260 -n "$0" -- "$@"`
262 # Note the quotes around `$TEMP': they are essential!
263 eval set -- "$TEMP"
265 # Temporary variables for the command line options
266 CLO_ZEBRA_MARC_FORMAT=""
267 CLO_ZEBRA_LANGUAGE=""
268 CLO_DEFAULTSQL=""
269 CLO_ADMINUSER=""
270 CLO_BIBLIOS_INDEXING_MODE=""
271 CLO_AUTHORITIES_INDEXING_MODE=""
272 CLO_MEMCACHED_SERVERS=""
273 CLO_MEMCACHED_PREFIX=""
276 while true ; do
277 case "$1" in
278 -c|--create-db)
279 op=create ; shift ;;
280 -r|--request-db)
281 op=request ; shift ;;
282 -p|--populate-db)
283 op=populate ; shift ;;
284 -u|--use-db)
285 op=use ; shift ;;
286 --use-memcached)
287 USE_MEMCACHED="yes" ; shift ;;
288 --memcached-servers)
289 CLO_MEMCACHED_SERVERS="$2" ; shift 2 ;;
290 --memcached-prefix)
291 CLO_MEMCACHED_PREFIX="$2" ; shift 2;;
292 -m|--marcflavor)
293 CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
294 -l|--zebralang)
295 CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
296 --auth-idx)
297 CLO_AUTHORITIES_INDEXING_MODE="$2" ; shift 2 ;;
298 --biblio-idx)
299 CLO_BIBLIOS_INDEXING_MODE="$2" ; shift 2 ;;
300 -d|--defaultsql)
301 CLO_DEFAULTSQL="$2" ; shift 2 ;;
302 -f|--configfile)
303 configfile="$2" ; shift 2 ;;
304 -s|--passwdfile)
305 CLO_PASSWDFILE="$2" ; shift 2 ;;
306 -b|--database)
307 CLO_DATABASE="$2" ; shift 2 ;;
308 -a|--adminuser)
309 CLO_ADMINUSER="$2" ; shift 2 ;;
310 -h|--help)
311 usage ; exit 0 ;;
313 shift ; break ;;
315 die "Internal error processing command line arguments" ;;
316 esac
317 done
319 # Load the configfile given on the command line
320 if [ "$configfile" != "" ]
321 then
322 if [ -e "$configfile" ]
323 then
324 . "$configfile"
325 else
326 die "$configfile does not exist.";
330 # Make sure options from the command line get the highest precedence
331 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
332 then
333 ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
335 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
336 then
337 ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
339 if [ "$CLO_DEFAULTSQL" != "" ]
340 then
341 DEFAULTSQL="$CLO_DEFAULTSQL"
343 if [ "$CLO_ADMINUSER" != "" ]
344 then
345 ADMINUSER="$CLO_ADMINUSER"
347 if [ "$CLO_PASSWDFILE" != "" ]
348 then
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
365 name="$1"
367 if [ "$USE_MEMCACHED" = "yes" ]; then
368 set_memcached $name
369 elif [ "$CLO_MEMCACHED_SERVERS" != "" ] || \
370 [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
372 MSG=`cat <<EOF
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.
378 EOF`
380 usage ; die $MSG
381 else
382 # Unset memcached-related variables
383 MEMCACHED_SERVERS=""
384 MEMCACHED_PREFIX=""
387 # Are we root? If not, the mod_rewrite check will fail and be confusing, so
388 # we look into this first.
389 if [[ $UID -ne 0 ]]
390 then
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'
396 then
397 cat 1>&2 <<EOM
399 Koha requires mod_rewrite to be enabled within Apache in order to run.
400 Typically this can be enabled with:
402 sudo a2enmod rewrite
407 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
408 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
411 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
412 then
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" = "" ]
422 then
423 mysqldb="koha_$name"
425 if [ "$CLO_DATABASE" != "" ]
426 then
427 mysqldb="$CLO_DATABASE"
430 if [ "$mysqluser" = "" ]
431 then
432 mysqluser="koha_$name"
434 mysqlhost="$(getmysqlhost)"
436 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
437 then
438 if [ "$mysqlpwd" = "" ]
439 then
440 mysqlpwd="$(pwgen -s 16 1)"
442 else
443 mysqlpwd="$(getinstancemysqlpassword $name)"
447 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
448 then
449 # Create new user and group.
450 username="$name-koha"
451 if getent passwd "$username" > /dev/null
452 then
453 die "User $username already exists."
455 if getent group "$username" > /dev/null
456 then
457 die "Group $username already exists."
459 adduser --no-create-home --disabled-login \
460 --gecos "Koha instance $username" \
461 --home "/var/lib/koha/$name" \
462 --quiet "$username"
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
470 # another server.
471 mysql_hostname="localhost"
472 # Set up MySQL database for this instance.
473 if [ "$op" = create ]
474 then
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\`;
480 FLUSH PRIVILEGES;
482 fi #`
484 if [ "$op" = use ]
485 then
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\`;
490 FLUSH PRIVILEGES;
492 fi #`
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 ]
520 then
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
528 password: $mysqlpwd
530 Thank you.
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"
536 echo "Thanks."
541 if [ "$op" = create ] || [ "$op" = populate ]
542 then
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
545 # something.
546 mysqluser=$(getinstancemysqluser $name)
547 mysqldb=$(getinstancemysqldatabase $name)
548 # Use the default database content if that exists.
549 if [ -e "$DEFAULTSQL" ]
550 then
551 # Populate the database with default content.
552 zcat "$DEFAULTSQL" |
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" |
560 perl -e '
561 use Digest::MD5 qw(md5_base64);
562 while (<>) { print md5_base64($_), "\n"; }')
563 mysql --host="$mysqlhost" --user="$mysqluser" \
564 --password="$mysqlpwd" <<eof
565 USE \`$mysqldb\`;
566 UPDATE borrowers
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
574 # old version.
575 koha-upgrade-schema "$name"
576 else
577 echo "Koha instance is empty, no staff user created."
582 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
583 then
584 # Reconfigure Apache.
585 a2ensite "$name"
586 service apache2 restart
588 # Start Zebra.
589 koha-start-zebra "$name"
593 if [ "$op" = request ]
594 then
595 koha-disable "$name"
598 echo <<eoh
600 Email for this instance is disabled. When you're ready to enable it, use:
601 koha-email-enable $name