Add local logging output to puppet hook

This change updates the puppet hook to write out logs to the local node
if verbose or debug is enabled for the hook. The logs will be written
out to the /var/log/puppet/ folder that currently is empty at all times.
We will continue to output logs to the console for the traditional heat
processing of the stdout and stderr but will also write out the logs
locally only if debug or verbose is enabled for the puppet apply.

Change-Id: Idfbfc030350827a4653b26cef7c7deca92b0e05d
Closes-Bug: #1536009
This commit is contained in:
Alex Schultz 2016-10-18 14:47:44 -06:00 committed by Thomas Herve
parent c9408f1706
commit 49b3efb3c6
3 changed files with 70 additions and 0 deletions

View File

@ -10,3 +10,7 @@ Hook Options:
modulepath: If set, puppet will use this filesystem path to load modules
tags: If set, puppet will use the specified value(s) to apply only a
subset of the catalog for a given manifest.
enable_debug: default False. Set to True to run puppet apply in debug mode
and have it captured on the node to /var/log/puppet/heat-debug.log
enable_verbose: default False. Set to True to run puppet apply in verbose mode
and have it captured on the node to /var/log/puppet/heat-verbose.log

View File

@ -67,6 +67,7 @@ def main(argv=sys.argv):
modulepath = c['options'].get('modulepath')
tags = c['options'].get('tags')
debug = c['options'].get('enable_debug', False)
verbose = c['options'].get('enable_verbose', False)
facts = {}
hiera = {}
@ -108,6 +109,10 @@ def main(argv=sys.argv):
f.write(c.get('config', '').encode('utf-8'))
cmd = [PUPPET_CMD, 'apply', '--detailed-exitcodes', fn]
# This is the default log destination to print out to the console and
# captured by heat via the subprocess method below.
cmd.insert(-1, '--logdest')
cmd.insert(-1, 'console')
if modulepath:
cmd.insert(-1, '--modulepath')
cmd.insert(-1, modulepath)
@ -116,6 +121,12 @@ def main(argv=sys.argv):
cmd.insert(-1, tags)
if debug:
cmd.insert(-1, '--debug')
cmd.insert(-1, '--logdest')
cmd.insert(-1, '/var/log/puppet/heat-debug.log')
if verbose:
cmd.insert(-1, '--verbose')
cmd.insert(-1, '--logdest')
cmd.insert(-1, '/var/log/puppet/heat-verbose.log')
prepare_dir(PUPPET_LOGDIR)
timestamp = re.sub('[:T]', '-', c['creation_time'])

View File

@ -31,6 +31,7 @@ class HookPuppetTest(common.RunScriptTest):
'enable_hiera': True,
'enable_facter': True,
'enable_debug': True,
'enable_verbose': True,
},
'inputs': [
{'name': 'foo', 'value': 'bar'},
@ -102,7 +103,14 @@ class HookPuppetTest(common.RunScriptTest):
self.fake_tool_path,
'apply',
'--detailed-exitcodes',
'--logdest',
'console',
'--debug',
'--logdest',
'/var/log/puppet/heat-debug.log',
'--verbose',
'--logdest',
'/var/log/puppet/heat-verbose.log',
puppet_script
],
state['args'])
@ -132,11 +140,44 @@ class HookPuppetTest(common.RunScriptTest):
self.fake_tool_path,
'apply',
'--detailed-exitcodes',
'--logdest',
'console',
'--verbose',
'--logdest',
'/var/log/puppet/heat-verbose.log',
puppet_script
],
state['args'])
self.data['options']['enable_debug'] = True
def test_hook_no_verbose(self):
self.data['options']['enable_verbose'] = False
self.env.update({
'TEST_RESPONSE': json.dumps({
'stdout': 'success',
'stderr': '',
}),
})
returncode, stdout, stderr = self.run_cmd(
[self.hook_path], self.env, json.dumps(self.data))
state = self.json_from_file(self.test_state_path)
puppet_script = self.working_dir.join('1234.pp')
self.assertEqual(
[
self.fake_tool_path,
'apply',
'--detailed-exitcodes',
'--logdest',
'console',
'--debug',
'--logdest',
'/var/log/puppet/heat-debug.log',
puppet_script
],
state['args'])
self.data['options']['enable_verbose'] = True
def test_hook_puppet_failed(self):
self.env.update({
@ -163,7 +204,14 @@ class HookPuppetTest(common.RunScriptTest):
self.fake_tool_path,
'apply',
'--detailed-exitcodes',
'--logdest',
'console',
'--debug',
'--logdest',
'/var/log/puppet/heat-debug.log',
'--verbose',
'--logdest',
'/var/log/puppet/heat-verbose.log',
puppet_script
],
state['args'])
@ -212,11 +260,18 @@ class HookPuppetTest(common.RunScriptTest):
self.fake_tool_path,
'apply',
'--detailed-exitcodes',
'--logdest',
'console',
'--modulepath',
modulepath,
'--tags',
'package,file',
'--debug',
'--logdest',
'/var/log/puppet/heat-debug.log',
'--verbose',
'--logdest',
'/var/log/puppet/heat-verbose.log',
puppet_script
],
state['args'])