From 44445a948225c61c9eba861925a65451c845ec1e Mon Sep 17 00:00:00 2001 From: Aaron3S Date: Mon, 15 Apr 2024 19:29:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=9B=A0=E4=BD=BF=E7=94=A8=20receptor=20runner=20=E9=80=A0?= =?UTF-8?q?=E6=88=90=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile-ce | 2 +- apps/assets/automations/base/manager.py | 2 +- .../commands/services/services/celery_base.py | 4 ++-- apps/{ops/ansible/modules => libs}/__init__.py | 0 .../modules_utils => libs/ansible}/__init__.py | 0 apps/{ops => libs}/ansible/ansible.cfg | 2 +- apps/libs/ansible/modules/__init__.py | 0 .../ansible/modules/custom_command.py | 2 +- .../ansible/modules/mongodb_ping.py | 0 .../ansible/modules/mongodb_user.py | 0 .../ansible/modules/oracle_info.py | 2 +- .../ansible/modules/oracle_ping.py | 2 +- .../ansible/modules/oracle_user.py | 2 +- apps/{ops => libs}/ansible/modules/rdp_ping.py | 0 apps/{ops => libs}/ansible/modules/ssh_ping.py | 2 +- .../ansible/modules/telnet_ping.py | 0 apps/libs/ansible/modules_utils/__init__.py | 0 .../ansible/modules_utils/custom_common.py | 0 .../ansible/modules_utils/oracle_common.py | 1 - apps/ops/ansible/runner.py | 2 +- receptor | 18 +++++++++++++----- 21 files changed, 24 insertions(+), 17 deletions(-) rename apps/{ops/ansible/modules => libs}/__init__.py (100%) rename apps/{ops/ansible/modules_utils => libs/ansible}/__init__.py (100%) rename apps/{ops => libs}/ansible/ansible.cfg (75%) create mode 100644 apps/libs/ansible/modules/__init__.py rename apps/{ops => libs}/ansible/modules/custom_command.py (98%) rename apps/{ops => libs}/ansible/modules/mongodb_ping.py (100%) rename apps/{ops => libs}/ansible/modules/mongodb_user.py (100%) rename apps/{ops => libs}/ansible/modules/oracle_info.py (99%) rename apps/{ops => libs}/ansible/modules/oracle_ping.py (97%) rename apps/{ops => libs}/ansible/modules/oracle_user.py (99%) rename apps/{ops => libs}/ansible/modules/rdp_ping.py (100%) rename apps/{ops => libs}/ansible/modules/ssh_ping.py (95%) rename apps/{ops => libs}/ansible/modules/telnet_ping.py (100%) create mode 100644 apps/libs/ansible/modules_utils/__init__.py rename apps/{ops => libs}/ansible/modules_utils/custom_common.py (100%) rename apps/{ops => libs}/ansible/modules_utils/oracle_common.py (99%) diff --git a/Dockerfile-ce b/Dockerfile-ce index cbdfbfb63..622532f6d 100644 --- a/Dockerfile-ce +++ b/Dockerfile-ce @@ -121,7 +121,7 @@ RUN set -ex \ COPY --from=stage-2 /opt/py3 /opt/py3 COPY --from=stage-1 /opt/jumpserver/release/jumpserver /opt/jumpserver -COPY --from=stage-1 /opt/jumpserver/release/jumpserver/apps/ops/ansible/ansible.cfg /etc/ansible/ +COPY --from=stage-1 /opt/jumpserver/release/jumpserver/apps/libs/ansible/ansible.cfg /etc/ansible/ WORKDIR /opt/jumpserver diff --git a/apps/assets/automations/base/manager.py b/apps/assets/automations/base/manager.py index 656c6751b..653ccd9b2 100644 --- a/apps/assets/automations/base/manager.py +++ b/apps/assets/automations/base/manager.py @@ -54,7 +54,7 @@ class SSHTunnelManager: not_valid.append(k) else: local_bind_port = server.local_bind_port - host['ansible_host'] = jms_asset['address'] = host['login_host'] = '127.0.0.1' + host['ansible_host'] = jms_asset['address'] = host['login_host'] = 'jms_celery' host['ansible_port'] = jms_asset['port'] = host['login_port'] = local_bind_port servers.append(server) diff --git a/apps/common/management/commands/services/services/celery_base.py b/apps/common/management/commands/services/services/celery_base.py index 4e63f4cbb..edfd7bb0e 100644 --- a/apps/common/management/commands/services/services/celery_base.py +++ b/apps/common/management/commands/services/services/celery_base.py @@ -12,8 +12,8 @@ class CeleryBaseService(BaseService): @property def cmd(self): print('\n- Start Celery as Distributed Task Queue: {}'.format(self.queue.capitalize())) - ansible_config_path = os.path.join(settings.APPS_DIR, 'ops', 'ansible', 'ansible.cfg') - ansible_modules_path = os.path.join(settings.APPS_DIR, 'ops', 'ansible', 'modules') + ansible_config_path = os.path.join(settings.APPS_DIR, 'libs', 'ansible', 'ansible.cfg') + ansible_modules_path = os.path.join(settings.APPS_DIR, 'libs', 'ansible', 'modules') os.environ.setdefault('LC_ALL', 'C.UTF-8') os.environ.setdefault('PYTHONOPTIMIZE', '1') os.environ.setdefault('ANSIBLE_FORCE_COLOR', 'True') diff --git a/apps/ops/ansible/modules/__init__.py b/apps/libs/__init__.py similarity index 100% rename from apps/ops/ansible/modules/__init__.py rename to apps/libs/__init__.py diff --git a/apps/ops/ansible/modules_utils/__init__.py b/apps/libs/ansible/__init__.py similarity index 100% rename from apps/ops/ansible/modules_utils/__init__.py rename to apps/libs/ansible/__init__.py diff --git a/apps/ops/ansible/ansible.cfg b/apps/libs/ansible/ansible.cfg similarity index 75% rename from apps/ops/ansible/ansible.cfg rename to apps/libs/ansible/ansible.cfg index 8e0d9918e..d8b66b880 100644 --- a/apps/ops/ansible/ansible.cfg +++ b/apps/libs/ansible/ansible.cfg @@ -1,7 +1,7 @@ [defaults] forks = 10 host_key_checking = False -library = /opt/jumpserver/apps/ops/ansible/modules:./modules +library = /opt/jumpserver/apps/libs/ansible/modules:./modules timeout = 65 [inventory] [privilege_escalation] diff --git a/apps/libs/ansible/modules/__init__.py b/apps/libs/ansible/modules/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/apps/ops/ansible/modules/custom_command.py b/apps/libs/ansible/modules/custom_command.py similarity index 98% rename from apps/ops/ansible/modules/custom_command.py rename to apps/libs/ansible/modules/custom_command.py index 947da2d31..55d0537ab 100644 --- a/apps/ops/ansible/modules/custom_command.py +++ b/apps/libs/ansible/modules/custom_command.py @@ -63,7 +63,7 @@ name: from ansible.module_utils.basic import AnsibleModule -from ops.ansible.modules_utils.custom_common import ( +from libs.ansible.modules_utils.custom_common import ( SSHClient, common_argument_spec ) diff --git a/apps/ops/ansible/modules/mongodb_ping.py b/apps/libs/ansible/modules/mongodb_ping.py similarity index 100% rename from apps/ops/ansible/modules/mongodb_ping.py rename to apps/libs/ansible/modules/mongodb_ping.py diff --git a/apps/ops/ansible/modules/mongodb_user.py b/apps/libs/ansible/modules/mongodb_user.py similarity index 100% rename from apps/ops/ansible/modules/mongodb_user.py rename to apps/libs/ansible/modules/mongodb_user.py diff --git a/apps/ops/ansible/modules/oracle_info.py b/apps/libs/ansible/modules/oracle_info.py similarity index 99% rename from apps/ops/ansible/modules/oracle_info.py rename to apps/libs/ansible/modules/oracle_info.py index 005206be9..301f4d3fc 100644 --- a/apps/ops/ansible/modules/oracle_info.py +++ b/apps/libs/ansible/modules/oracle_info.py @@ -91,7 +91,7 @@ users: from ansible.module_utils.basic import AnsibleModule -from ops.ansible.modules_utils.oracle_common import ( +from libs.ansible.modules_utils.oracle_common import ( OracleClient, oracle_common_argument_spec ) diff --git a/apps/ops/ansible/modules/oracle_ping.py b/apps/libs/ansible/modules/oracle_ping.py similarity index 97% rename from apps/ops/ansible/modules/oracle_ping.py rename to apps/libs/ansible/modules/oracle_ping.py index df1069d11..668c16ea6 100644 --- a/apps/ops/ansible/modules/oracle_ping.py +++ b/apps/libs/ansible/modules/oracle_ping.py @@ -46,7 +46,7 @@ conn_err_msg: ''' from ansible.module_utils.basic import AnsibleModule -from ops.ansible.modules_utils.oracle_common import ( +from libs.ansible.modules_utils.oracle_common import ( OracleClient, oracle_common_argument_spec ) diff --git a/apps/ops/ansible/modules/oracle_user.py b/apps/libs/ansible/modules/oracle_user.py similarity index 99% rename from apps/ops/ansible/modules/oracle_user.py rename to apps/libs/ansible/modules/oracle_user.py index c1d485e40..8a70b78e9 100644 --- a/apps/ops/ansible/modules/oracle_user.py +++ b/apps/libs/ansible/modules/oracle_user.py @@ -93,7 +93,7 @@ name: from ansible.module_utils.basic import AnsibleModule -from ops.ansible.modules_utils.oracle_common import ( +from libs.ansible.modules_utils.oracle_common import ( OracleClient, oracle_common_argument_spec ) diff --git a/apps/ops/ansible/modules/rdp_ping.py b/apps/libs/ansible/modules/rdp_ping.py similarity index 100% rename from apps/ops/ansible/modules/rdp_ping.py rename to apps/libs/ansible/modules/rdp_ping.py diff --git a/apps/ops/ansible/modules/ssh_ping.py b/apps/libs/ansible/modules/ssh_ping.py similarity index 95% rename from apps/ops/ansible/modules/ssh_ping.py rename to apps/libs/ansible/modules/ssh_ping.py index 33df9f969..f5b08c63b 100644 --- a/apps/ops/ansible/modules/ssh_ping.py +++ b/apps/libs/ansible/modules/ssh_ping.py @@ -34,7 +34,7 @@ is_available: from ansible.module_utils.basic import AnsibleModule -from ops.ansible.modules_utils.custom_common import ( +from libs.ansible.modules_utils.custom_common import ( SSHClient, common_argument_spec ) diff --git a/apps/ops/ansible/modules/telnet_ping.py b/apps/libs/ansible/modules/telnet_ping.py similarity index 100% rename from apps/ops/ansible/modules/telnet_ping.py rename to apps/libs/ansible/modules/telnet_ping.py diff --git a/apps/libs/ansible/modules_utils/__init__.py b/apps/libs/ansible/modules_utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/apps/ops/ansible/modules_utils/custom_common.py b/apps/libs/ansible/modules_utils/custom_common.py similarity index 100% rename from apps/ops/ansible/modules_utils/custom_common.py rename to apps/libs/ansible/modules_utils/custom_common.py diff --git a/apps/ops/ansible/modules_utils/oracle_common.py b/apps/libs/ansible/modules_utils/oracle_common.py similarity index 99% rename from apps/ops/ansible/modules_utils/oracle_common.py rename to apps/libs/ansible/modules_utils/oracle_common.py index a89543bc1..024272a1f 100644 --- a/apps/ops/ansible/modules_utils/oracle_common.py +++ b/apps/libs/ansible/modules_utils/oracle_common.py @@ -91,4 +91,3 @@ class OracleClient(object): self._conn.close() except: pass - diff --git a/apps/ops/ansible/runner.py b/apps/ops/ansible/runner.py index b19e51618..1d808f196 100644 --- a/apps/ops/ansible/runner.py +++ b/apps/ops/ansible/runner.py @@ -101,7 +101,7 @@ class PlaybookRunner: entry = os.path.basename(self.playbook) playbook_dir = os.path.dirname(self.playbook) project_playbook_dir = os.path.join(self.project_dir, "project") - shutil.copytree(playbook_dir, project_playbook_dir) + shutil.copytree(playbook_dir, project_playbook_dir, dirs_exist_ok=True) self.playbook = entry def run(self, verbosity=0, **kwargs): diff --git a/receptor b/receptor index 9518e2c99..f9a17b711 100755 --- a/receptor +++ b/receptor @@ -13,20 +13,26 @@ import psutil from psutil import NoSuchProcess ANSIBLE_RUNNER_COMMAND = "ansible-runner" -DEFAULT_SHARE_DIR = "data/share" + +PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) +APPS_DIR = os.path.join(PROJECT_DIR, 'apps') +TEMP_DIR = os.path.join(PROJECT_DIR, "tmp") + +DEFAULT_SHARE_DIR = os.path.join(PROJECT_DIR, "data", "share") +DEFAULT_ANSIBLE_MODULES_DIR = os.path.join(APPS_DIR, "libs", "ansible", "modules") DEFAULT_CONTROL_SOCK_PATH = os.path.join(DEFAULT_SHARE_DIR, "control.sock") logger = logging.getLogger(__name__) +os.chdir(APPS_DIR) + class ReceptorService: def __init__(self): - self.pid_file = "tmp/receptor.pid" + self.pid_file = os.path.join(TEMP_DIR, "receptor.pid") self.receptor_command = [ 'receptor', '--local-only', - '--log-level', - 'level=Debug', '--node', 'id=primary', '--control-service', 'service=control', @@ -39,7 +45,7 @@ class ReceptorService: '--work-command', 'worktype={}'.format("kill"), 'command={}'.format("python"), - "params=receptor kill", + "params={} kill".format(os.path.join(PROJECT_DIR, "receptor")), 'allowruntimeparams=true' ] @@ -67,6 +73,8 @@ class ReceptorService: os.remove(self.pid_file) os.environ.update({'LOCAL_CONNECTION_ENABLED': '1'}) + os.environ.setdefault('ANSIBLE_LIBRARY', DEFAULT_ANSIBLE_MODULES_DIR) + os.environ.update({'PYTHONPATH': APPS_DIR}) process = subprocess.Popen(self.receptor_command) with open(self.pid_file, 'w') as f: f.write(str(process.pid))