Allow glance-cache-* find their config files

By default, glance-cache-cleaner looks for glance-cleaner.conf but
its actual config file is glance-cache.conf.

Change the find_config_file() argument to conf_name and make it an
optional argument to load_paste_app(). This allows us to specify
a different config file for the glance apps than their app name.

find_config_file() is also changed to raise an exception if no
config file is found.

Change-Id: Iec71218f5b2f0a7f057ea02f4549bd743892cc65
This commit is contained in:
Mark McLoughlin 2011-11-09 22:51:20 +00:00
parent ec99ac38ae
commit 1f979ca757
7 changed files with 64 additions and 44 deletions

View File

@ -70,7 +70,8 @@ if __name__ == '__main__':
(options, args) = config.parse_options(oparser)
try:
conf, app = config.load_paste_app('glance-cleaner', options, args)
conf, app = config.load_paste_app('glance-cleaner', options, args,
'glance-cache')
app.run()
except RuntimeError, e:
sys.exit("ERROR: %s" % e)

View File

@ -60,7 +60,8 @@ if __name__ == '__main__':
(options, args) = config.parse_options(oparser)
try:
conf, app = config.load_paste_app('glance-prefetcher', options, args)
conf, app = config.load_paste_app('glance-prefetcher', options, args,
'glance-cache')
app.run()
except RuntimeError, e:
sys.exit("ERROR: %s" % e)

View File

@ -62,7 +62,8 @@ if __name__ == '__main__':
(options, args) = config.parse_options(oparser)
try:
conf, app = config.load_paste_app('glance-pruner', options, args)
conf, app = config.load_paste_app('glance-pruner', options, args,
'glance-cache')
app.run()
except RuntimeError, e:
sys.exit("ERROR: %s" % e)

View File

@ -137,9 +137,12 @@ def do_start(server, options, args):
pid_file = '/var/run/glance/%s.pid' % server
else:
pid_file = os.path.abspath(options['pid_file'])
conf_file = config.find_config_file(server, options, args)
if not conf_file:
sys.exit("Could not find any configuration file to use!")
try:
conf_file = config.find_config_file(server, options, args)
except RuntimeError, err:
sys.exit("Could not find any configuration file to use: %s" % err)
launch_args = [(conf_file, pid_file)]
# start all servers

View File

@ -124,8 +124,8 @@ def main():
try:
# We load the glance-registry config section because
# sql_connection is only part of the glance registry.
conf_file, conf = config.load_paste_config('glance-registry',
options, args)
conf_file = config.find_config_file('glance-registry', options, args)
conf = config.load_paste_config(conf_file, 'glance-registry')
config.setup_logging(options, conf)
except RuntimeError, e:
sys.exit("ERROR: %s" % e)

View File

@ -181,20 +181,22 @@ def setup_logging(options, conf):
root_logger.addHandler(handler)
def find_config_file(app_name, options, args):
def find_config_file(conf_name, options, args):
"""
Return the first config file found for an application.
We search for the paste config file in the following order:
* If --config-file option is used, use that
* If args[0] is a file, use that
* Search for $app.conf in standard directories:
* Search for $conf_name.conf in standard directories:
* ~.glance/
* ~
* /etc/glance
* /etc
:retval Full path to config file, or None if no config file found
:retval Full path to config file.
:raises RuntimeError if the config file cannot be found.
"""
fix_path = lambda p: os.path.abspath(os.path.expanduser(p))
@ -205,62 +207,48 @@ def find_config_file(app_name, options, args):
if os.path.exists(args[0]):
return fix_path(args[0])
# Handle standard directory search for $app_name.conf
# Handle standard directory search for $conf_name.conf
config_file_dirs = [fix_path(os.path.join('~', '.glance')),
fix_path('~'),
'/etc/glance/',
'/etc']
for cfg_dir in config_file_dirs:
cfg_file = os.path.join(cfg_dir, '%s.conf' % app_name)
cfg_file = os.path.join(cfg_dir, '%s.conf' % conf_name)
if os.path.exists(cfg_file):
return cfg_file
raise RuntimeError("Unable to locate %s configuration file." % conf_name)
def load_paste_config(app_name, options, args):
def load_paste_config(conf_file, app_name):
"""
Looks for a config file to use for an app and returns the
config file path and a configuration mapping from a paste config file.
We search for the paste config file in the following order:
* If --config-file option is used, use that
* If args[0] is a file, use that
* Search for $app_name.conf in standard directories:
* ~.glance/
* ~
* /etc/glance
* /etc
Load the configuration mapping from a paste config file.
:param conf_file: The path to the paste config file.
:param app_name: Name of the application to load config for, or None.
None signifies to only load the [DEFAULT] section of
the config file.
:param options: Set of typed options returned from parse_options()
:param args: Command line arguments from argv[1:]
:retval Tuple of (conf_file, conf)
:retval The configuration mapping.
:raises RuntimeError when config file cannot be located or there was a
problem loading the configuration file.
:raises RuntimeError when there was a problem loading the configuration
file.
"""
conf_file = find_config_file(app_name, options, args)
if not conf_file:
raise RuntimeError("Unable to locate any configuration file. "
"Cannot load application %s" % app_name)
try:
conf = deploy.appconfig("config:%s" % conf_file, name=app_name)
return conf_file, conf
return deploy.appconfig("config:%s" % conf_file, name=app_name)
except Exception, e:
raise RuntimeError("Error trying to load config %s: %s"
% (conf_file, e))
def load_paste_app(app_name, options, args):
def load_paste_app(app_name, options, args, conf_name=None):
"""
Builds and returns a WSGI app from a paste config file.
We search for the paste config file in the following order:
* If --config-file option is used, use that
* If args[0] is a file, use that
* Search for $app_name.conf in standard directories:
* Search for $conf_name.conf in standard directories:
* ~.glance/
* ~
* /etc/glance
@ -269,11 +257,16 @@ def load_paste_app(app_name, options, args):
:param app_name: Name of the application to load
:param options: Set of typed options returned from parse_options()
:param args: Command line arguments from argv[1:]
:param conf_name: Name of config file to load, defaults to app_name
:raises RuntimeError when config file cannot be located or application
cannot be loaded from config file
"""
conf_file, conf = load_paste_config(app_name, options, args)
if conf_name is None:
conf_name = app_name
conf_file = find_config_file(conf_name, options, args)
conf = load_paste_config(conf_file, app_name)
try:
# Setup logging early, supplying both the CLI options and the

View File

@ -26,6 +26,7 @@ from glance.api.middleware import version_negotiation
from glance.api.v1 import images
from glance.api.v1 import members
from glance.common import config
from glance.image_cache import pruner
class TestOptionParsing(unittest.TestCase):
@ -128,9 +129,9 @@ class TestConfigFiles(unittest.TestCase):
def test_config_file_not_found(self):
self.stubs.Set(os.path, 'exists', lambda p: False)
path = config.find_config_file('glance-foo', {}, [])
self.assertIsNone(path)
self.assertRaises(RuntimeError,
config.find_config_file,
'glance-foo', {}, [])
class TestPasteConfig(unittest.TestCase):
@ -138,9 +139,8 @@ class TestPasteConfig(unittest.TestCase):
def test_load_paste_config(self):
path = os.path.join(os.getcwd(), 'etc/glance-api.conf')
conf_file, conf = config.load_paste_config('glance-api', {}, [path])
conf = config.load_paste_config(path, 'glance-api')
self.assertEquals(path, conf_file)
self.assertEquals('file', conf['default_store'])
@ -164,3 +164,24 @@ class TestPasteApp(unittest.TestCase):
self.assertEquals('file', conf['default_store'])
self.assertEquals(version_negotiation.VersionNegotiationFilter,
type(app))
def test_load_paste_app_with_conf_name(self):
def fake_join(*args):
if len(args) == 2 and \
args[0].endswith('.glance') and \
args[1] == 'glance-cache.conf':
return os.path.join(os.getcwd(), 'etc', args[1])
else:
return orig_join(*args)
orig_join = os.path.join
self.stubs.Set(os.path, 'join', fake_join)
self.stubs.Set(config, 'setup_logging', lambda *a: None)
self.stubs.Set(pruner, 'app_factory', lambda *a: 'pruner')
conf, app = config.load_paste_app('glance-pruner', {}, [],
'glance-cache')
self.assertEquals('86400', conf['image_cache_stall_time'])
self.assertEquals('pruner', app)