Detect apache version

We try to detect apache's version.
Some distributions do not allow launching apache commands (even for detection)
if not root, so we gracefully fall back on 2.4 version configuration file
We alse add an --apache-version option to force the version in case
detection would be wrong.

Closes Bug: #1438773

Change-Id: I7d28c319601cf3919068be4ac52dd10f58a82557
This commit is contained in:
Yves-Gwenael Bourhis 2015-03-31 18:20:20 +02:00
parent c8f4f5360f
commit 42bc31b386
2 changed files with 49 additions and 2 deletions

View File

@ -24,12 +24,16 @@
WSGIPassAuthorization On
WSGIScriptAlias / {{ WSGI_FILE }}
{% if APACHE2_VERSION >= 2.4 %}
<Location "/">
Require all granted
</Location>
{% else %}
<Location "/">
Order Allow,Deny
Allow from all
</Location>
{% endif %}
Alias /static {{ STATIC_PATH }}
<Location "/static">
SetHandler None

View File

@ -15,7 +15,9 @@ from __future__ import print_function
from optparse import make_option # noqa
import os
import re
import socket
import subprocess
import sys
import warnings
@ -33,6 +35,14 @@ CURDIR = os.path.realpath(os.path.dirname(__file__))
PROJECT_PATH = os.path.realpath(os.path.join(CURDIR, '../..'))
STATIC_PATH = os.path.realpath(os.path.join(PROJECT_PATH, '../static'))
# Known apache regular expression to retrieve it's version
APACHE_VERSION_REG = r'Apache/(?P<version>[\d.]*)'
# Known apache commands to retrieve it's version
APACHE2_VERSION_CMDS = (
(('/usr/sbin/apache2ctl', '-V'), APACHE_VERSION_REG),
(('/usr/sbin/apache2', '-v'), APACHE_VERSION_REG),
)
# Known apache log directory locations
APACHE_LOG_DIRS = (
'/var/log/httpd', # RHEL / Red Hat / CentOS / Fedora Linux
@ -94,6 +104,29 @@ if virtualenv:
if os.path.exists(activate_this):
context['ACTIVATE_THIS'] = activate_this
# Try to detect apache's version
# We fallback on 2.4.
context['APACHE2_VERSION'] = 2.4
APACHE2_VERSION = None
for cmd in APACHE2_VERSION_CMDS:
if os.path.exists(cmd[0][0]):
try:
reg = re.compile(cmd[1])
res = reg.search(
subprocess.check_output(cmd[0], stderr=subprocess.STDOUT))
if res:
APACHE2_VERSION = res.group('version')
break
except subprocess.CalledProcessError:
pass
if APACHE2_VERSION:
ver_nums = APACHE2_VERSION.split('.')
if len(ver_nums) >= 2:
try:
context['APACHE2_VERSION'] = float('.'.join(ver_nums[:2]))
except ValueError:
pass
def find_apache_log_dir():
for log_dir in APACHE_LOG_DIRS:
@ -190,6 +223,14 @@ location you desire, e.g.::
"the path to the SSLCertificateKeyFile "
"(default : %s)") % context['SSLKEY'],
metavar="SSLKEY"),
make_option("--apache-version",
dest="apache_version",
type="float",
help=("Use with the --apache option to define the apache "
"major (as a floating point number) version "
"(default : %s)."
% context['APACHE2_VERSION']),
metavar="APACHE_VERSION"),
make_option("-w", "--wsgi",
default=False, action="store_true", dest="wsgi",
help="generate the horizon.wsgi file"),
@ -213,6 +254,8 @@ location you desire, e.g.::
context['SSLCERT'] = options['sslcert']
if options.get('sslkey'):
context['SSLKEY'] = options['sslkey']
if options.get('apache_version'):
context['APACHE2_VERSION'] = options['apache_version']
if options.get('namedhost'):
context['NAMEDHOST'] = context['VHOSTNAME']