2017-12-29 15:53:45 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
2019-08-23 10:23:07 +00:00
|
|
|
|
from django.db.models.signals import (
|
2020-11-23 09:26:06 +00:00
|
|
|
|
post_save, m2m_changed, pre_delete, post_delete, pre_save
|
2019-08-23 10:23:07 +00:00
|
|
|
|
)
|
2017-12-29 15:53:45 +00:00
|
|
|
|
from django.dispatch import receiver
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
|
from common.exceptions import M2MReverseNotAllowed
|
2021-02-05 05:29:29 +00:00
|
|
|
|
from common.const.signals import POST_ADD, POST_REMOVE, PRE_REMOVE
|
2020-03-12 08:24:38 +00:00
|
|
|
|
from common.utils import get_logger
|
2019-06-13 10:58:43 +00:00
|
|
|
|
from common.decorator import on_transaction_commit
|
2021-02-05 05:29:29 +00:00
|
|
|
|
from assets.models import Asset, SystemUser, Node
|
2020-08-16 15:08:58 +00:00
|
|
|
|
from users.models import User
|
2021-02-05 05:29:29 +00:00
|
|
|
|
from assets.tasks import (
|
2019-03-18 02:15:33 +00:00
|
|
|
|
update_assets_hardware_info_util,
|
|
|
|
|
test_asset_connectivity_util,
|
2020-03-16 11:15:29 +00:00
|
|
|
|
push_system_user_to_assets_manual,
|
|
|
|
|
push_system_user_to_assets,
|
2019-09-19 09:27:49 +00:00
|
|
|
|
add_nodes_assets_to_system_users
|
2019-03-18 02:15:33 +00:00
|
|
|
|
)
|
2017-12-29 15:53:45 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_asset_hardware_info_on_created(asset):
|
|
|
|
|
logger.debug("Update asset `{}` hardware info".format(asset))
|
|
|
|
|
update_assets_hardware_info_util.delay([asset])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_asset_conn_on_created(asset):
|
2018-12-18 09:28:45 +00:00
|
|
|
|
logger.debug("Test asset `{}` connectivity".format(asset))
|
|
|
|
|
test_asset_connectivity_util.delay([asset])
|
2017-12-29 15:53:45 +00:00
|
|
|
|
|
|
|
|
|
|
2020-11-23 09:26:06 +00:00
|
|
|
|
@receiver(pre_save, sender=Node)
|
|
|
|
|
def on_node_pre_save(sender, instance: Node, **kwargs):
|
|
|
|
|
instance.parent_key = instance.compute_parent_key()
|
|
|
|
|
|
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
|
@receiver(post_save, sender=Asset)
|
2019-06-13 10:58:43 +00:00
|
|
|
|
@on_transaction_commit
|
2018-02-25 10:08:00 +00:00
|
|
|
|
def on_asset_created_or_update(sender, instance=None, created=False, **kwargs):
|
2019-08-23 10:23:07 +00:00
|
|
|
|
"""
|
|
|
|
|
当资产创建时,更新硬件信息,更新可连接性
|
2019-08-28 03:43:55 +00:00
|
|
|
|
确保资产必须属于一个节点
|
2019-08-23 10:23:07 +00:00
|
|
|
|
"""
|
2018-02-25 10:08:00 +00:00
|
|
|
|
if created:
|
2019-08-28 03:43:55 +00:00
|
|
|
|
logger.info("Asset create signal recv: {}".format(instance))
|
2019-06-13 10:58:43 +00:00
|
|
|
|
|
|
|
|
|
# 获取资产硬件信息
|
2017-12-29 15:53:45 +00:00
|
|
|
|
update_asset_hardware_info_on_created(instance)
|
|
|
|
|
test_asset_conn_on_created(instance)
|
2018-02-09 03:12:40 +00:00
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
|
# 确保资产存在一个节点
|
|
|
|
|
has_node = instance.nodes.all().exists()
|
|
|
|
|
if not has_node:
|
|
|
|
|
instance.nodes.add(Node.org_root())
|
2018-12-17 03:49:57 +00:00
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=SystemUser, dispatch_uid="jms")
|
2020-10-26 11:52:50 +00:00
|
|
|
|
@on_transaction_commit
|
|
|
|
|
def on_system_user_update(instance: SystemUser, created, **kwargs):
|
2019-08-23 10:23:07 +00:00
|
|
|
|
"""
|
|
|
|
|
当系统用户更新时,可能更新了秘钥,用户名等,这时要自动推送系统用户到资产上,
|
|
|
|
|
其实应该当 用户名,密码,秘钥 sudo等更新时再推送,这里偷个懒,
|
|
|
|
|
这里直接取了 instance.assets 因为nodes和系统用户发生变化时,会自动将nodes下的资产
|
|
|
|
|
关联到上面
|
|
|
|
|
"""
|
2018-02-09 03:12:40 +00:00
|
|
|
|
if instance and not created:
|
2019-08-28 03:43:55 +00:00
|
|
|
|
logger.info("System user update signal recv: {}".format(instance))
|
2019-08-23 10:23:07 +00:00
|
|
|
|
assets = instance.assets.all().valid()
|
2020-10-26 11:52:50 +00:00
|
|
|
|
push_system_user_to_assets.delay(instance.id, [_asset.id for _asset in assets])
|
2018-02-09 03:12:40 +00:00
|
|
|
|
|
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
|
@receiver(m2m_changed, sender=SystemUser.assets.through)
|
2020-11-26 11:53:15 +00:00
|
|
|
|
@on_transaction_commit
|
2020-10-26 11:52:50 +00:00
|
|
|
|
def on_system_user_assets_change(instance, action, model, pk_set, **kwargs):
|
2019-08-23 10:23:07 +00:00
|
|
|
|
"""
|
|
|
|
|
当系统用户和资产关系发生变化时,应该重新推送系统用户到新添加的资产中
|
|
|
|
|
"""
|
2020-08-16 15:08:58 +00:00
|
|
|
|
if action != POST_ADD:
|
2019-08-28 03:43:55 +00:00
|
|
|
|
return
|
|
|
|
|
logger.debug("System user assets change signal recv: {}".format(instance))
|
|
|
|
|
if model == Asset:
|
2021-03-08 02:08:51 +00:00
|
|
|
|
system_user_ids = [instance.id]
|
|
|
|
|
asset_ids = pk_set
|
2019-08-28 03:43:55 +00:00
|
|
|
|
else:
|
2021-03-08 02:08:51 +00:00
|
|
|
|
system_user_ids = pk_set
|
|
|
|
|
asset_ids = [instance.id]
|
|
|
|
|
for system_user_id in system_user_ids:
|
|
|
|
|
push_system_user_to_assets.delay(system_user_id, asset_ids)
|
2019-08-28 03:43:55 +00:00
|
|
|
|
|
|
|
|
|
|
2020-03-16 11:15:29 +00:00
|
|
|
|
@receiver(m2m_changed, sender=SystemUser.users.through)
|
2020-11-26 11:53:15 +00:00
|
|
|
|
@on_transaction_commit
|
2020-11-25 05:28:01 +00:00
|
|
|
|
def on_system_user_users_change(sender, instance: SystemUser, action, model, pk_set, reverse, **kwargs):
|
2020-03-16 11:15:29 +00:00
|
|
|
|
"""
|
|
|
|
|
当系统用户和用户关系发生变化时,应该重新推送系统用户资产中
|
|
|
|
|
"""
|
2020-08-16 15:08:58 +00:00
|
|
|
|
if action != POST_ADD:
|
2020-03-16 11:15:29 +00:00
|
|
|
|
return
|
2020-11-25 05:28:01 +00:00
|
|
|
|
|
|
|
|
|
if reverse:
|
|
|
|
|
raise M2MReverseNotAllowed
|
|
|
|
|
|
2020-03-16 11:15:29 +00:00
|
|
|
|
if not instance.username_same_with_user:
|
|
|
|
|
return
|
2020-11-25 05:28:01 +00:00
|
|
|
|
|
2020-03-16 11:15:29 +00:00
|
|
|
|
logger.debug("System user users change signal recv: {}".format(instance))
|
2020-11-25 05:28:01 +00:00
|
|
|
|
usernames = model.objects.filter(pk__in=pk_set).values_list('username', flat=True)
|
|
|
|
|
|
|
|
|
|
for username in usernames:
|
|
|
|
|
push_system_user_to_assets_manual.delay(instance, username)
|
2020-03-16 11:15:29 +00:00
|
|
|
|
|
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
|
@receiver(m2m_changed, sender=SystemUser.nodes.through)
|
2020-11-26 11:53:15 +00:00
|
|
|
|
@on_transaction_commit
|
2019-08-28 03:43:55 +00:00
|
|
|
|
def on_system_user_nodes_change(sender, instance=None, action=None, model=None, pk_set=None, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
当系统用户和节点关系发生变化时,应该将节点下资产关联到新的系统用户上
|
|
|
|
|
"""
|
2020-08-16 15:08:58 +00:00
|
|
|
|
if action != POST_ADD:
|
2019-08-28 03:43:55 +00:00
|
|
|
|
return
|
2019-09-19 09:27:49 +00:00
|
|
|
|
logger.info("System user nodes update signal recv: {}".format(instance))
|
2019-08-28 03:43:55 +00:00
|
|
|
|
|
|
|
|
|
queryset = model.objects.filter(pk__in=pk_set)
|
|
|
|
|
if model == Node:
|
|
|
|
|
nodes_keys = queryset.values_list('key', flat=True)
|
|
|
|
|
system_users = [instance]
|
|
|
|
|
else:
|
|
|
|
|
nodes_keys = [instance.key]
|
|
|
|
|
system_users = queryset
|
2019-09-19 09:27:49 +00:00
|
|
|
|
add_nodes_assets_to_system_users.delay(nodes_keys, system_users)
|
2019-08-28 03:43:55 +00:00
|
|
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
@receiver(m2m_changed, sender=SystemUser.groups.through)
|
2020-08-16 15:08:58 +00:00
|
|
|
|
def on_system_user_groups_change(instance, action, pk_set, reverse, **kwargs):
|
2020-03-12 08:24:38 +00:00
|
|
|
|
"""
|
|
|
|
|
当系统用户和用户组关系发生变化时,应该将组下用户关联到新的系统用户上
|
|
|
|
|
"""
|
2020-08-16 15:08:58 +00:00
|
|
|
|
if action != POST_ADD:
|
2020-03-12 08:24:38 +00:00
|
|
|
|
return
|
2020-08-16 15:08:58 +00:00
|
|
|
|
if reverse:
|
|
|
|
|
raise M2MReverseNotAllowed
|
2020-03-12 08:24:38 +00:00
|
|
|
|
logger.info("System user groups update signal recv: {}".format(instance))
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
|
users = User.objects.filter(groups__id__in=pk_set).distinct()
|
2020-10-26 10:23:52 +00:00
|
|
|
|
instance.users.add(*users)
|
2019-08-23 10:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
|
@receiver(m2m_changed, sender=Asset.nodes.through)
|
2020-08-16 15:08:58 +00:00
|
|
|
|
def on_asset_nodes_add(instance, action, reverse, pk_set, **kwargs):
|
2019-08-23 10:23:07 +00:00
|
|
|
|
"""
|
2020-08-16 15:08:58 +00:00
|
|
|
|
本操作共访问 4 次数据库
|
|
|
|
|
|
2019-08-23 10:23:07 +00:00
|
|
|
|
当资产的节点发生变化时,或者 当节点的资产关系发生变化时,
|
|
|
|
|
节点下新增的资产,添加到节点关联的系统用户中
|
2019-08-28 03:43:55 +00:00
|
|
|
|
"""
|
2020-08-16 15:08:58 +00:00
|
|
|
|
if action != POST_ADD:
|
2019-08-28 03:43:55 +00:00
|
|
|
|
return
|
|
|
|
|
logger.debug("Assets node add signal recv: {}".format(action))
|
2020-08-16 15:08:58 +00:00
|
|
|
|
if reverse:
|
2020-01-13 04:50:01 +00:00
|
|
|
|
nodes = [instance.key]
|
2020-08-16 15:08:58 +00:00
|
|
|
|
asset_ids = pk_set
|
|
|
|
|
else:
|
|
|
|
|
nodes = Node.objects.filter(pk__in=pk_set).values_list('key', flat=True)
|
|
|
|
|
asset_ids = [instance.id]
|
|
|
|
|
|
2020-01-10 08:46:18 +00:00
|
|
|
|
# 节点资产发生变化时,将资产关联到节点及祖先节点关联的系统用户, 只关注新增的
|
|
|
|
|
nodes_ancestors_keys = set()
|
|
|
|
|
for node in nodes:
|
2020-08-16 15:08:58 +00:00
|
|
|
|
nodes_ancestors_keys.update(Node.get_node_ancestor_keys(node, with_self=True))
|
|
|
|
|
|
|
|
|
|
# 查询所有祖先节点关联的系统用户,都是要跟资产建立关系的
|
|
|
|
|
system_user_ids = SystemUser.objects.filter(
|
|
|
|
|
nodes__key__in=nodes_ancestors_keys
|
|
|
|
|
).distinct().values_list('id', flat=True)
|
|
|
|
|
|
|
|
|
|
# 查询所有已存在的关系
|
|
|
|
|
m2m_model = SystemUser.assets.through
|
|
|
|
|
exist = set(m2m_model.objects.filter(
|
|
|
|
|
systemuser_id__in=system_user_ids, asset_id__in=asset_ids
|
|
|
|
|
).values_list('systemuser_id', 'asset_id'))
|
|
|
|
|
# TODO 优化
|
|
|
|
|
to_create = []
|
|
|
|
|
for system_user_id in system_user_ids:
|
|
|
|
|
asset_ids_to_push = []
|
|
|
|
|
for asset_id in asset_ids:
|
|
|
|
|
if (system_user_id, asset_id) in exist:
|
|
|
|
|
continue
|
|
|
|
|
asset_ids_to_push.append(asset_id)
|
|
|
|
|
to_create.append(m2m_model(
|
|
|
|
|
systemuser_id=system_user_id,
|
|
|
|
|
asset_id=asset_id
|
|
|
|
|
))
|
2021-03-17 02:24:12 +00:00
|
|
|
|
if asset_ids_to_push:
|
|
|
|
|
push_system_user_to_assets.delay(system_user_id, asset_ids_to_push)
|
2020-08-16 15:08:58 +00:00
|
|
|
|
m2m_model.objects.bulk_create(to_create)
|
|
|
|
|
|
|
|
|
|
|
2020-10-13 08:42:41 +00:00
|
|
|
|
RELATED_NODE_IDS = '_related_node_ids'
|
2019-08-28 03:43:55 +00:00
|
|
|
|
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
|
@receiver(pre_delete, sender=Asset)
|
2020-10-13 08:42:41 +00:00
|
|
|
|
def on_asset_delete(instance: Asset, using, **kwargs):
|
|
|
|
|
node_ids = set(Node.objects.filter(
|
2020-08-16 15:08:58 +00:00
|
|
|
|
assets=instance
|
2020-10-13 08:42:41 +00:00
|
|
|
|
).distinct().values_list('id', flat=True))
|
|
|
|
|
setattr(instance, RELATED_NODE_IDS, node_ids)
|
|
|
|
|
m2m_changed.send(
|
|
|
|
|
sender=Asset.nodes.through, instance=instance, reverse=False,
|
|
|
|
|
model=Node, pk_set=node_ids, using=using, action=PRE_REMOVE
|
|
|
|
|
)
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Asset)
|
2020-10-13 08:42:41 +00:00
|
|
|
|
def on_asset_post_delete(instance: Asset, using, **kwargs):
|
|
|
|
|
node_ids = getattr(instance, RELATED_NODE_IDS, None)
|
|
|
|
|
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
|
|
|
|
|
)
|