mirror of
https://github.com/ansible/awx.git
synced 2026-02-28 08:18:43 -03:30
Merge pull request #582 from AlanCoding/smart_computed2
update smart inventory computed fields
This commit is contained in:
@@ -334,8 +334,13 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin):
|
|||||||
active_hosts = self.hosts
|
active_hosts = self.hosts
|
||||||
failed_hosts = active_hosts.filter(has_active_failures=True)
|
failed_hosts = active_hosts.filter(has_active_failures=True)
|
||||||
active_groups = self.groups
|
active_groups = self.groups
|
||||||
|
if self.kind == 'smart':
|
||||||
|
active_groups = active_groups.none()
|
||||||
failed_groups = active_groups.filter(has_active_failures=True)
|
failed_groups = active_groups.filter(has_active_failures=True)
|
||||||
active_inventory_sources = self.inventory_sources.filter(source__in=CLOUD_INVENTORY_SOURCES)
|
if self.kind == 'smart':
|
||||||
|
active_inventory_sources = self.inventory_sources.none()
|
||||||
|
else:
|
||||||
|
active_inventory_sources = self.inventory_sources.filter(source__in=CLOUD_INVENTORY_SOURCES)
|
||||||
failed_inventory_sources = active_inventory_sources.filter(last_job_failed=True)
|
failed_inventory_sources = active_inventory_sources.filter(last_job_failed=True)
|
||||||
computed_fields = {
|
computed_fields = {
|
||||||
'has_active_failures': bool(failed_hosts.count()),
|
'has_active_failures': bool(failed_hosts.count()),
|
||||||
@@ -399,6 +404,10 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin):
|
|||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
self._update_host_smart_inventory_memeberships()
|
self._update_host_smart_inventory_memeberships()
|
||||||
super(Inventory, self).save(*args, **kwargs)
|
super(Inventory, self).save(*args, **kwargs)
|
||||||
|
if (self.kind == 'smart' and 'host_filter' in kwargs.get('update_fields', ['host_filter']) and
|
||||||
|
connection.vendor != 'sqlite'):
|
||||||
|
# Minimal update of host_count for smart inventory host filter changes
|
||||||
|
self.update_computed_fields(update_groups=False, update_hosts=False)
|
||||||
|
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
self._update_host_smart_inventory_memeberships()
|
self._update_host_smart_inventory_memeberships()
|
||||||
|
|||||||
@@ -423,13 +423,22 @@ def update_host_smart_inventory_memberships():
|
|||||||
smart_inventories = Inventory.objects.filter(kind='smart', host_filter__isnull=False, pending_deletion=False)
|
smart_inventories = Inventory.objects.filter(kind='smart', host_filter__isnull=False, pending_deletion=False)
|
||||||
SmartInventoryMembership.objects.all().delete()
|
SmartInventoryMembership.objects.all().delete()
|
||||||
memberships = []
|
memberships = []
|
||||||
|
changed_inventories = set([])
|
||||||
for smart_inventory in smart_inventories:
|
for smart_inventory in smart_inventories:
|
||||||
memberships.extend([SmartInventoryMembership(inventory_id=smart_inventory.id, host_id=host_id[0])
|
add_for_inventory = [
|
||||||
for host_id in smart_inventory.hosts.values_list('id')])
|
SmartInventoryMembership(inventory_id=smart_inventory.id, host_id=host_id[0])
|
||||||
|
for host_id in smart_inventory.hosts.values_list('id')
|
||||||
|
]
|
||||||
|
memberships.extend(add_for_inventory)
|
||||||
|
if add_for_inventory:
|
||||||
|
changed_inventories.add(smart_inventory)
|
||||||
SmartInventoryMembership.objects.bulk_create(memberships)
|
SmartInventoryMembership.objects.bulk_create(memberships)
|
||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
logger.error("Update Host Smart Inventory Memberships failed due to an exception: " + str(e))
|
logger.error("Update Host Smart Inventory Memberships failed due to an exception: " + str(e))
|
||||||
return
|
return
|
||||||
|
# Update computed fields for changed inventories outside atomic action
|
||||||
|
for smart_inventory in changed_inventories:
|
||||||
|
smart_inventory.update_computed_fields(update_groups=False, update_hosts=False)
|
||||||
|
|
||||||
|
|
||||||
@task(bind=True, queue='tower', base=LogErrorsTask, max_retries=5)
|
@task(bind=True, queue='tower', base=LogErrorsTask, max_retries=5)
|
||||||
|
|||||||
Reference in New Issue
Block a user