|
|
|
@ -13,6 +13,25 @@ SHELL_EXECUTABLE = '/bin/bash' # Гарантируем использовани
|
|
|
|
|
|
|
|
|
|
|
|
# --- Функции, выполняющие действия ---
|
|
|
|
# --- Функции, выполняющие действия ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_service_running(is_running_cmd, service_name):
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Проверяет, запущен ли сервис, с помощью предоставленной команды.
|
|
|
|
|
|
|
|
Возвращает True, если код возврата 0.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
print(f"[{service_name}] Checking if service is already running...", flush=True)
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
subprocess.run(
|
|
|
|
|
|
|
|
is_running_cmd,
|
|
|
|
|
|
|
|
shell=True,
|
|
|
|
|
|
|
|
check=True,
|
|
|
|
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
|
|
|
|
executable=SHELL_EXECUTABLE
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def are_users_there(check_cmd, service_name):
|
|
|
|
def are_users_there(check_cmd, service_name):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Выполняет команду проверки через shell.
|
|
|
|
Выполняет команду проверки через shell.
|
|
|
|
@ -107,9 +126,16 @@ def run_service_butler(config):
|
|
|
|
Основной цикл конечного автомата для одного сервиса.
|
|
|
|
Основной цикл конечного автомата для одного сервиса.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
ctx = { "wasted": 0 }
|
|
|
|
ctx = { "wasted": 0 }
|
|
|
|
state = "wait_client" # Всегда начинаем с ожидания клиента
|
|
|
|
service_name = config['service_name']
|
|
|
|
|
|
|
|
|
|
|
|
print(f"[{config['service_name']}] Butler process started.", flush=True)
|
|
|
|
print(f"[{service_name}] Butler process started.", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if is_service_running(config['is_running_command'], service_name):
|
|
|
|
|
|
|
|
print(f"[{service_name}] Service is already running. Starting in 'check_users' state.", flush=True)
|
|
|
|
|
|
|
|
state = "check_users"
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
print(f"[{service_name}] Service is not running. Starting in 'wait_client' state.", flush=True)
|
|
|
|
|
|
|
|
state = "wait_client"
|
|
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
state_fn = state_map[state]
|
|
|
|
state_fn = state_map[state]
|
|
|
|
@ -143,6 +169,7 @@ def main():
|
|
|
|
"service_name": service_name,
|
|
|
|
"service_name": service_name,
|
|
|
|
"start_command": config.get(service_name, 'start_command'),
|
|
|
|
"start_command": config.get(service_name, 'start_command'),
|
|
|
|
"stop_command": config.get(service_name, 'stop_command'),
|
|
|
|
"stop_command": config.get(service_name, 'stop_command'),
|
|
|
|
|
|
|
|
"is_running_command": config.get(service_name, 'is_running_command'),
|
|
|
|
"listen_port": config.getint(service_name, 'listen_port'),
|
|
|
|
"listen_port": config.getint(service_name, 'listen_port'),
|
|
|
|
"listen_host": config.get(service_name, 'listen_host'),
|
|
|
|
"listen_host": config.get(service_name, 'listen_host'),
|
|
|
|
"check_command": config.get(service_name, 'check_command'),
|
|
|
|
"check_command": config.get(service_name, 'check_command'),
|
|
|
|
|