2019-09-17 10:59:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from celery import shared_task
|
2023-01-16 11:02:09 +00:00
|
|
|
from django.utils.translation import gettext_noop, gettext_lazy as _
|
2019-09-17 10:59:54 +00:00
|
|
|
|
2022-11-01 11:37:50 +00:00
|
|
|
from common.utils import get_logger
|
2023-01-16 11:02:09 +00:00
|
|
|
from assets.const import AutomationTypes
|
2022-09-29 12:44:45 +00:00
|
|
|
from orgs.utils import org_aware_func, tmp_to_root_org
|
2019-09-17 10:59:54 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from .common import automation_execute_start
|
|
|
|
|
2019-09-17 10:59:54 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
__all__ = [
|
2023-01-16 11:02:09 +00:00
|
|
|
'update_assets_fact_util',
|
2022-11-01 11:37:50 +00:00
|
|
|
'update_node_assets_hardware_info_manual',
|
2021-01-06 03:08:35 +00:00
|
|
|
'update_assets_hardware_info_manual',
|
2019-09-17 10:59:54 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
def update_fact_util(assets=None, nodes=None, task_name=None):
|
2022-11-01 11:37:50 +00:00
|
|
|
from assets.models import GatherFactsAutomation
|
2019-09-17 10:59:54 +00:00
|
|
|
if task_name is None:
|
2022-01-13 06:18:32 +00:00
|
|
|
task_name = gettext_noop("Update some assets hardware info. ")
|
2022-11-01 11:37:50 +00:00
|
|
|
task_name = GatherFactsAutomation.generate_unique_name(task_name)
|
2022-11-02 09:27:47 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
nodes = nodes or []
|
|
|
|
assets = assets or []
|
|
|
|
child_snapshot = {
|
|
|
|
'assets': [str(asset.id) for asset in assets],
|
|
|
|
'nodes': [str(node.id) for node in nodes],
|
|
|
|
}
|
|
|
|
tp = AutomationTypes.gather_facts
|
|
|
|
automation_execute_start(task_name, tp, child_snapshot)
|
|
|
|
|
|
|
|
|
|
|
|
@org_aware_func('assets')
|
|
|
|
def update_assets_fact_util(assets=None, task_name=None):
|
|
|
|
if assets is None:
|
|
|
|
logger.info("No assets to update hardware info")
|
|
|
|
return
|
|
|
|
|
|
|
|
update_fact_util(assets=assets, task_name=task_name)
|
2022-11-02 09:27:47 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
@org_aware_func('nodes')
|
|
|
|
def update_nodes_fact_util(nodes=None, task_name=None):
|
|
|
|
if nodes is None:
|
|
|
|
logger.info("No nodes to update hardware info")
|
|
|
|
return
|
|
|
|
update_fact_util(nodes=nodes, task_name=task_name)
|
2019-09-17 10:59:54 +00:00
|
|
|
|
|
|
|
|
2022-11-15 08:29:40 +00:00
|
|
|
@shared_task(queue="ansible", verbose_name=_('Manually update the hardware information of assets'))
|
2022-09-29 12:44:45 +00:00
|
|
|
def update_assets_hardware_info_manual(asset_ids):
|
2022-11-01 11:37:50 +00:00
|
|
|
from assets.models import Asset
|
2023-01-16 11:02:09 +00:00
|
|
|
assets = Asset.objects.filter(id__in=asset_ids)
|
2022-11-01 11:37:50 +00:00
|
|
|
task_name = gettext_noop("Update assets hardware info: ")
|
2023-01-16 11:02:09 +00:00
|
|
|
update_assets_fact_util(assets=assets, task_name=task_name)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
2022-11-15 08:29:40 +00:00
|
|
|
@shared_task(queue="ansible", verbose_name=_('Manually update the hardware information of assets under a node'))
|
2022-09-29 12:44:45 +00:00
|
|
|
def update_node_assets_hardware_info_manual(node_id):
|
2022-11-01 11:37:50 +00:00
|
|
|
from assets.models import Node
|
2023-01-16 11:02:09 +00:00
|
|
|
node = Node.objects.get(id=node_id)
|
2022-11-01 11:37:50 +00:00
|
|
|
task_name = gettext_noop("Update node asset hardware information: ")
|
2023-01-16 11:02:09 +00:00
|
|
|
update_nodes_fact_util(nodes=[node], task_name=task_name)
|