From 6eb8eb9af3e1d71944725433606c56b1323bd7c7 Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Thu, 12 Sep 2013 17:58:51 -0700 Subject: [PATCH] Store list of collected configs in cachedir This will allow tools like os-apply-config to read the list even when they are run out of band from os-collect-config. Change-Id: Ic4eaf649e234f4a1367d20c7ec52e93e787a7bb3 --- README.rst | 2 +- os_collect_config/cache.py | 12 ++++++++++++ os_collect_config/collect.py | 2 ++ os_collect_config/tests/test_cache.py | 10 ++++++++++ os_collect_config/tests/test_collect.py | 8 +++++++- 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index aadcba0..75cda52 100644 --- a/README.rst +++ b/README.rst @@ -25,7 +25,7 @@ path = MyResource stack_name = my.stack ``` -These sources will be polled and whenever any of them changes, default.command will be run. OS_CONFIG_FILES will be set in the environment as a colon (":") separated list of the current copy of each metadata source. So in the example above, "os-refresh-config" would be executed with something like this in OS_CONFIG_FILES: +These sources will be polled and whenever any of them changes, default.command will be run. A file will be written to the cache dir, os_config_files.json, which will be a json list of the file paths to the current copy of each metadata source. This list will also be set as a colon separated list in the environment variable OS_CONFIG_FILES for the command that is run. So in the example above, "os-refresh-config" would be executed with something like this in OS_CONFIG_FILES: ``` /var/run/os-collect-config/ec2.json:/var/run/os-collect-config/cfn.json diff --git a/os_collect_config/cache.py b/os_collect_config/cache.py index 6d23a1e..689600a 100644 --- a/os_collect_config/cache.py +++ b/os_collect_config/cache.py @@ -74,3 +74,15 @@ def commit(name): dest_path = get_path(name) if os.path.exists(dest_path): shutil.copy(dest_path, '%s.last' % dest_path) + + +def store_meta_list(name, data_keys): + '''Store a json list of the files that should be present after store.''' + final_list = [get_path(k) for k in data_keys] + dest = get_path(name) + with tempfile.NamedTemporaryFile(prefix='tmp_meta_list.', + dir=os.path.dirname(dest), + delete=False) as out: + out.write(json.dumps(final_list)) + os.rename(out.name, dest) + return dest diff --git a/os_collect_config/collect.py b/os_collect_config/collect.py index e08dd94..5a9531f 100644 --- a/os_collect_config/collect.py +++ b/os_collect_config/collect.py @@ -116,6 +116,8 @@ def collect_all(collectors, store=False, requests_impl_map=None): else: paths_or_content[collector] = content + if any_changed: + cache.store_meta_list('os_config_files', collectors) return (any_changed, paths_or_content) diff --git a/os_collect_config/tests/test_cache.py b/os_collect_config/tests/test_cache.py index 2a8e323..680b67f 100644 --- a/os_collect_config/tests/test_cache.py +++ b/os_collect_config/tests/test_cache.py @@ -14,8 +14,10 @@ # limitations under the License. import fixtures +import json import os import testtools +from testtools import matchers from os_collect_config import cache @@ -73,5 +75,13 @@ class TestCache(testtools.TestCase): self.assertTrue(changed) self.assertTrue(os.path.exists(path)) + # And the meta list + list_path = cache.store_meta_list('foo_list', ['foo']) + self.assertTrue(os.path.exists(list_path)) + with open(list_path) as list_file: + list_list = json.loads(list_file.read()) + self.assertThat(list_list, matchers.IsInstance(list)) + self.assertIn(path, list_list) + def test_commit_no_cache(self): self.assertEquals(None, cache.commit('neversaved')) diff --git a/os_collect_config/tests/test_collect.py b/os_collect_config/tests/test_collect.py index e715084..8b59fd7 100644 --- a/os_collect_config/tests/test_collect.py +++ b/os_collect_config/tests/test_collect.py @@ -90,8 +90,14 @@ class TestCollect(testtools.TestCase): self._call_main(occ_args) proc_args = calls[0] self.assertEqual(expected_cmd, proc_args['args']) + list_path = os.path.join(cache_dir.path, 'os_config_files.json') + with open(list_path) as list_file: + config_list = json.loads(list_file.read()) + self.assertThat(config_list, matchers.IsInstance(list)) + env_config_list = proc_args['env']['OS_CONFIG_FILES'].split(':') + self.assertEquals(env_config_list, config_list) keys_found = set() - for path in proc_args['env']['OS_CONFIG_FILES'].split(':'): + for path in env_config_list: self.assertTrue(os.path.exists(path)) with open(path) as cfg_file: contents = json.loads(cfg_file.read())