Bug 21111: Add --exclude-indexes to koha-run-backups
[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, el, en (default), es, fr, 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   --smtp-host host          SMTP host name
80   --smtp-port NN            SMTP port
81   --smtp-timeout NN         Connection timeout in seconds
82   --smtp-ssl-mode mode      SSL mode. Options are 'disabled' (default), 'ssl' and 'starttls'.
83   --smtp-user-name user     User name to be used on SMTP auth
84   --smtp-password  pass     Password to authenticate SMTP
85   --smtp-debug              Enable debug mode for SMTP
86   --help,-h                 Show this help.
87
88 Note: the instance name cannot be longer that 11 chars.
89
90 EOF
91 }
92
93 # UPPER CASE VARIABLES - from configfile or default value
94 # lower case variables - generated within this script
95 generate_config_file() {
96     touch "$2"
97     chown "root:$username" "$2"
98     chmod 0640 "$2"
99     sed -e "s/__KOHA_CONF_DIR__/\/etc\/koha\/sites\/$name/g" \
100         -e "s/__KOHASITE__/$name/g" \
101         -e "s/__OPACPORT__/$OPACPORT/g" \
102         -e "s/__INTRAPORT__/$INTRAPORT/g" \
103         -e "s/__OPACSERVER__/$opacdomain/g" \
104         -e "s/__INTRASERVER__/$intradomain/g" \
105         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
106         -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
107         -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
108         -e "s/__SRU_BIBLIOS_PORT__/$SRU_SERVER_PORT/g" \
109         -e "s/__START_SRU_PUBLICSERVER__/$START_SRU_PUBLICSERVER/g" \
110         -e "s/__END_SRU_PUBLICSERVER__/$END_SRU_PUBLICSERVER/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/__ELASTICSEARCH_SERVER__/${ELASTICSEARCH_SERVER}/g" \
117         -e "s/__UNIXUSER__/$username/g" \
118         -e "s/__UNIXGROUP__/$username/g" \
119         -e "s#__TEMPLATE_CACHE_DIR__#$TEMPLATE_CACHE_DIR#g" \
120         -e "s#__TIMEZONE__#$TIMEZONE#g" \
121         -e "s#__BCRYPT_SETTINGS__#$BCRYPT_SETTINGS#g" \
122         -e "s#__UPLOAD_PATH__#$UPLOAD_PATH#g" \
123         -e "s#__TMP_PATH__#$TMP_PATH#g" \
124         -e "s/__LOG_DIR__/\/var\/log\/koha\/$name/g" \
125         -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
126         -e "s/__MEMCACHED_NAMESPACE__/$MEMCACHED_NAMESPACE/g" \
127         -e "s/__MEMCACHED_SERVERS__/$MEMCACHED_SERVERS/g" \
128         -e "s/__SMTP_HOST__/$SMTP_HOST/g" \
129         -e "s/__SMTP_PORT__/$SMTP_PORT/g" \
130         -e "s/__SMTP_TIMEOUT__/$SMTP_TIMEOUT/g" \
131         -e "s/__SMTP_SSL_MODE__/$SMTP_SSL_MODE/g" \
132         -e "s/__SMTP_USER_NAME__/$SMTP_USER_NAME/g" \
133         -e "s/__SMTP_PASSWORD__/$SMTP_PASSWORD/g" \
134         -e "s/__SMTP_DEBUG__/$SMTP_DEBUG/g" \
135         "/etc/koha/$1" > "$2"
136
137 }
138
139 getmysqlhost() {
140     if [ ! -f /etc/mysql/debian.cnf ]
141     then
142         echo localhost
143         return
144     fi
145     awk '
146         BEGIN { FS="=" }
147         $1 ~/\[/ { inclient=0 }
148         $1 ~/\[client\]/ { inclient=1; next }
149         inclient==1 && $1 ~/host/ { gsub(/ /, "", $2); print $2 }' \
150         /etc/mysql/koha-common.cnf
151 }
152
153 getinstancemysqlpassword() {
154     xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
155 }
156
157 getinstancemysqluser() {
158     xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
159 }
160
161 getinstancemysqldatabase() {
162     xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
163 }
164
165 check_apache_config()
166 {
167
168     # Check that mpm_itk is installed and enabled
169     if ! /usr/sbin/apachectl -M | grep -q 'mpm_itk'; then
170         # Check Apache version
171         APACHE_DISABLE_MPM_MSG=""
172         if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
173             # mpm_event or mpm_worker need to be disabled first. mpm_itk depends
174             # on mpm_prefork, which is enabled if needed. See
175             # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=734865
176             if /usr/sbin/apachectl -M | grep -q 'mpm_event'; then
177                 APACHE_DISABLE_MPM_MSG=" sudo a2dismod mpm_event ;"
178             elif /usr/sbin/apachectl -M | grep -q 'mpm_worker'; then
179                 APACHE_DISABLE_MPM_MSG=" sudo a2dismod mpm_worker ;"
180             # else mpm_prefork: a2enmod mpm_itk works
181             fi
182         # else Apache 2.2: a2enmod mpm_itk works
183         fi
184
185         cat 1>&2  <<EOM
186
187 Koha requires mpm_itk to be enabled within Apache in order to run.
188 Typically this can be enabled with:
189
190    $APACHE_DISABLE_MPM_MSG sudo a2enmod mpm_itk
191 EOM
192
193         die
194     fi
195
196     # Check that mod_rewrite is installed and enabled.
197     if ! /usr/sbin/apachectl -M | grep -q 'rewrite_module'; then
198         cat 1>&2  <<EOM
199
200 Koha requires mod_rewrite to be enabled within Apache in order to run.
201 Typically this can be enabled with:
202
203     sudo a2enmod rewrite
204 EOM
205         die
206     fi
207
208     # Check that the CGI module is installed and enabled
209     # (Apache 2.4 may not have it by default.)
210     if ! /usr/sbin/apachectl -M | grep -q 'cgi_module'; then
211         cat 1>&2 << EOM
212 Koha requires mod_cgi to be enabled within Apache in order to run.
213 Typically this can be enabled with:
214
215     sudo a2enmod cgi
216 EOM
217         die
218     fi
219
220     # Check that mod_ssl is installed and enabled.
221     if [ "$CLO_LETSENCRYPT" = "yes" ]; then
222         if ! /usr/sbin/apachectl -M | grep -q 'ssl_module'; then
223             cat 1>&2  <<EOM
224
225 Koha requires mod_ssl to be enabled within Apache in order to run with --letsencrypt.
226 Typically this can be enabled with:
227
228     sudo a2enmod ssl
229 EOM
230             die
231         fi
232     fi
233
234 }
235
236 set_memcached()
237 {
238     local instance="$1"
239
240     if [ "$CLO_MEMCACHED_SERVERS" != "" ]; then
241         MEMCACHED_SERVERS=$CLO_MEMCACHED_SERVERS
242     else
243         if [ "$MEMCACHED_SERVERS" = "" ]; then
244             MEMCACHED_SERVERS=$DEFAULT_MEMCACHED_SERVERS
245         # else: was set by the koha-sites.conf file
246         fi
247     fi
248
249     if [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
250         MEMCACHED_NAMESPACE="$CLO_MEMCACHED_PREFIX$instance"
251     else
252         if [ "$MEMCACHED_PREFIX" != "" ]; then
253             MEMCACHED_NAMESPACE="$MEMCACHED_PREFIX$instance"
254         else
255             MEMCACHED_NAMESPACE="$DEFAULT_MEMCACHED_PREFIX$instance"
256         fi
257     fi
258
259 }
260
261 set_smtp()
262 {
263     if [ "$CLO_SMTP_HOST" != "" ]; then
264         SMTP_HOST=$CLO_SMTP_HOST
265     fi
266
267     if [ "$CLO_SMTP_PORT" != "" ]; then
268         SMTP_PORT=$CLO_SMTP_PORT
269     fi
270
271     if [ "$CLO_SMTP_TIMEOUT" != "" ]; then
272         SMTP_TIMEOUT=$CLO_SMTP_TIMEOUT
273     fi
274
275     if [ "$CLO_SMTP_SSL_MODE" != "" ]; then
276         SMTP_SSL_MODE=$CLO_SMTP_SSL_MODE
277     fi
278
279     if [ "$CLO_SMTP_USER_NAME" != "" ]; then
280         SMTP_USER_NAME=$CLO_SMTP_USER_NAME
281     fi
282
283     if [ "$CLO_SMTP_PASSWORD" != "" ]; then
284         SMTP_PASSWORD=$CLO_SMTP_PASSWORD
285     fi
286
287     if [ "$CLO_SMTP_DEBUG" != "" ]; then
288         SMTP_DEBUG=$CLO_SMTP_DEBUG
289     fi
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 # SMTP config
390 SMTP_HOST="localhost"
391 SMTP_PORT="25"
392 SMTP_TIMEOUT="120"
393 SMTP_SSL_MODE="disabled"
394 SMTP_USER_NAME=""
395 SMTP_PASSWORD=""
396 SMTP_DEBUG="0"
397
398 # memcached variables
399 USE_MEMCACHED="yes"
400 MEMCACHED_SERVERS=""
401 MEMCACHED_PREFIX=""
402 # elasticsearch config
403 ELASTICSEARCH_SERVER="localhost:9200"
404 # hardcoded memcached defaults
405 DEFAULT_MEMCACHED_SERVERS="127.0.0.1:11211"
406 DEFAULT_MEMCACHED_PREFIX="koha_"
407 # hardcoded instance base path
408 INSTANCE_PATH_BASE="/var/lib/koha"
409 UPLOAD_DIR="uploads"
410 UPLOAD_PATH=""
411 # timezone defaults to empty
412 TIMEZONE=""
413 # hardcoded upload_tmp_path
414 TMP_DIR="tmp"
415 TMP_PATH=""
416 # cache base dir
417 CACHE_DIR_BASE="/var/cache/koha"
418 # Generate a randomizaed API secret
419 API_SECRET="$(pwgen -s 64 1)"
420 # SRU server variables
421 ENABLE_SRU="no"
422 SRU_SERVER_PORT=""
423 # hardcoded default SRU server port
424 DEFAULT_SRU_SERVER_PORT="7090"
425 START_SRU_PUBLICSERVER="<!--"
426 END_SRU_PUBLICSERVER="-->"
427
428 APACHE_CONFIGFILE=""
429
430 if [ -e /etc/koha/koha-sites.conf ]
431 then
432     . /etc/koha/koha-sites.conf
433 fi
434
435 [ $# -ge 1 ] && [ $# -le 16 ] || ( usage ; die "Error: wrong parameters" )
436
437 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:,smtp-host:,smtp-port,smtp-timeout:,smtp-ssl-mode:smtp-user-name:,smtp-password:,smtp-debug,letsencrypt, \
438      -n "$0" -- "$@"`
439
440 # Note the quotes around `$TEMP': they are essential!
441 eval set -- "$TEMP"
442
443 # Temporary variables for the command line options
444 CLO_ZEBRA_MARC_FORMAT=""
445 CLO_ZEBRA_LANGUAGE=""
446 CLO_DEFAULTSQL=""
447 CLO_ADMINUSER=""
448 CLO_MEMCACHED_SERVERS=""
449 CLO_MEMCACHED_PREFIX=""
450 CLO_ELASTICSEARCH_SERVER=""
451 CLO_UPLOAD_PATH=""
452 CLO_TMP_PATH=""
453 CLO_LETSENCRYPT=""
454 CLO_TEMPLATE_CACHE_DIR=""
455 CLO_TIMEZONE=""
456 CLO_SMTP_HOST=""
457 CLO_SMTP_PORT=""
458 CLO_SMTP_TIMEOUT=""
459 CLO_SMTP_SSL_MODE=""
460 CLO_SMTP_USER_NAME=""
461 CLO_SMTP_PASSWORD=""
462 CLO_SMTP_DEBUG=""
463
464 while true ; do
465     case "$1" in
466         -c|--create-db)
467             op=create ; shift ;;
468         -r|--request-db)
469             op=request ; shift ;;
470         -p|--populate-db)
471             op=populate ; shift ;;
472         -u|--use-db)
473             op=use ; shift ;;
474         --memcached-servers)
475             CLO_MEMCACHED_SERVERS="$2" ; shift 2 ;;
476         --memcached-prefix)
477             CLO_MEMCACHED_PREFIX="$2" ; shift 2;;
478         --elasticsearch-server)
479             CLO_ELASTICSEARCH_SERVER="$2" ; shift 2 ;;
480         -m|--marcflavor)
481             CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
482         -l|--zebralang)
483             CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
484         -d|--defaultsql)
485             CLO_DEFAULTSQL="$2" ; shift 2 ;;
486         -f|--configfile)
487             configfile="$2" ; shift 2 ;;
488         -s|--passwdfile)
489             CLO_PASSWDFILE="$2" ; shift 2 ;;
490         -b|--database)
491             CLO_DATABASE="$2" ; shift 2 ;;
492         --dbhost)
493             CLO_DBHOST="$2" ; shift 2 ;;
494         -a|--adminuser)
495             CLO_ADMINUSER="$2" ; shift 2 ;;
496         --enable-sru)
497             ENABLE_SRU="yes" ; shift ;;
498         --smtp-debug)
499             CLO_SMTP_DEBUG="1" ; shift ;;
500         --smtp-host)
501             CLO_SMTP_HOST="$2" ; shift 2 ;;
502         --smtp-port)
503             CLO_SMTP_PORT="$2" ; shift 2 ;;
504         --smtp-timeout)
505             CLO_SMTP_TIMEOUT="$2" ; shift 2 ;;
506         --smtp-ssl-mode)
507             CLO_SMTP_SSL_MODE="$2" ; shift 2 ;;
508         --smtp-user-name)
509             CLO_SMTP_USER_NAME="$2" ; shift 2 ;;
510         --smtp-password)
511             CLO_SMTP_PASSWORD="$2" ; shift 2 ;;
512         --sru-port)
513             SRU_SERVER_PORT="$2" ; shift 2 ;;
514         --template-cache-dir)
515             CLO_TEMPLATE_CACHE_DIR="$2" ; shift 2 ;;
516         --timezone)
517             CLO_TIMEZONE="$2" ; shift 2 ;;
518         --upload-path)
519             CLO_UPLOAD_PATH="$2" ; shift 2 ;;
520         --tmp-path)
521             CLO_TMP_PATH="$2" ; shift 2 ;;
522         --letsencrypt)
523             CLO_LETSENCRYPT="yes" ; shift ;;
524         -h|--help)
525             usage ; exit 0 ;;
526         --)
527             shift ; break ;;
528         *)
529             die "Internal error processing command line arguments" ;;
530     esac
531 done
532
533 # Load the configfile given on the command line
534 if [ "$configfile" != "" ]
535 then
536     if [ -e "$configfile" ]
537     then
538         . "$configfile"
539     else
540         die "$configfile does not exist.";
541     fi
542 fi
543
544 # Make sure options from the command line get the highest precedence
545 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
546 then
547     ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
548 fi
549 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
550 then
551     ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
552 fi
553 if [ "$CLO_DEFAULTSQL" != "" ]
554 then
555     DEFAULTSQL="$CLO_DEFAULTSQL"
556 fi
557 if [ "$CLO_ADMINUSER" != "" ]
558 then
559     ADMINUSER="$CLO_ADMINUSER"
560 fi
561 if [ "$CLO_PASSWDFILE" != "" ]
562 then
563     PASSWDFILE="$CLO_PASSWDFILE"
564 fi
565
566 if [ "$CLO_TIMEZONE" != "" ]; then
567     TIMEZONE=$CLO_TIMEZONE
568 fi
569
570 if [ "${CLO_ELASTICSEARCH_SERVER}" != "" ]; then
571     ELASTICSEARCH_SERVER="${CLO_ELASTICSEARCH_SERVER}"
572 fi
573
574 BCRYPT_SETTINGS=$(htpasswd -bnBC 10 "" password | tr -d ':\n' | sed 's/$2y/$2a/');
575
576
577 if [ "$ENABLE_SRU" != "no" ]; then
578     enable_sru_server
579 fi
580
581 [ $# -ge 1 ] || ( usage ; die "Missing instance name..." )
582
583 name="$1"
584
585 set_smtp
586 set_upload_path $name
587 set_tmp_path $name
588
589 if [ "$op" = use ] && [ "$CLO_DATABASE" = "" ] &&
590    ( [ ! -f "$PASSWDFILE" ] || [ ! `cat $PASSWDFILE | grep "^$name:"` ] )
591 then
592     cat <<NO_DB
593 --use-db must have a database name. It can be specified in a readable
594 password file ($PASSWDFILE). Using --passwdfile overrides the default
595 /usr/koha/passwd file. Each line of a passwd file should be in the format of:
596     instance:username:password:dbname:dbhost
597 A database name can also be specified using '--database dbname'.
598 NO_DB
599     die;
600 fi
601
602 if [ "$USE_MEMCACHED" = "no" ]; then
603     MEMCACHED_SERVERS=""
604     MEMCACHED_NAMESPACE=""
605     MEMCACHED_PREFIX=""
606 else
607     set_memcached $name
608 fi
609
610 # Set template cache dir
611 if [ "$CLO_TEMPLATE_CACHE_DIR" != "" ]; then
612     TEMPLATE_CACHE_DIR="$CLO_TEMPLATE_CACHE_DIR"
613 else
614     TEMPLATE_CACHE_DIR="$CACHE_DIR_BASE/$name/templates"
615 fi
616
617 # Are we root? If not, the mod_rewrite check will fail and be confusing, so
618 # we look into this first.
619 if [[ $UID -ne 0 ]]
620 then
621     die "This script must be run with root privileges."
622 fi
623
624 # Check everything is ok with Apache, die otherwise
625 check_apache_config
626
627 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
628 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
629
630 # Check everything is ok with letsencrypt, die otherwise
631 if [ "$CLO_LETSENCRYPT" = "yes" ]; then
632     check_letsencrypt
633 fi
634
635 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
636 then
637     passwdline=`cat $PASSWDFILE | grep "^$name:"`
638     mysqluser=`echo $passwdline | cut -d ":" -f 2`
639     mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
640     mysqldb=`echo $passwdline | cut -d ":" -f 4`
641     mysqlhost=`echo $passwdline | cut -d ":" -f 5`
642 fi
643
644 # The order of precedence for MySQL database name is:
645 # default < passwd file < command line
646 if [ "$mysqldb" = "" ]
647 then
648     mysqldb="koha_$name"
649 fi
650
651 if [ "$CLO_DATABASE" != "" ]
652 then
653     mysqldb="$CLO_DATABASE"
654 fi
655
656 if [ "$mysqluser" = "" ]
657 then
658     mysqluser="koha_$name"
659 fi
660
661 if [ "$CLO_DBHOST" != "" ]
662 then
663     mysqlhost="$CLO_DBHOST"
664 fi
665
666 if [ "$mysqlhost" = "" ]
667 then
668     mysqlhost="$(getmysqlhost)"
669 fi
670
671 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
672 then
673     if [ "$mysqlpwd" = "" ]
674     then
675         mysqlpwd="$(pwgen -s 15 1)"
676         mysqlpwd="$mysqlpwd@"
677     fi
678 else
679     mysqlpwd="$(getinstancemysqlpassword $name)"
680 fi
681
682
683 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
684 then
685     # Create new user and group.
686     username="$name-koha"
687     if getent passwd "$username" > /dev/null
688     then
689         die "User $username already exists."
690     fi
691     if getent group "$username" > /dev/null
692     then
693         die "Group $username already exists."
694     fi
695     adduser --no-create-home --disabled-login \
696         --gecos "Koha instance $username" \
697         --home "/var/lib/koha/$name" \
698         --quiet "$username"
699
700     # Create the site-specific directories.
701     koha-create-dirs "$name"
702
703     # Generate Zebra database password.
704     zebrapwd="$(pwgen -s 16 1)"
705     # Future enhancement: make this configurable for when your db is on
706     # another server.
707     mysql_hostname="localhost"
708     # Set up MySQL database for this instance.
709     if [ "$op" = create ]
710     then
711         if [ ! -e /etc/mysql/debian.cnf ]; then
712             MYSQL_OPTIONS="-u root"
713             echo "WARNING: The koha-common.cnf file is a dead soft link!"
714         else
715             MYSQL_OPTIONS="--defaults-extra-file=/etc/mysql/koha-common.cnf"
716         fi
717         mysql $MYSQL_OPTIONS <<eof
718 CREATE DATABASE \`$mysqldb\`;
719 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
720 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`@'$mysql_hostname';
721 FLUSH PRIVILEGES;
722 eof
723     fi #`
724
725     if [ "$CLO_LETSENCRYPT" = "yes" ]; then
726         APACHE_CONFIGFILE="apache-site-https.conf.in"
727     else
728         APACHE_CONFIGFILE="apache-site.conf.in"
729     fi
730     # Generate and install Apache site-available file and log dir.
731     generate_config_file $APACHE_CONFIGFILE \
732         "/etc/apache2/sites-available/$name.conf"
733     mkdir "/var/log/koha/$name"
734     chown "$username:$username" "/var/log/koha/$name"
735
736
737     # Generate and install main Koha config file.
738     generate_config_file koha-conf-site.xml.in \
739         "/etc/koha/sites/$name/koha-conf.xml"
740
741     # Generate and install the log4perl config file.
742     generate_config_file log4perl-site.conf.in \
743         "/etc/koha/sites/$name/log4perl.conf"
744
745     # Generate and install Zebra config files.
746     generate_config_file zebra-biblios-dom-site.cfg.in \
747         "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
748     generate_config_file zebra-authorities-dom-site.cfg.in \
749         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
750     generate_config_file zebra.passwd.in \
751         "/etc/koha/sites/$name/zebra.passwd"
752
753     # Create a GPG-encrypted file for requesting a DB to be set up.
754     if [ "$op" = request ]
755     then
756         touch "$name-db-request.txt"
757         chmod 0600 "$name-db-request.txt"
758         cat > "$name-db-request.txt" << eof
759 Please create a MySQL database and user on $mysqlhost as follows:
760
761 database name: $mysqldb
762 database user: $mysqluser
763      password: $mysqlpwd
764
765 Thank you.
766 eof
767
768         echo "See $name-db-request.txt for database creation request."
769         echo "Please forward it to the right person, and then run"
770         echo "$0 --populate-db $name"
771         echo "Thanks."
772     fi
773 fi
774
775
776 if [ "$op" = create ] || [ "$op" = populate ]
777 then
778     # Re-fetch the passwords from the config we've generated, allows it
779     # to be different from what we set, in case the user had to change
780     # something.
781     mysqluser=$(getinstancemysqluser $name)
782     mysqldb=$(getinstancemysqldatabase $name)
783     # Use the default database content if that exists.
784     if [ -e "$DEFAULTSQL" ]
785     then
786         # Populate the database with default content.
787         zcat -f "$DEFAULTSQL" |
788         sed "s/__KOHASITE__/koha_$name/g" |
789         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
790
791
792         # Change the default user's password.
793         staffpass="$(pwgen 12 1)"
794         staffdigest=$(echo -n "$staffpass" |
795                       perl -e '
796                             use Digest::MD5 qw(md5_base64); 
797                             while (<>) { print md5_base64($_), "\n"; }')
798         mysql --host="$mysqlhost" --user="$mysqluser" \
799 --password="$mysqlpwd" <<eof
800 USE \`$mysqldb\`;
801 UPDATE borrowers 
802 SET password = '$staffdigest' 
803 WHERE borrowernumber = $ADMINUSER;
804 eof
805         #`
806         echo "staff user password is '$staffpass' but keep that secret"
807
808         # Upgrade the database schema, just in case the dump was from an 
809         # old version.
810         koha-upgrade-schema "$name"
811     else
812         echo "Koha instance is empty, no staff user created."
813     fi
814 fi
815
816
817 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
818 then
819     # Reconfigure Apache.
820     if ! {
821         a2ensite "$name" > /dev/null 2>&1 ||
822             a2ensite "${name}.conf" > /dev/null 2>&1
823     }; then
824         echo "Warning: problem enabling $name in Apache" >&2
825     fi
826     service apache2 restart
827
828     # Start Zebra.
829     koha-zebra --start "$name"
830
831     # Start worker
832     koha-worker --start "$name"
833
834     if [ "$USE_INDEXER_DAEMON" = "yes" ]; then
835         # Start Indexer daemon
836         koha-indexer --start "$name"
837     fi
838
839     if [ "$CLO_LETSENCRYPT" = "yes" ]; then
840         # Get letsencrypt certificates
841         letsencrypt_instance
842     fi
843
844     chown $username:$username /var/log/koha/$name/*.log
845 fi
846
847
848 if [ "$op" = request ]
849 then
850     koha-disable "$name"
851 fi
852
853 echo <<eoh
854
855 Email for this instance is disabled. When you're ready to enable it, use:
856 koha-email-enable $name
857 eoh