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
This commit is contained in:
Clint Byrum 2013-09-12 17:58:51 -07:00
parent c2b1b3909c
commit 6eb8eb9af3
5 changed files with 32 additions and 2 deletions

View File

@ -25,7 +25,7 @@ path = MyResource
stack_name = my.stack 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 /var/run/os-collect-config/ec2.json:/var/run/os-collect-config/cfn.json

View File

@ -74,3 +74,15 @@ def commit(name):
dest_path = get_path(name) dest_path = get_path(name)
if os.path.exists(dest_path): if os.path.exists(dest_path):
shutil.copy(dest_path, '%s.last' % 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

View File

@ -116,6 +116,8 @@ def collect_all(collectors, store=False, requests_impl_map=None):
else: else:
paths_or_content[collector] = content paths_or_content[collector] = content
if any_changed:
cache.store_meta_list('os_config_files', collectors)
return (any_changed, paths_or_content) return (any_changed, paths_or_content)

View File

@ -14,8 +14,10 @@
# limitations under the License. # limitations under the License.
import fixtures import fixtures
import json
import os import os
import testtools import testtools
from testtools import matchers
from os_collect_config import cache from os_collect_config import cache
@ -73,5 +75,13 @@ class TestCache(testtools.TestCase):
self.assertTrue(changed) self.assertTrue(changed)
self.assertTrue(os.path.exists(path)) 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): def test_commit_no_cache(self):
self.assertEquals(None, cache.commit('neversaved')) self.assertEquals(None, cache.commit('neversaved'))

View File

@ -90,8 +90,14 @@ class TestCollect(testtools.TestCase):
self._call_main(occ_args) self._call_main(occ_args)
proc_args = calls[0] proc_args = calls[0]
self.assertEqual(expected_cmd, proc_args['args']) 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() 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)) self.assertTrue(os.path.exists(path))
with open(path) as cfg_file: with open(path) as cfg_file:
contents = json.loads(cfg_file.read()) contents = json.loads(cfg_file.read())