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
|
|
@ -31,6 +31,7 @@ from libervia.backend.core import patches
|
|||
from libervia.backend.models.tor import TorData
|
||||
from libervia.backend.tools.config import (
|
||||
BridgeSettings,
|
||||
ComponentSettings,
|
||||
DBusBridgeSettings,
|
||||
PbBridgeSettings,
|
||||
)
|
||||
|
|
@ -296,6 +297,8 @@ class LiberviaBackend(service.Service):
|
|||
self.stopService()
|
||||
sys.exit(C.EXIT_ERROR)
|
||||
|
||||
await self._sync_components_profiles()
|
||||
|
||||
self.initialised.callback(None)
|
||||
log.info(_("Backend is ready"))
|
||||
|
||||
|
|
@ -535,6 +538,109 @@ class LiberviaBackend(service.Service):
|
|||
defers_list.append(utils.as_deferred(unload))
|
||||
return defers_list
|
||||
|
||||
async def _sync_components_profiles(self) -> None:
|
||||
"""Synchronize components profiles data with settings.
|
||||
|
||||
``jid`` and ``secret`` are used in settings as helpers to easily create
|
||||
autoconnecting components. This method syncs those settings with actual profiles.
|
||||
"""
|
||||
log.debug("Synchronizing component settings.")
|
||||
for component_field_name in G.config.component.__class__.model_fields:
|
||||
component_settings = cast(
|
||||
ComponentSettings, getattr(G.config.component, component_field_name)
|
||||
)
|
||||
|
||||
if component_settings.jid is not None:
|
||||
# Automatic setting fields are used
|
||||
assert component_settings.secret is not None
|
||||
entry_point = component_settings._import_name
|
||||
assert entry_point is not None
|
||||
profile = entry_point
|
||||
if not G.storage.has_profile(entry_point):
|
||||
log.info(f"Creating profile {profile!r}")
|
||||
await G.host.memory.create_profile(profile, "", entry_point)
|
||||
# FIXME: why do we need to start session to set parameters? To check
|
||||
# after parameters refactoring.
|
||||
await G.host.memory.start_session("", profile)
|
||||
await G.host.memory.param_set(
|
||||
"JabberID",
|
||||
str(component_settings.jid),
|
||||
"Connection",
|
||||
profile_key=profile,
|
||||
)
|
||||
await G.host.memory.param_set(
|
||||
"Password",
|
||||
component_settings.secret,
|
||||
"Connection",
|
||||
profile_key=profile,
|
||||
)
|
||||
await G.host.memory.param_set(
|
||||
"autoconnect_backend",
|
||||
component_settings.autoconnect,
|
||||
"Connection",
|
||||
profile_key=profile,
|
||||
)
|
||||
log.info(f"Component profile {profile!r} set for auto-connection.")
|
||||
else:
|
||||
# Profile exists, check if values correspond to settings
|
||||
try:
|
||||
# FIXME: As for new profile condition above, we need to check if
|
||||
# we can skip session starting to set profile parameters.
|
||||
await G.host.memory.start_session("", profile)
|
||||
except exceptions.PasswordError:
|
||||
# Component passwords should be empty to allow for autoconnection.
|
||||
log.warning(
|
||||
f"Can't set automatically component profile for {profile}: "
|
||||
"the password is not empty as expected."
|
||||
)
|
||||
continue
|
||||
current_jid = await G.host.memory.param_get_a_async(
|
||||
"JabberID", "Connection", profile_key=profile
|
||||
)
|
||||
if current_jid != str(component_settings.jid):
|
||||
log.warning(
|
||||
f"Component profile {profile!r} JID mismatch: "
|
||||
f"current={current_jid}, settings={component_settings.jid}, "
|
||||
f"updating from settings"
|
||||
)
|
||||
await G.host.memory.param_set(
|
||||
"JabberID",
|
||||
str(component_settings.jid),
|
||||
"Connection",
|
||||
profile_key=profile,
|
||||
)
|
||||
|
||||
current_password = await G.host.memory.param_get_a_async(
|
||||
"Password", "Connection", profile_key=profile
|
||||
)
|
||||
if current_password != component_settings.secret:
|
||||
log.warning(
|
||||
f"Component profile {profile!r} secret mismatch, "
|
||||
f"updating from settings"
|
||||
)
|
||||
await G.host.memory.param_set(
|
||||
"Password",
|
||||
component_settings.secret,
|
||||
"Connection",
|
||||
profile_key=profile,
|
||||
)
|
||||
|
||||
current_autoconnect = await G.host.memory.param_get_a_async(
|
||||
"autoconnect_backend", "Connection", profile_key=profile
|
||||
)
|
||||
expected_autoconnect = component_settings.autoconnect
|
||||
if current_autoconnect != expected_autoconnect:
|
||||
log.warning(
|
||||
f"Component profile {profile!r} autoconnect setting"
|
||||
f" mismatch, updating from settings"
|
||||
)
|
||||
await G.host.memory.param_set(
|
||||
"autoconnect_backend",
|
||||
expected_autoconnect,
|
||||
"Connection",
|
||||
profile_key=profile,
|
||||
)
|
||||
|
||||
def _connect(self, profile_key, password="", options=None):
|
||||
profile = self.memory.get_profile_name(profile_key)
|
||||
return defer.ensureDeferred(self.connect(profile, password, options))
|
||||
|
|
|
|||
Loading…
Reference in a new issue