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