Merge "Allow changing the collectors that are used."
This commit is contained in:
@@ -27,6 +27,7 @@ from os_collect_config import exc
|
||||
from os_collect_config import heat_local
|
||||
from oslo.config import cfg
|
||||
|
||||
DEFAULT_COLLECTORS = ['ec2', 'cfn', 'heat_local']
|
||||
opts = [
|
||||
cfg.StrOpt('command',
|
||||
short='c',
|
||||
@@ -34,12 +35,21 @@ opts = [
|
||||
cfg.StrOpt('cachedir',
|
||||
default='/var/run/os-collect-config',
|
||||
help='Directory in which to store local cache of metadata'),
|
||||
cfg.MultiStrOpt(
|
||||
'collectors',
|
||||
positional=True,
|
||||
default=DEFAULT_COLLECTORS,
|
||||
help='List the collectors to use. When command is specified the'
|
||||
'collections will be emitted in the order given by this option.'
|
||||
' (default: %s)' % ' '.join(DEFAULT_COLLECTORS)),
|
||||
]
|
||||
|
||||
CONF = cfg.CONF
|
||||
logger = log.getLogger('os-collect-config')
|
||||
|
||||
COLLECTORS = (ec2, cfn, heat_local)
|
||||
COLLECTORS = {ec2.name: ec2,
|
||||
cfn.name: cfn,
|
||||
heat_local.name: heat_local}
|
||||
|
||||
|
||||
def setup_conf():
|
||||
@@ -70,24 +80,25 @@ def collect_all(collectors, store=False, requests_impl_map=None):
|
||||
paths_or_content = {}
|
||||
|
||||
for collector in collectors:
|
||||
if requests_impl_map and collector.name in requests_impl_map:
|
||||
requests_impl = requests_impl_map[collector.name]
|
||||
module = COLLECTORS[collector]
|
||||
if requests_impl_map and collector in requests_impl_map:
|
||||
requests_impl = requests_impl_map[collector]
|
||||
else:
|
||||
requests_impl = common.requests
|
||||
|
||||
try:
|
||||
content = collector.Collector(
|
||||
content = module.Collector(
|
||||
requests_impl=requests_impl).collect()
|
||||
except exc.SourceNotAvailable:
|
||||
logger.warn('Source [%s] Unavailable.' % collector.name)
|
||||
logger.warn('Source [%s] Unavailable.' % collector)
|
||||
continue
|
||||
|
||||
if store:
|
||||
(changed, path) = cache.store(collector.name, content)
|
||||
(changed, path) = cache.store(collector, content)
|
||||
any_changed |= changed
|
||||
paths_or_content.append(path)
|
||||
else:
|
||||
paths_or_content[collector.name] = content
|
||||
paths_or_content[collector] = content
|
||||
|
||||
return (any_changed, paths_or_content)
|
||||
|
||||
@@ -97,7 +108,8 @@ def __main__(args=sys.argv, requests_impl_map=None):
|
||||
CONF(args=args[1:], prog="os-collect-config")
|
||||
log.setup("os-collect-config")
|
||||
|
||||
(any_changed, content) = collect_all(COLLECTORS, store=bool(CONF.command),
|
||||
(any_changed, content) = collect_all(cfg.CONF.collectors,
|
||||
store=bool(CONF.command),
|
||||
requests_impl_map=requests_impl_map)
|
||||
if CONF.command:
|
||||
if any_changed:
|
||||
@@ -105,8 +117,8 @@ def __main__(args=sys.argv, requests_impl_map=None):
|
||||
env["OS_CONFIG_FILES"] = ':'.join(content)
|
||||
logger.info("Executing %s" % CONF.command)
|
||||
subprocess.call(CONF.command, env=env, shell=True)
|
||||
for collector in COLLECTORS:
|
||||
cache.commit(collector.name)
|
||||
for collector in cfg.CONF.collectors:
|
||||
cache.commit(collector)
|
||||
else:
|
||||
logger.debug("No changes detected.")
|
||||
else:
|
||||
|
||||
@@ -147,12 +147,15 @@ class TestCollectAll(testtools.TestCase):
|
||||
cfg.CONF.cfn.secret_access_key = 'FEDCBA9876543210'
|
||||
cfg.CONF.heat_local.path = [_setup_local_metadata(self)]
|
||||
|
||||
def _call_collect_all(self, store, requests_impl_map=None):
|
||||
def _call_collect_all(
|
||||
self, store, requests_impl_map=None, collectors=None):
|
||||
if requests_impl_map is None:
|
||||
requests_impl_map = {'ec2': test_ec2.FakeRequests,
|
||||
'cfn': test_cfn.FakeRequests(self)}
|
||||
if collectors is None:
|
||||
collectors = cfg.CONF.collectors
|
||||
return collect.collect_all(
|
||||
collect.COLLECTORS,
|
||||
collectors,
|
||||
store=store,
|
||||
requests_impl_map=requests_impl_map)
|
||||
|
||||
@@ -160,19 +163,31 @@ class TestCollectAll(testtools.TestCase):
|
||||
(any_changed, paths) = self._call_collect_all(store=True)
|
||||
self.assertTrue(any_changed)
|
||||
self.assertThat(paths, matchers.IsInstance(list))
|
||||
for collector in collect.COLLECTORS:
|
||||
for collector in cfg.CONF.collectors:
|
||||
self.assertIn(os.path.join(self.cache_dir.path, '%s.json' %
|
||||
collector.name),
|
||||
collector),
|
||||
paths)
|
||||
self.assertTrue(any_changed)
|
||||
|
||||
def test_collect_all_store_alt_order(self):
|
||||
# Ensure different than default
|
||||
new_list = list(reversed(cfg.CONF.collectors))
|
||||
(any_changed, paths) = self._call_collect_all(store=True,
|
||||
collectors=new_list)
|
||||
self.assertTrue(any_changed)
|
||||
self.assertThat(paths, matchers.IsInstance(list))
|
||||
expected_paths = [
|
||||
os.path.join(self.cache_dir.path, '%s.json' % collector)
|
||||
for collector in new_list]
|
||||
self.assertEquals(expected_paths, paths)
|
||||
|
||||
def test_collect_all_nostore(self):
|
||||
(any_changed, content) = self._call_collect_all(store=False)
|
||||
self.assertFalse(any_changed)
|
||||
self.assertThat(content, matchers.IsInstance(dict))
|
||||
for collector in collect.COLLECTORS:
|
||||
self.assertIn(collector.name, content)
|
||||
self.assertThat(content[collector.name], matchers.IsInstance(dict))
|
||||
for collector in cfg.CONF.collectors:
|
||||
self.assertIn(collector, content)
|
||||
self.assertThat(content[collector], matchers.IsInstance(dict))
|
||||
|
||||
def test_collect_all_ec2_unavailable(self):
|
||||
requests_impl_map = {'ec2': test_ec2.FakeFailRequests,
|
||||
|
||||
Reference in New Issue
Block a user