From f1276d736ab9539108ddf5d95edd57b0ed525057 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 16 Apr 2024 16:00:16 -0700 Subject: [PATCH] nextcloud: Implement enable/disable container Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/action_utils.py | 42 ++++++++++++++++++++++++++ plinth/modules/nextcloud/__init__.py | 20 +++++++++++- plinth/modules/nextcloud/privileged.py | 18 +++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/plinth/action_utils.py b/plinth/action_utils.py index b073a7591..9afbd6cd3 100644 --- a/plinth/action_utils.py +++ b/plinth/action_utils.py @@ -11,6 +11,8 @@ import subprocess import tempfile from contextlib import contextmanager +import augeas + logger = logging.getLogger(__name__) UWSGI_ENABLED_PATH = '/etc/uwsgi/apps-enabled/{config_name}.ini' @@ -553,6 +555,46 @@ WantedBy=default.target service_daemon_reload() +def _podman_augeus(container_name: str): + """Return an augues instance to edit container configuration file.""" + aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + + augeas.Augeas.NO_MODL_AUTOLOAD) + container = f'/etc/containers/systemd/{container_name}.container' + aug.transform('Systemd', container) + aug.set('/augeas/context', '/files' + container) + aug.load() + return aug + + +def podman_is_enabled(container_name: str) -> bool: + """Return whether the container to start on boot.""" + aug = _podman_augeus(container_name) + aug = _podman_augeus(container_name) + value = 'default.target' + key = 'Install/WantedBy' + return any( + (aug.get(match_ + '/value') == value for match_ in aug.match(key))) + + +def podman_enable(container_name: str): + """Enable container to start on boot.""" + aug = _podman_augeus(container_name) + value = 'default.target' + key = 'Install/WantedBy' + found = any( + (aug.get(match_ + '/value') == value for match_ in aug.match(key))) + if not found: + aug.set(f'{key}[last() +1]/value', value) + aug.save() + + +def podman_disable(container_name: str): + """Disable container to start on boot.""" + aug = _podman_augeus(container_name) + aug.remove('Install/WantedBy') + aug.save() + + def podman_uninstall(container_name: str, volume_name: str, image_name: str, volume_path: str): """Remove a podman container's components and systemd unit.""" diff --git a/plinth/modules/nextcloud/__init__.py b/plinth/modules/nextcloud/__init__.py index 9ae5dc3f7..6ccb63f0e 100644 --- a/plinth/modules/nextcloud/__init__.py +++ b/plinth/modules/nextcloud/__init__.py @@ -105,7 +105,7 @@ class NextcloudApp(app_module.App): daemon = SharedDaemon('shared-daemon-nextcloud-mysql', 'mysql') self.add(daemon) - daemon = Daemon('daemon-nextcloud', 'nextcloud-freedombox') + daemon = NextcloudDaemon('daemon-nextcloud', 'nextcloud-freedombox') self.add(daemon) daemon = Daemon('daemon-nextcloud-timer', @@ -145,6 +145,24 @@ class NextcloudApp(app_module.App): return results +class NextcloudDaemon(Daemon): + """Component to manage Nextcloud container service.""" + + def is_enabled(self): + """Return if the daemon/unit is enabled.""" + return privileged.is_enabled() + + def enable(self): + """Run operations to enable the daemon/unit.""" + super().enable() + privileged.enable() + + def disable(self): + """Run operations to disable the daemon/unit.""" + super().disable() + privileged.disable() + + class NextcloudBackupRestore(BackupRestore): """Component to backup/restore Nextcloud.""" diff --git a/plinth/modules/nextcloud/privileged.py b/plinth/modules/nextcloud/privileged.py index d9c1a0b47..679ab212f 100644 --- a/plinth/modules/nextcloud/privileged.py +++ b/plinth/modules/nextcloud/privileged.py @@ -86,6 +86,24 @@ def _run_occ(*args, **kwargs) -> subprocess.CompletedProcess: return _run_in_container('/var/www/html/occ', *args, **kwargs) +@privileged +def is_enabled() -> bool: + """Return if the systemd container service is enabled.""" + return action_utils.podman_is_enabled(CONTAINER_NAME) + + +@privileged +def enable(): + """Enable the systemd container service.""" + action_utils.podman_enable(CONTAINER_NAME) + + +@privileged +def disable(): + """Disable the systemd container service.""" + action_utils.podman_disable(CONTAINER_NAME) + + @privileged def get_domain(): """Return domain name set in Nextcloud."""