jumpserver/apps/assets/automations/gather_facts/manager.py

36 lines
1.2 KiB
Python
Raw Normal View History

2022-10-25 10:43:34 +00:00
from assets.const import AutomationTypes
from common.utils import get_logger
from .format_asset_info import FormatAssetInfo
2022-10-12 10:08:57 +00:00
from ..base.manager import BasePlaybookManager
2022-10-17 03:22:21 +00:00
logger = get_logger(__name__)
2022-10-12 10:08:57 +00:00
class GatherFactsManager(BasePlaybookManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2022-10-14 10:59:28 +00:00
self.host_asset_mapper = {}
2022-10-12 10:08:57 +00:00
2022-10-14 10:59:28 +00:00
@classmethod
def method_type(cls):
2022-10-25 10:43:34 +00:00
return AutomationTypes.gather_facts
2022-10-12 10:08:57 +00:00
2022-10-14 10:59:28 +00:00
def host_callback(self, host, asset=None, **kwargs):
super().host_callback(host, asset=asset, **kwargs)
self.host_asset_mapper[host['name']] = asset
2022-10-17 03:22:21 +00:00
return host
2022-10-12 10:08:57 +00:00
def format_asset_info(self, tp, info):
info = FormatAssetInfo(tp).run(self.method_id_meta_mapper, info)
return info
2022-10-14 10:59:28 +00:00
def on_host_success(self, host, result):
2022-10-25 10:43:34 +00:00
info = result.get('debug', {}).get('res', {}).get('info', {})
2022-10-17 03:22:21 +00:00
asset = self.host_asset_mapper.get(host)
if asset and info:
info = self.format_asset_info(asset.type, info)
asset.gathered_info = info
asset.save(update_fields=['gathered_info'])
2022-10-17 03:22:21 +00:00
else:
2022-10-28 10:28:41 +00:00
logger.error("Not found info: {}".format(host))