From 42bc31b38687ff8c41319d0e9384e628adbb0a21 Mon Sep 17 00:00:00 2001 From: Yves-Gwenael Bourhis Date: Tue, 31 Mar 2015 18:20:20 +0200 Subject: [PATCH] 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 --- .../commands/apache_vhost.conf.template | 8 +++- .../management/commands/make_web_conf.py | 43 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/openstack_dashboard/management/commands/apache_vhost.conf.template b/openstack_dashboard/management/commands/apache_vhost.conf.template index 5db9318527..06fe27fd6d 100644 --- a/openstack_dashboard/management/commands/apache_vhost.conf.template +++ b/openstack_dashboard/management/commands/apache_vhost.conf.template @@ -24,12 +24,16 @@ WSGIPassAuthorization On WSGIScriptAlias / {{ WSGI_FILE }} - +{% if APACHE2_VERSION >= 2.4 %} + + Require all granted + +{% else %} Order Allow,Deny Allow from all - +{% endif %} Alias /static {{ STATIC_PATH }} SetHandler None diff --git a/openstack_dashboard/management/commands/make_web_conf.py b/openstack_dashboard/management/commands/make_web_conf.py index 38d1de887c..9cbfe61d4b 100644 --- a/openstack_dashboard/management/commands/make_web_conf.py +++ b/openstack_dashboard/management/commands/make_web_conf.py @@ -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[\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']