diff --git a/apps/common/management/commands/services/command.py b/apps/common/management/commands/services/command.py index 35658d7ec..34208bc22 100644 --- a/apps/common/management/commands/services/command.py +++ b/apps/common/management/commands/services/command.py @@ -17,7 +17,6 @@ class Services(TextChoices): web = 'web', 'web' celery = 'celery', 'celery' task = 'task', 'task' - receptor = 'receptor', 'receptor' all = 'all', 'all' @classmethod @@ -29,7 +28,6 @@ class Services(TextChoices): cls.celery_default: services.CeleryDefaultService, cls.celery_ansible: services.CeleryAnsibleService, cls.beat: services.BeatService, - cls.receptor: services.ReceptorService } return services_map.get(name) @@ -45,13 +43,9 @@ class Services(TextChoices): def task_services(cls): return cls.celery_services() + [cls.beat] - @classmethod - def receptor_services(cls): - return [cls.receptor] - @classmethod def all_services(cls): - return cls.web_services() + cls.task_services() + cls.receptor_services() + return cls.web_services() + cls.task_services() @classmethod def export_services_values(cls): diff --git a/apps/common/management/commands/services/services/__init__.py b/apps/common/management/commands/services/services/__init__.py index 85a4ead63..35329a7d4 100644 --- a/apps/common/management/commands/services/services/__init__.py +++ b/apps/common/management/commands/services/services/__init__.py @@ -3,4 +3,3 @@ from .celery_ansible import * from .celery_default import * from .flower import * from .gunicorn import * -from .receptor import * diff --git a/apps/common/management/commands/services/services/receptor.py b/apps/common/management/commands/services/services/receptor.py deleted file mode 100644 index 38976343f..000000000 --- a/apps/common/management/commands/services/services/receptor.py +++ /dev/null @@ -1,32 +0,0 @@ -from .base import BaseService -from ..hands import * - -__all__ = ['ReceptorService'] - -ANSIBLE_RUNNER_COMMAND = "ansible-runner" - - -class ReceptorService(BaseService): - @property - def cmd(self): - print("\n- Start Receptor as Ansible Runner Sandbox") - - cmd = [ - 'receptor', - '--local-only', - '--node', 'id=primary', - '--control-service', - 'service=control', - 'filename=/opt/jumpserver/data/share/control.sock', - '--work-command', - 'worktype={}'.format(ANSIBLE_RUNNER_COMMAND), - 'command={}'.format(ANSIBLE_RUNNER_COMMAND), - 'params=worker', - 'allowruntimeparams=true' - ] - - return cmd - - @property - def cwd(self): - return APPS_DIR diff --git a/entrypoint.sh b/entrypoint.sh index 58ed0b104..b5515df47 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -19,6 +19,8 @@ if [[ "$action" == "bash" || "$action" == "sh" ]];then elif [[ "$action" == "sleep" ]];then echo "Sleep 365 days" sleep 365d +elif [[ "$service" == "receptor" ]];then + python receptor "$action" else - python jms "${action}" "${service}" -fi + python jms "$action" "$service" +fi \ No newline at end of file diff --git a/receptor b/receptor new file mode 100755 index 000000000..15170f0ba --- /dev/null +++ b/receptor @@ -0,0 +1,108 @@ +#!/usr/bin/env python3 +# coding: utf-8 + +import argparse +import subprocess +import os +import signal + +ANSIBLE_RUNNER_COMMAND = "ansible-runner" +DEFAULT_CONTROL_SOCK_PATH = "/opt/jumpserver/data/share/control.sock" + + +class ReceptorService: + def __init__(self): + self.pid_file = "tmp/receptor.pid" + self.receptor_command = [ + 'receptor', + '--local-only', + '--node', 'id=primary', + '--control-service', + 'service=control', + 'filename={}'.format(DEFAULT_CONTROL_SOCK_PATH), + '--work-command', + 'worktype={}'.format(ANSIBLE_RUNNER_COMMAND), + 'command={}'.format(ANSIBLE_RUNNER_COMMAND), + 'params=worker', + 'allowruntimeparams=true' + ] + + def start(self): + if os.path.exists(self.pid_file): + with open(self.pid_file, 'r') as f: + pid_str = f.read() + try: + pid = int(pid_str) + os.kill(pid, 0) + print("\n- Receptor service is already running.") + return + except ProcessLookupError: + print("\n- PID file exists but process does not, starting Receptor...") + except ValueError: + print("\n- PID file is corrupted, starting Receptor...") + os.remove(self.pid_file) + + process = subprocess.Popen(self.receptor_command) + with open(self.pid_file, 'w') as f: + f.write(str(process.pid)) + print("\n- Receptor service started successfully.") + + def stop(self): + if not os.path.exists(self.pid_file): + print("\n- Receptor service is not running.") + return + with open(self.pid_file, 'r') as f: + pid = int(f.read()) + try: + os.kill(pid, signal.SIGTERM) + os.remove(self.pid_file) + print("\n- Receptor service stopped successfully.") + except ProcessLookupError: + print("\n- Failed to stop Receptor service: Process does not exist.") + os.remove(self.pid_file) + + def restart(self): + self.stop() + self.start() + + def status(self): + if os.path.exists(self.pid_file): + with open(self.pid_file, 'r') as f: + pid_str = f.read() + try: + pid = int(pid_str) + os.kill(pid, 0) + print("\n- Receptor service is running.") + return + except ProcessLookupError: + print("\n- Receptor service is not running.") + else: + print("\n- Receptor service is not running.") + + +def handle_receptor_action(action): + srv = ReceptorService() + if action == "start": + srv.start() + elif action == 'stop': + srv.stop() + elif action == "restart": + srv.restart() + elif action == "status": + srv.status() + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=""" + Jumpserver receptor service control tools; + """ + ) + parser.add_argument( + 'action', type=str, + choices=("start", "stop", "restart", "status"), + help="Action to run" + ) + + args = parser.parse_args() + handle_receptor_action(args.action)