Refactor config file service facade wrappers and mark TASK-06 complete in Docs/Tasks.md
This commit is contained in:
@@ -25,7 +25,13 @@ from app.exceptions import (
|
||||
ConfigWriteError,
|
||||
JailNotFoundInConfigError,
|
||||
)
|
||||
import app.services.config_file_service as config_file_service
|
||||
import app.services.jail_service as jail_service
|
||||
from app.utils.config_file_utils import (
|
||||
_get_active_jail_names,
|
||||
_parse_jails_sync,
|
||||
_safe_jail_name,
|
||||
build_parser,
|
||||
)
|
||||
from app.models.config import (
|
||||
ActionConfig,
|
||||
ActionConfigUpdate,
|
||||
@@ -39,6 +45,22 @@ from app.utils.async_utils import run_blocking
|
||||
|
||||
log: structlog.stdlib.BoundLogger = structlog.get_logger()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Internal wrappers for shared config helpers.
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _parse_jails_sync(config_dir: Path) -> tuple[dict[str, dict[str, str]], Path]:
|
||||
from app.services import config_file_service
|
||||
|
||||
return config_file_service._parse_jails_sync(config_dir)
|
||||
|
||||
|
||||
async def _get_active_jail_names(socket_path: str) -> set[str]:
|
||||
from app.services import config_file_service
|
||||
|
||||
return await config_file_service._get_active_jail_names(socket_path)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Constants
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -254,7 +276,7 @@ def _append_jail_action_sync(
|
||||
|
||||
local_path = jail_d / f"{jail_name}.local"
|
||||
|
||||
parser = config_file_service.build_parser()
|
||||
parser = build_parser()
|
||||
if local_path.is_file():
|
||||
try:
|
||||
parser.read(str(local_path), encoding="utf-8")
|
||||
@@ -343,7 +365,7 @@ def _remove_jail_action_sync(
|
||||
if not local_path.is_file():
|
||||
return
|
||||
|
||||
parser = config_file_service.build_parser()
|
||||
parser = build_parser()
|
||||
try:
|
||||
parser.read(str(local_path), encoding="utf-8")
|
||||
except (configparser.Error, OSError) as exc:
|
||||
@@ -476,11 +498,13 @@ async def list_actions(
|
||||
"""
|
||||
action_d = Path(config_dir) / "action.d"
|
||||
|
||||
raw_actions: list[tuple[str, str, str, bool, str]] = await run_blocking( _parse_actions_sync, action_d)
|
||||
from app.services import config_file_service
|
||||
|
||||
raw_actions: list[tuple[str, str, str, bool, str]] = await run_blocking(_parse_actions_sync, action_d)
|
||||
|
||||
all_jails_result, active_names = await asyncio.gather(
|
||||
run_blocking(config_file_service._parse_jails_sync, Path(config_dir)),
|
||||
config_file_service._get_active_jail_names(socket_path),
|
||||
run_blocking(_parse_jails_sync, Path(config_dir)),
|
||||
_get_active_jail_names(socket_path),
|
||||
)
|
||||
all_jails, _source_files = all_jails_result
|
||||
|
||||
@@ -572,13 +596,13 @@ async def get_action(
|
||||
else:
|
||||
raise ActionNotFoundError(base_name)
|
||||
|
||||
content, has_local, source_path = await run_blocking( _read)
|
||||
content, has_local, source_path = await run_blocking(_read)
|
||||
|
||||
cfg = conffile_parser.parse_action_file(content, name=base_name, filename=f"{base_name}.conf")
|
||||
|
||||
all_jails_result, active_names = await asyncio.gather(
|
||||
run_blocking(config_file_service._parse_jails_sync, Path(config_dir)),
|
||||
config_file_service._get_active_jail_names(socket_path),
|
||||
run_blocking(_parse_jails_sync, Path(config_dir)),
|
||||
_get_active_jail_names(socket_path),
|
||||
)
|
||||
all_jails, _source_files = all_jails_result
|
||||
action_to_jails = _build_action_to_jails_map(all_jails, active_names)
|
||||
@@ -663,6 +687,8 @@ async def update_action(
|
||||
|
||||
if do_reload:
|
||||
try:
|
||||
from app.services import config_file_service
|
||||
|
||||
await config_file_service.jail_service.reload_all(socket_path)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning(
|
||||
@@ -731,6 +757,8 @@ async def create_action(
|
||||
|
||||
if do_reload:
|
||||
try:
|
||||
from app.services import config_file_service
|
||||
|
||||
await config_file_service.jail_service.reload_all(socket_path)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning(
|
||||
@@ -823,11 +851,10 @@ async def assign_action_to_jail(
|
||||
``action.d/``.
|
||||
ConfigWriteError: If writing fails.
|
||||
"""
|
||||
config_file_service.safe_jail_name(jail_name)
|
||||
_safe_jail_name(jail_name)
|
||||
_safe_action_name(req.action_name)
|
||||
|
||||
|
||||
all_jails, _src = await run_blocking(config_file_service._parse_jails_sync, Path(config_dir))
|
||||
all_jails, _src = await run_blocking(_parse_jails_sync, Path(config_dir))
|
||||
if jail_name not in all_jails:
|
||||
raise JailNotFoundInConfigError(jail_name)
|
||||
|
||||
@@ -857,6 +884,8 @@ async def assign_action_to_jail(
|
||||
|
||||
if do_reload:
|
||||
try:
|
||||
from app.services import config_file_service
|
||||
|
||||
await config_file_service.jail_service.reload_all(socket_path)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning(
|
||||
@@ -900,11 +929,10 @@ async def remove_action_from_jail(
|
||||
JailNotFoundError: If *jail_name* is not defined in any config.
|
||||
ConfigWriteError: If writing fails.
|
||||
"""
|
||||
config_file_service.safe_jail_name(jail_name)
|
||||
_safe_jail_name(jail_name)
|
||||
_safe_action_name(action_name)
|
||||
|
||||
|
||||
all_jails, _src = await run_blocking(config_file_service._parse_jails_sync, Path(config_dir))
|
||||
all_jails, _src = await run_blocking(_parse_jails_sync, Path(config_dir))
|
||||
if jail_name not in all_jails:
|
||||
raise JailNotFoundInConfigError(jail_name)
|
||||
|
||||
@@ -916,6 +944,8 @@ async def remove_action_from_jail(
|
||||
|
||||
if do_reload:
|
||||
try:
|
||||
from app.services import config_file_service
|
||||
|
||||
await config_file_service.jail_service.reload_all(socket_path)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
log.warning(
|
||||
|
||||
Reference in New Issue
Block a user