mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-19 12:36:06 +00:00
transmission: Simplify actions using the privileged decorator
Tests: - Get and set the storage path. - Functional tests pass. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
a68776d04b
commit
15038ae24c
@ -1,63 +0,0 @@
|
||||
#!/usr/bin/python3
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
Configuration helper for Transmission daemon.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
|
||||
from plinth import action_utils
|
||||
|
||||
TRANSMISSION_CONFIG = '/etc/transmission-daemon/settings.json'
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
"""Return parsed command line arguments as dictionary."""
|
||||
parser = argparse.ArgumentParser()
|
||||
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
|
||||
|
||||
subparsers.add_parser('get-configuration',
|
||||
help='Return the current configuration')
|
||||
subparsers.add_parser(
|
||||
'merge-configuration',
|
||||
help='Merge JSON configuration from stdin with existing')
|
||||
|
||||
subparsers.required = True
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def subcommand_get_configuration(_):
|
||||
"""Return the current configuration in JSON format."""
|
||||
configuration = open(TRANSMISSION_CONFIG, 'r').read()
|
||||
print(configuration)
|
||||
|
||||
|
||||
def subcommand_merge_configuration(arguments):
|
||||
"""Merge given JSON configuration with existing configuration."""
|
||||
configuration = sys.stdin.read()
|
||||
configuration = json.loads(configuration)
|
||||
|
||||
current_configuration = open(TRANSMISSION_CONFIG, 'r').read()
|
||||
current_configuration = json.loads(current_configuration)
|
||||
|
||||
new_configuration = current_configuration
|
||||
new_configuration.update(configuration)
|
||||
new_configuration = json.dumps(new_configuration, indent=4, sort_keys=True)
|
||||
|
||||
open(TRANSMISSION_CONFIG, 'w').write(new_configuration)
|
||||
action_utils.service_reload('transmission-daemon')
|
||||
|
||||
|
||||
def main():
|
||||
"""Parse arguments and perform all duties."""
|
||||
arguments = parse_arguments()
|
||||
|
||||
subcommand = arguments.subcommand.replace('-', '_')
|
||||
subcommand_method = globals()['subcommand_' + subcommand]
|
||||
subcommand_method(arguments)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@ -3,12 +3,9 @@
|
||||
FreedomBox app to configure Transmission server.
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from plinth import actions
|
||||
from plinth import app as app_module
|
||||
from plinth import cfg, frontpage, menu
|
||||
from plinth.daemon import Daemon
|
||||
@ -20,7 +17,7 @@ from plinth.modules.users.components import UsersAndGroups
|
||||
from plinth.package import Packages
|
||||
from plinth.utils import format_lazy
|
||||
|
||||
from . import manifest
|
||||
from . import manifest, privileged
|
||||
|
||||
_description = [
|
||||
_('Transmission is a BitTorrent client with a web interface.'),
|
||||
@ -130,8 +127,6 @@ def setup(helper, old_version=None):
|
||||
'rpc-whitelist-enabled': False,
|
||||
'rpc-authentication-required': False
|
||||
}
|
||||
helper.call('post', actions.superuser_run, 'transmission',
|
||||
['merge-configuration'],
|
||||
input=json.dumps(new_configuration).encode())
|
||||
helper.call('post', privileged.merge_configuration, new_configuration)
|
||||
add_user_to_share_group(SYSTEM_USER, TransmissionApp.DAEMON)
|
||||
helper.call('post', app.enable)
|
||||
|
||||
33
plinth/modules/transmission/privileged.py
Normal file
33
plinth/modules/transmission/privileged.py
Normal file
@ -0,0 +1,33 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
Configuration helper for Transmission daemon.
|
||||
"""
|
||||
|
||||
import json
|
||||
import pathlib
|
||||
from typing import Union
|
||||
|
||||
from plinth import action_utils
|
||||
from plinth.actions import privileged
|
||||
|
||||
_transmission_config = pathlib.Path('/etc/transmission-daemon/settings.json')
|
||||
|
||||
|
||||
@privileged
|
||||
def get_configuration() -> dict[str, str]:
|
||||
"""Return the current configuration in JSON format."""
|
||||
return json.loads(_transmission_config.read_text(encoding='utf-8'))
|
||||
|
||||
|
||||
@privileged
|
||||
def merge_configuration(configuration: dict[str, Union[str, bool]]) -> None:
|
||||
"""Merge given JSON configuration with existing configuration."""
|
||||
current_configuration = _transmission_config.read_bytes()
|
||||
current_configuration = json.loads(current_configuration)
|
||||
|
||||
new_configuration = current_configuration
|
||||
new_configuration.update(configuration)
|
||||
new_configuration = json.dumps(new_configuration, indent=4, sort_keys=True)
|
||||
|
||||
_transmission_config.write_text(new_configuration, encoding='utf-8')
|
||||
action_utils.service_reload('transmission-daemon')
|
||||
@ -3,15 +3,15 @@
|
||||
FreedomBox app for configuring Transmission Server.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import socket
|
||||
|
||||
from django.contrib import messages
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from plinth import actions, views
|
||||
from plinth import views
|
||||
|
||||
from . import privileged
|
||||
from .forms import TransmissionForm
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -25,9 +25,7 @@ class TransmissionAppView(views.AppView):
|
||||
def get_initial(self):
|
||||
"""Get the current settings from Transmission server."""
|
||||
status = super().get_initial()
|
||||
configuration = actions.superuser_run('transmission',
|
||||
['get-configuration'])
|
||||
configuration = json.loads(configuration)
|
||||
configuration = privileged.get_configuration()
|
||||
status['storage_path'] = configuration['download-dir']
|
||||
status['hostname'] = socket.gethostname()
|
||||
|
||||
@ -41,9 +39,7 @@ class TransmissionAppView(views.AppView):
|
||||
new_configuration = {
|
||||
'download-dir': new_status['storage_path'],
|
||||
}
|
||||
|
||||
actions.superuser_run('transmission', ['merge-configuration'],
|
||||
input=json.dumps(new_configuration).encode())
|
||||
privileged.merge_configuration(new_configuration)
|
||||
messages.success(self.request, _('Configuration updated'))
|
||||
|
||||
return super().form_valid(form)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user