In order directive search

This commit is contained in:
James Kasten
2015-07-09 16:15:45 -07:00
parent 9d17ac7347
commit ac32e54798
2 changed files with 30 additions and 35 deletions
+5 -6
View File
@@ -65,7 +65,6 @@ class ApacheConfigurator(augeas_configurator.AugeasConfigurator):
is not ready to handle very complex configurations.
.. todo:: Add support for config file variables Define rootDir /var/www/
.. todo:: Add proper support for module configuration
The API of this class will change in the coming weeks as the exact
needs of clients are clarified with the new and developing protocol.
@@ -198,14 +197,15 @@ class ApacheConfigurator(augeas_configurator.AugeasConfigurator):
logger.info("Deploying Certificate to VirtualHost %s", vhost.filep)
self.aug.set(path["cert_path"][0], cert_path)
self.aug.set(path["cert_key"][0], key_path)
# Assign the final directives; order is maintained in find_dir
self.aug.set(path["cert_path"][-1], cert_path)
self.aug.set(path["cert_key"][-1], key_path)
if chain_path is not None:
if not path["chain_path"]:
self.parser.add_dir(
vhost.path, "SSLCertificateChainFile", chain_path)
else:
self.aug.set(path["chain_path"][0], chain_path)
self.aug.set(path["chain_path"][-1], chain_path)
self.save_notes += ("Changed vhost at %s with addresses of %s\n" %
(vhost.filep,
@@ -430,8 +430,7 @@ class ApacheConfigurator(augeas_configurator.AugeasConfigurator):
# Check for Listen 443
# Note: This could be made to also look for ip:443 combo
# TODO: Need to search only open directives and IfMod mod_ssl.c
if len(self.parser.find_dir(parser.case_i("Listen"), "443")) == 0:
if not self.parser.find_dir(parser.case_i("Listen"), "443"):
logger.debug("No Listen 443 directive found. Setting the "
"Apache Server to Listen on port 443")
path = self.parser.add_dir_to_ifmodssl(
+25 -29
View File
@@ -15,12 +15,11 @@ logger = logging.getLogger(__name__)
class ApacheParser(object):
"""Class handles the fine details of parsing the Apache Configuration.
.. todo:: Make parsing general... remove sites-available etc...
:ivar str root: Normalized absolute path to the server root
directory. Without trailing slash.
.. todo:: Handle UnDefine Directive
.. todo:: Handle Define directive for parameters within find_dirs
"""
def __init__(self, aug, root, ssl_options, ctl):
# This uses the binary, so it can be done first.
@@ -75,7 +74,7 @@ class ApacheParser(object):
..todo:: Also use apache2ctl -V for compiled parameters
"""
stdout = self._get_runtime_info(ctl)
stdout = self._get_runtime_cfg(ctl)
variables = dict()
matches = re.compile(r"Define: ([^ \n]*)").findall(stdout)
@@ -89,7 +88,8 @@ class ApacheParser(object):
"Error parsing Apache runtime variables")
parts = match.partition("=")
variables[parts[0]] = parts[2]
print variables
return variables
def _get_runtime_cfg(self, ctl):
"""Get runtime configuration info.
@@ -212,7 +212,6 @@ class ApacheParser(object):
Recursively searches through config files to find directives
Directives should be in the form of a case insensitive regex currently
.. todo:: Add order to directives returned. Last directive comes last..
.. todo:: arg should probably be a list
.. todo:: Check //* notation for including directories not intended
to be included.
@@ -237,8 +236,6 @@ class ApacheParser(object):
if not start:
start = get_aug_path(self.loc["root"])
# Debug code
# print "find_dir:", directive, "arg:", arg, " | Looking in:", start
# No regexp code
# if arg is None:
# matches = self.aug.match(start +
@@ -251,34 +248,33 @@ class ApacheParser(object):
# includes = self.aug.match(start +
# "//* [self::directive='Include']/* [label()='arg']")
if arg is None:
matches = self.aug.match(("%s//*[self::directive=~regexp('%s')]/arg"
% (start, directive)))
else:
matches = self.aug.match(("%s//*[self::directive=~regexp('%s')]/*"
"[self::arg=~regexp('%s')]" %
(start, directive, arg)))
regex = "(%s)|(%s)|(%s)" % (directive,
case_i("Include"),
case_i("IncludeOptional"))
matches = self.aug.match(
"%s//*[self::directive=~regexp('%s')]" % (start, regex))
matches = self._exclude_dirs(matches)
incl_regex = "(%s)|(%s)" % (case_i("Include"),
case_i("IncludeOptional"))
includes = self.aug.match(("%s//* [self::directive=~regexp('%s')]/* "
"[label()='arg']" % (start, incl_regex)))
if arg is None:
arg_suffix = "/arg"
else:
arg_suffix = "/*[self::arg=~regexp('%s')]" % arg
includes = self._exclude_dirs(includes)
ordered_matches = []
# for inc in includes:
# print inc, self.aug.get(inc)
for match in matches:
dir = self.aug.get(match).lower()
if dir == "include" or dir == "includeoptional":
# start[6:] to strip off /files
ordered_matches.extend(self.find_dir(
directive, arg, self._get_include_path(
strip_dir(start[6:]), self.aug.get(match + "/arg"))))
else:
ordered_matches.extend(self.aug.match(match + arg_suffix))
for include in includes:
# start[6:] to strip off /files
matches.extend(self.find_dir(
directive, arg, self._get_include_path(
strip_dir(start[6:]), self.aug.get(include))))
return matches
return ordered_matches
def _exclude_dirs(self, matches):
"""Exclude directives that are not loaded into the configuration."""