mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
config: Add new component for managing drop-in /etc/ config files
- Use two separate strategies: one to symlink from /etc to files in /usr/ and two to copy files from /usr to /etc. The latter is needed for some rare cases where symlinks don't work because the daemon runs in a chrooted environment. - Update tutorial and reference sections in developer documentation. Tests: - Unit tests works and are comprehensive. - Using the component in apps works. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
9cc5ecc909
commit
598e1e3b4a
9 files changed
+470
-12
No files matched your search
@@ -1,6 +1,7 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Package holding all the privileged actions outside of apps."""
|
||||
|
||||
from .config import dropin_link, dropin_unlink
|
||||
from .packages import (filter_conffile_packages, install,
|
||||
is_package_manager_busy, remove, update)
|
||||
from .service import (disable, enable, is_enabled, is_running, mask, reload,
|
||||
@@ -9,5 +10,6 @@ from .service import (disable, enable, is_enabled, is_running, mask, reload,
|
||||
__all__ = [
|
||||
'filter_conffile_packages', 'install', 'is_package_manager_busy', 'remove',
|
||||
'update', 'disable', 'enable', 'is_enabled', 'is_running', 'mask',
|
||||
'reload', 'restart', 'start', 'stop', 'try_restart', 'unmask'
|
||||
'reload', 'restart', 'start', 'stop', 'try_restart', 'unmask',
|
||||
'dropin_link', 'dropin_unlink'
|
||||
]
|
||||
@@ -0,0 +1,53 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Symlink and unlink configuration files into /etc."""
|
||||
|
||||
import importlib
|
||||
import inspect
|
||||
import shutil
|
||||
|
||||
from plinth import app as app_module
|
||||
from plinth import module_loader
|
||||
from plinth.actions import privileged
|
||||
|
||||
|
||||
def _assert_managed_dropin_config(app_id: str, path: str):
|
||||
"""Check that this is a path managed by the specified app."""
|
||||
module_path = module_loader.get_module_import_path(app_id)
|
||||
module = importlib.import_module(module_path)
|
||||
module_classes = inspect.getmembers(module, inspect.isclass)
|
||||
app_classes = [
|
||||
cls[1] for cls in module_classes if issubclass(cls[1], app_module.App)
|
||||
]
|
||||
|
||||
for cls in app_classes:
|
||||
app = cls()
|
||||
from plinth.config import DropinConfigs
|
||||
components = app.get_components_of_type(DropinConfigs)
|
||||
for component in components:
|
||||
if path in component.etc_paths:
|
||||
return
|
||||
|
||||
raise AssertionError('Not a managed drop-in config')
|
||||
|
||||
|
||||
@privileged
|
||||
def dropin_link(app_id: str, path: str, copy_only: bool):
|
||||
"""Create a symlink from /etc/ to /usr/share/freedombox/etc."""
|
||||
_assert_managed_dropin_config(app_id, path)
|
||||
from plinth.config import DropinConfigs
|
||||
target = DropinConfigs._get_target_path(path)
|
||||
etc_path = DropinConfigs._get_etc_path(path)
|
||||
etc_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
if copy_only:
|
||||
shutil.copyfile(target, etc_path)
|
||||
else:
|
||||
etc_path.symlink_to(target)
|
||||
|
||||
|
||||
@privileged
|
||||
def dropin_unlink(app_id: str, path: str, missing_ok: bool = False):
|
||||
"""Remove a symlink in /etc/."""
|
||||
_assert_managed_dropin_config(app_id, path)
|
||||
from plinth.config import DropinConfigs
|
||||
etc_path = DropinConfigs._get_etc_path(path)
|
||||
etc_path.unlink(missing_ok=missing_ok)
|
||||
Reference in new issue
Block a user