Plumb job variables through to ansible

Change-Id: Ifc64fce1870fa2c0d205d6177af4896614303226
This commit is contained in:
James E. Blair 2017-02-24 23:07:21 -05:00
parent e1e92d677f
commit 490cf04f71
7 changed files with 33 additions and 7 deletions

View File

@ -732,7 +732,7 @@ class RecordingLaunchServer(zuul.launcher.server.LaunchServer):
self.running_builds.append(build)
self.job_builds[job.unique] = build
args = json.loads(job.arguments)
args['zuul']['_test'] = dict(test_root=self._test_root)
args['vars']['zuul']['_test'] = dict(test_root=self._test_root)
job.arguments = json.dumps(args)
self.job_workers[job.unique] = RecordingAnsibleJob(self, job)
self.job_workers[job.unique].run()

View File

@ -1,7 +1,7 @@
- hosts: all
tasks:
- file:
path: "{{zuul._test.test_root}}/{{zuul.uuid}}.flag"
path: "{{flagpath}}"
state: touch
- copy:
src: "{{zuul._test.test_root}}/{{zuul.uuid}}.flag"

View File

@ -40,5 +40,7 @@
name: python27
pre-run: pre
post-run: post
vars:
flagpath: "{{zuul._test.test_root}}/{{zuul.uuid}}.flag"
roles:
- zuul: bare-role

View File

@ -116,6 +116,7 @@ class JobParser(object):
'_source_context': model.SourceContext,
'roles': to_list(role),
'repos': to_list(str),
'vars': dict,
}
return vs.Schema(job)
@ -206,6 +207,10 @@ class JobParser(object):
roles.append(r)
job.roles = job.roles.union(set(roles))
variables = conf.get('vars', None)
if variables:
job.updateVariables(variables)
# If the definition for this job came from a project repo,
# implicitly apply a branch matcher for the branch it was on.
if (not job.source_context.trusted):

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
import gear
import json
import logging
@ -276,7 +277,6 @@ class LaunchClient(object):
params['ZUUL_URL'] = item.current_build_set.zuul_url
params['ZUUL_VOTING'] = job.voting and '1' or '0'
if hasattr(item.change, 'refspec'):
zuul_params['branch'] = item.change.branch
changes_str = '^'.join(
['%s:%s:%s' % (i.change.project.name, i.change.branch,
i.change.refspec)
@ -352,7 +352,8 @@ class LaunchClient(object):
public_ipv6=node.public_ipv6,
public_ipv4=node.public_ipv4))
params['nodes'] = nodes
params['zuul'] = zuul_params
params['vars'] = copy.deepcopy(job.variables)
params['vars']['zuul'] = zuul_params
projects = set()
if job.repos:
for repo in job.repos:

View File

@ -814,7 +814,7 @@ class AnsibleJob(object):
self.jobdir.known_hosts))
with open(self.jobdir.vars, 'w') as vars_yaml:
zuul_vars = dict(zuul=args['zuul'])
zuul_vars = dict(args['vars'])
zuul_vars['zuul']['launcher'] = dict(src_root=self.jobdir.src_root,
log_root=self.jobdir.log_root)
vars_yaml.write(

View File

@ -687,7 +687,7 @@ class Job(object):
# project-pipeline.
self.execution_attributes = dict(
timeout=None,
# variables={},
variables={},
nodeset=NodeSet(),
auth={},
workspace=None,
@ -756,6 +756,22 @@ class Job(object):
if not self.run:
self.run = self.implied_run
def updateVariables(self, other_vars):
v = self.variables
Job._deepUpdate(v, other_vars)
self.variables = v
@staticmethod
def _deepUpdate(a, b):
# Merge nested dictionaries if possible, otherwise, overwrite
# the value in 'a' with the value in 'b'.
for k, bv in b.items():
av = a.get(k)
if isinstance(av, dict) and isinstance(bv, dict):
Job._deepUpdate(av, bv)
else:
a[k] = bv
def inheritFrom(self, other):
"""Copy the inheritable attributes which have been set on the other
job to this job."""
@ -796,7 +812,7 @@ class Job(object):
"%s=%s with variant %s" % (
repr(self), k, other._get(k),
repr(other)))
if k not in set(['pre_run', 'post_run', 'roles']):
if k not in set(['pre_run', 'post_run', 'roles', 'variables']):
setattr(self, k, copy.deepcopy(other._get(k)))
# Don't set final above so that we don't trip an error halfway
@ -810,6 +826,8 @@ class Job(object):
self.post_run = other.post_run + self.post_run
if other._get('roles') is not None:
self.roles = self.roles.union(other.roles)
if other._get('variables') is not None:
self.updateVariables(other.variables)
for k in self.context_attributes:
if (other._get(k) is not None and