diff options
author | 2019-03-26 23:27:31 +0100 | |
---|---|---|
committer | 2019-04-02 10:38:30 +0200 | |
commit | 74a5571d7fdae809663b0c16db2edb6aa9f8d42f (patch) | |
tree | f0398aaa6656cec1273834de2078c97e29fab17f | |
parent | apache2ctl: Don't show grep usage message with leading "-" options. (diff) | |
download | apache-74a5571d7fdae809663b0c16db2edb6aa9f8d42f.tar.gz apache-74a5571d7fdae809663b0c16db2edb6aa9f8d42f.tar.bz2 apache-74a5571d7fdae809663b0c16db2edb6aa9f8d42f.zip |
apache2ctl: Add suport for systemd
Signed-off-by: Lars Wendler <polynomial-c@gentoo.org>
-rw-r--r-- | 2.4/scripts/apache2ctl | 182 |
1 files changed, 165 insertions, 17 deletions
diff --git a/2.4/scripts/apache2ctl b/2.4/scripts/apache2ctl index 8031b8b..392ac4c 100644 --- a/2.4/scripts/apache2ctl +++ b/2.4/scripts/apache2ctl @@ -6,23 +6,171 @@ APACHE_RC_CONF="/etc/conf.d/apache2" # List of init script verbs that should be passed forward RC_VERBS="start stop restart checkconfd configtest modules virtualhosts configdump fullstatus graceful gracefulstop reload" -load_rc_config() { - [ -f "${APACHE_RC_CONF}" ] || return 1 - if ! grep -q '^[[:space:]]*APACHE2_OPTS' ${APACHE_RC_CONF} ; then - echo "Cannot find APACHE2_OPTS in ${APACHE_RC_CONF}" - exit 1 - fi - . ${APACHE_RC_CONF} - export APACHE2_OPTS - export SERVERROOT="${SERVERROOT:-/usr/@LIBDIR@/apache2}" - export CONFIGFILE="${CONFIGFILE:-/etc/apache2/httpd.conf}" + +function is_systemd() { + [ $(ps --no-headers -o comm 1) == "systemd" ] && return 0 + return 1 +} + +function load_rc_config() { + [ -f "${APACHE_RC_CONF}" ] || return 1 + if ! grep -q '^[[:space:]]*APACHE2_OPTS' ${APACHE_RC_CONF} ; then + echo "Cannot find APACHE2_OPTS in ${APACHE_RC_CONF}" + exit 1 + fi + . ${APACHE_RC_CONF} + export APACHE2_OPTS + export SERVERROOT="${SERVERROOT:-/usr/lib64/apache2}" + export CONFIGFILE="${CONFIGFILE:-/etc/apache2/httpd.conf}" +} + +# Basically the code from '/etc/init.d/apache2::reload()', updated to run without open-rc +function reload() { + RELOAD_TYPE="${RELOAD_TYPE:-graceful}" + + if [ "${RELOAD_TYPE}" = "restart" ]; then + ${APACHE2} ${APACHE2_OPTS} -k restart + elif [ "${RELOAD_TYPE}" = "graceful" ]; then + ${APACHE2} ${APACHE2_OPTS} -k graceful + else + echo "${RELOAD_TYPE} is not a valid RELOAD_TYPE. Please edit /etc/conf.d/apache2" + fi +} + +# Basically the code from '/etc/init.d/apache2::fullstatus()', updated to run without open-rc +function fullstatus() { + LYNX="${LYNX:-lynx -dump}" + STATUSURL="${STATUSURL:-http://localhost/server-status}" + + if ! command -v $(set -- ${LYNX}; echo $1) 2>&1 >/dev/null; then + echo 'lynx not found! you need to emerge www-client/lynx' + else + ${LYNX} ${STATUSURL} + fi + return $? } -# If first parameter is a verb defined in $RC_VERBS, pass the command to init script. -# In other cases, compile command line and run the command on apache binary. -if echo "${RC_VERBS}" | grep -q -- "${1}" ; then - exec /etc/init.d/apache2 "${@}" -else - load_rc_config || exit 1 - ${APACHE2} ${APACHE2_OPTS} -d ${SERVERROOT} -f ${CONFIGFILE} "${@}" +# Basically the code from '/etc/init.d/apache2::checkconfd()', updated to run without open-rc +function checkconfd() { + if [ ! -d ${SERVERROOT} ]; then + echo "SERVERROOT does not exist: ${SERVERROOT}" + return 1 + fi +} + +# Basically the code from '/etc/init.d/apache2::checkconfig()', updated to run without open-rc +function configtest() { + checkconfd || return 1 + + OUTPUT=$( ${APACHE2} ${APACHE2_OPTS} -t 2>&1 ) + ret=$? + if [ $ret -ne 0 ]; then + echo "apache2 has detected an error in your setup:" + printf "%s\n" "${OUTPUT}" + fi + + return $ret +} + +# Basically the code from '/etc/init.d/apache2::configdump()', updated to run without open-rc +function configdump() { + INFOURL="${INFOURL:-http://localhost/server-info}" + + if ! command -v $(set -- ${LYNX}; echo $1) 2>&1 >/dev/null; then + echo "lynx not found! you need to emerge www-client/lynx" + else + echo "${APACHE2} started with '${APACHE2_OPTS}'" + for i in config server list; do + ${LYNX} "${INFOURL}/?${i}" | sed '/Apache Server Information/d;/^[[:space:]]\+[_]\+$/Q' + done + fi +} + + +# If systemd IS NOT detected, run the legacy apache2ctl code +if ! is_systemd; then + # If first parameter is a verb defined in $RC_VERBS, pass the command to init script. + # In other cases, compile command line and run the command on apache binary. + if echo "${RC_VERBS}" | grep -q -- "${1}" ; then + exec /etc/init.d/apache2 "${@}" + else + load_rc_config || exit 1 + ${APACHE2} ${APACHE2_OPTS} -d ${SERVERROOT} -f ${CONFIGFILE} "${@}" + fi + exit 0 fi + +# If systemd IS detected, load the config and parse the argument +load_rc_config || exit 1 + +# Append the server root and configuration file parameters to the +# user's APACHE2_OPTS. +APACHE2_OPTS="${APACHE2_OPTS} -d ${SERVERROOT}" +APACHE2_OPTS="${APACHE2_OPTS} -f ${CONFIGFILE}" + +case $1 in +# Original apachectl options +# See: https://httpd.apache.org/docs/2.4/programs/apachectl.html +start|stop|restart|status) + /bin/systemctl $1 apache2.service + ERROR=$? + ;; + +reload) + reload + ERROR=$? + ;; + +fullstatus) + fullstatus + ERROR=$? + ;; + +graceful) + configtest || exit 1 + /bin/systemctl reload apache2.service + ERROR=$? + ;; + +gracefulstop|graceful-stop) + configtest || exit 1 + /bin/systemctl stop apache2.service + ERROR=$? + ;; + +configtest) + configtest + ERROR=$? + ;; + +checkconfd) + checkconfd + ERROR=$? + ;; + +configdump) + configtest || exit 1 + configdump + ERROR=$? + ;; + +virtualhosts) + configtest || exit 1 + ${APACHE2} ${APACHE2_OPTS} -S + ERROR=$? + ;; + +modules) + configtest || exit 1 + ${APACHE2} ${APACHE2_OPTS} -M 2>&1 + ERROR=$? + ;; + +# For all other options fall back to the legacy way of handling them +*) + ${APACHE2} ${APACHE2_OPTS} "${@}" + ERROR=$? + ;; +esac + +exit $ERROR |