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