mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-05-14 11:02:55 +00:00
* feat: 优化代码结构,receptor开关,修改为 tcp 通信 * fix: 修改导包路径 * fix: 修复错别字 * fix: 修改导包路径 * perf: 优化代码 * fix: 修复任务不执行的问题 * perf: 优化配置项名称 * perf: 优化代码结构 * perf: 优化代码 --------- Co-authored-by: Aaron3S <chenyang@fit2cloud.com>
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
from django.conf import settings
|
|
from django.utils.functional import LazyObject
|
|
|
|
from ops.ansible import AnsibleReceptorRunner, AnsibleNativeRunner
|
|
from ops.ansible.runners.base import BaseRunner
|
|
|
|
__all__ = ['interface']
|
|
|
|
|
|
class _LazyRunnerInterface(LazyObject):
|
|
|
|
def _setup(self):
|
|
self._wrapped = self.make_interface()
|
|
|
|
@staticmethod
|
|
def make_interface():
|
|
runner_type = AnsibleReceptorRunner \
|
|
if settings.ANSIBLE_RECEPTOR_ENABLED else AnsibleNativeRunner
|
|
gateway_host = settings.ANSIBLE_RECEPTOR_GATEWAY_PROXY_HOST \
|
|
if settings.ANSIBLE_RECEPTOR_GATEWAY_PROXY_HOST else '127.0.0.1'
|
|
return RunnerInterface(runner_type=runner_type, gateway_proxy_host=gateway_host)
|
|
|
|
|
|
interface = _LazyRunnerInterface()
|
|
|
|
|
|
class RunnerInterface:
|
|
def __init__(self, runner_type, gateway_proxy_host='127.0.0.1'):
|
|
if not issubclass(runner_type, BaseRunner):
|
|
raise TypeError(f'{runner_type} can not cast to {BaseRunner}')
|
|
self._runner_type = runner_type
|
|
self._gateway_proxy_host = gateway_proxy_host
|
|
|
|
def get_gateway_proxy_host(self):
|
|
return self._gateway_proxy_host
|
|
|
|
def get_runner_type(self):
|
|
return self._runner_type
|
|
|
|
def kill_process(self, pid):
|
|
return self._runner_type.kill_precess(pid)
|
|
|
|
def run(self, **kwargs):
|
|
runner_type = self.get_runner_type()
|
|
runner = runner_type(**kwargs)
|
|
return runner.run()
|