Skip to content

Commit

Permalink
Merge pull request ansible#763 from AlanCoding/remember_where_you_cam…
Browse files Browse the repository at this point in the history
…e_from

add AWX meta extra_vars for workflow + schedule
  • Loading branch information
AlanCoding authored Dec 4, 2017
2 parents 21bdea0 + eecf997 commit 42d8368
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 30 deletions.
11 changes: 11 additions & 0 deletions awx/main/models/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,17 @@ def preferred_instance_groups(self):
return self.global_instance_groups
return selected_groups

def awx_meta_vars(self):
r = super(Job, self).awx_meta_vars()
if self.project:
for name in ('awx', 'tower'):
r['{}_project_revision'.format(name)] = self.project.scm_revision
if self.job_template:
for name in ('awx', 'tower'):
r['{}_job_template_id'.format(name)] = self.job_template.pk
r['{}_job_template_name'.format(name)] = self.job_template.name
return r

'''
JobNotificationMixin
'''
Expand Down
41 changes: 38 additions & 3 deletions awx/main/models/unified_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,15 +854,21 @@ def result_stdout_raw_limited(self, start_line=0, end_line=None, redact_sensitiv
def result_stdout_limited(self, start_line=0, end_line=None, redact_sensitive=False):
return self._result_stdout_raw_limited(start_line, end_line, redact_sensitive, escape_ascii=True)

@property
def workflow_job_id(self):
workflow_job = self.get_workflow_job()
if workflow_job:
return workflow_job.pk
return None

@property
def spawned_by_workflow(self):
return self.launch_type == 'workflow'

@property
def workflow_job_id(self):
def get_workflow_job(self):
if self.spawned_by_workflow:
try:
return self.unified_job_node.workflow_job.pk
return self.unified_job_node.workflow_job
except UnifiedJob.unified_job_node.RelatedObjectDoesNotExist:
pass
return None
Expand Down Expand Up @@ -1129,3 +1135,32 @@ def global_instance_groups(self):
if default_instance_group.exists():
return [default_instance_group.first()]
return []

def awx_meta_vars(self):
'''
The result of this method is used as extra_vars of a job launched
by AWX, for purposes of client playbook hooks
'''
r = {}
for name in ('awx', 'tower'):
r['{}_job_id'.format(name)] = self.pk
r['{}_job_launch_type'.format(name)] = self.launch_type
if self.created_by:
for name in ('awx', 'tower'):
r['{}_user_id'.format(name)] = self.created_by.pk
r['{}_user_name'.format(name)] = self.created_by.username
else:
wj = self.get_workflow_job()
if wj:
for name in ('awx', 'tower'):
r['{}_workflow_job_id'.format(name)] = wj.pk
r['{}_workflow_job_name'.format(name)] = wj.name
if wj.created_by:
for name in ('awx', 'tower'):
r['{}_user_id'.format(name)] = wj.created_by.pk
r['{}_user_name'.format(name)] = wj.created_by.username
if self.schedule:
for name in ('awx', 'tower'):
r['{}_schedule_id'.format(name)] = self.schedule.pk
r['{}_schedule_name'.format(name)] = self.schedule.name
return r
27 changes: 2 additions & 25 deletions awx/main/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,31 +1145,8 @@ def build_args(self, job, **kwargs):
args.append('--start-at-task=%s' % job.start_at_task)

# Define special extra_vars for AWX, combine with job.extra_vars.
extra_vars = {
'tower_job_id': job.pk,
'tower_job_launch_type': job.launch_type,
'awx_job_id': job.pk,
'awx_job_launch_type': job.launch_type,
}
if job.project:
extra_vars.update({
'tower_project_revision': job.project.scm_revision,
'awx_project_revision': job.project.scm_revision,
})
if job.job_template:
extra_vars.update({
'tower_job_template_id': job.job_template.pk,
'tower_job_template_name': job.job_template.name,
'awx_job_template_id': job.job_template.pk,
'awx_job_template_name': job.job_template.name,
})
if job.created_by:
extra_vars.update({
'tower_user_id': job.created_by.pk,
'tower_user_name': job.created_by.username,
'awx_user_id': job.created_by.pk,
'awx_user_name': job.created_by.username,
})
extra_vars = job.awx_meta_vars()

if job.extra_vars_dict:
if kwargs.get('display', False) and job.job_template:
extra_vars.update(json.loads(job.display_extra_vars()))
Expand Down
38 changes: 37 additions & 1 deletion awx/main/tests/functional/models/test_unified_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.contrib.contenttypes.models import ContentType

# AWX
from awx.main.models import UnifiedJobTemplate, Job, JobTemplate, WorkflowJobTemplate, Project
from awx.main.models import UnifiedJobTemplate, Job, JobTemplate, WorkflowJobTemplate, Project, WorkflowJob, Schedule
from awx.main.models.ha import InstanceGroup


Expand Down Expand Up @@ -110,3 +110,39 @@ class MockTaskClass:
link=success_callback,
queue='thepentagon',
task_id='something')


@pytest.mark.django_db
class TestMetaVars:
'''
Extension of unit tests with same class name
'''

def test_workflow_job_metavars(self, admin_user):
workflow_job = WorkflowJob.objects.create(
name='workflow-job',
created_by=admin_user
)
job = Job.objects.create(
name='fake-job',
launch_type='workflow'
)
workflow_job.workflow_nodes.create(job=job)
data = job.awx_meta_vars()
assert data['awx_user_name'] == admin_user.username
assert data['awx_workflow_job_id'] == workflow_job.pk

def test_scheduled_job_metavars(self, job_template, admin_user):
schedule = Schedule.objects.create(
name='job-schedule',
rrule='DTSTART:20171129T155939z\nFREQ=MONTHLY',
unified_job_template=job_template
)
job = Job.objects.create(
name='fake-job',
launch_type='workflow',
schedule=schedule,
job_template=job_template
)
data = job.awx_meta_vars()
assert data['awx_schedule_id'] == schedule.pk
1 change: 1 addition & 0 deletions awx/main/tests/unit/models/test_survey_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def job(mocker):
'pk': 1, 'job_template.pk': 1, 'job_template.name': '',
'created_by.pk': 1, 'created_by.username': 'admin',
'launch_type': 'manual',
'awx_meta_vars.return_value': {},
'inventory.get_script_data.return_value': {}})
ret.project = mocker.MagicMock(scm_revision='asdf1234')
return ret
Expand Down
47 changes: 46 additions & 1 deletion awx/main/tests/unit/models/test_unified_job_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
UnifiedJob,
WorkflowJob,
WorkflowJobNode,
Job
Job,
User,
Project,
JobTemplate
)


Expand Down Expand Up @@ -61,3 +64,45 @@ def test_log_representation():
assert job.log_format == 'job 4 (running)'
assert uj.log_format == 'unified_job 4 (running)'


class TestMetaVars:
'''
Corresponding functional test exists for cases with indirect relationships
'''

def test_job_metavars(self):
maker = User(username='joe', pk=47, id=47)
assert Job(
name='fake-job',
pk=42, id=42,
launch_type='manual',
created_by=maker
).awx_meta_vars() == {
'tower_job_id': 42,
'awx_job_id': 42,
'tower_job_launch_type': 'manual',
'awx_job_launch_type': 'manual',
'awx_user_name': 'joe',
'tower_user_name': 'joe',
'awx_user_id': 47,
'tower_user_id': 47
}

def test_project_update_metavars(self):
data = Job(
name='fake-job',
pk=40, id=40,
launch_type='manual',
project=Project(
name='jobs-sync',
scm_revision='12345444'
),
job_template=JobTemplate(
name='jobs-jt',
id=92, pk=92
)
).awx_meta_vars()
assert data['awx_project_revision'] == '12345444'
assert 'tower_job_template_id' in data
assert data['tower_job_template_id'] == 92
assert data['tower_job_template_name'] == 'jobs-jt'

0 comments on commit 42d8368

Please sign in to comment.