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
This commit is contained in:
Clint Byrum 2013-09-12 16:08:39 -07:00
parent f21f986ff4
commit 635955def4
2 changed files with 45 additions and 3 deletions

View File

@ -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:

View File

@ -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)