From 635955def487c1cff4d1bb3b6b60a668cb09a4a6 Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Thu, 12 Sep 2013 16:08:39 -0700 Subject: [PATCH] Add --print option to force printing The use case for --print is an administrator wanting to view the metadata that os-collect-config sees without running any commands. Fixes bug #1213195 Change-Id: I0251f2c70574aeaa79997ce822d2a5ffbe08e345 --- os_collect_config/collect.py | 12 ++++++--- os_collect_config/tests/test_collect.py | 36 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/os_collect_config/collect.py b/os_collect_config/collect.py index c7983ab..2c9460d 100644 --- a/os_collect_config/collect.py +++ b/os_collect_config/collect.py @@ -62,6 +62,11 @@ opts = [ default=False, help='Pass this to force running the command even if nothing' ' has changed.'), + cfg.BoolOpt('print', dest='print_only', + default=False, + help='Query normally, print the resulting configs as a json' + ' map, and exit immediately without running command if it is' + ' configured.'), ] CONF = cfg.CONF @@ -183,12 +188,13 @@ def __main__(args=sys.argv, requests_impl_map=None): config_files = CONF.config_file config_hash = getfilehash(config_files) while True: + store_and_run = bool(CONF.command and not CONF.print_only) (any_changed, content) = collect_all( cfg.CONF.collectors, - store=bool(CONF.command), + store=store_and_run, requests_impl_map=requests_impl_map) - if CONF.command: - if any_changed or cfg.CONF.force: + if store_and_run: + if any_changed or CONF.force: # ignore HUP now since we will reexec after commit anyway signal.signal(signal.SIGHUP, signal.SIG_IGN) try: diff --git a/os_collect_config/tests/test_collect.py b/os_collect_config/tests/test_collect.py index 7bc7c9e..476f3d5 100644 --- a/os_collect_config/tests/test_collect.py +++ b/os_collect_config/tests/test_collect.py @@ -192,6 +192,7 @@ class TestCollect(testtools.TestCase): '--config-file', '/dev/null', '--print-cachedir', ] + output = self.useFixture(fixtures.StringStream('stdout')) self.useFixture( fixtures.MonkeyPatch('sys.stdout', output.stream)) @@ -199,6 +200,41 @@ class TestCollect(testtools.TestCase): cache_dir = output.getDetails()['stdout'].as_text().strip() self.assertEquals(fake_cachedir.path, cache_dir) + def test_main_print_only(self): + cache_dir = self.useFixture(fixtures.TempDir()) + fake_metadata = _setup_local_metadata(self) + args = [ + 'os-collect-config', + '--command', 'bar', + '--cachedir', cache_dir.path, + '--config-file', '/dev/null', + '--print', + '--cfn-metadata-url', + 'http://127.0.0.1:8000/v1/', + '--cfn-stack-name', + 'foo', + '--cfn-path', + 'foo.Metadata', + '--cfn-access-key-id', + '0123456789ABCDEF', + '--cfn-secret-access-key', + 'FEDCBA9876543210', + '--heat_local-path', fake_metadata, + ] + + def fake_popen(args): + self.fail('Called command instead of printing') + self.useFixture(fixtures.FakePopen(fake_popen)) + output = self.useFixture(fixtures.StringStream('stdout')) + self.useFixture( + fixtures.MonkeyPatch('sys.stdout', output.stream)) + self._call_main(args) + out_struct = json.loads(output.getDetails()['stdout'].as_text()) + self.assertThat(out_struct, matchers.IsInstance(dict)) + self.assertIn('cfn', out_struct) + self.assertIn('heat_local', out_struct) + self.assertIn('ec2', out_struct) + def test_main_invalid_collector(self): fake_args = ['os-collect-config', 'invalid'] self.assertRaises(exc.InvalidArguments, self._call_main, fake_args)