mirror of
https://github.com/ansible/awx.git
synced 2026-03-26 13:25:02 -02:30
Ensure Smart Inventory hosts are assigned job history
This commit is contained in:
@@ -1185,30 +1185,48 @@ class JobEvent(CreatedModifiedModel):
|
|||||||
parent = parent[0]
|
parent = parent[0]
|
||||||
parent._update_hosts(qs.values_list('id', flat=True))
|
parent._update_hosts(qs.values_list('id', flat=True))
|
||||||
|
|
||||||
def _update_host_summary_from_stats(self):
|
def _hostnames(self):
|
||||||
from awx.main.models.inventory import Host
|
|
||||||
hostnames = set()
|
hostnames = set()
|
||||||
try:
|
try:
|
||||||
for stat in ('changed', 'dark', 'failures', 'ok', 'processed', 'skipped'):
|
for stat in ('changed', 'dark', 'failures', 'ok', 'processed', 'skipped'):
|
||||||
hostnames.update(self.event_data.get(stat, {}).keys())
|
hostnames.update(self.event_data.get(stat, {}).keys())
|
||||||
except AttributeError: # In case event_data or v isn't a dict.
|
except AttributeError: # In case event_data or v isn't a dict.
|
||||||
pass
|
pass
|
||||||
|
return hostnames
|
||||||
|
|
||||||
|
def _update_smart_inventory_hosts(self, hostnames):
|
||||||
|
'''If the job the job_event is for was run using a Smart Inventory
|
||||||
|
update the hosts fields related to job history and summary.
|
||||||
|
'''
|
||||||
with ignore_inventory_computed_fields():
|
with ignore_inventory_computed_fields():
|
||||||
qs = Host.objects.filter(inventory__jobs__id=self.job_id,
|
if hasattr(self.job, 'inventory') and self.job.inventory.kind == 'smart':
|
||||||
name__in=hostnames)
|
logger.debug(self.job.inventory)
|
||||||
|
smart_hosts = self.job.inventory.hosts.filter(name__in=hostnames)
|
||||||
|
for smart_host in smart_hosts:
|
||||||
|
host_summary = self.job.job_host_summaries.get(host_name=smart_host.name)
|
||||||
|
smart_host.inventory.jobs.add(self.job)
|
||||||
|
smart_host.last_job_id = self.job_id
|
||||||
|
smart_host.last_job_host_summary_id = host_summary.pk
|
||||||
|
smart_host.save()
|
||||||
|
|
||||||
|
def _update_host_summary_from_stats(self, hostnames):
|
||||||
|
with ignore_inventory_computed_fields():
|
||||||
|
from awx.main.models.inventory import Host
|
||||||
|
qs = Host.objects.filter(inventory__jobs__id=self.job_id, name__in=hostnames)
|
||||||
job = self.job
|
job = self.job
|
||||||
for host in hostnames:
|
for host in hostnames:
|
||||||
host_stats = {}
|
host_stats = {}
|
||||||
for stat in ('changed', 'dark', 'failures', 'ok', 'processed', 'skipped'):
|
for stat in ('changed', 'dark', 'failures', 'ok', 'processed', 'skipped'):
|
||||||
try:
|
try:
|
||||||
host_stats[stat] = self.event_data.get(stat, {}).get(host, 0)
|
host_stats[stat] = self.event_data.get(stat, {}).get(host, 0)
|
||||||
except AttributeError: # in case event_data[stat] isn't a dict.
|
except AttributeError: # in case event_data[stat] isn't a dict.
|
||||||
pass
|
pass
|
||||||
if qs.filter(name=host).exists():
|
if qs.filter(name=host).exists():
|
||||||
host_actual = qs.get(name=host)
|
host_actual = qs.get(name=host)
|
||||||
host_summary, created = job.job_host_summaries.get_or_create(host=host_actual, host_name=host_actual.name, defaults=host_stats)
|
host_summary, created = job.job_host_summaries.get_or_create(host=host_actual, host_name=host_actual.name, defaults=host_stats)
|
||||||
else:
|
else:
|
||||||
host_summary, created = job.job_host_summaries.get_or_create(host_name=host, defaults=host_stats)
|
host_summary, created = job.job_host_summaries.get_or_create(host_name=host, defaults=host_stats)
|
||||||
|
|
||||||
if not created:
|
if not created:
|
||||||
update_fields = []
|
update_fields = []
|
||||||
for stat, value in host_stats.items():
|
for stat, value in host_stats.items():
|
||||||
@@ -1217,8 +1235,6 @@ class JobEvent(CreatedModifiedModel):
|
|||||||
update_fields.append(stat)
|
update_fields.append(stat)
|
||||||
if update_fields:
|
if update_fields:
|
||||||
host_summary.save(update_fields=update_fields)
|
host_summary.save(update_fields=update_fields)
|
||||||
job.inventory.update_computed_fields()
|
|
||||||
emit_channel_notification('jobs-summary', dict(group_name='jobs', unified_job_id=job.id))
|
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
from awx.main.models.inventory import Host
|
from awx.main.models.inventory import Host
|
||||||
@@ -1249,7 +1265,13 @@ class JobEvent(CreatedModifiedModel):
|
|||||||
self._update_hosts()
|
self._update_hosts()
|
||||||
if self.event == 'playbook_on_stats':
|
if self.event == 'playbook_on_stats':
|
||||||
self._update_parents_failed_and_changed()
|
self._update_parents_failed_and_changed()
|
||||||
self._update_host_summary_from_stats()
|
|
||||||
|
hostnames = self._hostnames()
|
||||||
|
self._update_host_summary_from_stats(hostnames)
|
||||||
|
self._update_smart_inventory_hosts(hostnames)
|
||||||
|
self.job.inventory.update_computed_fields()
|
||||||
|
|
||||||
|
emit_channel_notification('jobs-summary', dict(group_name='jobs', unified_job_id=self.job.id))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_from_data(self, **kwargs):
|
def create_from_data(self, **kwargs):
|
||||||
|
|||||||
Reference in New Issue
Block a user