mirror of
https://github.com/jumpserver/jumpserver.git
synced 2026-01-29 21:51:31 +00:00
perf: 修改改密
This commit is contained in:
@@ -71,7 +71,7 @@ class DefaultCallback:
|
||||
def runner_on_start(self, event_data, **kwargs):
|
||||
pass
|
||||
|
||||
def runer_retry(self, event_data, **kwargs):
|
||||
def runner_retry(self, event_data, **kwargs):
|
||||
pass
|
||||
|
||||
def runner_on_file_diff(self, event_data, **kwargs):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# ~*~ coding: utf-8 ~*~
|
||||
from collections import defaultdict
|
||||
import json
|
||||
import os
|
||||
from collections import defaultdict
|
||||
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
@@ -10,7 +10,7 @@ __all__ = ['JMSInventory']
|
||||
|
||||
|
||||
class JMSInventory:
|
||||
def __init__(self, assets, account='', account_policy='smart', host_var_callback=None):
|
||||
def __init__(self, assets, account='', account_policy='smart', host_var_callback=None, host_duplicator=None):
|
||||
"""
|
||||
:param assets:
|
||||
:param account: account username name if not set use account_policy
|
||||
@@ -21,6 +21,7 @@ class JMSInventory:
|
||||
self.account_username = account
|
||||
self.account_policy = account_policy
|
||||
self.host_var_callback = host_var_callback
|
||||
self.host_duplicator = host_duplicator
|
||||
|
||||
@staticmethod
|
||||
def clean_assets(assets):
|
||||
@@ -59,11 +60,16 @@ class JMSInventory:
|
||||
return {"ansible_ssh_common_args": proxy_command}
|
||||
|
||||
def asset_to_host(self, asset, account, automation, protocols):
|
||||
host = {'name': asset.name, 'vars': {
|
||||
'asset_id': str(asset.id), 'asset_name': asset.name,
|
||||
'asset_type': asset.type, 'asset_category': asset.category,
|
||||
host = {
|
||||
'name': asset.name,
|
||||
'asset': {
|
||||
'id': asset.id, 'name': asset.name, 'ip': asset.ip,
|
||||
'type': asset.type, 'category': asset.category,
|
||||
'protocol': asset.protocol, 'port': asset.port,
|
||||
'protocols': [{'name': p.name, 'port': p.port} for p in protocols],
|
||||
},
|
||||
'exclude': ''
|
||||
}}
|
||||
}
|
||||
ansible_connection = automation.ansible_config.get('ansible_connection', 'ssh')
|
||||
gateway = None
|
||||
if asset.domain:
|
||||
@@ -91,15 +97,15 @@ class JMSInventory:
|
||||
elif account.secret_type == 'private_key' and account.secret:
|
||||
host['ssh_private_key'] = account.private_key_file
|
||||
else:
|
||||
host['vars']['exclude'] = _("No account found")
|
||||
host['exclude'] = _("No account found")
|
||||
|
||||
if gateway:
|
||||
host['vars'].update(self.make_proxy_command(gateway))
|
||||
host.update(self.make_proxy_command(gateway))
|
||||
|
||||
if self.host_var_callback:
|
||||
callback_var = self.host_var_callback(asset)
|
||||
if isinstance(callback_var, dict):
|
||||
host['vars'].update(callback_var)
|
||||
host.update(callback_var)
|
||||
return host
|
||||
|
||||
def select_account(self, asset):
|
||||
@@ -137,8 +143,11 @@ class JMSInventory:
|
||||
account = self.select_account(asset)
|
||||
host = self.asset_to_host(asset, account, automation, protocols)
|
||||
if not automation.ansible_enabled:
|
||||
host['vars']['exclude'] = _('Ansible disabled')
|
||||
hosts.append(host)
|
||||
host['exclude'] = _('Ansible disabled')
|
||||
if self.host_duplicator:
|
||||
hosts.extend(self.host_duplicator(host, asset=asset, account=account, platform=platform))
|
||||
else:
|
||||
hosts.append(host)
|
||||
|
||||
exclude_hosts = list(filter(lambda x: x.get('exclude'), hosts))
|
||||
if exclude_hosts:
|
||||
@@ -150,8 +159,6 @@ class JMSInventory:
|
||||
data = {'all': {'hosts': {}}}
|
||||
for host in hosts:
|
||||
name = host.pop('name')
|
||||
var = host.pop('vars', {})
|
||||
host.update(var)
|
||||
data['all']['hosts'][name] = host
|
||||
return data
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ class BaseAnsibleExecution(models.Model):
|
||||
creator = models.ForeignKey('users.User', verbose_name=_("Creator"), on_delete=models.SET_NULL, null=True)
|
||||
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Date created'))
|
||||
date_start = models.DateTimeField(null=True, verbose_name=_('Date start'), db_index=True)
|
||||
date_finished = models.DateTimeField(null=True)
|
||||
date_finished = models.DateTimeField(null=True, verbose_name=_("Date finished"))
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
from .endpoint import *
|
||||
@@ -1,2 +0,0 @@
|
||||
from .manager import *
|
||||
from .handlers import *
|
||||
@@ -1,16 +0,0 @@
|
||||
"""
|
||||
执行改密计划的基类
|
||||
"""
|
||||
from common.utils import get_logger
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
|
||||
class BaseHandler:
|
||||
def __init__(self, task, show_step_info=True):
|
||||
self.task = task
|
||||
self.conn = None
|
||||
self.retry_times = 3
|
||||
self.current_step = 0
|
||||
self.is_frozen = False # 任务状态冻结标志
|
||||
self.show_step_info = show_step_info
|
||||
@@ -1,78 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
import time
|
||||
from openpyxl import Workbook
|
||||
from django.utils import timezone
|
||||
|
||||
from common.utils import get_logger
|
||||
from common.utils.timezone import local_now_display
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
|
||||
class BaseExecutionManager:
|
||||
task_back_up_serializer: None
|
||||
|
||||
def __init__(self, execution):
|
||||
self.execution = execution
|
||||
self.date_start = timezone.now()
|
||||
self.time_start = time.time()
|
||||
self.date_end = None
|
||||
self.time_end = None
|
||||
self.timedelta = 0
|
||||
self.total_tasks = []
|
||||
|
||||
def on_tasks_pre_run(self, tasks):
|
||||
raise NotImplementedError
|
||||
|
||||
def on_per_task_pre_run(self, task, total, index):
|
||||
raise NotImplementedError
|
||||
|
||||
def create_csv_file(self, tasks, file_name):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_handler_cls(self):
|
||||
raise NotImplemented
|
||||
|
||||
def do_run(self):
|
||||
tasks = self.total_tasks = self.execution.create_plan_tasks()
|
||||
self.on_tasks_pre_run(tasks)
|
||||
total = len(tasks)
|
||||
|
||||
for index, task in enumerate(tasks, start=1):
|
||||
self.on_per_task_pre_run(task, total, index)
|
||||
task.start(show_step_info=False)
|
||||
|
||||
def pre_run(self):
|
||||
self.execution.date_start = self.date_start
|
||||
self.execution.save()
|
||||
self.show_execution_steps()
|
||||
|
||||
def show_execution_steps(self):
|
||||
pass
|
||||
|
||||
def show_summary(self):
|
||||
split_line = '#' * 40
|
||||
summary = self.execution.result_summary
|
||||
logger.info(f'\n{split_line} 改密计划执行结果汇总 {split_line}')
|
||||
logger.info(
|
||||
'\n成功: {succeed}, 失败: {failed}, 总数: {total}\n'
|
||||
''.format(**summary)
|
||||
)
|
||||
|
||||
def post_run(self):
|
||||
self.time_end = time.time()
|
||||
self.date_end = timezone.now()
|
||||
|
||||
logger.info('\n\n' + '-' * 80)
|
||||
logger.info('任务执行结束 {}\n'.format(local_now_display()))
|
||||
self.timedelta = int(self.time_end - self.time_start)
|
||||
logger.info('用时: {}s'.format(self.timedelta))
|
||||
self.execution.timedelta = self.timedelta
|
||||
self.execution.save()
|
||||
self.show_summary()
|
||||
|
||||
def run(self):
|
||||
self.pre_run()
|
||||
self.do_run()
|
||||
self.post_run()
|
||||
@@ -1,2 +0,0 @@
|
||||
from .manager import *
|
||||
from .handlers import *
|
||||
@@ -1,10 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from common.utils import get_logger
|
||||
from ..base import BaseHandler
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class ChangeAuthHandler(BaseHandler):
|
||||
pass
|
||||
@@ -1,12 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from common.utils import get_logger
|
||||
from ..base import BaseExecutionManager
|
||||
from .handlers import ChangeAuthHandler
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class ChangeAuthExecutionManager(BaseExecutionManager):
|
||||
def get_handler_cls(self):
|
||||
return ChangeAuthHandler
|
||||
@@ -1,2 +0,0 @@
|
||||
from .manager import *
|
||||
from .handlers import *
|
||||
@@ -1,10 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from common.utils import get_logger
|
||||
from ..base import BaseHandler
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class CollectHandler(BaseHandler):
|
||||
pass
|
||||
@@ -1,10 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from common.utils import get_logger
|
||||
from ..base import BaseExecutionManager
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class CollectExecutionManager(object):
|
||||
pass
|
||||
@@ -1,31 +0,0 @@
|
||||
from ops.const import StrategyChoice
|
||||
from .push import PushExecutionManager, PushHandler
|
||||
from .verify import VerifyExecutionManager, VerifyHandler
|
||||
from .collect import CollectExecutionManager, CollectHandler
|
||||
from .change_auth import ChangeAuthExecutionManager, ChangeAuthHandler
|
||||
|
||||
|
||||
class ExecutionManager:
|
||||
manager_type = {
|
||||
StrategyChoice.push: PushExecutionManager,
|
||||
StrategyChoice.verify: VerifyExecutionManager,
|
||||
StrategyChoice.collect: CollectExecutionManager,
|
||||
StrategyChoice.change_password: ChangeAuthExecutionManager,
|
||||
}
|
||||
|
||||
def __new__(cls, execution):
|
||||
manager = cls.manager_type[execution.manager_type]
|
||||
return manager(execution)
|
||||
|
||||
|
||||
class TaskHandler:
|
||||
handler_type = {
|
||||
StrategyChoice.push: PushHandler,
|
||||
StrategyChoice.verify: VerifyHandler,
|
||||
StrategyChoice.collect: CollectHandler,
|
||||
StrategyChoice.change_password: ChangeAuthHandler,
|
||||
}
|
||||
|
||||
def __new__(cls, task, show_step_info):
|
||||
handler = cls.handler_type[task.handler_type]
|
||||
return handler(task, show_step_info)
|
||||
@@ -1,2 +0,0 @@
|
||||
from .manager import *
|
||||
from .handlers import *
|
||||
@@ -1,10 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from common.utils import get_logger
|
||||
from ..base import BaseHandler
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class PushHandler(BaseHandler):
|
||||
pass
|
||||
@@ -1,10 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from common.utils import get_logger
|
||||
from ..base import BaseExecutionManager
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class PushExecutionManager(BaseExecutionManager):
|
||||
pass
|
||||
@@ -1,2 +0,0 @@
|
||||
from .manager import *
|
||||
from .handlers import *
|
||||
@@ -1,10 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from common.utils import get_logger
|
||||
from ..base import BaseHandler
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class VerifyHandler(BaseHandler):
|
||||
pass
|
||||
@@ -1,10 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from common.utils import get_logger
|
||||
from ..base import BaseExecutionManager
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class VerifyExecutionManager(BaseExecutionManager):
|
||||
pass
|
||||
@@ -8,7 +8,7 @@ from common.utils import get_logger, get_object_or_none
|
||||
from orgs.utils import org_aware_func
|
||||
from jumpserver.const import PROJECT_DIR
|
||||
|
||||
from .models import Task, AdHoc
|
||||
from .models import AdHoc
|
||||
from .const import DEFAULT_PASSWORD_RULES
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
Reference in New Issue
Block a user