2022-11-01 11:37:50 +00:00
|
|
|
from assets.const import AutomationTypes, Connectivity
|
2023-01-31 10:51:04 +00:00
|
|
|
from common.utils import get_logger
|
2022-10-12 10:08:57 +00:00
|
|
|
from ..base.manager import BasePlaybookManager
|
|
|
|
|
2022-10-27 10:53:10 +00:00
|
|
|
logger = get_logger(__name__)
|
2022-10-12 10:08:57 +00:00
|
|
|
|
|
|
|
|
2022-10-27 10:53:10 +00:00
|
|
|
class PingManager(BasePlaybookManager):
|
2023-11-16 03:26:05 +00:00
|
|
|
ansible_account_prefer = ''
|
|
|
|
|
2022-10-28 10:28:41 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2022-11-01 11:37:50 +00:00
|
|
|
self.host_asset_and_account_mapper = {}
|
2022-10-28 10:28:41 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def method_type(cls):
|
|
|
|
return AutomationTypes.ping
|
|
|
|
|
2023-04-14 10:31:09 +00:00
|
|
|
def host_callback(self, host, asset=None, account=None, automation=None, **kwargs):
|
|
|
|
super().host_callback(
|
|
|
|
host, asset=asset, account=account, automation=automation, **kwargs
|
|
|
|
)
|
2022-11-01 11:37:50 +00:00
|
|
|
self.host_asset_and_account_mapper[host['name']] = (asset, account)
|
2022-10-28 10:28:41 +00:00
|
|
|
return host
|
2022-11-01 11:37:50 +00:00
|
|
|
|
|
|
|
def on_host_success(self, host, result):
|
|
|
|
asset, account = self.host_asset_and_account_mapper.get(host)
|
2024-03-21 03:05:04 +00:00
|
|
|
try:
|
|
|
|
asset.set_connectivity(Connectivity.OK)
|
|
|
|
if not account:
|
|
|
|
return
|
|
|
|
account.set_connectivity(Connectivity.OK)
|
|
|
|
except Exception as e:
|
|
|
|
print(f'\033[31m Update account {account.name} or '
|
|
|
|
f'update asset {asset.name} connectivity failed: {e} \033[0m\n')
|
2022-11-01 11:37:50 +00:00
|
|
|
|
|
|
|
def on_host_error(self, host, error, result):
|
|
|
|
asset, account = self.host_asset_and_account_mapper.get(host)
|
2024-03-21 03:05:04 +00:00
|
|
|
try:
|
|
|
|
asset.set_connectivity(Connectivity.ERR)
|
|
|
|
if not account:
|
|
|
|
return
|
|
|
|
account.set_connectivity(Connectivity.ERR)
|
|
|
|
except Exception as e:
|
|
|
|
print(f'\033[31m Update account {account.name} or '
|
|
|
|
f'update asset {asset.name} connectivity failed: {e} \033[0m\n')
|