core (main, config): Component auto-configuration:
New `jid`, `secret` and `autoconnect` field are used for component settings. When used, backend will automatically create the profile and set its relevant parameters, or check that existing profile is in sync (and update it if necessary). This make component installation easier to Libervia, as it is not necessary anymore to use the CLI frontend to install a component. rel 476
This commit is contained in:
parent
51776603e0
commit
a9fa2eeed4
1 changed files with 159 additions and 16 deletions
|
|
@ -22,7 +22,7 @@
|
|||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
from socket import getfqdn
|
||||
from typing import Annotated, Any, Literal, TypeVar, cast
|
||||
from typing import Annotated, Any, Literal, Self, TypeVar, cast
|
||||
|
||||
from pydantic import (
|
||||
AfterValidator,
|
||||
|
|
@ -32,6 +32,7 @@ from pydantic import (
|
|||
ValidationError,
|
||||
create_model,
|
||||
field_validator,
|
||||
model_validator,
|
||||
)
|
||||
from pydantic.types import PathType
|
||||
from pydantic_settings import (
|
||||
|
|
@ -45,6 +46,7 @@ from libervia.backend.core import exceptions
|
|||
from libervia.backend.core.constants import Const as C, xdg_data_home
|
||||
from libervia.backend.core.i18n import _
|
||||
from libervia.backend.core.log import getLogger
|
||||
from libervia.backend.models.types import JIDType
|
||||
from libervia.backend.tools.common.files_utils import get_downloads_dir
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
|
@ -103,6 +105,7 @@ class DynamicSettingsBase(ConfigOptions):
|
|||
|
||||
_key: str = ""
|
||||
_sub_key: str | None = None
|
||||
_import_name: str | None = None
|
||||
|
||||
@classmethod
|
||||
def _init_subclass(cls, /, key: str, plugin_info: dict | None = None) -> None:
|
||||
|
|
@ -127,6 +130,7 @@ class DynamicSettingsBase(ConfigOptions):
|
|||
f"There is already a plugin registered for {key!r}.{sub_key!r}."
|
||||
)
|
||||
register[sub_key] = cls
|
||||
cls._import_name = plugin_info.get(C.PI_IMPORT_NAME)
|
||||
else:
|
||||
cls._sub_key = None
|
||||
DynamicSettings[key] = cls
|
||||
|
|
@ -143,10 +147,40 @@ class PluginSettings(DynamicSettingsBase):
|
|||
class ComponentSettings(DynamicSettingsBase):
|
||||
"""Configuration options for components."""
|
||||
|
||||
jid: JIDType | None = Field(
|
||||
default=None,
|
||||
description=(
|
||||
"JID of the component. If used, the component profile will be created"
|
||||
" automatically (with the component entry point used as name), and"
|
||||
" auto-connect on backend startup."
|
||||
),
|
||||
)
|
||||
secret: str | None = Field(
|
||||
default=None,
|
||||
description=(
|
||||
"Shared secret between the component and the XMPP server. To be used with"
|
||||
" ``jid`` field for automatic configuration and connetion."
|
||||
),
|
||||
)
|
||||
autoconnect: bool = Field(
|
||||
default=True,
|
||||
description=(
|
||||
"Whether to automatically connect this component on backend startup. Used"
|
||||
" when ``jid`` and ``secret`` are set."
|
||||
),
|
||||
)
|
||||
|
||||
def __init_subclass__(cls, /, plugin_info: dict, **kwargs) -> None:
|
||||
super().__init_subclass__(**kwargs)
|
||||
super()._init_subclass(key="component", plugin_info=plugin_info)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def jid_and_secret_together(self) -> Self:
|
||||
"""Check that "jid" and "secret" are (un)set at the same time"""
|
||||
if (self.jid is None) ^ (self.secret is None):
|
||||
raise ValueError('You must set "jid" and "secret" together.')
|
||||
return self
|
||||
|
||||
|
||||
class FrontendSettings(DynamicSettingsBase):
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue