Use ConfigOpts.find_file() to locate policy.json
Use cfg's new helper method to find policy config file. The basic behavior is "look alongside the config file" with a fall back to the standard default config paths. Change-Id: Ic2a7187378eab9c9066118132465280993778a5e
This commit is contained in:
parent
8dc3baa45a
commit
fab58441a2
@ -64,28 +64,6 @@ def import_object(import_str, *args, **kw):
|
|||||||
return cls(*args, **kw)
|
return cls(*args, **kw)
|
||||||
|
|
||||||
|
|
||||||
def find_config(config_path):
|
|
||||||
"""Find a configuration file using the given hint.
|
|
||||||
|
|
||||||
:param config_path: Full or relative path to the config.
|
|
||||||
:returns: Full path of the config, if it exists.
|
|
||||||
|
|
||||||
"""
|
|
||||||
possible_locations = [
|
|
||||||
config_path,
|
|
||||||
os.path.join('etc', 'keystone', config_path),
|
|
||||||
os.path.join('etc', config_path),
|
|
||||||
os.path.join(config_path),
|
|
||||||
'/etc/keystone/%s' % config_path,
|
|
||||||
]
|
|
||||||
|
|
||||||
for path in possible_locations:
|
|
||||||
if os.path.exists(path):
|
|
||||||
return os.path.abspath(path)
|
|
||||||
|
|
||||||
raise Exception('Config not found: %s', os.path.abspath(config_path))
|
|
||||||
|
|
||||||
|
|
||||||
def read_cached_file(filename, cache_info, reload_func=None):
|
def read_cached_file(filename, cache_info, reload_func=None):
|
||||||
"""Read from a file if it has been modified.
|
"""Read from a file if it has been modified.
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||||
|
|
||||||
# Copyright 2011 Red Hat, Inc.
|
# Copyright 2012 Red Hat, Inc.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
# not use this file except in compliance with the License. You may obtain
|
# not use this file except in compliance with the License. You may obtain
|
||||||
@ -325,6 +325,52 @@ class ConfigFileValueError(Error):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _get_config_dirs(project=None):
|
||||||
|
"""Return a list of directors where config files may be located.
|
||||||
|
|
||||||
|
:param project: an optional project name
|
||||||
|
|
||||||
|
If a project is specified, following directories are returned::
|
||||||
|
|
||||||
|
~/.${project}/
|
||||||
|
~/
|
||||||
|
/etc/${project}/
|
||||||
|
/etc/
|
||||||
|
|
||||||
|
Otherwise, these directories::
|
||||||
|
|
||||||
|
~/
|
||||||
|
/etc/
|
||||||
|
"""
|
||||||
|
fix_path = lambda p: os.path.abspath(os.path.expanduser(p))
|
||||||
|
|
||||||
|
cfg_dirs = [
|
||||||
|
fix_path(os.path.join('~', '.' + project)) if project else None,
|
||||||
|
fix_path('~'),
|
||||||
|
os.path.join('/etc', project) if project else None,
|
||||||
|
'/etc'
|
||||||
|
]
|
||||||
|
|
||||||
|
return filter(bool, cfg_dirs)
|
||||||
|
|
||||||
|
|
||||||
|
def _search_dirs(dirs, basename, extension=""):
|
||||||
|
"""Search a list of directories for a given filename.
|
||||||
|
|
||||||
|
Iterator over the supplied directories, returning the first file
|
||||||
|
found with the supplied name and extension.
|
||||||
|
|
||||||
|
:param dirs: a list of directories
|
||||||
|
:param basename: the filename, e.g. 'glance-api'
|
||||||
|
:param extension: the file extension, e.g. '.conf'
|
||||||
|
:returns: the path to a matching file, or None
|
||||||
|
"""
|
||||||
|
for d in dirs:
|
||||||
|
path = os.path.join(d, '%s%s' % (basename, extension))
|
||||||
|
if os.path.exists(path):
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
def find_config_files(project=None, prog=None, extension='.conf'):
|
def find_config_files(project=None, prog=None, extension='.conf'):
|
||||||
"""Return a list of default configuration files.
|
"""Return a list of default configuration files.
|
||||||
|
|
||||||
@ -353,26 +399,12 @@ def find_config_files(project=None, prog=None, extension='.conf'):
|
|||||||
if prog is None:
|
if prog is None:
|
||||||
prog = os.path.basename(sys.argv[0])
|
prog = os.path.basename(sys.argv[0])
|
||||||
|
|
||||||
fix_path = lambda p: os.path.abspath(os.path.expanduser(p))
|
cfg_dirs = _get_config_dirs(project)
|
||||||
|
|
||||||
cfg_dirs = [
|
|
||||||
fix_path(os.path.join('~', '.' + project)) if project else None,
|
|
||||||
fix_path('~'),
|
|
||||||
os.path.join('/etc', project) if project else None,
|
|
||||||
'/etc'
|
|
||||||
]
|
|
||||||
cfg_dirs = filter(bool, cfg_dirs)
|
|
||||||
|
|
||||||
def search_dirs(dirs, basename, extension):
|
|
||||||
for d in dirs:
|
|
||||||
path = os.path.join(d, '%s%s' % (basename, extension))
|
|
||||||
if os.path.exists(path):
|
|
||||||
return path
|
|
||||||
|
|
||||||
config_files = []
|
config_files = []
|
||||||
if project:
|
if project:
|
||||||
config_files.append(search_dirs(cfg_dirs, project, extension))
|
config_files.append(_search_dirs(cfg_dirs, project, extension))
|
||||||
config_files.append(search_dirs(cfg_dirs, prog, extension))
|
config_files.append(_search_dirs(cfg_dirs, prog, extension))
|
||||||
|
|
||||||
return filter(bool, config_files)
|
return filter(bool, config_files)
|
||||||
|
|
||||||
@ -1060,6 +1092,34 @@ class ConfigOpts(collections.Mapping):
|
|||||||
This it the default behaviour."""
|
This it the default behaviour."""
|
||||||
self._oparser.enable_interspersed_args()
|
self._oparser.enable_interspersed_args()
|
||||||
|
|
||||||
|
def find_file(self, name):
|
||||||
|
"""Locate a file located alongside the config files.
|
||||||
|
|
||||||
|
Search for a file with the supplied basename in the directories
|
||||||
|
which we have already loaded config files from and other known
|
||||||
|
configuration directories.
|
||||||
|
|
||||||
|
The directory, if any, supplied by the config_dir option is
|
||||||
|
searched first. Then the config_file option is iterated over
|
||||||
|
and each of the base directories of the config_files values
|
||||||
|
are searched. Failing both of these, the standard directories
|
||||||
|
searched by the module level find_config_files() function is
|
||||||
|
used. The first matching file is returned.
|
||||||
|
|
||||||
|
:param basename: the filename, e.g. 'policy.json'
|
||||||
|
:returns: the path to a matching file, or None
|
||||||
|
"""
|
||||||
|
dirs = []
|
||||||
|
if self.config_dir:
|
||||||
|
dirs.append(self.config_dir)
|
||||||
|
|
||||||
|
for cf in reversed(self.config_file):
|
||||||
|
dirs.append(os.path.dirname(cf))
|
||||||
|
|
||||||
|
dirs.extend(_get_config_dirs(self.project))
|
||||||
|
|
||||||
|
return _search_dirs(dirs, name)
|
||||||
|
|
||||||
def log_opt_values(self, logger, lvl):
|
def log_opt_values(self, logger, lvl):
|
||||||
"""Log the value of all registered opts.
|
"""Log the value of all registered opts.
|
||||||
|
|
||||||
|
@ -17,6 +17,8 @@
|
|||||||
|
|
||||||
"""Rules-based Policy Engine."""
|
"""Rules-based Policy Engine."""
|
||||||
|
|
||||||
|
import os.path
|
||||||
|
|
||||||
from keystone import config
|
from keystone import config
|
||||||
from keystone import exception
|
from keystone import exception
|
||||||
from keystone import policy
|
from keystone import policy
|
||||||
@ -59,7 +61,9 @@ def init():
|
|||||||
global _POLICY_PATH
|
global _POLICY_PATH
|
||||||
global _POLICY_CACHE
|
global _POLICY_CACHE
|
||||||
if not _POLICY_PATH:
|
if not _POLICY_PATH:
|
||||||
_POLICY_PATH = utils.find_config(CONF.policy_file)
|
_POLICY_PATH = CONF.policy_file
|
||||||
|
if not os.path.exists(_POLICY_PATH):
|
||||||
|
_POLICY_PATH = CONF.find_file(_POLICY_PATH)
|
||||||
utils.read_cached_file(_POLICY_PATH,
|
utils.read_cached_file(_POLICY_PATH,
|
||||||
_POLICY_CACHE,
|
_POLICY_CACHE,
|
||||||
reload_func=_set_brain)
|
reload_func=_set_brain)
|
||||||
|
Loading…
Reference in New Issue
Block a user