Extract fail2ban restart orchestration into jail_service
This commit is contained in:
@@ -29,6 +29,7 @@ from app.models.jail import (
|
||||
JailStatus,
|
||||
JailSummary,
|
||||
)
|
||||
from app.utils.config_file_utils import start_daemon, wait_for_fail2ban
|
||||
from app.utils.fail2ban_client import (
|
||||
Fail2BanClient,
|
||||
Fail2BanCommand,
|
||||
@@ -759,6 +760,48 @@ async def restart(socket_path: str) -> None:
|
||||
raise JailOperationError(str(exc)) from exc
|
||||
|
||||
|
||||
async def restart_daemon(
|
||||
socket_path: str,
|
||||
start_cmd_parts: list[str],
|
||||
max_wait_seconds: float = _SOCKET_TIMEOUT,
|
||||
) -> bool:
|
||||
"""Restart the fail2ban daemon and verify it comes back online.
|
||||
|
||||
This function stops the daemon through the socket, starts it with the
|
||||
configured command, and probes the socket until fail2ban accepts status
|
||||
requests again.
|
||||
|
||||
Args:
|
||||
socket_path: Path to the fail2ban Unix domain socket.
|
||||
start_cmd_parts: The configured fail2ban start command split into
|
||||
executable and arguments.
|
||||
max_wait_seconds: The maximum number of seconds to wait for the daemon
|
||||
to become responsive after starting.
|
||||
|
||||
Returns:
|
||||
``True`` when the daemon is started and responsive.
|
||||
``False`` when the command failed or fail2ban never became responsive.
|
||||
|
||||
Raises:
|
||||
JailOperationError: If the stop command failed.
|
||||
~app.utils.fail2ban_client.Fail2BanConnectionError: If the socket
|
||||
cannot be reached while stopping fail2ban.
|
||||
"""
|
||||
await restart(socket_path)
|
||||
|
||||
if not await start_daemon(start_cmd_parts):
|
||||
log.warning(
|
||||
"fail2ban_start_command_failed",
|
||||
command=" ".join(start_cmd_parts),
|
||||
)
|
||||
return False
|
||||
|
||||
return await wait_for_fail2ban(
|
||||
socket_path,
|
||||
max_wait_seconds=max_wait_seconds,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Public API — Ban / Unban
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user