Merge "Allow changing the collectors that are used."

This commit is contained in:
Jenkins
2013-07-07 17:52:37 +00:00
committed by Gerrit Code Review
2 changed files with 44 additions and 17 deletions

View File

@@ -27,6 +27,7 @@ from os_collect_config import exc
from os_collect_config import heat_local from os_collect_config import heat_local
from oslo.config import cfg from oslo.config import cfg
DEFAULT_COLLECTORS = ['ec2', 'cfn', 'heat_local']
opts = [ opts = [
cfg.StrOpt('command', cfg.StrOpt('command',
short='c', short='c',
@@ -34,12 +35,21 @@ opts = [
cfg.StrOpt('cachedir', cfg.StrOpt('cachedir',
default='/var/run/os-collect-config', default='/var/run/os-collect-config',
help='Directory in which to store local cache of metadata'), 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 CONF = cfg.CONF
logger = log.getLogger('os-collect-config') 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(): def setup_conf():
@@ -70,24 +80,25 @@ def collect_all(collectors, store=False, requests_impl_map=None):
paths_or_content = {} paths_or_content = {}
for collector in collectors: for collector in collectors:
if requests_impl_map and collector.name in requests_impl_map: module = COLLECTORS[collector]
requests_impl = requests_impl_map[collector.name] if requests_impl_map and collector in requests_impl_map:
requests_impl = requests_impl_map[collector]
else: else:
requests_impl = common.requests requests_impl = common.requests
try: try:
content = collector.Collector( content = module.Collector(
requests_impl=requests_impl).collect() requests_impl=requests_impl).collect()
except exc.SourceNotAvailable: except exc.SourceNotAvailable:
logger.warn('Source [%s] Unavailable.' % collector.name) logger.warn('Source [%s] Unavailable.' % collector)
continue continue
if store: if store:
(changed, path) = cache.store(collector.name, content) (changed, path) = cache.store(collector, content)
any_changed |= changed any_changed |= changed
paths_or_content.append(path) paths_or_content.append(path)
else: else:
paths_or_content[collector.name] = content paths_or_content[collector] = content
return (any_changed, paths_or_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") CONF(args=args[1:], prog="os-collect-config")
log.setup("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) requests_impl_map=requests_impl_map)
if CONF.command: if CONF.command:
if any_changed: if any_changed:
@@ -105,8 +117,8 @@ def __main__(args=sys.argv, requests_impl_map=None):
env["OS_CONFIG_FILES"] = ':'.join(content) env["OS_CONFIG_FILES"] = ':'.join(content)
logger.info("Executing %s" % CONF.command) logger.info("Executing %s" % CONF.command)
subprocess.call(CONF.command, env=env, shell=True) subprocess.call(CONF.command, env=env, shell=True)
for collector in COLLECTORS: for collector in cfg.CONF.collectors:
cache.commit(collector.name) cache.commit(collector)
else: else:
logger.debug("No changes detected.") logger.debug("No changes detected.")
else: else:

View File

@@ -147,12 +147,15 @@ class TestCollectAll(testtools.TestCase):
cfg.CONF.cfn.secret_access_key = 'FEDCBA9876543210' cfg.CONF.cfn.secret_access_key = 'FEDCBA9876543210'
cfg.CONF.heat_local.path = [_setup_local_metadata(self)] 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: if requests_impl_map is None:
requests_impl_map = {'ec2': test_ec2.FakeRequests, requests_impl_map = {'ec2': test_ec2.FakeRequests,
'cfn': test_cfn.FakeRequests(self)} 'cfn': test_cfn.FakeRequests(self)}
if collectors is None:
collectors = cfg.CONF.collectors
return collect.collect_all( return collect.collect_all(
collect.COLLECTORS, collectors,
store=store, store=store,
requests_impl_map=requests_impl_map) requests_impl_map=requests_impl_map)
@@ -160,19 +163,31 @@ class TestCollectAll(testtools.TestCase):
(any_changed, paths) = self._call_collect_all(store=True) (any_changed, paths) = self._call_collect_all(store=True)
self.assertTrue(any_changed) self.assertTrue(any_changed)
self.assertThat(paths, matchers.IsInstance(list)) 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' % self.assertIn(os.path.join(self.cache_dir.path, '%s.json' %
collector.name), collector),
paths) paths)
self.assertTrue(any_changed) 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): def test_collect_all_nostore(self):
(any_changed, content) = self._call_collect_all(store=False) (any_changed, content) = self._call_collect_all(store=False)
self.assertFalse(any_changed) self.assertFalse(any_changed)
self.assertThat(content, matchers.IsInstance(dict)) self.assertThat(content, matchers.IsInstance(dict))
for collector in collect.COLLECTORS: for collector in cfg.CONF.collectors:
self.assertIn(collector.name, content) self.assertIn(collector, content)
self.assertThat(content[collector.name], matchers.IsInstance(dict)) self.assertThat(content[collector], matchers.IsInstance(dict))
def test_collect_all_ec2_unavailable(self): def test_collect_all_ec2_unavailable(self):
requests_impl_map = {'ec2': test_ec2.FakeFailRequests, requests_impl_map = {'ec2': test_ec2.FakeFailRequests,