mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-26 12:46:08 +00:00
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>
This commit is contained in:
parent
f501bc99ab
commit
b8b2c85eea
@ -0,0 +1,16 @@
|
|||||||
|
[Service]
|
||||||
|
ExecStart=
|
||||||
|
ExecStart=/usr/bin/deluge-web --config /var/lib/deluged/config/ --base=deluge --do-not-daemonize
|
||||||
|
LockPersonality=yes
|
||||||
|
NoNewPrivileges=yes
|
||||||
|
PrivateDevices=yes
|
||||||
|
PrivateTmp=yes
|
||||||
|
ProtectControlGroups=yes
|
||||||
|
ProtectKernelLogs=yes
|
||||||
|
ProtectKernelModules=yes
|
||||||
|
ProtectKernelTunables=yes
|
||||||
|
ProtectSystem=yes
|
||||||
|
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
|
||||||
|
RestrictRealtime=yes
|
||||||
|
StateDirectory=deluged
|
||||||
|
SystemCallArchitectures=native
|
||||||
@ -3,70 +3,21 @@
|
|||||||
|
|
||||||
import pathlib
|
import pathlib
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import augeas
|
|
||||||
|
|
||||||
from plinth import action_utils
|
from plinth import action_utils
|
||||||
from plinth.actions import privileged
|
from plinth.actions import privileged
|
||||||
from plinth.modules.deluge.utils import Config
|
from plinth.modules.deluge.utils import Config
|
||||||
|
|
||||||
DELUGED_DEFAULT_FILE = '/etc/default/deluged'
|
|
||||||
DELUGE_CONF_DIR = pathlib.Path('/var/lib/deluged/config/')
|
DELUGE_CONF_DIR = pathlib.Path('/var/lib/deluged/config/')
|
||||||
|
|
||||||
DELUGE_WEB_SYSTEMD_SERVICE_PATH = '/etc/systemd/system/deluge-web.service'
|
|
||||||
DELUGE_WEB_SYSTEMD_SERVICE = f'''
|
|
||||||
#
|
|
||||||
# This file is managed and overwritten by Plinth. If you wish to edit
|
|
||||||
# it, disable Deluge in Plinth, remove this file and manage it manually.
|
|
||||||
#
|
|
||||||
[Unit]
|
|
||||||
Description=Deluge Web Interface
|
|
||||||
Documentation=man:deluge-web(1)
|
|
||||||
After=network.target
|
|
||||||
After=deluged.service
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
ExecStart=/usr/bin/deluge-web --config {DELUGE_CONF_DIR} --base=deluge --do-not-daemonize
|
|
||||||
Restart=on-failure
|
|
||||||
User=debian-deluged
|
|
||||||
Group=debian-deluged
|
|
||||||
LockPersonality=yes
|
|
||||||
NoNewPrivileges=yes
|
|
||||||
PrivateDevices=yes
|
|
||||||
PrivateTmp=yes
|
|
||||||
ProtectControlGroups=yes
|
|
||||||
ProtectKernelLogs=yes
|
|
||||||
ProtectKernelModules=yes
|
|
||||||
ProtectKernelTunables=yes
|
|
||||||
ProtectSystem=yes
|
|
||||||
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
|
|
||||||
RestrictRealtime=yes
|
|
||||||
StateDirectory=deluged
|
|
||||||
SystemCallArchitectures=native
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
||||||
''' # noqa: E501
|
|
||||||
|
|
||||||
|
|
||||||
def _set_configuration(filename, parameter, value):
|
def _set_configuration(filename, parameter, value):
|
||||||
"""Set the configuration parameter."""
|
"""Set the configuration parameter."""
|
||||||
deluged_is_running = action_utils.service_is_running('deluged')
|
with action_utils.service_ensure_stopped('deluge-web'):
|
||||||
if deluged_is_running:
|
with action_utils.service_ensure_stopped('deluged'):
|
||||||
action_utils.service_stop('deluged')
|
with Config(DELUGE_CONF_DIR / filename) as config:
|
||||||
deluge_web_is_running = action_utils.service_is_running('deluge-web')
|
config.content[parameter] = value
|
||||||
if deluge_web_is_running:
|
|
||||||
action_utils.service_stop('deluge-web')
|
|
||||||
|
|
||||||
with Config(DELUGE_CONF_DIR / filename) as config:
|
|
||||||
config.content[parameter] = value
|
|
||||||
|
|
||||||
if deluged_is_running:
|
|
||||||
action_utils.service_start('deluged')
|
|
||||||
if deluge_web_is_running:
|
|
||||||
action_utils.service_start('deluge-web')
|
|
||||||
|
|
||||||
|
|
||||||
def _get_host_id():
|
def _get_host_id():
|
||||||
@ -79,20 +30,6 @@ def _get_host_id():
|
|||||||
return config.content['hosts'][0][0]
|
return config.content['hosts'][0][0]
|
||||||
|
|
||||||
|
|
||||||
def _set_deluged_daemon_options():
|
|
||||||
"""Set deluged daemon options."""
|
|
||||||
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
|
|
||||||
augeas.Augeas.NO_MODL_AUTOLOAD)
|
|
||||||
aug.set('/augeas/load/Shellvars/lens', 'Shellvars.lns')
|
|
||||||
aug.set('/augeas/load/Shellvars/incl[last() + 1]', DELUGED_DEFAULT_FILE)
|
|
||||||
aug.load()
|
|
||||||
aug.set('/files' + DELUGED_DEFAULT_FILE + '/ENABLE_DELUGED', '1')
|
|
||||||
# overwrite daemon args, use default config directory (same as deluge-web)
|
|
||||||
aug.set('/files' + DELUGED_DEFAULT_FILE + '/DAEMON_ARGS',
|
|
||||||
'"-d -l /var/log/deluged/daemon.log -L info"')
|
|
||||||
aug.save()
|
|
||||||
|
|
||||||
|
|
||||||
@privileged
|
@privileged
|
||||||
def get_configuration() -> dict[str, str]:
|
def get_configuration() -> dict[str, str]:
|
||||||
"""Return the current deluged configuration."""
|
"""Return the current deluged configuration."""
|
||||||
@ -111,56 +48,44 @@ def set_configuration(download_location: str):
|
|||||||
@privileged
|
@privileged
|
||||||
def setup():
|
def setup():
|
||||||
"""Perform initial setup for deluge."""
|
"""Perform initial setup for deluge."""
|
||||||
with open(DELUGE_WEB_SYSTEMD_SERVICE_PATH, 'w',
|
old_service_path = pathlib.Path('/etc/systemd/system/deluge-web.service')
|
||||||
encoding='utf-8') as file_handle:
|
if old_service_path.exists():
|
||||||
file_handle.write(DELUGE_WEB_SYSTEMD_SERVICE)
|
old_service_path.unlink(missing_ok=True)
|
||||||
|
|
||||||
_set_deluged_daemon_options()
|
action_utils.service_daemon_reload()
|
||||||
|
action_utils.service_try_restart('deluged')
|
||||||
subprocess.check_call(['systemctl', 'daemon-reload'])
|
|
||||||
|
|
||||||
# Restarting an old deluge-web service stops also possible deluged process
|
|
||||||
# that was started from the web interface.
|
|
||||||
action_utils.service_try_restart('deluge-web')
|
action_utils.service_try_restart('deluge-web')
|
||||||
|
|
||||||
# Wait until core configuration is available so that status of the app can
|
# Wait until core configuration is available so that status of the app can
|
||||||
# be shown properly.
|
# be shown properly.
|
||||||
_wait_for_configuration('deluged', 'core.conf')
|
_wait_for_configuration()
|
||||||
|
|
||||||
# Configure deluge-web to autoconnect to the default deluged daemon.
|
# Configure deluge-web to autoconnect to the default deluged daemon.
|
||||||
_wait_for_configuration('deluge-web', 'web.conf')
|
|
||||||
host_id = _get_host_id()
|
host_id = _get_host_id()
|
||||||
_set_configuration('web.conf', 'default_daemon', host_id)
|
_set_configuration('web.conf', 'default_daemon', host_id)
|
||||||
|
|
||||||
|
|
||||||
def _wait_for_configuration(service, file_name):
|
def _wait_for_configuration():
|
||||||
"""Wait until configuration file has been created."""
|
"""Wait until configuration file has been created."""
|
||||||
conf_file = DELUGE_CONF_DIR / file_name
|
core_file = DELUGE_CONF_DIR / 'core.conf'
|
||||||
if conf_file.exists():
|
web_file = DELUGE_CONF_DIR / 'web.conf'
|
||||||
|
if core_file.exists() and web_file.exists():
|
||||||
return
|
return
|
||||||
|
|
||||||
# deluge-web creates files on first run. deluged on the other than differs
|
# deluge-web and deluged create files on first run.
|
||||||
# in version. Older version in Debian Buster creates the files after a
|
with action_utils.service_ensure_running('deluged'):
|
||||||
# restart while newer versions create the files on first run. The following
|
with action_utils.service_ensure_running('deluge-web'):
|
||||||
# approach is slightly better for create-on-exit case.
|
for _ in range(60):
|
||||||
is_running = action_utils.service_is_running(service)
|
if core_file.exists() and web_file.exists():
|
||||||
for interval in range(7):
|
break
|
||||||
action_utils.service_restart(service)
|
|
||||||
if conf_file.exists():
|
|
||||||
break
|
|
||||||
|
|
||||||
print('Waiting for {service} configuration')
|
time.sleep(1)
|
||||||
time.sleep(2**interval) # Exponentially increase the time waited
|
else:
|
||||||
else:
|
raise Exception('Unable to setup configuration')
|
||||||
raise Exception(f'Unable to setup {service}.')
|
|
||||||
|
|
||||||
if not is_running:
|
|
||||||
action_utils.service_stop(service)
|
|
||||||
|
|
||||||
|
|
||||||
@privileged
|
@privileged
|
||||||
def uninstall():
|
def uninstall():
|
||||||
"""Remove configuration and service files."""
|
"""Remove configuration and service files."""
|
||||||
# /etc/default/deluged is removed on purge
|
# /etc/default/deluged is removed on purge
|
||||||
pathlib.Path(DELUGE_WEB_SYSTEMD_SERVICE_PATH).unlink(missing_ok=True)
|
|
||||||
shutil.rmtree(DELUGE_CONF_DIR, ignore_errors=True)
|
shutil.rmtree(DELUGE_CONF_DIR, ignore_errors=True)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user