From 317e83c38fcc2009a5156da55c49a1cf339c7d6a Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Fri, 26 Aug 2022 14:34:46 -0700 Subject: [PATCH] syncthing: Use privileged decorator for actions Tests: - Functional tests succeed (noticed intermittent failure) - Initial setup succeeds - User/group are created. /var/lib/syncthing is created with proper user/group ownership. - In configuration file, authentication notification is disabled - Syncthing web interface is accessible - Authentication related notification is not shown. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/syncthing/__init__.py | 11 ++--- .../modules/syncthing/privileged.py | 41 ++++--------------- 2 files changed, 11 insertions(+), 41 deletions(-) rename actions/syncthing => plinth/modules/syncthing/privileged.py (70%) mode change 100755 => 100644 diff --git a/plinth/modules/syncthing/__init__.py b/plinth/modules/syncthing/__init__.py index 2f0a9eb88..770883447 100644 --- a/plinth/modules/syncthing/__init__.py +++ b/plinth/modules/syncthing/__init__.py @@ -1,11 +1,8 @@ # SPDX-License-Identifier: AGPL-3.0-or-later -""" -FreedomBox app to configure Syncthing. -""" +"""FreedomBox app to configure Syncthing.""" 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 @@ -17,7 +14,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 = [ _('Syncthing is an application to synchronize files across multiple ' @@ -106,13 +103,13 @@ class SyncthingApp(app_module.App): def setup(self, old_version): """Install and configure the app.""" super().setup(old_version) - actions.superuser_run('syncthing', ['setup']) + privileged.setup() add_user_to_share_group(SYSTEM_USER, SyncthingApp.DAEMON) if not old_version: self.enable() - actions.superuser_run('syncthing', ['setup-config']) + privileged.setup_config() if old_version == 1 and self.is_enabled(): self.get_component('firewall-syncthing-ports').enable() diff --git a/actions/syncthing b/plinth/modules/syncthing/privileged.py old mode 100755 new mode 100644 similarity index 70% rename from actions/syncthing rename to plinth/modules/syncthing/privileged.py index 200dbb117..efa8c40f7 --- a/actions/syncthing +++ b/plinth/modules/syncthing/privileged.py @@ -1,10 +1,6 @@ -#!/usr/bin/python3 # SPDX-License-Identifier: AGPL-3.0-or-later -""" -Configuration helper for Syncthing. -""" +"""Configure Syncthing.""" -import argparse import grp import os import pwd @@ -15,24 +11,12 @@ import time import augeas from plinth import action_utils +from plinth.actions import privileged DATA_DIR = '/var/lib/syncthing' CONF_FILE = DATA_DIR + '/.config/syncthing/config.xml' -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('setup', help='Setup Syncthing') - - subparsers.add_parser('setup-config', help='Setup Syncthing configuration') - - subparsers.required = True - return parser.parse_args() - - def augeas_load(): """Initialize Augeas.""" aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + @@ -42,8 +26,9 @@ def augeas_load(): return aug -def subcommand_setup(_): - """Actions to be performed before installing Syncthing""" +@privileged +def setup(): + """Perform post-install actions for Syncthing.""" # Create syncthing group if needed. try: grp.getgrnam('syncthing') @@ -65,7 +50,8 @@ def subcommand_setup(_): shutil.chown(DATA_DIR, user='syncthing', group='syncthing') -def subcommand_setup_config(_): +@privileged +def setup_config(): """Make configuration changes.""" # wait until the configuration file is created by the syncthing daemon timeout = 300 @@ -94,16 +80,3 @@ def subcommand_setup_config(_): if conf_changed: action_utils.service_try_restart('syncthing@syncthing') - - -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()