Bug 10733: Memcached on package installs
[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 fi
347
348 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
349 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
350
351
352 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
353 then
354     passwdline=`cat $PASSWDFILE | grep "^$name:"`
355     mysqluser=`echo $passwdline | cut -d ":" -f 2`
356     mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
357     mysqldb=`echo $passwdline | cut -d ":" -f 4`
358 fi
359
360 # The order of precedence for MySQL database name is:
361 # default < passwd file < command line
362 if [ "$mysqldb" = "" ]
363 then
364     mysqldb="koha_$name"
365 fi
366 if [ "$CLO_DATABASE" != "" ]
367 then
368     mysqldb="$CLO_DATABASE"
369 fi
370
371 if [ "$mysqluser" = "" ]
372 then
373     mysqluser="koha_$name"
374 fi
375 mysqlhost="$(getmysqlhost)"
376
377 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
378 then
379     if [ "$mysqlpwd" = "" ]
380     then
381         mysqlpwd="$(pwgen -s 16 1)"
382     fi
383 else
384     mysqlpwd="$(getinstancemysqlpassword $name)"
385 fi
386
387
388 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
389 then
390     # Create new user and group.
391     username="$name-koha"
392     if getent passwd "$username" > /dev/null
393     then
394         die "User $username already exists."
395     fi
396     if getent group "$username" > /dev/null
397     then
398         die "Group $username already exists."
399     fi
400     adduser --no-create-home --disabled-login \
401         --gecos "Koha instance $username" \
402         --home "/var/lib/koha/$name" \
403         --quiet "$username"
404
405     # Create the site-specific directories.
406     koha-create-dirs "$name"
407
408     # Generate Zebra database password.
409     zebrapwd="$(pwgen -s 16 1)"
410     # Future enhancement: make this configurable for when your db is on
411     # another server.
412     mysql_hostname="localhost"
413     # Set up MySQL database for this instance.
414     if [ "$op" = create ]
415     then
416         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
417 CREATE DATABASE \`$mysqldb\`;
418 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
419 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
420 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
421 FLUSH PRIVILEGES;
422 eof
423     fi #`
424
425     if [ "$op" = use ]
426     then
427         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
428 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
429 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
430 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
431 FLUSH PRIVILEGES;
432 eof
433     fi #`
434
435     # Generate and install Apache site-available file and log dir.
436     generate_config_file apache-site.conf.in \
437         "/etc/apache2/sites-available/$name"
438     mkdir "/var/log/koha/$name"
439     chown "$username:$username" "/var/log/koha/$name"
440
441
442     # Generate and install main Koha config file.
443     generate_config_file koha-conf-site.xml.in \
444         "/etc/koha/sites/$name/koha-conf.xml"
445
446     # Generate and install Zebra config files.
447     generate_config_file zebra-biblios-site.cfg.in \
448         "/etc/koha/sites/$name/zebra-biblios.cfg"
449     generate_config_file zebra-biblios-dom-site.cfg.in \
450         "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
451     generate_config_file zebra-authorities-site.cfg.in \
452         "/etc/koha/sites/$name/zebra-authorities.cfg"
453     generate_config_file zebra-authorities-dom-site.cfg.in \
454         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
455     generate_config_file zebra.passwd.in \
456         "/etc/koha/sites/$name/zebra.passwd"
457
458
459     # Create a GPG-encrypted file for requesting a DB to be set up.
460     if [ "$op" = request ]
461     then
462         touch "$name-db-request.txt"
463         chmod 0600 "$name-db-request.txt"
464         cat > "$name-db-request.txt" << eof
465 Please create a MySQL database and user on $mysqlhost as follows:
466
467 database name: $mysqldb
468 database user: $mysqluser
469      password: $mysqlpwd
470
471 Thank you.
472 eof
473
474         echo "See $name-db-request.txt for database creation request."
475         echo "Please forward it to the right person, and then run"
476         echo "$0 --populate-db $name"
477         echo "Thanks."
478     fi
479 fi
480
481
482 if [ "$op" = create ] || [ "$op" = populate ]
483 then
484     # Re-fetch the passwords from the config we've generated, allows it
485     # to be different from what we set, in case the user had to change
486     # something.
487     mysqluser=$(getinstancemysqluser $name)
488     mysqldb=$(getinstancemysqldatabase $name)
489     # Use the default database content if that exists.
490     if [ -e "$DEFAULTSQL" ]
491     then
492         # Populate the database with default content.
493         zcat "$DEFAULTSQL" |
494         sed "s/__KOHASITE__/$name/g" |
495         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
496
497
498         # Change the default user's password.
499         staffpass="$(pwgen 12 1)"
500         staffdigest=$(echo -n "$staffpass" |
501                       perl -e '
502                             use Digest::MD5 qw(md5_base64); 
503                             while (<>) { print md5_base64($_), "\n"; }')
504         mysql --host="$mysqlhost" --user="$mysqluser" \
505 --password="$mysqlpwd" <<eof
506 USE \`$mysqldb\`;
507 UPDATE borrowers 
508 SET password = '$staffdigest' 
509 WHERE borrowernumber = $ADMINUSER;
510 eof
511         #`
512         echo "staff user password is '$staffpass' but keep that secret"
513
514         # Upgrade the database schema, just in case the dump was from an 
515         # old version.
516         koha-upgrade-schema "$name"
517     else
518         echo "Koha instance is empty, no staff user created."
519     fi
520 fi
521
522
523 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
524 then
525     # Reconfigure Apache.
526     a2ensite "$name"
527     service apache2 restart
528
529     # Start Zebra.
530     koha-start-zebra "$name"
531 fi
532
533
534 if [ "$op" = request ]
535 then
536     koha-disable "$name"
537 fi
538
539 echo <<eoh
540
541 Email for this instance is disabled. When you're ready to enable it, use:
542 koha-email-enable $name
543 eoh