2022-10-19 09:05:21 +00:00
|
|
|
from celery import shared_task
|
2022-11-15 08:29:40 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-10-19 09:05:21 +00:00
|
|
|
|
2022-11-11 08:13:16 +00:00
|
|
|
from assets.const import AutomationTypes
|
2023-02-20 10:00:29 +00:00
|
|
|
from common.utils import get_logger, get_object_or_none
|
|
|
|
from orgs.utils import tmp_to_root_org, tmp_to_org
|
2022-10-19 09:05:21 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2023-02-24 09:59:32 +00:00
|
|
|
def task_activity_callback(self, pid, trigger, tp, *args, **kwargs):
|
2023-02-17 09:14:53 +00:00
|
|
|
model = AutomationTypes.get_type_model(tp)
|
|
|
|
with tmp_to_root_org():
|
|
|
|
instance = get_object_or_none(model, pk=pid)
|
|
|
|
if not instance:
|
|
|
|
return
|
|
|
|
if not instance.latest_execution:
|
|
|
|
return
|
|
|
|
resource_ids = instance.latest_execution.get_all_asset_ids()
|
|
|
|
return resource_ids, instance.org_id
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(
|
|
|
|
queue='ansible', verbose_name=_('Asset execute automation'),
|
|
|
|
activity_callback=task_activity_callback
|
|
|
|
)
|
2023-02-20 10:00:29 +00:00
|
|
|
def execute_asset_automation_task(pid, trigger, tp):
|
2022-11-11 11:15:43 +00:00
|
|
|
model = AutomationTypes.get_type_model(tp)
|
2022-10-19 09:05:21 +00:00
|
|
|
with tmp_to_root_org():
|
2022-11-08 09:54:51 +00:00
|
|
|
instance = get_object_or_none(model, pk=pid)
|
2022-10-19 09:05:21 +00:00
|
|
|
if not instance:
|
2022-10-25 10:43:34 +00:00
|
|
|
logger.error("No automation task found: {}".format(pid))
|
2022-10-19 09:05:21 +00:00
|
|
|
return
|
|
|
|
with tmp_to_org(instance.org):
|
|
|
|
instance.execute(trigger)
|