Make playbook run meta info less fragile

Pass in and use info from the JobDirPlaybook rather than trying to strip
path elements from the playbook name.

Change-Id: Ifcd6f05e27c987d40db23b3dcec344c2eb786d7c
This commit is contained in:
Monty Taylor 2017-07-20 02:05:42 +09:00
parent 210e218fea
commit 1c39ec7361
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
3 changed files with 21 additions and 39 deletions

View File

@ -24,7 +24,6 @@ __metaclass__ = type
import json import json
import os import os
import re
from ansible.plugins.callback import CallbackBase from ansible.plugins.callback import CallbackBase
try: try:
@ -59,7 +58,6 @@ class CallbackModule(CallbackBase):
# Get the hostvars from just one host - the vars we're looking for will # Get the hostvars from just one host - the vars we're looking for will
# be identical on all of them # be identical on all of them
hostvars = next(iter(play._variable_manager._hostvars.values())) hostvars = next(iter(play._variable_manager._hostvars.values()))
playbook = self._playbook_name
self._playbook_name = None self._playbook_name = None
# TODO(mordred) For now, protect specific variable lookups to make it # TODO(mordred) For now, protect specific variable lookups to make it
@ -68,24 +66,16 @@ class CallbackModule(CallbackBase):
# tool. # tool.
phase = hostvars.get('zuul_execution_phase') phase = hostvars.get('zuul_execution_phase')
index = hostvars.get('zuul_execution_phase_index') index = hostvars.get('zuul_execution_phase_index')
playbook = hostvars.get('zuul_execution_canonical_name_and_path')
# imply work_dir from src_root trusted = hostvars.get('zuul_execution_trusted')
src_root = hostvars.get('zuul', {}).get('executor', {}).get('src_root') trusted = True if trusted == "True" else False
if src_root: branch = hostvars.get('zuul_execution_branch')
# Ensure work_dir has a trailing / for ease of stripping
work_dir = os.path.dirname(
os.path.dirname(src_root)).rstrip('/') + '/'
# Strip work_dir from the beginning of the playbook name.
playbook = playbook.replace(work_dir, '')
# Lop off the first two path elements - ansible/pre_playbook_0
playbook = re.sub('(un)?trusted/project_[^/]+/', '', playbook)
# Remove yaml suffix
playbook = os.path.splitext(playbook)[0]
self.playbook['playbook'] = playbook self.playbook['playbook'] = playbook
self.playbook['phase'] = phase self.playbook['phase'] = phase
self.playbook['index'] = index self.playbook['index'] = index
self.playbook['trusted'] = trusted
self.playbook['branch'] = branch
def _new_play(self, play): def _new_play(self, play):
return { return {

View File

@ -23,7 +23,6 @@ import datetime
import logging import logging
import json import json
import os import os
import re
import socket import socket
import threading import threading
import time import time
@ -165,34 +164,20 @@ class CallbackModule(default.CallbackModule):
# Get the hostvars from just one host - the vars we're looking for will # Get the hostvars from just one host - the vars we're looking for will
# be identical on all of them # be identical on all of them
hostvars = next(iter(self._play._variable_manager._hostvars.values())) hostvars = next(iter(self._play._variable_manager._hostvars.values()))
playbook = self._playbook_name
self._playbook_name = None self._playbook_name = None
# TODO(mordred) For now, protect specific variable lookups to make it phase = hostvars.get('zuul_execution_phase', '')
# not absurdly strange to run local tests with the callback plugin playbook = hostvars.get('zuul_execution_canonical_name_and_path')
# enabled. Remove once we have a "run playbook like zuul runs playbook" trusted = hostvars.get('zuul_execution_trusted')
# tool. trusted = 'trusted' if trusted == "True" else 'untrusted'
phase = hostvars.get('zuul_execution_phase') branch = hostvars.get('zuul_execution_branch')
if phase and phase != 'run': if phase and phase != 'run':
phase = '{phase}-run'.format(phase=phase) phase = '{phase}-run'.format(phase=phase)
phase = phase.upper() phase = phase.upper()
# imply work_dir from src_root self._log("{phase} [{trusted} : {playbook}@{branch}]".format(
src_root = hostvars.get('zuul', {}).get('executor', {}).get('src_root') trusted=trusted, phase=phase, playbook=playbook, branch=branch))
if src_root:
# Ensure work_dir has a trailing / for ease of stripping
work_dir = os.path.dirname(
os.path.dirname(src_root)).rstrip('/') + '/'
# Strip work_dir from the beginning of the playbook name.
playbook = playbook.replace(work_dir, '')
# Lop off the first two path elements - ansible/pre_playbook_0
playbook = re.sub('(un)?trusted/project_[^/]+/', '', playbook)
# Remove yaml suffix
playbook = os.path.splitext(playbook)[0]
self._log("{phase} [{playbook}]".format(
phase=phase, playbook=playbook))
def v2_playbook_on_play_start(self, play): def v2_playbook_on_play_start(self, play):
self._play = play self._play = play

View File

@ -1423,6 +1423,13 @@ class AnsibleJob(object):
if index is not None: if index is not None:
cmd.extend(['-e', 'zuul_execution_phase_index=%s' % index]) cmd.extend(['-e', 'zuul_execution_phase_index=%s' % index])
cmd.extend(['-e', 'zuul_execution_trusted=%s' % str(playbook.trusted)])
cmd.extend([
'-e',
'zuul_execution_canonical_name_and_path=%s'
% playbook.canonical_name_and_path])
cmd.extend(['-e', 'zuul_execution_branch=%s' % str(playbook.branch)])
result, code = self.runAnsible( result, code = self.runAnsible(
cmd=cmd, timeout=timeout, cmd=cmd, timeout=timeout,
config_file=playbook.ansible_config, config_file=playbook.ansible_config,