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
changes/84/485284/2
Monty Taylor 5 years ago
parent 210e218fea
commit 1c39ec7361
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
  1. 22
      zuul/ansible/callback/zuul_json.py
  2. 31
      zuul/ansible/callback/zuul_stream.py
  3. 7
      zuul/executor/server.py

@ -24,7 +24,6 @@ __metaclass__ = type
import json
import os
import re
from ansible.plugins.callback import CallbackBase
try:
@ -59,7 +58,6 @@ class CallbackModule(CallbackBase):
# Get the hostvars from just one host - the vars we're looking for will
# be identical on all of them
hostvars = next(iter(play._variable_manager._hostvars.values()))
playbook = self._playbook_name
self._playbook_name = None
# TODO(mordred) For now, protect specific variable lookups to make it
@ -68,24 +66,16 @@ class CallbackModule(CallbackBase):
# tool.
phase = hostvars.get('zuul_execution_phase')
index = hostvars.get('zuul_execution_phase_index')
# imply work_dir from src_root
src_root = hostvars.get('zuul', {}).get('executor', {}).get('src_root')
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]
playbook = hostvars.get('zuul_execution_canonical_name_and_path')
trusted = hostvars.get('zuul_execution_trusted')
trusted = True if trusted == "True" else False
branch = hostvars.get('zuul_execution_branch')
self.playbook['playbook'] = playbook
self.playbook['phase'] = phase
self.playbook['index'] = index
self.playbook['trusted'] = trusted
self.playbook['branch'] = branch
def _new_play(self, play):
return {

@ -23,7 +23,6 @@ import datetime
import logging
import json
import os
import re
import socket
import threading
import time
@ -165,34 +164,20 @@ class CallbackModule(default.CallbackModule):
# Get the hostvars from just one host - the vars we're looking for will
# be identical on all of them
hostvars = next(iter(self._play._variable_manager._hostvars.values()))
playbook = self._playbook_name
self._playbook_name = None
# TODO(mordred) For now, protect specific variable lookups to make it
# not absurdly strange to run local tests with the callback plugin
# enabled. Remove once we have a "run playbook like zuul runs playbook"
# tool.
phase = hostvars.get('zuul_execution_phase')
phase = hostvars.get('zuul_execution_phase', '')
playbook = hostvars.get('zuul_execution_canonical_name_and_path')
trusted = hostvars.get('zuul_execution_trusted')
trusted = 'trusted' if trusted == "True" else 'untrusted'
branch = hostvars.get('zuul_execution_branch')
if phase and phase != 'run':
phase = '{phase}-run'.format(phase=phase)
phase = phase.upper()
# imply work_dir from src_root
src_root = hostvars.get('zuul', {}).get('executor', {}).get('src_root')
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))
self._log("{phase} [{trusted} : {playbook}@{branch}]".format(
trusted=trusted, phase=phase, playbook=playbook, branch=branch))
def v2_playbook_on_play_start(self, play):
self._play = play

@ -1423,6 +1423,13 @@ class AnsibleJob(object):
if index is not None:
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(
cmd=cmd, timeout=timeout,
config_file=playbook.ansible_config,

Loading…
Cancel
Save