Bug 12393 - Depend on SSL module for LWP, for OverDrive
[koha_fer] / debian / scripts / koha-create
index 7f05eb0..841ef86 100755 (executable)
 
 set -e
 
-usage="Usage: $0 [--create-db|--request-db|--populate-db|--use-db] \
-    [--marcflavor marc21|normarc|unimarc] \
-    [--zebralang en|es|fr|nb|ru|uk] \
-    [--auth-idx dom|grs1] [--biblio-idx dom|grs1] \
-    [--use-memcached] \
-    [--memcached-servers server:port] [--memcached-prefix prefix] \
-    [--defaultsql /path/to/some.sql] \
-    [--configfile /path/to/config] [--passwdfile /path/to/passwd] \
-    [--database database] [--adminuser n] instancename"
-
-die() {
-    echo "$@" 1>&2
+# include helper functions
+if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
+    . "/usr/share/koha/bin/koha-functions.sh"
+else
+    echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
     exit 1
+fi
+
+usage()
+{
+    local scriptname=$0
+    cat <<EOF
+
+Creates new Koha instances.
+
+Usage:
+
+  $scriptname [DB usage mode] [options] instancename
+
+DB usage mode:
+  --create-db               Create a new database on localhost. (default).
+  --request-db              Creates a instancename-db-request.txt file where
+                            you adjust your DB settings and re-run with --populate-db.
+  --populate-db             Finish the installation you started with --request-db after
+                            you adjusted the instancename-db-request.txt file.
+  --use-db                  Use this option if you already created and populated your DB.
+
+Options:
+  --marcflavor flavor       Set the MARC flavor. Valid values are marc21 (default),
+                            normarc and unimarc.
+  --zebralang lang          Choose the primary language for Zebra indexing. Valid
+                            values are en (default), es, fr, nb, ru and uk.
+  --auth-idx idx_mode       Set the indexing mode for authority records. Valid
+                            values are dom (default) and grs1.
+  --biblio-idx idx_mode     Set the indexing mode for bibliographic records.
+                            Valid values are dom (default) and grs1.
+  --use-memcached           Set the instance to make use of memcache.
+  --memcached-servers str   Set a comma-separated list of host:port memcached servers.
+  --memcached-prefix str    Set the desired prefix for the instance memcached namespace.
+  --defaultsql some.sql     Specify a default SQL file to be loaded on the DB.
+  --configfile cfg_file     Specify an alternate config file for reading default values.
+  --passwdfile passwd       Specify an alternate passwd file.
+  --database dbname         Enforce the use of the specified DB name (64 char limit)
+  --adminuser n             Explicit the admin user ID in the DB. Relevant in
+                            conjunction with --defaultsql and --populate-db.
+  --help,-h                 Show this help.
+
+Note: the instance name cannot be longer that 11 chars.
+
+EOF
 }
 
 # UPPER CASE VARIABLES - from configfile or default value
@@ -89,6 +126,63 @@ getinstancemysqldatabase() {
     xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
 }
 
+check_apache_config()
+{
+
+    # Check that mpm_itk is installed and enabled
+    if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_itk'; then
+        # Check Apache version
+        APACHE_DISABLE_MPM_MSG=""
+        if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
+            # mpm_event or mpm_worker need to be disabled first. mpm_itk depends
+            # on mpm_prefork, which is enabled if needed. See
+            # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=734865
+            if /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_event'; then
+                APACHE_DISABLE_MPM_MSG=" sudo a2dismod mpm_event ;"
+            elif /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_worker'; then
+                APACHE_DISABLE_MPM_MSG=" sudo a2dismod mpm_worker ;"
+            # else mpm_prefork: a2enmod mpm_itk works
+            fi
+        # else Apache 2.2: a2enmod mpm_itk works
+        fi
+
+        cat 1>&2  <<EOM
+
+Koha requires mpm_itk to be enabled within Apache in order to run.
+Typically this can be enabled with:
+
+   $APACHE_DISABLE_MPM_MSG sudo a2enmod mpm_itk
+EOM
+
+        die
+    fi
+
+    # Check that mod_rewrite is installed and enabled.
+    if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'rewrite_module'; then
+        cat 1>&2  <<EOM
+
+Koha requires mod_rewrite to be enabled within Apache in order to run.
+Typically this can be enabled with:
+
+    sudo a2enmod rewrite
+EOM
+        die
+    fi
+
+    # Check that the CGI module is installed and enabled
+    # (Apache 2.4 may not have it by default.)
+    if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'cgi_module'; then
+        cat 1>&2 << EOM
+Koha requires mod_cgi to be enabled within Apache in order to run.
+Typically this can be enabled with:
+
+    sudo a2enmod cgi
+EOM
+        die
+    fi
+
+}
+
 set_biblios_indexing_mode()
 {
     local indexing_mode=$1
@@ -220,9 +314,9 @@ then
     . /etc/koha/koha-sites.conf
 fi
 
-[ $# -ge 2 ] && [ $# -le 16 ] || die $usage
+[ $# -ge 1 ] && [ $# -le 16 ] || ( usage ; die "Error: wrong parameters" )
 
-TEMP=`getopt -o crpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,use-memcached,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser:,memcached-servers:,memcached-prefix:, \
+TEMP=`getopt -o chrpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,use-memcached,help,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser:,memcached-servers:,memcached-prefix:, \
      -n "$0" -- "$@"`
 
 # Note the quotes around `$TEMP': they are essential!
@@ -257,11 +351,11 @@ while true ; do
             CLO_MEMCACHED_PREFIX="$2" ; shift 2;;
         -m|--marcflavor)
             CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
-    -l|--zebralang)
+        -l|--zebralang)
             CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
-    --auth-idx)
+        --auth-idx)
             CLO_AUTHORITIES_INDEXING_MODE="$2" ; shift 2 ;;
-    --biblio-idx)
+        --biblio-idx)
             CLO_BIBLIOS_INDEXING_MODE="$2" ; shift 2 ;;
         -d|--defaultsql)
             CLO_DEFAULTSQL="$2" ; shift 2 ;;
@@ -273,6 +367,8 @@ while true ; do
             CLO_DATABASE="$2" ; shift 2 ;;
         -a|--adminuser)
             CLO_ADMINUSER="$2" ; shift 2 ;;
+        -h|--help)
+            usage ; exit 0 ;;
         --)
             shift ; break ;;
         *)
@@ -337,18 +433,27 @@ elif [ "$CLO_MEMCACHED_SERVERS" != "" ] || \
 
 Error: you provided memcached configuration switches but memcached is not enabled.
 Please set USE_MEMCACHED="yes" on /etc/koha/koha-sites.conf or use the
---use-memcached optio switch to enable it.
+--use-memcached option switch to enable it.
 
 EOF`
 
-    echo $usage
-    die $MSG
+    usage ; die $MSG
 else
     # Unset memcached-related variables
     MEMCACHED_SERVERS=""
     MEMCACHED_PREFIX=""
 fi
 
+# Are we root? If not, the mod_rewrite check will fail and be confusing, so
+# we look into this first.
+if [[ $UID -ne 0 ]]
+then
+    die "This script must be run with root privileges."
+fi
+
+# Check everything is ok with Apache, die otherwise
+check_apache_config
+
 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
 
@@ -438,7 +543,7 @@ eof
 
     # Generate and install Apache site-available file and log dir.
     generate_config_file apache-site.conf.in \
-        "/etc/apache2/sites-available/$name"
+        "/etc/apache2/sites-available/$name.conf"
     mkdir "/var/log/koha/$name"
     chown "$username:$username" "/var/log/koha/$name"
 
@@ -527,7 +632,12 @@ fi
 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
 then
     # Reconfigure Apache.
-    a2ensite "$name"
+    if ! {
+        a2ensite "$name" > /dev/null 2>&1 ||
+            a2ensite "${name}.conf" > /dev/null 2>&1
+    }; then
+        echo "Warning: problem enabling $name in Apache" >&2
+    fi
     service apache2 restart
 
     # Start Zebra.