mirror of
https://github.com/ansible/awx.git
synced 2026-04-14 06:29:25 -02:30
delete inventories in the background via a celery task
see: #4382 see: #6279
This commit is contained in:
@@ -70,6 +70,13 @@ class Migration(migrations.Migration):
|
||||
unique_together=set([('host', 'inventory')]),
|
||||
),
|
||||
|
||||
# Background Inventory deletion
|
||||
migrations.AddField(
|
||||
model_name='inventory',
|
||||
name='pending_deletion',
|
||||
field=models.BooleanField(default=False, help_text='Flag indicating the inventory is being deleted.', editable=False),
|
||||
),
|
||||
|
||||
# Facts
|
||||
migrations.AlterField(
|
||||
model_name='fact',
|
||||
|
||||
@@ -11,7 +11,7 @@ import os.path
|
||||
|
||||
# Django
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.db import models, connection
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.db import transaction
|
||||
from django.core.exceptions import ValidationError
|
||||
@@ -20,6 +20,7 @@ from django.utils.timezone import now
|
||||
# AWX
|
||||
from awx.api.versioning import reverse
|
||||
from awx.main.constants import CLOUD_PROVIDERS
|
||||
from awx.main.consumers import emit_channel_notification
|
||||
from awx.main.fields import (
|
||||
ImplicitRoleField,
|
||||
JSONBField,
|
||||
@@ -153,6 +154,11 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin):
|
||||
null=True,
|
||||
default=None,
|
||||
)
|
||||
pending_deletion = models.BooleanField(
|
||||
default=False,
|
||||
editable=False,
|
||||
help_text=_('Flag indicating the inventory is being deleted.'),
|
||||
)
|
||||
|
||||
|
||||
def get_absolute_url(self, request=None):
|
||||
@@ -351,6 +357,12 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin):
|
||||
iobj.save(update_fields=computed_fields.keys())
|
||||
logger.debug("Finished updating inventory computed fields")
|
||||
|
||||
def websocket_emit_status(self, status):
|
||||
connection.on_commit(lambda: emit_channel_notification(
|
||||
'inventories-status_changed',
|
||||
{'group_name': 'inventories', 'inventory_id': self.id, 'status': status}
|
||||
))
|
||||
|
||||
@property
|
||||
def root_groups(self):
|
||||
group_pks = self.groups.values_list('pk', flat=True)
|
||||
|
||||
@@ -52,7 +52,7 @@ from awx.main.isolated import run, isolated_manager
|
||||
from awx.main.utils import (get_ansible_version, get_ssh_version, decrypt_field, update_scm_url,
|
||||
check_proot_installed, build_proot_temp_dir,
|
||||
wrap_args_with_proot, get_system_task_capacity, OutputEventFilter,
|
||||
parse_yaml_or_json)
|
||||
parse_yaml_or_json, ignore_inventory_computed_fields, ignore_inventory_group_removal)
|
||||
from awx.main.utils.reload import restart_local_services, stop_local_services
|
||||
from awx.main.utils.handlers import configure_external_logger
|
||||
from awx.main.consumers import emit_channel_notification
|
||||
@@ -356,6 +356,23 @@ def update_host_smart_inventory_memberships():
|
||||
return
|
||||
|
||||
|
||||
@task(queue='tower')
|
||||
def delete_inventory(inventory_id):
|
||||
i = Inventory.objects.filter(id=inventory_id)
|
||||
if not i.exists():
|
||||
logger.error("Delete Inventory failed due to missing inventory: " + str(inventory_id))
|
||||
return
|
||||
i = i[0]
|
||||
with ignore_inventory_computed_fields(), \
|
||||
ignore_inventory_group_removal():
|
||||
i.delete()
|
||||
emit_channel_notification(
|
||||
'inventories-status_changed',
|
||||
{'group_name': 'inventories', 'inventory_id': inventory_id, 'status': 'deleted'}
|
||||
)
|
||||
logger.debug('Deleted inventory: %s' % inventory_id)
|
||||
|
||||
|
||||
class BaseTask(Task):
|
||||
name = None
|
||||
model = None
|
||||
|
||||
Reference in New Issue
Block a user