mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-28 08:06:27 +00:00
refactor: 整合系统用户和管理用户 (#6236)
* perf: 整合系统用户和管理用户 * stash stash perf: 优化系统用户和资产的表结构 * perf: 添加信号 * perf: 添加算法 * perf: 去掉 asset user backends * perf: 整理系统用户api * perfF: 暂存一下 * stash * perf: 暂存一下 * perf: 暂存 * xxx * perf: ... * stash it * xxx * xxx * xxx * xxx * xxx * stash it * 修改Protocols * perf: 修改创建authbook信号 * perf: 添加auth info * .stash * perf: 基本完成 * perf: 修复完成 * perf: 修复更改的id * perf: 修复迁移过去数量不对的问题 * perf: 修改systemuser * fix: 修复批量编辑近期的问题 * fix: 修复authbook加载的问题 * xxx Co-authored-by: ibuler <ibuler@qq.com>
This commit is contained in:
107
apps/assets/tasks/account_connectivity.py
Normal file
107
apps/assets/tasks/account_connectivity.py
Normal file
@@ -0,0 +1,107 @@
|
||||
# ~*~ coding: utf-8 ~*~
|
||||
|
||||
from celery import shared_task
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from common.utils import get_logger
|
||||
from orgs.utils import org_aware_func
|
||||
from ..models import Connectivity
|
||||
from . import const
|
||||
from .utils import check_asset_can_run_ansible
|
||||
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
|
||||
__all__ = [
|
||||
'test_account_connectivity_util', 'test_accounts_connectivity_manual',
|
||||
'get_test_account_connectivity_tasks', 'test_user_connectivity',
|
||||
'run_adhoc',
|
||||
]
|
||||
|
||||
|
||||
def get_test_account_connectivity_tasks(asset):
|
||||
if asset.is_unixlike():
|
||||
tasks = const.PING_UNIXLIKE_TASKS
|
||||
elif asset.is_windows():
|
||||
tasks = const.PING_WINDOWS_TASKS
|
||||
else:
|
||||
msg = _(
|
||||
"The asset {} system platform {} does not "
|
||||
"support run Ansible tasks".format(asset.hostname, asset.platform)
|
||||
)
|
||||
logger.info(msg)
|
||||
tasks = []
|
||||
return tasks
|
||||
|
||||
|
||||
def run_adhoc(task_name, tasks, inventory):
|
||||
"""
|
||||
:param task_name
|
||||
:param tasks
|
||||
:param inventory
|
||||
"""
|
||||
from ops.ansible.runner import AdHocRunner
|
||||
runner = AdHocRunner(inventory, options=const.TASK_OPTIONS)
|
||||
result = runner.run(tasks, 'all', task_name)
|
||||
return result.results_raw, result.results_summary
|
||||
|
||||
|
||||
def test_user_connectivity(task_name, asset, username, password=None, private_key=None):
|
||||
"""
|
||||
:param task_name
|
||||
:param asset
|
||||
:param username
|
||||
:param password
|
||||
:param private_key
|
||||
"""
|
||||
from ops.inventory import JMSCustomInventory
|
||||
|
||||
tasks = get_test_account_connectivity_tasks(asset)
|
||||
if not tasks:
|
||||
logger.debug("No tasks ")
|
||||
return {}, {}
|
||||
inventory = JMSCustomInventory(
|
||||
assets=[asset], username=username, password=password,
|
||||
private_key=private_key
|
||||
)
|
||||
raw, summary = run_adhoc(
|
||||
task_name=task_name, tasks=tasks, inventory=inventory
|
||||
)
|
||||
return raw, summary
|
||||
|
||||
|
||||
@org_aware_func("account")
|
||||
def test_account_connectivity_util(account, task_name):
|
||||
"""
|
||||
:param account: <AuthBook>对象
|
||||
:param task_name:
|
||||
:return:
|
||||
"""
|
||||
if not check_asset_can_run_ansible(account.asset):
|
||||
return
|
||||
|
||||
try:
|
||||
raw, summary = test_user_connectivity(
|
||||
task_name=task_name, asset=account.asset,
|
||||
username=account.username, password=account.password,
|
||||
private_key=account.private_key_file
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warn("Failed run adhoc {}, {}".format(task_name, e))
|
||||
return
|
||||
|
||||
if summary.get('success'):
|
||||
account.set_connectivity(Connectivity.ok)
|
||||
else:
|
||||
account.set_connectivity(Connectivity.failed)
|
||||
|
||||
|
||||
@shared_task(queue="ansible")
|
||||
def test_accounts_connectivity_manual(accounts):
|
||||
"""
|
||||
:param accounts: <AuthBook>对象
|
||||
"""
|
||||
for account in accounts:
|
||||
task_name = _("Test account connectivity: {}").format(account)
|
||||
test_account_connectivity_util(account, task_name)
|
Reference in New Issue
Block a user