Bug 14302: Remove GRS1 specific code
[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 # Read configuration variable file if it is present
20 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
21
22 set -e
23
24 # include helper functions
25 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
26     . "/usr/share/koha/bin/koha-functions.sh"
27 else
28     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
29     exit 1
30 fi
31
32 usage()
33 {
34     local scriptname=$0
35     cat <<EOF
36
37 Creates new Koha instances.
38
39 Usage:
40
41   $scriptname [DB usage mode] [options] instancename
42
43 DB usage mode:
44   --create-db               Create a new database on localhost. (default).
45   --request-db              Creates a instancename-db-request.txt file where
46                             you adjust your DB settings and re-run with --populate-db.
47   --populate-db             Finish the installation you started with --request-db after
48                             you adjusted the instancename-db-request.txt file.
49   --use-db                  Use this option if you already created and populated your DB.
50
51 Options:
52   --marcflavor flavor       Set the MARC flavor. Valid values are marc21 (default),
53                             normarc and unimarc.
54   --zebralang lang          Choose the primary language for Zebra indexing. Valid
55                             values are cs, en (default), es, fr, gr, nb, ru and uk.
56   --auth-idx idx_mode       Set the indexing mode for authority records. Valid
57                             values are dom (default) and grs1.
58   --biblio-idx idx_mode     Set the indexing mode for bibliographic records.
59                             Valid values are dom (default) and grs1.
60   --memcached-servers str   Set a comma-separated list of host:port memcached servers.
61   --memcached-prefix str    Set the desired prefix for the instance memcached namespace.
62   --enable-sru              Enable the Z39.50/SRU server (default: disabled).
63   --sru-port                Specifiy a TCP port number for the Z39.50/SRU server
64                             to listen on. (default: 7090).
65   --defaultsql some.sql     Specify a default SQL file to be loaded on the DB.
66   --configfile cfg_file     Specify an alternate config file for reading default values.
67   --passwdfile passwd       Specify an alternate passwd file.
68   --dbhost host             Enforce the use of the specified DB server
69   --database dbname         Enforce the use of the specified DB name (64 char limit)
70   --adminuser n             Explicit the admin user ID in the DB. Relevant in
71                             conjunction with --defaultsql and --populate-db.
72   --template-cache-dir      Set a user defined template_cache_dir. It defaults to
73                             /var/cache/koha/<instance>/templates
74   --timezone time/zone      Specify a timezone. e.g. America/Argentina
75   --upload-path dir         Set a user defined upload_path. It defaults to
76                             /var/lib/koha/<instance>/uploads
77   --tmp-path dir            Set a user defined tmp_path. It defaults to
78                             /var/lib/koha/<instance>/tmp
79   --letsencrypt             Set up a https-only site with letsencrypt certificates
80   --help,-h                 Show this help.
81
82 Note: the instance name cannot be longer that 11 chars.
83
84 EOF
85 }
86
87 # UPPER CASE VARIABLES - from configfile or default value
88 # lower case variables - generated within this script
89 generate_config_file() {
90     touch "$2"
91     chown "root:$username" "$2"
92     chmod 0640 "$2"
93     sed -e "s/__KOHA_CONF_DIR__/\/etc\/koha\/sites\/$name/g" \
94         -e "s/__KOHASITE__/$name/g" \
95         -e "s/__OPACPORT__/$OPACPORT/g" \
96         -e "s/__INTRAPORT__/$INTRAPORT/g" \
97         -e "s/__OPACSERVER__/$opacdomain/g" \
98         -e "s/__INTRASERVER__/$intradomain/g" \
99         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
100         -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
101         -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
102         -e "s/__SRU_BIBLIOS_PORT__/$SRU_SERVER_PORT/g" \
103         -e "s/__START_SRU_PUBLICSERVER__/$START_SRU_PUBLICSERVER/g" \
104         -e "s/__END_SRU_PUBLICSERVER__/$END_SRU_PUBLICSERVER/g" \
105         -e "s/__ZEBRA_BIBLIOS_CFG__/$ZEBRA_BIBLIOS_CFG/g" \
106         -e "s/__ZEBRA_AUTHORITIES_CFG__/$ZEBRA_AUTHORITIES_CFG/g" \
107         -e "s/__START_BIBLIOS_RETRIEVAL_INFO__/`echo $START_BIBLIOS_RETRIEVAL_INFO`/g" \
108         -e "s/__END_BIBLIOS_RETRIEVAL_INFO__/`echo $END_BIBLIOS_RETRIEVAL_INFO`/g" \
109         -e "s/__START_AUTHORITIES_RETRIEVAL_INFO__/`echo $START_AUTHORITIES_RETRIEVAL_INFO`/g" \
110         -e "s/__END_AUTHORITIES_RETRIEVAL_INFO__/`echo $END_AUTHORITIES_RETRIEVAL_INFO`/g" \
111         -e "s/__API_SECRET__/$API_SECRET/g" \
112         -e "s/__DB_NAME__/$mysqldb/g" \
113         -e "s/__DB_HOST__/$mysqlhost/g" \
114         -e "s/__DB_USER__/$mysqluser/g" \
115         -e "s/__DB_PASS__/$mysqlpwd/g" \
116         -e "s/__UNIXUSER__/$username/g" \
117         -e "s/__UNIXGROUP__/$username/g" \
118         -e "s#__TEMPLATE_CACHE_DIR__#$TEMPLATE_CACHE_DIR#g" \
119         -e "s#__TIMEZONE__#$TIMEZONE#g" \
120         -e "s#__UPLOAD_PATH__#$UPLOAD_PATH#g" \
121         -e "s#__TMP_PATH__#$TMP_PATH#g" \
122         -e "s/__LOG_DIR__/\/var\/log\/koha\/$name/g" \
123         -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
124         -e "s/__MEMCACHED_NAMESPACE__/$MEMCACHED_NAMESPACE/g" \
125         -e "s/__MEMCACHED_SERVERS__/$MEMCACHED_SERVERS/g" \
126         "/etc/koha/$1" > "$2"
127
128 }
129
130 getmysqlhost() {
131     if [ ! -f /etc/mysql/debian.cnf ]
132     then
133         echo localhost
134         return
135     fi
136     awk '
137         BEGIN { FS="=" }
138         $1 ~/\[/ { inclient=0 }
139         $1 ~/\[client\]/ { inclient=1; next }
140         inclient==1 && $1 ~/host/ { gsub(/ /, "", $2); print $2 }' \
141         /etc/mysql/koha-common.cnf
142 }
143
144 getinstancemysqlpassword() {
145     xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
146 }
147
148 getinstancemysqluser() {
149     xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
150 }
151
152 getinstancemysqldatabase() {
153     xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
154 }
155
156 check_apache_config()
157 {
158
159     # Check that mpm_itk is installed and enabled
160     if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_itk'; then
161         # Check Apache version
162         APACHE_DISABLE_MPM_MSG=""
163         if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
164             # mpm_event or mpm_worker need to be disabled first. mpm_itk depends
165             # on mpm_prefork, which is enabled if needed. See
166             # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=734865
167             if /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_event'; then
168                 APACHE_DISABLE_MPM_MSG=" sudo a2dismod mpm_event ;"
169             elif /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_worker'; then
170                 APACHE_DISABLE_MPM_MSG=" sudo a2dismod mpm_worker ;"
171             # else mpm_prefork: a2enmod mpm_itk works
172             fi
173         # else Apache 2.2: a2enmod mpm_itk works
174         fi
175
176         cat 1>&2  <<EOM
177
178 Koha requires mpm_itk to be enabled within Apache in order to run.
179 Typically this can be enabled with:
180
181    $APACHE_DISABLE_MPM_MSG sudo a2enmod mpm_itk
182 EOM
183
184         die
185     fi
186
187     # Check that mod_rewrite is installed and enabled.
188     if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'rewrite_module'; then
189         cat 1>&2  <<EOM
190
191 Koha requires mod_rewrite to be enabled within Apache in order to run.
192 Typically this can be enabled with:
193
194     sudo a2enmod rewrite
195 EOM
196         die
197     fi
198
199     # Check that the CGI module is installed and enabled
200     # (Apache 2.4 may not have it by default.)
201     if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'cgi_module'; then
202         cat 1>&2 << EOM
203 Koha requires mod_cgi to be enabled within Apache in order to run.
204 Typically this can be enabled with:
205
206     sudo a2enmod cgi
207 EOM
208         die
209     fi
210
211     # Check that mod_ssl is installed and enabled.
212     if [ "$CLO_LETSENCRYPT" = "yes" ]; then
213         if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'ssl_module'; then
214             cat 1>&2  <<EOM
215
216 Koha requires mod_ssl to be enabled within Apache in order to run with --letsencrypt.
217 Typically this can be enabled with:
218
219     sudo a2enmod ssl
220 EOM
221             die
222         fi
223     fi
224
225 }
226
227 set_biblios_indexing_mode()
228 {
229     local marc_format=$1
230
231     START_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
232     <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-bib-dom.xml"\n
233                 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
234      <xi:fallback>\n
235       <retrievalinfo>
236 EOF`
237
238     END_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
239       <\/retrievalinfo>\n
240      <\/xi:fallback>\n
241     <\/xi:include>
242 EOF`
243     ZEBRA_BIBLIOS_CFG="zebra-biblios-dom.cfg"
244 }
245
246
247 set_authorities_indexing_mode()
248 {
249     local marc_format=$1
250
251     START_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
252     <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-auth-dom.xml"\n
253                 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
254      <xi:fallback>\n
255       <retrievalinfo>
256 EOF`
257
258     END_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
259       <\/retrievalinfo>\n
260      <\/xi:fallback>\n
261     <\/xi:include>\n
262 EOF`
263     ZEBRA_AUTHORITIES_CFG="zebra-authorities-dom.cfg"
264 }
265
266
267 set_memcached()
268 {
269     local instance="$1"
270
271     if [ "$CLO_MEMCACHED_SERVERS" != "" ]; then
272         MEMCACHED_SERVERS=$CLO_MEMCACHED_SERVERS
273     else
274         if [ "$MEMCACHED_SERVERS" = "" ]; then
275             MEMCACHED_SERVERS=$DEFAULT_MEMCACHED_SERVERS
276         # else: was set by the koha-sites.conf file
277         fi
278     fi
279
280     if [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
281         MEMCACHED_NAMESPACE="$CLO_MEMCACHED_PREFIX$instance"
282     else
283         if [ "$MEMCACHED_PREFIX" != "" ]; then
284             MEMCACHED_NAMESPACE="$MEMCACHED_PREFIX$instance"
285         else
286             MEMCACHED_NAMESPACE="$DEFAULT_MEMCACHED_PREFIX$instance"
287         fi
288     fi
289
290 }
291
292 set_upload_path()
293 {
294     local instance="$1"
295
296     if [ "$CLO_UPLOAD_PATH" != "" ]; then
297         UPLOAD_PATH=$CLO_UPLOAD_PATH
298     else
299         UPLOAD_PATH="$INSTANCE_PATH_BASE/$instance/$UPLOAD_DIR"
300     fi
301 }
302
303 set_tmp_path()
304 {
305     local instance="$1"
306
307     if [ "$CLO_TMP_PATH" != "" ]; then
308         TMP_PATH=$CLO_TMP_PATH
309     else
310         TMP_PATH="$INSTANCE_PATH_BASE/$instance/$TMP_DIR"
311     fi
312 }
313
314 enable_sru_server()
315 {
316     # remove the commenting symbols
317     START_SRU_PUBLICSERVER=""
318     END_SRU_PUBLICSERVER=""
319     if [ "$SRU_SERVER_PORT" = "" ]; then
320         # --sru-port not passed, use the default
321         SRU_SERVER_PORT=$DEFAULT_SRU_SERVER_PORT
322     fi
323 }
324
325 check_letsencrypt()
326 {
327     if [ $(dpkg-query -W -f='${Status}' letsencrypt 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
328         set +e
329         apt-cache show letsencrypt &>/dev/null
330         local aptcacheshow=$?
331         set -e
332         if [ $aptcacheshow -eq 0 ]; then
333                 read -r -p "The letsencrypt package is not installed. Do it now?  [y/N] " response
334                 if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
335                     local debrelease="$(lsb_release -c -s)"
336                     if [ $debrelease = "jessie" ]; then
337                         apt-get install -y -t jessie-backports letsencrypt
338                     else
339                         apt-get install -y letsencrypt
340                     fi
341                 else
342                     die "You have to install letsencrypt to use the --letsencrypt parameter."
343                 fi
344         else
345             echo "No installation candidate available for package letsencrypt."
346             if [[ -f /usr/bin/letsencrypt ]]; then
347                 read -r -p "If you have a symlink from /usr/bin/letsencrypt to letsencrypt-auto, it should work. [y/N] " response
348                 if [[ ! $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
349                     die "You have to install letsencrypt to use the --letsencrypt parameter."
350                 fi
351             else
352                 die "You can create a symlink from /usr/bin/letsencrypt to letsencrypt-auto."
353             fi
354         fi
355     fi
356 }
357
358 letsencrypt_instance()
359 {
360     # Get letsencrypt certificates
361     letsencrypt --agree-tos --renew-by-default --webroot certonly \
362         -w /usr/share/koha/opac/htdocs/ -d $opacdomain -w /usr/share/koha/intranet/htdocs/ -d $intradomain
363     # enable all ssl settings (apache won't start with these before certs are present)
364     sed -i "s:^\s*#\(\s*SSL.*\)$:\1:" "/etc/apache2/sites-available/$name.conf"
365     # change port from 80 to 443. (apache won't start if it is 443 without certs present)
366     sed -i "s:^\s*\(<VirtualHost \*\:\)80> #https$:\1443>:" "/etc/apache2/sites-available/$name.conf"
367     # enable redirect from http to https on port 80
368     sed -i "s:^\s*#\(.*\)#nohttps$:\1:" "/etc/apache2/sites-available/$name.conf"
369     # make koha-list --letsencrypt aware of this instance # could be done by checking apache conf instead
370     echo -e "opacdomain=\"$opacdomain\"\nintradomain=\"$intradomain\"" > /var/lib/koha/$name/letsencrypt.enabled
371     # restart apache with working certs
372     service apache2 restart
373 }
374
375 # Set defaults and read config file, if it exists.
376 DOMAIN=""
377 OPACPORT="80"
378 OPACPREFIX=""
379 OPACSUFFIX=""
380 INTRAPORT="8080"
381 INTRAPREFIX=""
382 INTRASUFFIX=""
383 DEFAULTSQL=""
384 ZEBRA_MARC_FORMAT="marc21"
385 ZEBRA_LANGUAGE="en"
386 ADMINUSER="1"
387 PASSWDFILE="/etc/koha/passwd"
388
389 # memcached variables
390 USE_MEMCACHED="yes"
391 MEMCACHED_SERVERS=""
392 MEMCACHED_PREFIX=""
393 # hardcoded memcached defaults
394 DEFAULT_MEMCACHED_SERVERS="127.0.0.1:11211"
395 DEFAULT_MEMCACHED_PREFIX="koha_"
396 # hardcoded instance base path
397 INSTANCE_PATH_BASE="/var/lib/koha"
398 UPLOAD_DIR="uploads"
399 UPLOAD_PATH=""
400 # timezone defaults to empty
401 TIMEZONE=""
402 # hardcoded upload_tmp_path
403 TMP_DIR="tmp"
404 TMP_PATH=""
405 # cache base dir
406 CACHE_DIR_BASE="/var/cache/koha"
407 # Generate a randomizaed API secret
408 API_SECRET="$(pwgen -s 64 1)"
409 # SRU server variables
410 ENABLE_SRU="no"
411 SRU_SERVER_PORT=""
412 # hardcoded default SRU server port
413 DEFAULT_SRU_SERVER_PORT="7090"
414 START_SRU_PUBLICSERVER="<!--"
415 END_SRU_PUBLICSERVER="-->"
416
417 START_BIBLIOS_RETRIEVAL_INFO=""
418 END_BIBLIOS_RETRIEVAL_INFO=""
419 START_AUTHORITIES_RETRIEVAL_INFO=""
420 END_AUTHORITIES_RETRIEVAL_INFO=""
421
422 APACHE_CONFIGFILE=""
423
424 if [ -e /etc/koha/koha-sites.conf ]
425 then
426     . /etc/koha/koha-sites.conf
427 fi
428
429 [ $# -ge 1 ] && [ $# -le 16 ] || ( usage ; die "Error: wrong parameters" )
430
431 TEMP=`getopt -o chrpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,enable-sru,sru-port:,help,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,dbhost:,database:,adminuser:,memcached-servers:,memcached-prefix:,template-cache-dir:,timezone:,upload-path:,tmp-path:,letsencrypt, \
432      -n "$0" -- "$@"`
433
434 # Note the quotes around `$TEMP': they are essential!
435 eval set -- "$TEMP"
436
437 # Temporary variables for the command line options
438 CLO_ZEBRA_MARC_FORMAT=""
439 CLO_ZEBRA_LANGUAGE=""
440 CLO_DEFAULTSQL=""
441 CLO_ADMINUSER=""
442 CLO_MEMCACHED_SERVERS=""
443 CLO_MEMCACHED_PREFIX=""
444 CLO_UPLOAD_PATH=""
445 CLO_TMP_PATH=""
446 CLO_LETSENCRYPT=""
447 CLO_TEMPLATE_CACHE_DIR=""
448 CLO_TIMEZONE=""
449
450 while true ; do
451     case "$1" in
452         -c|--create-db)
453             op=create ; shift ;;
454         -r|--request-db)
455             op=request ; shift ;;
456         -p|--populate-db)
457             op=populate ; shift ;;
458         -u|--use-db)
459             op=use ; shift ;;
460         --memcached-servers)
461             CLO_MEMCACHED_SERVERS="$2" ; shift 2 ;;
462         --memcached-prefix)
463             CLO_MEMCACHED_PREFIX="$2" ; shift 2;;
464         -m|--marcflavor)
465             CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
466         -l|--zebralang)
467             CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
468         -d|--defaultsql)
469             CLO_DEFAULTSQL="$2" ; shift 2 ;;
470         -f|--configfile)
471             configfile="$2" ; shift 2 ;;
472         -s|--passwdfile)
473             CLO_PASSWDFILE="$2" ; shift 2 ;;
474         -b|--database)
475             CLO_DATABASE="$2" ; shift 2 ;;
476         --dbhost)
477             CLO_DBHOST="$2" ; shift 2 ;;
478         -a|--adminuser)
479             CLO_ADMINUSER="$2" ; shift 2 ;;
480         --enable-sru)
481             ENABLE_SRU="yes" ; shift ;;
482         --sru-port)
483             SRU_SERVER_PORT="$2" ; shift 2 ;;
484         --template-cache-dir)
485             CLO_TEMPLATE_CACHE_DIR="$2" ; shift 2 ;;
486         --timezone)
487             CLO_TIMEZONE="$2" ; shift 2 ;;
488         --upload-path)
489             CLO_UPLOAD_PATH="$2" ; shift 2 ;;
490         --tmp-path)
491             CLO_TMP_PATH="$2" ; shift 2 ;;
492         --letsencrypt)
493             CLO_LETSENCRYPT="yes" ; shift ;;
494         -h|--help)
495             usage ; exit 0 ;;
496         --)
497             shift ; break ;;
498         *)
499             die "Internal error processing command line arguments" ;;
500     esac
501 done
502
503 # Load the configfile given on the command line
504 if [ "$configfile" != "" ]
505 then
506     if [ -e "$configfile" ]
507     then
508         . "$configfile"
509     else
510         die "$configfile does not exist.";
511     fi
512 fi
513
514 # Make sure options from the command line get the highest precedence
515 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
516 then
517     ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
518 fi
519 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
520 then
521     ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
522 fi
523 if [ "$CLO_DEFAULTSQL" != "" ]
524 then
525     DEFAULTSQL="$CLO_DEFAULTSQL"
526 fi
527 if [ "$CLO_ADMINUSER" != "" ]
528 then
529     ADMINUSER="$CLO_ADMINUSER"
530 fi
531 if [ "$CLO_PASSWDFILE" != "" ]
532 then
533     PASSWDFILE="$CLO_PASSWDFILE"
534 fi
535
536 if [ "$CLO_TIMEZONE" != "" ]; then
537     TIMEZONE=$CLO_TIMEZONE
538 fi
539
540 if [ "$CLO_BIBLIOS_INDEXING_MODE" !=  "" ]; then
541     BIBLIOS_INDEXING_MODE=$CLO_BIBLIOS_INDEXING_MODE
542 fi
543
544 set_biblios_indexing_mode $ZEBRA_MARC_FORMAT
545
546 if [ "$ENABLE_SRU" != "no" ]; then
547     enable_sru_server
548 fi
549
550 set_authorities_indexing_mode $ZEBRA_MARC_FORMAT
551
552 [ $# -ge 1 ] || ( usage ; die "Missing instance name..." )
553
554 name="$1"
555
556 set_upload_path $name
557 set_tmp_path $name
558
559 if [ "$op" = use ] && [ "$CLO_DATABASE" = "" ] &&
560    ( [ ! -f "$PASSWDFILE" ] || [ ! `cat $PASSWDFILE | grep "^$name:"` ] )
561 then
562     cat <<NO_DB
563 --use-db must have a database name. It can be specified in a readable
564 password file ($PASSWDFILE). Using --passwdfile overrides the default
565 /usr/koha/passwd file. Each line of a passwd file should be in the format of:
566     instance:username:password:dbname:dbhost
567 A database name can also be specified using '--database dbname'.
568 NO_DB
569     die;
570 fi
571
572 set_memcached $name
573
574 # Set template cache dir
575 if [ "$CLO_TEMPLATE_CACHE_DIR" != "" ]; then
576     TEMPLATE_CACHE_DIR="$CLO_TEMPLATE_CACHE_DIR"
577 else
578     TEMPLATE_CACHE_DIR="$CACHE_DIR_BASE/$name/templates"
579 fi
580
581 # Are we root? If not, the mod_rewrite check will fail and be confusing, so
582 # we look into this first.
583 if [[ $UID -ne 0 ]]
584 then
585     die "This script must be run with root privileges."
586 fi
587
588 # Check everything is ok with Apache, die otherwise
589 check_apache_config
590
591 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
592 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
593
594 # Check everything is ok with letsencrypt, die otherwise
595 if [ "$CLO_LETSENCRYPT" = "yes" ]; then
596     check_letsencrypt
597 fi
598
599 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
600 then
601     passwdline=`cat $PASSWDFILE | grep "^$name:"`
602     mysqluser=`echo $passwdline | cut -d ":" -f 2`
603     mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
604     mysqldb=`echo $passwdline | cut -d ":" -f 4`
605     mysqlhost=`echo $passwdline | cut -d ":" -f 5`
606 fi
607
608 # The order of precedence for MySQL database name is:
609 # default < passwd file < command line
610 if [ "$mysqldb" = "" ]
611 then
612     mysqldb="koha_$name"
613 fi
614
615 if [ "$CLO_DATABASE" != "" ]
616 then
617     mysqldb="$CLO_DATABASE"
618 fi
619
620 if [ "$mysqluser" = "" ]
621 then
622     mysqluser="koha_$name"
623 fi
624
625 if [ "$CLO_DBHOST" != "" ]
626 then
627     mysqlhost="$CLO_DBHOST"
628 fi
629
630 if [ "$mysqlhost" = "" ]
631 then
632     mysqlhost="$(getmysqlhost)"
633 fi
634
635 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
636 then
637     if [ "$mysqlpwd" = "" ]
638     then
639         mysqlpwd="$(pwgen -s 16 1)"
640     fi
641 else
642     mysqlpwd="$(getinstancemysqlpassword $name)"
643 fi
644
645
646 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
647 then
648     # Create new user and group.
649     username="$name-koha"
650     if getent passwd "$username" > /dev/null
651     then
652         die "User $username already exists."
653     fi
654     if getent group "$username" > /dev/null
655     then
656         die "Group $username already exists."
657     fi
658     adduser --no-create-home --disabled-login \
659         --gecos "Koha instance $username" \
660         --home "/var/lib/koha/$name" \
661         --quiet "$username"
662
663     # Create the site-specific directories.
664     koha-create-dirs "$name"
665
666     # Generate Zebra database password.
667     zebrapwd="$(pwgen -s 16 1)"
668     # Future enhancement: make this configurable for when your db is on
669     # another server.
670     mysql_hostname="localhost"
671     # Set up MySQL database for this instance.
672     if [ "$op" = create ]
673     then
674         if [ ! -e /etc/mysql/debian.cnf ]; then
675             MYSQL_OPTIONS="-u root"
676             echo "WARNING: The koha-common.cnf file is a dead soft link!"
677         else
678             MYSQL_OPTIONS="--defaults-extra-file=/etc/mysql/koha-common.cnf"
679         fi
680         mysql $MYSQL_OPTIONS <<eof
681 CREATE DATABASE \`$mysqldb\`;
682 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
683 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
684 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
685 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`@'$mysql_hostname';
686 FLUSH PRIVILEGES;
687 eof
688     fi #`
689
690     if [ "$CLO_LETSENCRYPT" = "yes" ]; then
691         APACHE_CONFIGFILE="apache-site-https.conf.in"
692     else
693         APACHE_CONFIGFILE="apache-site.conf.in"
694     fi
695     # Generate and install Apache site-available file and log dir.
696     generate_config_file $APACHE_CONFIGFILE \
697         "/etc/apache2/sites-available/$name.conf"
698     mkdir "/var/log/koha/$name"
699     chown "$username:$username" "/var/log/koha/$name"
700
701
702     # Generate and install main Koha config file.
703     generate_config_file koha-conf-site.xml.in \
704         "/etc/koha/sites/$name/koha-conf.xml"
705
706     # Generate and install the log4perl config file.
707     generate_config_file log4perl-site.conf.in \
708         "/etc/koha/sites/$name/log4perl.conf"
709
710     # Generate and install Zebra config files.
711     generate_config_file zebra-biblios-dom-site.cfg.in \
712         "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
713     generate_config_file zebra-authorities-dom-site.cfg.in \
714         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
715     generate_config_file zebra.passwd.in \
716         "/etc/koha/sites/$name/zebra.passwd"
717
718     # Create a GPG-encrypted file for requesting a DB to be set up.
719     if [ "$op" = request ]
720     then
721         touch "$name-db-request.txt"
722         chmod 0600 "$name-db-request.txt"
723         cat > "$name-db-request.txt" << eof
724 Please create a MySQL database and user on $mysqlhost as follows:
725
726 database name: $mysqldb
727 database user: $mysqluser
728      password: $mysqlpwd
729
730 Thank you.
731 eof
732
733         echo "See $name-db-request.txt for database creation request."
734         echo "Please forward it to the right person, and then run"
735         echo "$0 --populate-db $name"
736         echo "Thanks."
737     fi
738 fi
739
740
741 if [ "$op" = create ] || [ "$op" = populate ]
742 then
743     # Re-fetch the passwords from the config we've generated, allows it
744     # to be different from what we set, in case the user had to change
745     # something.
746     mysqluser=$(getinstancemysqluser $name)
747     mysqldb=$(getinstancemysqldatabase $name)
748     # Use the default database content if that exists.
749     if [ -e "$DEFAULTSQL" ]
750     then
751         # Populate the database with default content.
752         zcat "$DEFAULTSQL" |
753         sed "s/__KOHASITE__/koha_$name/g" |
754         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
755
756
757         # Change the default user's password.
758         staffpass="$(pwgen 12 1)"
759         staffdigest=$(echo -n "$staffpass" |
760                       perl -e '
761                             use Digest::MD5 qw(md5_base64); 
762                             while (<>) { print md5_base64($_), "\n"; }')
763         mysql --host="$mysqlhost" --user="$mysqluser" \
764 --password="$mysqlpwd" <<eof
765 USE \`$mysqldb\`;
766 UPDATE borrowers 
767 SET password = '$staffdigest' 
768 WHERE borrowernumber = $ADMINUSER;
769 eof
770         #`
771         echo "staff user password is '$staffpass' but keep that secret"
772
773         # Upgrade the database schema, just in case the dump was from an 
774         # old version.
775         koha-upgrade-schema "$name"
776     else
777         echo "Koha instance is empty, no staff user created."
778     fi
779 fi
780
781
782 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
783 then
784     # Reconfigure Apache.
785     if ! {
786         a2ensite "$name" > /dev/null 2>&1 ||
787             a2ensite "${name}.conf" > /dev/null 2>&1
788     }; then
789         echo "Warning: problem enabling $name in Apache" >&2
790     fi
791     service apache2 restart
792
793     # Start Zebra.
794     koha-zebra --start "$name"
795
796     if [ "$USE_INDEXER_DAEMON" = "yes" ]; then
797         # Start Indexer daemon
798         koha-indexer --start "$name"
799     fi
800
801     if [ "$CLO_LETSENCRYPT" = "yes" ]; then
802         # Get letsencrypt certificates
803         letsencrypt_instance
804     fi
805 fi
806
807
808 if [ "$op" = request ]
809 then
810     koha-disable "$name"
811 fi
812
813 echo <<eoh
814
815 Email for this instance is disabled. When you're ready to enable it, use:
816 koha-email-enable $name
817 eoh