Koha-clms/debian/scripts/koha-plack

421 lines
12 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#
# Copyright 2015 Theke Solutions
# Copyright 2016 Koha-Suomi
#
# 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 <https://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|--reload|--status|--development [--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
--reload Reload the plack daemon for the specified instances,
letting the busy workers finish processing their
requests before restarting them
--enable Enable plack for the specified instances
--disable Disable plack for the specified instances
--status Show the status of Plack for the specified instances
--debugger Enable running Plack in debug mode
--debugger-key Specify the key the IDE is expecting
--debugger-location Specify the host:port for your debugger tool (defaults
to localhost:9000)
--debugger-path Specify the path for the debugger library
--development|-dev Enable development environment
--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
if [ "$(koha_init_backend)" = "systemd" ]; then
koha_service_ctl start plack "$instancename"
else
_sysv_start_plack "$instancename"
fi
}
stop_plack()
{
local instancename=$1
if [ "$(koha_init_backend)" = "systemd" ]; then
koha_service_ctl stop plack "$instancename"
else
_sysv_stop_plack "$instancename"
fi
}
restart_plack()
{
local instancename=$1
if [ "$(koha_init_backend)" = "systemd" ]; then
koha_service_ctl restart plack "$instancename"
else
_sysv_restart_plack "$instancename"
fi
}
reload_plack()
{
local instancename=$1
if [ "$(koha_init_backend)" = "systemd" ]; then
koha_service_ctl reload plack "$instancename"
else
_sysv_reload_plack "$instancename"
fi
}
plack_status()
{
local name=$1
if [ "$(koha_init_backend)" = "systemd" ]; then
koha_service_ctl status plack "$name"
else
_sysv_plack_status "$name"
fi
}
enable_plack()
{
local instancename=$1
local instancefile=$(get_apache_config_for "$instancename")
alreadyopac=0
alreadyintra=0
failopac=0
failintra=0
if ! is_plack_enabled_opac $instancefile; then
# Uncomment the plack related lines for OPAC
sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:\1:' "$instancefile"
if ! is_plack_enabled_opac $instancefile; then
[ "${quiet}" != "yes" ] && warn "Plack not enabled for ${instancename} OPAC"
failopac=1
else
[ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename} OPAC"
fi
else
[ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename} OPAC"
alreadyopac=1
fi
if ! is_plack_enabled_intranet $instancefile; then
# Uncomment the plack related lines for intranet
sed -i 's:^\s*#\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:\1:' "$instancefile"
if ! is_plack_enabled_intranet $instancefile; then
[ "${quiet}" != "yes" ] && warn "Plack not enabled for ${instancename} Intranet"
failintra=1
else
[ "${quiet}" != "yes" ] && warn "Plack enabled for ${instancename} Intranet"
fi
else
[ "${quiet}" != "yes" ] && warn "Plack already enabled for ${instancename} Intranet"
alreadyintra=1
fi
# Under systemd the unit has to be enabled as well, whatever the Apache
# side looked like. Done after the config edits so a systemctl failure
# cannot leave them half-applied under set -e.
if [ "$(koha_init_backend)" = "systemd" ]; then
koha_service_ctl enable plack "$instancename" || \
warn "Could not enable koha-plack@${instancename}.service"
fi
# Fail if it was already plack enabled.
if [ $alreadyopac -eq 1 ] && [ $alreadyintra -eq 1 ] ; then
return 1
elif [ "$alreadyopac" != "$alreadyintra" ]; then
[ "${quiet}" != "yes" ] && warn "$instancename had a plack configuration error. Please confirm it is corrected."
fi
# Succeed if both or any plack were turned on.
if [ $failopac -eq 0 ] || [ $failintra -eq 0 ] ; then
return 0
else
return 1
fi
}
disable_plack()
{
local instancename=$1
local instancefile=$(get_apache_config_for "$instancename")
alreadyopac=0
alreadyintra=0
failopac=0
failintra=0
if is_plack_enabled_opac $instancefile ; then
# Comment the plack related lines for OPAC
sed -i 's:^\(\s*Include /etc/koha/apache-shared-opac-plack.conf\)$:#\1:' "$instancefile"
if is_plack_enabled_opac $instancefile ; then
[ "${quiet}" != "yes" ] && warn "Plack not disabled for ${instancename} OPAC"
failopac=1
else
[ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename} OPAC"
fi
else
[ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename} OPAC"
alreadyopac=1
fi
if is_plack_enabled_intranet $instancefile; then
# Comment the plack related lines for intranet
sed -i 's:^\(\s*Include /etc/koha/apache-shared-intranet-plack.conf\)$:#\1:' "$instancefile"
if is_plack_enabled_intranet $instancefile; then
[ "${quiet}" != "yes" ] && warn "Plack not disabled for ${instancename} Intranet"
failintra=1
else
[ "${quiet}" != "yes" ] && warn "Plack disabled for ${instancename} Intranet"
fi
else
[ "${quiet}" != "yes" ] && warn "Plack already disabled for ${instancename} Intranet"
alreadyintra=1
fi
# Under systemd the unit has to be disabled as well; done after the
# Apache edits for the same reason as in enable_plack.
if [ "$(koha_init_backend)" = "systemd" ]; then
koha_service_ctl disable plack "$instancename" || \
warn "Could not disable koha-plack@${instancename}.service"
fi
# Fail if it was already plack disabled.
if [ $alreadyopac -eq 1 ] && [ $alreadyintra -eq 1 ] ; then
return 1
elif [ "$alreadyopac" != "$alreadyintra" ]; then
[ "${quiet}" != "yes" ] && warn "$instancename had a plack configuration error. Please confirm it is corrected."
fi
# Succeed if both or any plack were turned off.
if [ $failopac -eq 0 ] || [ $failintra -eq 0 ] ; then
return 0
else
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
}
_do_instance() {
local name=$1
#NOTE: It's very important that PERL5LIB and KOHA_HOME are local
# By being local, we keep the PERL5LIB and KOHA_HOME specific
# to the instance that we're working on right now.
# If you disagree, please look at BZ 25778 and BZ 43159.
local PERL5LIB=$PERL5LIB
local KOHA_HOME=$KOHA_HOME
adjust_paths_git_install $name
PERL5LIB=$PERL5LIB:$KOHA_HOME/installer:$KOHA_HOME/lib/installer
if [ "$KOHA_BINDIR" = "misc" ]; then GIT_INSTALL=1; else GIT_INSTALL=""; fi
export PERL5LIB KOHA_HOME GIT_INSTALL
# If debugger is enabled, add the debugger lib path to PERL5LIB if appropriate
# FIXME: many of these variables should be set in a higher scope
if [ "$debugger" = "yes" ]; then
if [ "$debugger_path" != "" ]; then
PERL5LIB="${debugger_path}":$PERL5LIB
fi
export PERL5DB="BEGIN { require q(${debugger_path}/perl5db.pl) }"
export PERLDB_OPTS="RemotePort=${debugger_location} async=1 LogFile=/var/log/koha/${name}/plack-debug.log"
export DBGP_IDEKEY=${debugger_key}
export PLACK_DEBUG=1
export PERL5OPT="-d"
fi
case $op in
"start")
start_plack $name
;;
"stop")
stop_plack $name
;;
"restart")
restart_plack $name
;;
"reload")
reload_plack $name
;;
"enable")
enable_plack $name
;;
"disable")
disable_plack $name
;;
"status")
plack_status $name
;;
*)
usage
;;
esac
}
STARMAN=$(which starman)
op=""
quiet="no"
debugger="no"
debugger_key=""
debugger_location="localhost:9000"
debugger_path=""
# 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 ;;
--reload)
set_action "reload"
shift ;;
--enable)
set_action "enable"
shift ;;
--disable)
set_action "disable"
shift ;;
--status)
set_action "status"
shift ;;
--debugger)
debugger="yes"
shift ;;
--debugger-key)
debugger_key="$2"
shift 2 ;;
--debugger-location)
debugger_location="$2"
shift 2 ;;
--debugger-path)
debugger_path="$2"
shift 2 ;;
-dev|--development)
development_environment=1
shift ;;
-*)
die "Error: invalid option switch ($1)" ;;
*)
# We expect the remaining stuff are the instance names
break ;;
esac
done
[ "${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
_do_instance $name
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