2019-09-17 10:59:54 +00:00
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
from itertools import groupby
|
2019-09-17 10:59:54 +00:00
|
|
|
|
from collections import defaultdict
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
2019-09-17 10:59:54 +00:00
|
|
|
|
from celery import shared_task
|
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
2021-05-19 09:59:15 +00:00
|
|
|
|
from assets.models import Asset
|
2019-09-17 10:59:54 +00:00
|
|
|
|
from common.utils import get_logger
|
2020-03-12 08:24:38 +00:00
|
|
|
|
from orgs.utils import tmp_to_org, org_aware_func
|
2021-07-12 12:11:53 +00:00
|
|
|
|
from ..models import SystemUser, Connectivity, AuthBook
|
2019-09-17 10:59:54 +00:00
|
|
|
|
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(__name__)
|
|
|
|
|
__all__ = [
|
|
|
|
|
'test_system_user_connectivity_util', 'test_system_user_connectivity_manual',
|
|
|
|
|
'test_system_user_connectivity_period', 'test_system_user_connectivity_a_asset',
|
2021-07-20 06:48:38 +00:00
|
|
|
|
'test_system_users_connectivity_a_asset'
|
2019-09-17 10:59:54 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2021-07-12 12:11:53 +00:00
|
|
|
|
def set_assets_accounts_connectivity(system_user, assets, results_summary):
|
|
|
|
|
asset_ids_ok = set()
|
|
|
|
|
asset_ids_failed = set()
|
|
|
|
|
|
|
|
|
|
asset_hostnames_ok = results_summary.get('contacted', {}).keys()
|
|
|
|
|
|
|
|
|
|
for asset in assets:
|
|
|
|
|
if asset.hostname in asset_hostnames_ok:
|
|
|
|
|
asset_ids_ok.add(asset.id)
|
|
|
|
|
else:
|
|
|
|
|
asset_ids_failed.add(asset.id)
|
|
|
|
|
|
|
|
|
|
accounts_ok = AuthBook.objects.filter(asset_id__in=asset_ids_ok, systemuser=system_user)
|
|
|
|
|
accounts_failed = AuthBook.objects.filter(asset_id__in=asset_ids_failed, systemuser=system_user)
|
|
|
|
|
|
|
|
|
|
AuthBook.bulk_set_connectivity(accounts_ok, Connectivity.ok)
|
|
|
|
|
AuthBook.bulk_set_connectivity(accounts_failed, Connectivity.failed)
|
|
|
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
@org_aware_func("system_user")
|
2019-09-17 10:59:54 +00:00
|
|
|
|
def test_system_user_connectivity_util(system_user, assets, task_name):
|
|
|
|
|
"""
|
|
|
|
|
Test system cant connect his assets or not.
|
|
|
|
|
:param system_user:
|
|
|
|
|
:param assets:
|
|
|
|
|
:param task_name:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
from ops.utils import update_or_create_ansible_task
|
|
|
|
|
|
2021-07-12 12:11:53 +00:00
|
|
|
|
if system_user.username_same_with_user:
|
|
|
|
|
logger.error(_("Dynamic system user not support test"))
|
|
|
|
|
return
|
|
|
|
|
|
2020-07-08 03:30:37 +00:00
|
|
|
|
# hosts = clean_ansible_task_hosts(assets, system_user=system_user)
|
|
|
|
|
# TODO: 这里不传递系统用户,因为clean_ansible_task_hosts会通过system_user来判断是否可以推送,
|
2021-07-12 12:11:53 +00:00
|
|
|
|
# 不符合测试可连接性逻辑, 后面需要优化此逻辑
|
2020-07-08 03:30:37 +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])
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
def run_task(_tasks, _hosts, _username):
|
2020-03-19 03:47:39 +00:00
|
|
|
|
old_name = "{}".format(system_user)
|
|
|
|
|
new_name = "{}({})".format(system_user.name, _username)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
_task_name = task_name.replace(old_name, new_name)
|
|
|
|
|
_task, created = update_or_create_ansible_task(
|
|
|
|
|
task_name=_task_name, hosts=_hosts, tasks=_tasks,
|
2019-09-17 10:59:54 +00:00
|
|
|
|
pattern='all', options=const.TASK_OPTIONS,
|
2021-07-14 08:49:28 +00:00
|
|
|
|
run_as=_username, system_user=system_user
|
2019-09-17 10:59:54 +00:00
|
|
|
|
)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
raw, summary = _task.run()
|
2019-09-17 10:59:54 +00:00
|
|
|
|
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
|
|
|
|
for platform, _hosts in platform_hosts_map.items():
|
|
|
|
|
if not _hosts:
|
|
|
|
|
continue
|
|
|
|
|
if platform not in ["unixlike", "windows"]:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
tasks = platform_tasks_map[platform]
|
|
|
|
|
print(_("Start test system user connectivity for platform: [{}]").format(platform))
|
|
|
|
|
print(_("Hosts count: {}").format(len(_hosts)))
|
|
|
|
|
# 用户名不是动态的,用户名则是一个
|
2021-07-12 12:11:53 +00:00
|
|
|
|
logger.debug("System user not has special auth")
|
|
|
|
|
run_task(tasks, _hosts, system_user.username)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
2021-07-12 12:11:53 +00:00
|
|
|
|
set_assets_accounts_connectivity(system_user, hosts, results_summary)
|
2019-09-17 10:59:54 +00:00
|
|
|
|
return results_summary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(queue="ansible")
|
2020-03-12 08:24:38 +00:00
|
|
|
|
@org_aware_func("system_user")
|
2021-05-19 09:59:15 +00:00
|
|
|
|
def test_system_user_connectivity_manual(system_user, asset_ids=None):
|
2019-09-17 10:59:54 +00:00
|
|
|
|
task_name = _("Test system user connectivity: {}").format(system_user)
|
2021-05-19 09:59:15 +00:00
|
|
|
|
if asset_ids:
|
|
|
|
|
assets = Asset.objects.filter(id__in=asset_ids)
|
|
|
|
|
else:
|
|
|
|
|
assets = system_user.get_related_assets()
|
2020-03-12 08:24:38 +00:00
|
|
|
|
test_system_user_connectivity_util(system_user, assets, task_name)
|
2019-09-17 10:59:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(queue="ansible")
|
2020-03-12 08:24:38 +00:00
|
|
|
|
@org_aware_func("system_user")
|
2019-09-17 10:59:54 +00:00
|
|
|
|
def test_system_user_connectivity_a_asset(system_user, asset):
|
|
|
|
|
task_name = _("Test system user connectivity: {} => {}").format(
|
|
|
|
|
system_user, asset
|
|
|
|
|
)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
test_system_user_connectivity_util(system_user, [asset], task_name)
|
2019-09-17 10:59:54 +00:00
|
|
|
|
|
|
|
|
|
|
2021-07-20 06:48:38 +00:00
|
|
|
|
@shared_task(queue="ansible")
|
|
|
|
|
def test_system_users_connectivity_a_asset(system_users, asset):
|
|
|
|
|
for system_user in system_users:
|
|
|
|
|
test_system_user_connectivity_a_asset(system_user, asset)
|
|
|
|
|
|
|
|
|
|
|
2019-09-17 10:59:54 +00:00
|
|
|
|
@shared_task(queue="ansible")
|
|
|
|
|
def test_system_user_connectivity_period():
|
|
|
|
|
if not const.PERIOD_TASK_ENABLED:
|
|
|
|
|
logger.debug("Period task disabled, test system user connectivity pass")
|
|
|
|
|
return
|
2020-03-12 08:24:38 +00:00
|
|
|
|
queryset_map = SystemUser.objects.all_group_by_org()
|
|
|
|
|
for org, system_user in queryset_map.items():
|
2019-09-17 10:59:54 +00:00
|
|
|
|
task_name = _("Test system user connectivity period: {}").format(system_user)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
with tmp_to_org(org):
|
|
|
|
|
assets = system_user.get_related_assets()
|
|
|
|
|
test_system_user_connectivity_util(system_user, assets, task_name)
|