Sunil Mohan Adapa b8b2c85eea
deluge: Cleanup and simplify setup code
- Use various action utilities to simplify daemon management.

- Instead of writing systemd service file to /etc ship an override file in
/usr/lib/systemd/system/*.d/. deluged and deluge-web both have systemd unit file
shipped. They are very close to what we want from them. Drop the old service file.

- Don't write /etc/default/deluged, it is not used in the systemd unit file.

- App's setup version has not been incremented as it has already been
incremented for the next release.

Tests:

- Fresh install of the app works in testing container. Functional tests work as
expected.

- /var/lib/deluged/config/web.conf is created when deluge-web is started
confirming that the correct configuration path is being used. Also app works on
/deluge web path.

- Old systemd unit file is removed when the setup is run when it is present to
due to deluge installation attempt previously.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
2024-12-13 13:48:51 +02:00

92 lines
2.9 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""Configuration helper for BitTorrent web client."""
import pathlib
import shutil
import time
from plinth import action_utils
from plinth.actions import privileged
from plinth.modules.deluge.utils import Config
DELUGE_CONF_DIR = pathlib.Path('/var/lib/deluged/config/')
def _set_configuration(filename, parameter, value):
"""Set the configuration parameter."""
with action_utils.service_ensure_stopped('deluge-web'):
with action_utils.service_ensure_stopped('deluged'):
with Config(DELUGE_CONF_DIR / filename) as config:
config.content[parameter] = value
def _get_host_id():
"""Get default host id."""
try:
with Config(DELUGE_CONF_DIR / 'hostlist.conf') as config:
return config.content['hosts'][0][0]
except FileNotFoundError:
with Config(DELUGE_CONF_DIR / 'hostlist.conf.1.2') as config:
return config.content['hosts'][0][0]
@privileged
def get_configuration() -> dict[str, str]:
"""Return the current deluged configuration."""
with Config(DELUGE_CONF_DIR / 'core.conf') as config:
download_location = config.content['download_location']
return {'download_location': download_location}
@privileged
def set_configuration(download_location: str):
"""Set the deluged configuration."""
_set_configuration('core.conf', 'download_location', download_location)
@privileged
def setup():
"""Perform initial setup for deluge."""
old_service_path = pathlib.Path('/etc/systemd/system/deluge-web.service')
if old_service_path.exists():
old_service_path.unlink(missing_ok=True)
action_utils.service_daemon_reload()
action_utils.service_try_restart('deluged')
action_utils.service_try_restart('deluge-web')
# Wait until core configuration is available so that status of the app can
# be shown properly.
_wait_for_configuration()
# Configure deluge-web to autoconnect to the default deluged daemon.
host_id = _get_host_id()
_set_configuration('web.conf', 'default_daemon', host_id)
def _wait_for_configuration():
"""Wait until configuration file has been created."""
core_file = DELUGE_CONF_DIR / 'core.conf'
web_file = DELUGE_CONF_DIR / 'web.conf'
if core_file.exists() and web_file.exists():
return
# deluge-web and deluged create files on first run.
with action_utils.service_ensure_running('deluged'):
with action_utils.service_ensure_running('deluge-web'):
for _ in range(60):
if core_file.exists() and web_file.exists():
break
time.sleep(1)
else:
raise Exception('Unable to setup configuration')
@privileged
def uninstall():
"""Remove configuration and service files."""
# /etc/default/deluged is removed on purge
shutil.rmtree(DELUGE_CONF_DIR, ignore_errors=True)