diff --git a/os_collect_config/cache.py b/os_collect_config/cache.py new file mode 100644 index 0000000..6d530b6 --- /dev/null +++ b/os_collect_config/cache.py @@ -0,0 +1,63 @@ +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import os +import shutil +import tempfile + +from oslo.config import cfg + + +def get_path(name): + return os.path.join(cfg.CONF.cachedir, '%s.json' % name) + + +def store(name, content): + if not os.path.exists(cfg.CONF.cachedir): + os.mkdir(cfg.CONF.cachedir) + + changed = False + dest_path = get_path(name) + orig_path = '%s.orig' % dest_path + last_path = '%s.last' % dest_path + + with tempfile.NamedTemporaryFile( + dir=cfg.CONF.cachedir, + delete=False) as new: + new.write(json.dumps(content, indent=1)) + new.flush() + if not os.path.exists(orig_path): + shutil.copy(new.name, orig_path) + changed = True + os.rename(new.name, dest_path) + + if not changed: + with open(dest_path) as now: + if os.path.exists(last_path): + with open(last_path) as then: + for now_line in now: + then_line = then.next() + if then_line != now_line: + changed = True + break + else: + changed = True + return (changed, dest_path) + + +def commit(name): + dest_path = get_path(name) + shutil.copy(dest_path, '%s.last' % dest_path) diff --git a/os_collect_config/collect.py b/os_collect_config/collect.py index 493a5b8..cd56961 100644 --- a/os_collect_config/collect.py +++ b/os_collect_config/collect.py @@ -15,11 +15,10 @@ import json import os -import shutil import subprocess -import tempfile from openstack.common import log +from os_collect_config import cache from os_collect_config import ec2 from oslo.config import cfg @@ -46,46 +45,6 @@ def setup_conf(): CONF.register_cli_opts(opts) -def cache_path(name): - return os.path.join(CONF.cachedir, '%s.json' % name) - - -def cache(name, content): - if not os.path.exists(CONF.cachedir): - os.mkdir(CONF.cachedir) - - changed = False - dest_path = cache_path(name) - orig_path = '%s.orig' % dest_path - last_path = '%s.last' % dest_path - - with tempfile.NamedTemporaryFile(dir=CONF.cachedir, delete=False) as new: - new.write(json.dumps(content, indent=1)) - new.flush() - if not os.path.exists(orig_path): - shutil.copy(new.name, orig_path) - changed = True - os.rename(new.name, dest_path) - - if not changed: - with open(dest_path) as now: - if os.path.exists(last_path): - with open(last_path) as then: - for now_line in now: - then_line = then.next() - if then_line != now_line: - changed = True - break - else: - changed = True - return (changed, dest_path) - - -def commit_cache(name): - dest_path = cache_path(name) - shutil.copy(dest_path, '%s.last' % dest_path) - - def __main__(): setup_conf() CONF(prog="os-collect-config") @@ -93,14 +52,14 @@ def __main__(): ec2_content = ec2.collect() if CONF.command: - (changed, ec2_path) = cache('ec2', ec2_content) + (changed, ec2_path) = cache.store('ec2', ec2_content) if changed: paths = [ec2_path] env = dict(os.environ) env["OS_CONFIG_FILES"] = ':'.join(paths) logger.info("Executing %s" % CONF.command) subprocess.call(CONF.command, env=env, shell=True) - commit_cache('ec2') + cache.commit('ec2') else: content = {'ec2': ec2_content} print json.dumps(content, indent=1) diff --git a/os_collect_config/tests/test_cache.py b/os_collect_config/tests/test_cache.py new file mode 100644 index 0000000..3bce2c0 --- /dev/null +++ b/os_collect_config/tests/test_cache.py @@ -0,0 +1,55 @@ +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import fixtures +import os +from oslo.config import cfg +import testtools + +from os_collect_config import cache +from os_collect_config import collect + + +class TestCache(testtools.TestCase): + def setUp(self): + super(TestCache, self).setUp() + cfg.CONF.reset() + + def tearDown(self): + cfg.CONF.reset() + super(TestCache, self).tearDown() + + def test_cache(self): + cache_root = self.useFixture(fixtures.TempDir()) + cache_dir = os.path.join(cache_root.path, 'cache') + collect.setup_conf() + cfg.CONF(['--cachedir', cache_dir]) + (changed, path) = cache.store('foo', {'a': 1}) + self.assertTrue(changed) + self.assertTrue(os.path.exists(cache_dir)) + self.assertTrue(os.path.exists(path)) + orig_path = '%s.orig' % path + self.assertTrue(os.path.exists(orig_path)) + + (changed, path) = cache.store('foo', {'a': 2}) + self.assertTrue(changed) + orig_path = '%s.orig' % path + with open(path) as now: + with open(orig_path) as then: + self.assertNotEquals(now.read(), then.read()) + + cache.commit('foo') + (changed, path) = cache.store('foo', {'a': 2}) + self.assertFalse(changed) diff --git a/os_collect_config/tests/test_collect.py b/os_collect_config/tests/test_collect.py index 42ee0ba..0ac17b1 100644 --- a/os_collect_config/tests/test_collect.py +++ b/os_collect_config/tests/test_collect.py @@ -92,36 +92,3 @@ class TestConf(testtools.TestCase): def test_setup_conf(self): collect.setup_conf() self.assertEquals('/var/run/os-collect-config', cfg.CONF.cachedir) - - -class TestCache(testtools.TestCase): - def setUp(self): - super(TestCache, self).setUp() - cfg.CONF.reset() - - def tearDown(self): - cfg.CONF.reset() - super(TestCache, self).tearDown() - - def test_cache(self): - cache_root = self.useFixture(fixtures.TempDir()) - cache_dir = os.path.join(cache_root.path, 'cache') - collect.setup_conf() - cfg.CONF(['--cachedir', cache_dir]) - (changed, path) = collect.cache('foo', {'a': 1}) - self.assertTrue(changed) - self.assertTrue(os.path.exists(cache_dir)) - self.assertTrue(os.path.exists(path)) - orig_path = '%s.orig' % path - self.assertTrue(os.path.exists(orig_path)) - - (changed, path) = collect.cache('foo', {'a': 2}) - self.assertTrue(changed) - orig_path = '%s.orig' % path - with open(path) as now: - with open(orig_path) as then: - self.assertNotEquals(now.read(), then.read()) - - collect.commit_cache('foo') - (changed, path) = collect.cache('foo', {'a': 2}) - self.assertFalse(changed)