2019-09-17 10:59:54 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2020-03-12 08:24:38 +00:00
|
|
|
from itertools import groupby
|
2019-09-17 10:59:54 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
from celery import shared_task
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
from common.utils import get_logger
|
2020-03-12 08:24:38 +00:00
|
|
|
from orgs.utils import org_aware_func
|
2019-09-17 10:59:54 +00:00
|
|
|
from ..models.utils import Connectivity
|
|
|
|
from . import const
|
2020-03-12 08:24:38 +00:00
|
|
|
from .utils import clean_ansible_task_hosts, group_asset_by_platform
|
2019-09-17 10:59:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
2020-03-12 08:24:38 +00:00
|
|
|
__all__ = [
|
|
|
|
'test_asset_connectivity_util', 'test_asset_connectivity_manual',
|
2021-01-06 03:08:35 +00:00
|
|
|
'test_node_assets_connectivity_manual', 'test_assets_connectivity_manual',
|
2020-03-12 08:24:38 +00:00
|
|
|
]
|
2019-09-17 10:59:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task(queue="ansible")
|
2020-03-12 08:24:38 +00:00
|
|
|
@org_aware_func("assets")
|
2019-09-17 10:59:54 +00:00
|
|
|
def test_asset_connectivity_util(assets, task_name=None):
|
|
|
|
from ops.utils import update_or_create_ansible_task
|
|
|
|
|
|
|
|
if task_name is None:
|
|
|
|
task_name = _("Test assets connectivity")
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
hosts = clean_ansible_task_hosts(assets)
|
2019-09-17 10:59:54 +00:00
|
|
|
if not hosts:
|
|
|
|
return {}
|
2020-03-12 08:24:38 +00:00
|
|
|
platform_hosts_map = {}
|
|
|
|
hosts_sorted = sorted(hosts, key=group_asset_by_platform)
|
|
|
|
platform_hosts = groupby(hosts_sorted, key=group_asset_by_platform)
|
|
|
|
for i in platform_hosts:
|
|
|
|
platform_hosts_map[i[0]] = list(i[1])
|
2019-09-17 10:59:54 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
platform_tasks_map = {
|
|
|
|
"unixlike": const.PING_UNIXLIKE_TASKS,
|
|
|
|
"windows": const.PING_WINDOWS_TASKS
|
2019-09-17 10:59:54 +00:00
|
|
|
}
|
|
|
|
results_summary = dict(
|
|
|
|
contacted=defaultdict(dict), dark=defaultdict(dict), success=True
|
|
|
|
)
|
2020-03-12 08:24:38 +00:00
|
|
|
for platform, _hosts in platform_hosts_map.items():
|
|
|
|
if not _hosts:
|
2019-09-17 10:59:54 +00:00
|
|
|
continue
|
2020-03-12 08:24:38 +00:00
|
|
|
logger.debug("System user not has special auth")
|
|
|
|
tasks = platform_tasks_map.get(platform)
|
2019-09-17 10:59:54 +00:00
|
|
|
task, created = update_or_create_ansible_task(
|
2020-03-12 08:24:38 +00:00
|
|
|
task_name=task_name, hosts=_hosts, tasks=tasks,
|
2019-09-17 10:59:54 +00:00
|
|
|
pattern='all', options=const.TASK_OPTIONS, run_as_admin=True,
|
|
|
|
)
|
|
|
|
raw, summary = task.run()
|
|
|
|
success = summary.get('success', False)
|
|
|
|
contacted = summary.get('contacted', {})
|
|
|
|
dark = summary.get('dark', {})
|
|
|
|
|
|
|
|
results_summary['success'] &= success
|
|
|
|
results_summary['contacted'].update(contacted)
|
|
|
|
results_summary['dark'].update(dark)
|
2020-03-12 08:24:38 +00:00
|
|
|
continue
|
2019-09-17 10:59:54 +00:00
|
|
|
|
|
|
|
for asset in assets:
|
|
|
|
if asset.hostname in results_summary.get('dark', {}).keys():
|
|
|
|
asset.connectivity = Connectivity.unreachable()
|
|
|
|
elif asset.hostname in results_summary.get('contacted', {}).keys():
|
|
|
|
asset.connectivity = Connectivity.reachable()
|
|
|
|
else:
|
|
|
|
asset.connectivity = Connectivity.unknown()
|
|
|
|
return results_summary
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(queue="ansible")
|
|
|
|
def test_asset_connectivity_manual(asset):
|
|
|
|
task_name = _("Test assets connectivity: {}").format(asset)
|
|
|
|
summary = test_asset_connectivity_util([asset], task_name=task_name)
|
|
|
|
|
|
|
|
if summary.get('dark'):
|
|
|
|
return False, summary['dark']
|
|
|
|
else:
|
|
|
|
return True, ""
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
2021-01-06 03:08:35 +00:00
|
|
|
@shared_task(queue="ansible")
|
|
|
|
def test_assets_connectivity_manual(assets):
|
|
|
|
task_name = _("Test assets connectivity: {}").format([asset.hostname for asset in assets])
|
|
|
|
summary = test_asset_connectivity_util(assets, task_name=task_name)
|
|
|
|
|
|
|
|
if summary.get('dark'):
|
|
|
|
return False, summary['dark']
|
|
|
|
else:
|
|
|
|
return True, ""
|
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
@shared_task(queue="ansible")
|
|
|
|
def test_node_assets_connectivity_manual(node):
|
|
|
|
task_name = _("Test if the assets under the node are connectable: {}".format(node.name))
|
|
|
|
assets = node.get_all_assets()
|
|
|
|
result = test_asset_connectivity_util(assets, task_name=task_name)
|
|
|
|
return result
|
|
|
|
|