Grab json log contents for final post playbook failures

If the final post playbook fails, something has gone wrong with log
uploading, which means it's very hard to debug. Grab the contents of the
json log file, extract the log for the last playbook and add it to the
executor log.

Change-Id: Ia930311e121c350e73e41b20e9b742b2eac9c9f6
This commit is contained in:
Monty Taylor 2017-10-10 11:57:29 -05:00
parent 80ac158acd
commit 0e2489a4cd
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
6 changed files with 82 additions and 1 deletions

View File

@ -0,0 +1,4 @@
- hosts: all
tasks:
- shell: echo "Failure test {{ zuul.executor.src_root }}"
- shell: exit 1

View File

@ -20,8 +20,19 @@
parent: base parent: base
name: job-output name: job-output
- job:
name: job-output-failure
run: playbooks/job-output
post-run: playbooks/job-output-failure-post
- project: - project:
name: org/project name: org/project
check: check:
jobs: jobs:
- job-output - job-output
- project:
name: org/project2
check:
jobs:
- job-output-failure

View File

@ -0,0 +1 @@
test

View File

@ -6,3 +6,4 @@
- common-config - common-config
untrusted-projects: untrusted-projects:
- org/project - org/project
- org/project2

View File

@ -14,7 +14,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import io
import json import json
import logging
import os import os
import textwrap import textwrap
import gc import gc
@ -1844,7 +1846,8 @@ class TestJobOutput(AnsibleZuulTestCase):
return f.read() return f.read()
def test_job_output(self): def test_job_output(self):
# Verify that command standard output appears in the job output # Verify that command standard output appears in the job output,
# and that failures in the final playbook get logged.
# This currently only verifies we receive output from # This currently only verifies we receive output from
# localhost. Notably, it does not verify we receive output # localhost. Notably, it does not verify we receive output
@ -1869,3 +1872,36 @@ class TestJobOutput(AnsibleZuulTestCase):
self.assertIn(token, self.assertIn(token,
self._get_file(self.history[0], self._get_file(self.history[0],
'work/logs/job-output.txt')) 'work/logs/job-output.txt'))
def test_job_output_failure_log(self):
logger = logging.getLogger('zuul.AnsibleJob')
output = io.StringIO()
logger.addHandler(logging.StreamHandler(output))
# Verify that a failure in the last post playbook emits the contents
# of the json output to the log
self.executor_server.keep_jobdir = True
A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
self.waitUntilSettled()
self.assertHistory([
dict(name='job-output-failure',
result='POST_FAILURE', changes='1,1'),
], ordered=False)
token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
j = json.loads(self._get_file(self.history[0],
'work/logs/job-output.json'))
self.assertEqual(token,
j[0]['plays'][0]['tasks'][0]
['hosts']['localhost']['stdout'])
print(self._get_file(self.history[0],
'work/logs/job-output.json'))
self.assertIn(token,
self._get_file(self.history[0],
'work/logs/job-output.txt'))
log_output = output.getvalue()
self.assertIn('Final playbook failed', log_output)
self.assertIn('Failure test', log_output)

View File

@ -1229,8 +1229,36 @@ class AnsibleJob(object):
# precedence over the post result. # precedence over the post result.
if not pre_failed: if not pre_failed:
result = 'POST_FAILURE' result = 'POST_FAILURE'
if (index + 1) == len(self.jobdir.post_playbooks):
self._logFinalPlaybookError()
return result return result
def _logFinalPlaybookError(self):
# Failures in the final post playbook can include failures
# uploading logs, which makes diagnosing issues difficult.
# Grab the output from the last playbook from the json
# file and log it.
json_output = self.jobdir.job_output_file.replace('txt', 'json')
self.log.debug("Final playbook failed")
if not os.path.exists(json_output):
self.log.debug("JSON logfile {logfile} is missing".format(
logfile=json_output))
return
try:
output = json.load(open(json_output, 'r'))
last_playbook = output[-1]
# Transform json to yaml - because it's easier to read and given
# the size of the data it'll be extra-hard to read this as an
# all on one line stringified nested dict.
yaml_out = yaml.safe_dump(last_playbook, default_flow_style=False)
for line in yaml_out.split('\n'):
self.log.debug(line)
except Exception:
self.log.exception(
"Could not decode json from {logfile}".format(
logfile=json_output))
def getHostList(self, args): def getHostList(self, args):
hosts = [] hosts = []
for node in args['nodes']: for node in args['nodes']: