]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
python/knot_resolver: KresBaseException added
authorAleš Mrázek <ales.mrazek@nic.cz>
Thu, 29 Aug 2024 11:30:27 +0000 (13:30 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 6 Sep 2024 22:28:31 +0000 (00:28 +0200)
12 files changed:
python/knot_resolver/__init__.py
python/knot_resolver/controller/exceptions.py [new file with mode: 0644]
python/knot_resolver/controller/interface.py
python/knot_resolver/controller/registered_workers.py
python/knot_resolver/controller/supervisord/__init__.py
python/knot_resolver/exceptions.py [new file with mode: 0644]
python/knot_resolver/manager/config_store.py
python/knot_resolver/manager/exceptions.py [deleted file]
python/knot_resolver/manager/kres_manager.py
python/knot_resolver/manager/server.py
python/knot_resolver/utils/modeling/exceptions.py
tests/manager/datamodel/types/test_base_types.py

index 8407cd6d5a7232de8f2635a607632ec97f39db6a..4529ec6830a2b3f4b5f2c2a19dab9db7d160a57e 100644 (file)
@@ -1,6 +1,6 @@
-from .datamodel import KresConfig
+from .exceptions import KresBaseException
 from .constants import VERSION
 
 __version__ = VERSION
 
-__all__ = ["KresConfig"]
+__all__ = ["KresBaseException"]
diff --git a/python/knot_resolver/controller/exceptions.py b/python/knot_resolver/controller/exceptions.py
new file mode 100644 (file)
index 0000000..149c298
--- /dev/null
@@ -0,0 +1,19 @@
+from typing import List
+
+from knot_resolver import KresBaseException
+
+
+class SubprocessControllerException(KresBaseException):
+    pass
+
+
+class SubprocessControllerExecException(Exception):
+    """
+    Exception that is used to deliberately terminate system startup
+    and make exec() of something else. This is used by the subprocess controller
+    as supervisord to run as the top-level process in a process tree hierarchy.
+    """
+
+    def __init__(self, exec_args: List[str], *args: object) -> None:
+        self.exec_args = exec_args
+        super().__init__(*args)
index 02bbaa500531bf4285c23acd334c6b1d13ce9e9f..ba9ac28d371b3e024ef4f1cc5f3ea4afef4ade38 100644 (file)
@@ -12,7 +12,7 @@ from weakref import WeakValueDictionary
 
 from knot_resolver.manager.constants import kresd_config_file, policy_loader_config_file
 from knot_resolver.datamodel.config_schema import KresConfig
-from knot_resolver.manager.exceptions import SubprocessControllerException
+from knot_resolver.controller.exceptions import SubprocessControllerException
 from knot_resolver.controller.registered_workers import register_worker, unregister_worker
 from knot_resolver.utils.async_utils import writefile
 
index 2d3176c373c3bb9bc741e6546b4979a95cf7d9c1..eed1adedbe67d4416bbdff8b8ee32288f6c84337 100644 (file)
@@ -2,7 +2,7 @@ import asyncio
 import logging
 from typing import TYPE_CHECKING, Dict, List, Tuple
 
-from knot_resolver.manager.exceptions import SubprocessControllerException
+from .exceptions import SubprocessControllerException
 
 if TYPE_CHECKING:
     from knot_resolver.controller.interface import KresID, Subprocess
index 592b76be2dc5eb226ee058fc1b0f4360f27ec83b..b9f3cae20545933e4e5390f28d25840fce9bf0f8 100644 (file)
@@ -9,7 +9,7 @@ import supervisor.xmlrpc  # type: ignore[import]
 from knot_resolver.compat.asyncio import async_in_a_thread
 from knot_resolver.manager.constants import supervisord_config_file, supervisord_pid_file, supervisord_sock_file
 from knot_resolver.datamodel.config_schema import KresConfig
-from knot_resolver.manager.exceptions import CancelStartupExecInsteadException, SubprocessControllerException
+from knot_resolver.controller.exceptions import SubprocessControllerExecException, SubprocessControllerException
 from knot_resolver.controller.interface import (
     KresID,
     Subprocess,
@@ -37,7 +37,7 @@ async def _exec_supervisord(config: KresConfig) -> NoReturn:
     logger.debug("Writing supervisord config")
     await write_config_file(config)
     logger.debug("Execing supervisord")
-    raise CancelStartupExecInsteadException(
+    raise SubprocessControllerExecException(
         [
             str(which.which("supervisord")),
             "supervisord",
diff --git a/python/knot_resolver/exceptions.py b/python/knot_resolver/exceptions.py
new file mode 100644 (file)
index 0000000..3e90b0b
--- /dev/null
@@ -0,0 +1,4 @@
+class KresBaseException(Exception):
+    """
+    Base class for all custom exceptions we use in Knot Resolver.
+    """
index 1c0174f230f0cc791dc10fd5883567c5872a7393..d783f7384e58b57f27f24d58a50b0f9f7476fa3b 100644 (file)
@@ -3,7 +3,7 @@ from asyncio import Lock
 from typing import Any, Awaitable, Callable, List, Tuple
 
 from knot_resolver.datamodel import KresConfig
-from knot_resolver.manager.exceptions import KresManagerException
+from knot_resolver import KresBaseException
 from knot_resolver.utils.functional import Result
 from knot_resolver.utils.modeling.exceptions import DataParsingError
 from knot_resolver.utils.modeling.types import NoneType
@@ -27,7 +27,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 KresManagerException("Configuration validation failed. The reasons are:\n - " + "\n - ".join(errs))
+            raise KresBaseException("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
deleted file mode 100644 (file)
index 5b05d98..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-from typing import List
-
-
-class CancelStartupExecInsteadException(Exception):
-    """
-    Exception used for terminating system startup and instead
-    causing an exec of something else. Could be used by subprocess
-    controllers such as supervisord to allow them to run as top-level
-    process in a process tree.
-    """
-
-    def __init__(self, exec_args: List[str], *args: object) -> None:
-        self.exec_args = exec_args
-        super().__init__(*args)
-
-
-class KresManagerException(Exception):
-    """
-    Base class for all custom exceptions we use in our code
-    """
-
-
-class SubprocessControllerException(KresManagerException):
-    pass
-
-
-class SubprocessControllerTimeoutException(KresManagerException):
-    pass
index 606f5b243d0d9ca9f5dc7cbec42673c2053ef728..913d872413ddbd0fee8b5818c52483395a87a89d 100644 (file)
@@ -17,7 +17,7 @@ from knot_resolver.constants import (
     FIX_COUNTER_DECREASE_INTERVAL_SEC,
     WATCHDOG_INTERVAL_SEC,
 )
-from knot_resolver.manager.exceptions import SubprocessControllerException
+from knot_resolver.controller.exceptions import SubprocessControllerException
 from knot_resolver.controller.interface import (
     Subprocess,
     SubprocessController,
@@ -31,7 +31,7 @@ from knot_resolver.controller.registered_workers import (
 from knot_resolver.utils.functional import Result
 from knot_resolver.utils.modeling.types import NoneType
 
-from knot_resolver import KresConfig
+from knot_resolver.datamodel import KresConfig
 
 logger = logging.getLogger(__name__)
 
index a25b5eb2a51995a625c36fa2ffc39e7126a8f595..d95bcf7d5cd6764c08ba6a2b9335f77f9e2acf91 100644 (file)
@@ -19,6 +19,7 @@ from aiohttp.web_runner import AppRunner, TCPSite, UnixSite
 from typing_extensions import Literal
 
 import knot_resolver.utils.custom_atexit as atexit
+from knot_resolver import KresBaseException
 from knot_resolver.constants import CONFIG_FILE_PATH_DEFAULT, PID_FILE_NAME
 from knot_resolver.manager import log, statistics
 from knot_resolver.compat import asyncio as asyncio_compat
@@ -28,8 +29,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.exceptions import CancelStartupExecInsteadException, KresManagerException
 from knot_resolver.controller import get_best_controller_implementation
+from knot_resolver.controller.exceptions import SubprocessControllerExecException
 from knot_resolver.controller.registered_workers import command_single_registered_worker
 from knot_resolver.utils import ignore_exceptions_optional
 from knot_resolver.utils.async_utils import readfile
@@ -65,7 +66,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 KresManagerException as e:
+    except KresBaseException as e:
         return web.Response(text=f"request processing failed:\n{e}", status=HTTPStatus.INTERNAL_SERVER_ERROR)
 
 
@@ -133,7 +134,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 KresManagerException as e:
+            except KresBaseException as e:
                 logger.error(f"Reloading of the configuration file failed: {e}")
                 logger.error("Configuration have NOT been changed.")
 
@@ -368,7 +369,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 KresManagerException("Requested API on unsupported configuration format.")
+                raise KresBaseException("Requested API on unsupported configuration format.")
             await nsite.start()
 
             # stop the old listen
@@ -399,7 +400,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 KresManagerException(
+            raise KresBaseException(
                 f"Manager is configured to load config file at {config} on startup, but the file does not exist."
             )
         else:
@@ -471,12 +472,12 @@ def _lock_working_directory(attempt: int = 0) -> None:
                     os.unlink(PID_FILE_NAME)
                     _lock_working_directory(attempt=attempt + 1)
                     return
-            raise KresManagerException(
+            raise KresBaseException(
                 "Another manager is running in the same working directory."
                 f" PID file is located at {os.getcwd()}/{PID_FILE_NAME}"
             ) from e
         else:
-            raise KresManagerException(
+            raise KresBaseException(
                 "Another manager is running in the same working directory."
                 f" PID file is located at {os.getcwd()}/{PID_FILE_NAME}"
             ) from e
@@ -566,7 +567,7 @@ async def start_server(config: Path = CONFIG_FILE_PATH_DEFAULT) -> int:
         # After we have loaded the configuration, we can start worring about subprocess management.
         manager = await _init_manager(config_store, server)
 
-    except CancelStartupExecInsteadException as e:
+    except SubprocessControllerExecException as e:
         # if we caught this exception, some component wants to perform a reexec during startup. Most likely, it would
         # be a subprocess manager like supervisord, which wants to make sure the manager runs under supervisord in
         # the process tree. So now we stop everything, and exec what we are told to. We are assuming, that the thing
@@ -582,7 +583,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 KresManagerException as e:
+    except KresBaseException as e:
         # We caught an error with a pretty error message. Just print it and exit.
         logger.error(e)
         return 1
index ea0573397fa1d860878a146974869992e9046b0e..478f5488489c84c9c545205000bc0f2df67b396f 100644 (file)
@@ -1,9 +1,9 @@
 from typing import Iterable, List
 
-from knot_resolver.manager.exceptions import KresManagerException
+from knot_resolver import KresBaseException
 
 
-class DataModelingBaseException(KresManagerException):
+class DataModelingBaseException(KresBaseException):
     """
     Base class for all exceptions used in modelling.
     """
index c74fe945ed82efdd0dbf6224e0c5d948e1f6d5d8..cf6248361d028cf6de132290f2da5869695f7f0a 100644 (file)
@@ -6,7 +6,7 @@ import pytest
 from pytest import raises
 
 from knot_resolver.datamodel.types.base_types import IntRangeBase, StringLengthBase
-from knot_resolver.manager.exceptions import KresManagerException
+from knot_resolver import KresBaseException
 
 
 @pytest.mark.parametrize("min,max", [(0, None), (None, 0), (1, 65535), (-65535, -1)])
@@ -34,7 +34,7 @@ def test_int_range_base(min: Optional[int], max: Optional[int]):
     invals.extend([random.randint(-sys.maxsize - 1, rmin - 1) for _ in range(n % 2)] if max else [])
 
     for inval in invals:
-        with raises(KresManagerException):
+        with raises(KresBaseException):
             Test(inval)
 
 
@@ -62,5 +62,5 @@ def test_str_bytes_length_base(min: Optional[int], max: Optional[int]):
     invals.extend(["x" * random.randint(1, rmin - 1) for _ in range(n % 2)] if max else [])
 
     for inval in invals:
-        with raises(KresManagerException):
+        with raises(KresBaseException):
             Test(inval)