[Koha-patches] [PATCH 1/5] Bug 13791: Plack out-of-the-box support on packages

Tomas Cohen Arazi tomascohen at gmail.com
Fri Aug 14 21:14:15 CEST 2015


From: Tomás Cohen Arazi <tomascohen at theke.io>

This patch introduces a koha-plack script that controls running Plack
processes for each instance. They are run using 'starman', listening
on a Unix Domain Socket (UDS):

  /var/run/koha/<instancename>/plack.sock

The plack configuration file[1] is expected to be on:

  /etc/koha/plack.psgi

It also adds the following helper functions to koha-functions.sh:

 - is_plack_enabled
 - is_plack_running

Done:
- koha-plack script
- suitable psgi file

[1] Yeah, a single file. Because we will be relying on multiple mount
points for each "app" (i.e. 'opac' and 'intranet', and 'api' ;-) )
---
 debian/scripts/koha-functions.sh |  31 ++++
 debian/scripts/koha-plack        | 297 +++++++++++++++++++++++++++++++++++++++
 debian/templates/plack.psgi      |  71 ++++++++++
 3 files changed, 399 insertions(+)
 create mode 100755 debian/scripts/koha-plack
 create mode 100644 debian/templates/plack.psgi

diff --git a/debian/scripts/koha-functions.sh b/debian/scripts/koha-functions.sh
index 8090fc5..06ca274 100755
--- a/debian/scripts/koha-functions.sh
+++ b/debian/scripts/koha-functions.sh
@@ -121,6 +121,37 @@ is_indexer_running()
     fi
 }
 
+is_plack_enabled()
+{
+    local site=$1
+    local instancefile=$(get_apache_config_for $site)
+
+    if [ "$instancefile" = "" ]; then
+        return 1
+    fi
+
+    if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-opac-plack.conf' \
+            "$instancefile" && \
+       grep -q '^[[:space:]]*Include /etc/koha/apache-shared-intranet-plack.conf' \
+            "$instancefile" ; then
+        return 1
+    else
+        return 0
+    fi
+}
+
+is_plack_running()
+{
+    local instancename=$1
+
+    if start-stop-daemon --pidfile "/var/run/koha/${instancename}/plack.pid" \
+            --status ; then
+        return 0
+    else
+        return 1
+    fi
+}
+
 get_instances()
 {
     find /etc/koha/sites -mindepth 1 -maxdepth 1\
diff --git a/debian/scripts/koha-plack b/debian/scripts/koha-plack
new file mode 100755
index 0000000..c481ee6
--- /dev/null
+++ b/debian/scripts/koha-plack
@@ -0,0 +1,297 @@
+#!/bin/bash
+#
+# Copyright 2015 Theke Solutions
+#
+# This file is part of Koha.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+set -e
+
+. /lib/lsb/init-functions
+
+# Read configuration variable file if it is present
+[ -r /etc/default/koha-common ] && . /etc/default/koha-common
+
+# 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=$(basename $0)
+
+    cat <<EOF
+$scriptname
+
+This script lets you manage the plack daemons for your Koha instances.
+
+Usage:
+$scriptname --start|--stop|--restart [--quiet|-q] instancename1 [instancename2...]
+$scriptname --enable|--disable instancename1 [instancename2]
+$scriptname -h|--help
+
+    --start               Start the plack daemon for the specified instances
+    --stop                Stop the plack daemon for the specified instances
+    --restart             Restart the plack daemon for the specified instances
+    --enable              Enable plack for the specified instances
+    --disable             Disable plack for the specified instances
+    --quiet|-q            Make the script quiet about non existent instance names
+                          (useful for calling from another scripts).
+    --help|-h             Display this help message
+
+EOF
+}
+
+start_plack()
+{
+    local instancename=$1
+
+    local PIDFILE="/var/run/koha/${instancename}/plack.pid"
+    local PLACKSOCKET="/var/run/koha/${instancename}/plack.sock"
+    local PSGIFILE="/etc/koha/plack.psgi"
+    local NAME="${instancename}-koha-plack"
+
+    STARMANOPTS="-M FindBin --max-requests 50 --workers 2 \
+                 --user=${instancename}-koha --group ${instancename}-koha \
+                 --pid ${PIDFILE} \
+                 --daemonize \
+                 --access-log /var/log/koha/${instancename}/plack.log \
+                 --error-log /var/log/koha/${instancename}/plack-error.log \
+                 -E deployment --socket ${PLACKSOCKET} ${PSGIFILE}"
+
+    if ! is_plack_running ${instancename}; then
+        export KOHA_CONF="/etc/koha/sites/${instancename}/koha-conf.xml"
+
+        log_daemon_msg "Starting Plack daemon for ${instancename}"
+
+        if ${STARMAN} ${STARMANOPTS}; then
+            log_end_msg 0
+        else
+            log_end_msg 1
+        fi
+    else
+        log_daemon_msg "Error: Plack already running for ${instancename}"
+        log_end_msg 1
+    fi
+}
+
+stop_plack()
+{
+    local instancename=$1
+
+    local PIDFILE="/var/run/koha/${instancename}/plack.pid"
+
+    if is_plack_running ${instancename}; then
+
+        log_daemon_msg "Stopping Plack daemon for ${instancename}"
+
+        if start-stop-daemon --pidfile ${PIDFILE} --stop; then
+            log_end_msg 0
+        else
+            log_end_msg 1
+        fi
+    else
+        log_daemon_msg "Error: Plack not running for ${instancename}"
+        log_end_msg 1
+    fi
+}
+
+restart_plack()
+{
+    local instancename=$1
+
+    local PIDFILE="/var/run/koha/${instancename}/plack.pid"
+
+    if is_plack_running ${instancename}; then
+
+        log_daemon_msg "Restarting Plack daemon for ${instancename}"
+
+        if stop_plack $instancename && start_plack $instancename; then
+            log_end_msg 0
+        else
+            log_end_msg 1
+        fi
+    else
+        log_daemon_msg "Error: Plack not running for ${instancename}"
+        log_end_msg 1
+    fi
+}
+
+enable_plack()
+{
+    local instancename=$1
+    local instancefile=$(get_apache_config_for "$instancename")
+
+    if ! is_plack_enabled $instancename; then
+        # Uncomment the plack related lines for OPAC and intranet
+        sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:\1:' "$instancefile"
+        sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:\1:' "$instancefile"
+        [ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename}"
+        return 0
+    else
+        [ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename}"
+        return 1
+    fi
+}
+
+disable_plack()
+{
+    local instancename=$1
+    local instancefile=$(get_apache_config_for "$instancename")
+
+    if is_plack_enabled $instancename; then
+        # Comment out the plack related lines for OPAC and intranet
+        sed -i 's:^\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:#\1:' "$instancefile"
+        sed -i 's:^\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:#\1:' "$instancefile"
+        [ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename}"
+        return 0
+    else
+        [ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename}"
+        return 1
+    fi
+}
+
+check_env_and_warn()
+{
+    local apache_version_ok="no"
+    local required_modules="headers proxy_http"
+    local missing_modules=""
+
+    if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
+        apache_version_ok="yes"
+    fi
+
+    for module in ${required_modules}; do
+        if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q ${module}; then
+            missing_modules="${missing_modules}${module} "
+        fi
+    done
+
+    if [ "${apache_version_ok}" != "yes" ]; then
+        warn "WARNING: koha-plack requires Apache 2.4.x and you don't have that."
+    fi
+
+    if [ "${missing_modules}" != "" ]; then
+        cat 1>&2 <<EOM
+WARNING: koha-plack requires some Apache modules that you are missing.
+You can install them with:
+
+    sudo a2enmod ${missing_modules}
+
+EOM
+
+    fi
+}
+
+set_action()
+{
+    if [ "$op" = "" ]; then
+        op=$1
+    else
+        die "Error: only one action can be specified."
+    fi
+}
+
+STARMAN=$(which starman)
+op=""
+quiet="no"
+
+# Read command line parameters
+while [ $# -gt 0 ]; do
+
+    case "$1" in
+        -h|--help)
+            usage ; exit 0 ;;
+        -q|--quiet)
+            quiet="yes"
+            shift ;;
+        --start)
+            set_action "start"
+            shift ;;
+        --stop)
+            set_action "stop"
+            shift ;;
+        --restart)
+            set_action "restart"
+            shift ;;
+        --enable)
+            set_action "enable"
+            shift ;;
+        --disable)
+            set_action "disable"
+            shift ;;
+        -*)
+            die "Error: invalid option switch ($1)" ;;
+        *)
+            # We expect the remaining stuff are the instance names
+            break ;;
+    esac
+
+done
+
+if [ -z $PERL5LIB ]; then
+    PERL5LIB="/usr/share/koha/lib"
+fi
+
+export PERL5LIB
+
+[ "${quiet}" != "yes" ] && check_env_and_warn
+
+if [ $# -gt 0 ]; then
+    # We have at least one instance name
+    for name in "$@"; do
+
+        if is_instance $name; then
+
+            case $op in
+                "start")
+                    start_plack $name
+                    ;;
+                "stop")
+                    stop_plack $name
+                    ;;
+                "restart")
+                    restart_plack $name
+                    ;;
+                "enable")
+                    enable_plack $name
+                    ;;
+                "disable")
+                    disable_plack $name
+                    ;;
+                *)
+                    usage
+                    ;;
+            esac
+
+        else
+            if [ "$quiet" = "no" ]; then
+                log_daemon_msg "Error: Invalid instance name $name"
+                log_end_msg 1
+            fi
+        fi
+
+    done
+else
+    if [ "$quiet" = "no" ]; then
+        warn "Error: you must provide at least one instance name"
+    fi
+fi
+
+exit 0
diff --git a/debian/templates/plack.psgi b/debian/templates/plack.psgi
new file mode 100644
index 0000000..f6730de
--- /dev/null
+++ b/debian/templates/plack.psgi
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+use lib("/usr/share/koha/lib");
+use lib("/usr/share/koha/lib/installer");
+
+use Plack::Builder; 
+use Plack::App::CGIBin;
+use Plack::App::Directory;
+use Plack::App::URLMap;
+
+# Pre-load libraries
+use C4::Boolean;
+use C4::Branch;
+use C4::Category;
+use C4::Dates;
+use C4::Koha;
+use C4::Languages;
+use C4::Letters;
+use C4::Members;
+use C4::XSLT;
+use Koha::Database;
+
+use CGI qw(-utf8 ); # we will loose -utf8 under plack, otherwise
+{
+    no warnings 'redefine';
+    my $old_new = \&CGI::new;
+    *CGI::new = sub {
+        my $q = $old_new->( @_ );
+        $CGI::PARAM_UTF8 = 1;
+        C4::Context->clear_syspref_cache();
+        return $q;
+    };
+}
+
+my $intranet = Plack::App::CGIBin->new(
+    root => '/usr/share/koha/intranet/cgi-bin'
+);
+
+my $opac = Plack::App::CGIBin->new(
+    root => '/usr/share/koha/opac/cgi-bin/opac'
+);
+
+# my $api  = Plack::App::CGIBin->new(
+#     root => '/usr/share/koha/api/'
+# );
+
+builder {
+
+    enable "ReverseProxy";
+    enable "Plack::Middleware::Static";
+
+    mount '/opac'     => $opac;
+    mount '/intranet' => $intranet;
+    # mount '/api'       => $api;
+};
+
-- 
2.5.0



More information about the Koha-patches mailing list