]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
python: manager: modules restructuring
authorAleš Mrázek <ales.mrazek@nic.cz>
Fri, 30 Aug 2024 08:03:41 +0000 (10:03 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 6 Sep 2024 22:28:31 +0000 (00:28 +0200)
- new KresManager exception
- imports formatting

python/knot_resolver/manager/config_store.py
python/knot_resolver/manager/exceptions.py [new file with mode: 0644]
python/knot_resolver/manager/logging.py [moved from python/knot_resolver/manager/log.py with 100% similarity]
python/knot_resolver/manager/main.py
python/knot_resolver/manager/manager.py [moved from python/knot_resolver/manager/kres_manager.py with 100% similarity]
python/knot_resolver/manager/server.py

index 08fbb71e178ffaea2e5b3acd93db0e23951bd6d9..214062b2b4bb453be9ab18273cacd19b3d8b81f4 100644 (file)
@@ -2,12 +2,13 @@ import asyncio
 from asyncio import Lock
 from typing import Any, Awaitable, Callable, List, Tuple
 
-from knot_resolver import KresBaseException
 from knot_resolver.datamodel import KresConfig
 from knot_resolver.utils.functional import Result
 from knot_resolver.utils.modeling.exceptions import DataParsingError
 from knot_resolver.utils.modeling.types import NoneType
 
+from .exceptions import KresManagerException
+
 VerifyCallback = Callable[[KresConfig, KresConfig], Awaitable[Result[None, str]]]
 UpdateCallback = Callable[[KresConfig], Awaitable[None]]
 
@@ -27,7 +28,7 @@ class ConfigStore:
         err_res = filter(lambda r: r.is_err(), results)
         errs = list(map(lambda r: r.unwrap_err(), err_res))
         if len(errs) > 0:
-            raise KresBaseException("Configuration validation failed. The reasons are:\n - " + "\n - ".join(errs))
+            raise KresManagerException("Configuration validation failed. The reasons are:\n - " + "\n - ".join(errs))
 
         async with self._update_lock:
             # update the stored config with the new version
diff --git a/python/knot_resolver/manager/exceptions.py b/python/knot_resolver/manager/exceptions.py
new file mode 100644 (file)
index 0000000..77bc4d9
--- /dev/null
@@ -0,0 +1,5 @@
+from knot_resolver import KresBaseException
+
+
+class KresManagerException(KresBaseException):
+    pass
index 673727e102f0be7b19913192a46150f810a5c456..7c2da663aa3c0c0acf6ae6321c494bb0607866e2 100644 (file)
@@ -11,7 +11,7 @@ from typing import NoReturn
 
 from knot_resolver import compat
 from knot_resolver.constants import CONFIG_FILE_PATH_DEFAULT, CONFIG_FILE_PATH_ENV_VAR, VERSION
-from knot_resolver.manager.log import logger_startup
+from knot_resolver.manager.logging import logger_startup
 from knot_resolver.manager.server import start_server
 
 
index a02f4ef9f5a9b9e29de43fddb057a1e8ae8f00c3..23fedddee7e608bcbe28f13a73b793dab2f909c0 100644 (file)
@@ -17,8 +17,6 @@ from aiohttp.web_app import Application
 from aiohttp.web_response import json_response
 from aiohttp.web_runner import AppRunner, TCPSite, UnixSite
 
-import knot_resolver.utils.custom_atexit as atexit
-from knot_resolver import KresBaseException
 from knot_resolver.compat import asyncio as asyncio_compat
 from knot_resolver.constants import CONFIG_FILE_PATH_DEFAULT, PID_FILE_NAME
 from knot_resolver.controller import get_best_controller_implementation
@@ -28,9 +26,8 @@ from knot_resolver.datamodel.cache_schema import CacheClearRPCSchema
 from knot_resolver.datamodel.config_schema import KresConfig, get_rundir_without_validation
 from knot_resolver.datamodel.globals import Context, set_global_validation_context
 from knot_resolver.datamodel.management_schema import ManagementSchema
-from knot_resolver.manager import log, statistics
-from knot_resolver.manager.config_store import ConfigStore
-from knot_resolver.manager.constants import init_user_constants
+from knot_resolver.manager import statistics
+from knot_resolver.utils import custom_atexit as atexit
 from knot_resolver.utils import ignore_exceptions_optional
 from knot_resolver.utils.async_utils import readfile
 from knot_resolver.utils.etag import structural_etag
@@ -41,7 +38,11 @@ from knot_resolver.utils.modeling.query import query
 from knot_resolver.utils.modeling.types import NoneType
 from knot_resolver.utils.systemd_notify import systemd_notify
 
-from .kres_manager import KresManager
+from .config_store import ConfigStore
+from .constants import init_user_constants
+from .exceptions import KresManagerException
+from .logging import logger_init
+from .manager import KresManager
 
 logger = logging.getLogger(__name__)
 
@@ -61,7 +62,7 @@ async def error_handler(request: web.Request, handler: Any) -> web.Response:
         return web.Response(text=f"validation of configuration failed:\n{e}", status=HTTPStatus.BAD_REQUEST)
     except DataParsingError as e:
         return web.Response(text=f"request processing error:\n{e}", status=HTTPStatus.BAD_REQUEST)
-    except KresBaseException as e:
+    except KresManagerException as e:
         return web.Response(text=f"request processing failed:\n{e}", status=HTTPStatus.INTERNAL_SERVER_ERROR)
 
 
@@ -129,7 +130,7 @@ class Server:
             except (DataParsingError, DataValidationError) as e:
                 logger.error(f"Failed to parse the updated configuration file: {e}")
                 logger.error("Configuration have NOT been changed.")
-            except KresBaseException as e:
+            except KresManagerException as e:
                 logger.error(f"Reloading of the configuration file failed: {e}")
                 logger.error("Configuration have NOT been changed.")
 
@@ -364,7 +365,7 @@ class Server:
                 nsite = web.TCPSite(self.runner, str(mgn.interface.addr), int(mgn.interface.port))
                 logger.info(f"Starting API HTTP server on http://{mgn.interface.addr}:{mgn.interface.port}")
             else:
-                raise KresBaseException("Requested API on unsupported configuration format.")
+                raise KresManagerException("Requested API on unsupported configuration format.")
             await nsite.start()
 
             # stop the old listen
@@ -395,7 +396,7 @@ async def _load_raw_config(config: Union[Path, Dict[str, Any]]) -> Dict[str, Any
     # Initial configuration of the manager
     if isinstance(config, Path):
         if not config.exists():
-            raise KresBaseException(
+            raise KresManagerException(
                 f"Manager is configured to load config file at {config} on startup, but the file does not exist."
             )
         else:
@@ -467,12 +468,12 @@ def _lock_working_directory(attempt: int = 0) -> None:
                     os.unlink(PID_FILE_NAME)
                     _lock_working_directory(attempt=attempt + 1)
                     return
-            raise KresBaseException(
+            raise KresManagerException(
                 "Another manager is running in the same working directory."
                 f" PID file is located at {os.getcwd()}/{PID_FILE_NAME}"
             ) from e
         else:
-            raise KresBaseException(
+            raise KresManagerException(
                 "Another manager is running in the same working directory."
                 f" PID file is located at {os.getcwd()}/{PID_FILE_NAME}"
             ) from e
@@ -550,7 +551,7 @@ async def start_server(config: Path = CONFIG_FILE_PATH_DEFAULT) -> int:
 
         # Up to this point, we have been logging to memory buffer. But now, when we have the configuration loaded, we
         # can flush the buffer into the proper place
-        await log.logger_init(config_store)
+        await logger_init(config_store)
 
         # With configuration on hand, we can initialize monitoring. We want to do this before any subprocesses are
         # started, therefore before initializing manager
@@ -578,7 +579,7 @@ async def start_server(config: Path = CONFIG_FILE_PATH_DEFAULT) -> int:
         # and finally exec what we were told to exec
         os.execl(*e.exec_args)
 
-    except KresBaseException as e:
+    except KresManagerException as e:
         # We caught an error with a pretty error message. Just print it and exit.
         logger.error(e)
         return 1