2021-07-08 06:23:18 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
2023-02-08 02:14:09 +00:00
|
|
|
|
from django.db.models.signals import (
|
2023-02-09 12:48:25 +00:00
|
|
|
|
m2m_changed, pre_delete, post_delete, pre_save, post_save
|
2023-02-08 02:14:09 +00:00
|
|
|
|
)
|
2021-07-08 06:23:18 +00:00
|
|
|
|
from django.dispatch import receiver
|
2023-02-08 05:53:21 +00:00
|
|
|
|
from django.utils.translation import gettext_noop
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
|
from assets.models import Asset, Node, Host, Database, Device, Web, Cloud
|
|
|
|
|
from assets.tasks import test_assets_connectivity_task, gather_assets_facts_task
|
|
|
|
|
from common.const.signals import POST_REMOVE, PRE_REMOVE
|
|
|
|
|
from common.decorators import on_transaction_commit, merge_delay_run, key_by_org
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from common.utils import get_logger
|
2023-02-17 09:14:53 +00:00
|
|
|
|
from orgs.utils import current_org
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(pre_save, sender=Node)
|
|
|
|
|
def on_node_pre_save(sender, instance: Node, **kwargs):
|
|
|
|
|
instance.parent_key = instance.compute_parent_key()
|
|
|
|
|
|
|
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
|
@merge_delay_run(ttl=5, key=key_by_org)
|
2023-02-13 11:14:00 +00:00
|
|
|
|
def test_assets_connectivity_handler(assets=()):
|
2023-02-08 05:53:21 +00:00
|
|
|
|
task_name = gettext_noop("Test assets connectivity ")
|
2023-02-20 06:06:26 +00:00
|
|
|
|
asset_ids = [a.id for a in assets]
|
|
|
|
|
test_assets_connectivity_task.delay(asset_ids, str(current_org.id), task_name)
|
2023-02-08 05:53:21 +00:00
|
|
|
|
|
|
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
|
@merge_delay_run(ttl=5, key=key_by_org)
|
2023-02-13 11:14:00 +00:00
|
|
|
|
def gather_assets_facts_handler(assets=()):
|
2023-02-09 12:48:25 +00:00
|
|
|
|
if not assets:
|
|
|
|
|
logger.info("No assets to update hardware info")
|
|
|
|
|
return
|
|
|
|
|
name = gettext_noop("Gather asset hardware info")
|
2023-02-20 06:06:26 +00:00
|
|
|
|
asset_ids = [a.id for a in assets]
|
|
|
|
|
gather_assets_facts_task.delay(asset_ids, str(current_org.id), task_name=name)
|
2023-02-09 12:48:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@merge_delay_run(ttl=5, key=key_by_org)
|
2023-02-13 11:14:00 +00:00
|
|
|
|
def ensure_asset_has_node(assets=()):
|
2023-02-09 12:48:25 +00:00
|
|
|
|
asset_ids = [asset.id for asset in assets]
|
|
|
|
|
has_ids = Asset.nodes.through.objects \
|
|
|
|
|
.filter(asset_id__in=asset_ids) \
|
|
|
|
|
.values_list('asset_id', flat=True)
|
|
|
|
|
need_ids = set(asset_ids) - set(has_ids)
|
|
|
|
|
if not need_ids:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
org_root = Node.org_root()
|
|
|
|
|
org_root.assets.add(*need_ids)
|
2023-02-08 05:53:21 +00:00
|
|
|
|
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
@receiver(post_save, sender=Asset)
|
|
|
|
|
@on_transaction_commit
|
2023-01-16 11:02:09 +00:00
|
|
|
|
def on_asset_create(sender, instance=None, created=False, **kwargs):
|
2021-07-08 06:23:18 +00:00
|
|
|
|
"""
|
|
|
|
|
当资产创建时,更新硬件信息,更新可连接性
|
|
|
|
|
确保资产必须属于一个节点
|
|
|
|
|
"""
|
2023-01-16 11:02:09 +00:00
|
|
|
|
if not created:
|
|
|
|
|
return
|
|
|
|
|
logger.info("Asset create signal recv: {}".format(instance))
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2024-01-23 08:45:59 +00:00
|
|
|
|
ensure_asset_has_node.delay(assets=(instance,))
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
|
# 获取资产硬件信息
|
2023-04-10 02:57:44 +00:00
|
|
|
|
auto_config = instance.auto_config
|
|
|
|
|
if auto_config.get('ping_enabled'):
|
2023-02-09 12:48:25 +00:00
|
|
|
|
logger.debug('Asset {} ping enabled, test connectivity'.format(instance.name))
|
2024-01-23 08:45:59 +00:00
|
|
|
|
test_assets_connectivity_handler.delay(assets=(instance,))
|
2023-04-10 02:57:44 +00:00
|
|
|
|
if auto_config.get('gather_facts_enabled'):
|
2023-02-09 12:48:25 +00:00
|
|
|
|
logger.debug('Asset {} gather facts enabled, gather facts'.format(instance.name))
|
2023-02-13 11:14:00 +00:00
|
|
|
|
gather_assets_facts_handler(assets=(instance,))
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2022-12-22 10:56:54 +00:00
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
RELATED_NODE_IDS = '_related_node_ids'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(pre_delete, sender=Asset)
|
|
|
|
|
def on_asset_delete(instance: Asset, using, **kwargs):
|
2023-02-09 12:48:25 +00:00
|
|
|
|
node_ids = Node.objects.filter(assets=instance) \
|
|
|
|
|
.distinct().values_list('id', flat=True)
|
2024-01-02 08:11:56 +00:00
|
|
|
|
node_ids = list(node_ids)
|
|
|
|
|
logger.debug("Asset pre delete signal recv: {}, node_ids: {}".format(instance, node_ids))
|
|
|
|
|
setattr(instance, RELATED_NODE_IDS, list(node_ids))
|
2021-07-08 06:23:18 +00:00
|
|
|
|
m2m_changed.send(
|
2023-02-09 12:48:25 +00:00
|
|
|
|
sender=Asset.nodes.through, instance=instance,
|
|
|
|
|
reverse=False, model=Node, pk_set=node_ids,
|
|
|
|
|
using=using, action=PRE_REMOVE
|
2021-07-08 06:23:18 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Asset)
|
|
|
|
|
def on_asset_post_delete(instance: Asset, using, **kwargs):
|
2023-02-16 08:51:03 +00:00
|
|
|
|
node_ids = getattr(instance, RELATED_NODE_IDS, [])
|
2024-01-02 08:11:56 +00:00
|
|
|
|
logger.debug("Asset post delete signal recv: {}, node_ids: {}".format(instance, node_ids))
|
2021-07-08 06:23:18 +00:00
|
|
|
|
if node_ids:
|
|
|
|
|
m2m_changed.send(
|
|
|
|
|
sender=Asset.nodes.through, instance=instance, reverse=False,
|
|
|
|
|
model=Node, pk_set=node_ids, using=using, action=POST_REMOVE
|
|
|
|
|
)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
|
|
|
2023-02-08 11:32:09 +00:00
|
|
|
|
@on_transaction_commit
|
|
|
|
|
def resend_to_asset_signals(sender, signal, instance, **kwargs):
|
|
|
|
|
signal.send(sender=Asset, instance=instance.asset_ptr, **kwargs)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for model in (Host, Database, Device, Web, Cloud):
|
|
|
|
|
for s in (pre_save, post_save):
|
|
|
|
|
s.connect(resend_to_asset_signals, sender=model)
|