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