Disable extra actions on save for inventory sources when running inventory import.

This commit is contained in:
Chris Church 2014-08-11 11:38:31 -04:00
parent 11e7b9b73a
commit d6b3e3079a
5 changed files with 22200 additions and 7 deletions

View File

@ -1177,6 +1177,8 @@ class Command(NoArgsCommand):
self.all_group.debug_tree()
# Merge/overwrite inventory into database.
if settings.SQL_DEBUG:
self.logger.warning('loading into database...')
with ignore_inventory_computed_fields():
if getattr(settings, 'ACTIVITY_STREAM_ENABLED_FOR_INVENTORY_SYNC', True):
self.load_into_database()

View File

@ -35,7 +35,7 @@ from awx.main.fields import AutoOneToOneField
from awx.main.models.base import *
from awx.main.models.jobs import Job
from awx.main.models.unified_jobs import *
from awx.main.utils import encrypt_field, ignore_inventory_computed_fields
from awx.main.utils import encrypt_field, ignore_inventory_computed_fields, _inventory_updates
__all__ = ['Inventory', 'Host', 'Group', 'InventorySource', 'InventoryUpdate']
@ -963,7 +963,8 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions):
if new_instance and self.inventory and replace_text in self.name:
self.name = self.name.replace(replace_text, str(self.pk))
self.save(update_fields=['name'])
self.inventory.update_computed_fields(update_groups=False, update_hosts=False)
if not getattr(_inventory_updates, 'is_updating', False):
self.inventory.update_computed_fields(update_groups=False, update_hosts=False)
def _get_current_status(self):
if self.source:

View File

@ -35,7 +35,7 @@ from djcelery.models import TaskMeta
# AWX
from awx.main.models.base import *
from awx.main.models.schedules import Schedule
from awx.main.utils import decrypt_field, get_type_for_model, emit_websocket_notification
from awx.main.utils import decrypt_field, get_type_for_model, emit_websocket_notification, _inventory_updates
__all__ = ['UnifiedJobTemplate', 'UnifiedJob']
@ -201,10 +201,11 @@ class UnifiedJobTemplate(PolymorphicModel, CommonModelNameNotUnique):
# if it hasn't been specified, then we're just doing a normal save.
update_fields = kwargs.get('update_fields', [])
# Update status and last_updated fields.
updated_fields = self._set_status_and_last_job_run(save=False)
for field in updated_fields:
if field not in update_fields:
update_fields.append(field)
if not getattr(_inventory_updates, 'is_updating', False):
updated_fields = self._set_status_and_last_job_run(save=False)
for field in updated_fields:
if field not in update_fields:
update_fields.append(field)
# Do the actual save.
super(UnifiedJobTemplate, self).save(*args, **kwargs)

View File

@ -847,6 +847,29 @@ class InventoryImportTest(BaseCommandMixin, BaseLiveServerTest):
self.assertNotEqual(new_inv.total_groups, 0)
self.assertElapsedLessThan(30)
@unittest.skipIf(getattr(settings, 'LOCAL_DEVELOPMENT', False),
'Skip this test in local development environments, '
'which may vary widely on memory.')
def test_splunk_inventory(self):
settings.DEBUG = True
new_inv = self.organizations[0].inventories.create(name='splunk')
self.assertEqual(new_inv.hosts.count(), 0)
self.assertEqual(new_inv.groups.count(), 0)
inv_file = os.path.join(os.path.dirname(__file__), 'data',
'splunk_inventory.py')
result, stdout, stderr = self.run_command('inventory_import',
inventory_id=new_inv.pk,
source=inv_file, verbosity=0)
self.assertEqual(result, None, stdout + stderr)
# Check that inventory is populated as expected within a reasonable
# amount of time. Computed fields should also be updated.
new_inv = Inventory.objects.get(pk=new_inv.pk)
self.assertNotEqual(new_inv.hosts.count(), 0)
self.assertNotEqual(new_inv.groups.count(), 0)
self.assertNotEqual(new_inv.total_hosts, 0)
self.assertNotEqual(new_inv.total_groups, 0)
self.assertElapsedLessThan(120)
def _get_ngroups_for_nhosts(self, n):
if n > 0:
return min(n, 10) + ((n - 1) / 10 + 1) + ((n - 1) / 100 + 1) + ((n - 1) / 1000 + 1)

File diff suppressed because it is too large Load Diff