From 9e31c8c6731e49e88d9129b54b6a988151d46fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Tue, 8 Aug 2023 13:35:43 +0200 Subject: [PATCH] support custom location for database persistent statefile (defaults to "capport.state" in cwd) --- capport-example.yaml | 1 + src/capport/comm/hub.py | 6 +++--- src/capport/config.py | 2 ++ src/capport/database.py | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/capport-example.yaml b/capport-example.yaml index b68c715..1f50aa6 100644 --- a/capport-example.yaml +++ b/capport-example.yaml @@ -11,3 +11,4 @@ server-names: - ... api-port: 8000 controller-port: 5000 +database-file: capport.state diff --git a/src/capport/comm/hub.py b/src/capport/comm/hub.py index 9a5cbc5..902beda 100644 --- a/src/capport/comm/hub.py +++ b/src/capport/comm/hub.py @@ -299,11 +299,11 @@ class Hub: self._hostname = socket.getfqdn() self._app = app self._is_controller = is_controller - state_filename: typing.Optional[str] + state_filename: str if is_controller: - state_filename = 'capport.state' + state_filename = config.database_file else: - state_filename = None + state_filename = '' self.database = capport.database.Database(state_filename=state_filename) self._anon_context = ssl.SSLContext() # python ssl doesn't support setting tls1.3 ciphers yet, so make sure we stay on 1.2 for now to enable anon diff --git a/src/capport/config.py b/src/capport/config.py index 03e2730..c0b4de2 100644 --- a/src/capport/config.py +++ b/src/capport/config.py @@ -23,6 +23,7 @@ class Config: session_timeout: int # in seconds api_port: int controller_port: int + database_file: str # empty str: disable database debug: bool @staticmethod @@ -58,6 +59,7 @@ class Config: session_timeout=data.pop('session-timeout', 3600), api_port=data.pop('api-port', 8000), controller_port=data.pop('controller-port', 5000), + database_file=str(data.pop('database-file', 'capport.state')), debug=data.pop('debug', False) ) if data: diff --git a/src/capport/database.py b/src/capport/database.py index 3907dbd..be65105 100644 --- a/src/capport/database.py +++ b/src/capport/database.py @@ -139,7 +139,7 @@ class NotReadyYet(Exception): class Database: - def __init__(self, state_filename: typing.Optional[str] = None): + def __init__(self, state_filename: str = ""): self._macs: dict[cptypes.MacAddress, MacEntry] = {} self._state_filename = state_filename self._changed_since_last_cleanup = False