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