From 20081ee5d1873fe396481c0a391a41973a863ce9 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 12 Jul 2022 17:41:50 -0700 Subject: [PATCH] roundcube: Use privileged to simplify actions Tests: - Functional tests pass - Same tests as previous patch for setting logging to syslog. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/roundcube/__init__.py | 26 ++------- .../modules/roundcube/privileged.py | 54 +++++-------------- plinth/modules/roundcube/views.py | 6 +-- 3 files changed, 20 insertions(+), 66 deletions(-) rename actions/roundcube => plinth/modules/roundcube/privileged.py (56%) mode change 100755 => 100644 diff --git a/plinth/modules/roundcube/__init__.py b/plinth/modules/roundcube/__init__.py index 57ac2d038..183ca5dea 100644 --- a/plinth/modules/roundcube/__init__.py +++ b/plinth/modules/roundcube/__init__.py @@ -3,11 +3,8 @@ FreedomBox app to configure Roundcube. """ -import json - from django.utils.translation import gettext_lazy as _ -from plinth import actions from plinth import app as app_module from plinth import frontpage, menu from plinth.modules.apache.components import Webserver @@ -15,7 +12,7 @@ from plinth.modules.backups.components import BackupRestore from plinth.modules.firewall.components import Firewall from plinth.package import Packages -from . import manifest +from . import manifest, privileged _description = [ _('Roundcube webmail is a browser-based multilingual IMAP ' @@ -94,14 +91,14 @@ class RoundcubeApp(app_module.App): def setup(helper, old_version=None): """Install and configure the module.""" - helper.call('pre', actions.superuser_run, 'roundcube', ['pre-install']) + helper.call('pre', privileged.pre_install) app.setup(old_version) - helper.call('post', actions.superuser_run, 'roundcube', ['setup']) + helper.call('post', privileged.setup) if old_version == 0: - set_config(local_only=True) + privileged.set_config(local_only=True) helper.call('post', app.enable) elif old_version <= 2: - set_config(get_config()['local_only']) + privileged.set_config(privileged.get_config()['local_only']) def force_upgrade(helper, packages): @@ -116,16 +113,3 @@ def force_upgrade(helper, packages): app.get_component('webserver-roundcube-freedombox').enable() return True - - -def get_config(): - """Return Rouncube configuration.""" - value = actions.superuser_run('roundcube', ['get-config']) - return json.loads(value) - - -def set_config(local_only): - """Set whether only local server should be allowed.""" - actions.superuser_run('roundcube', - ['set-config', '--local-only', - str(local_only)]) diff --git a/actions/roundcube b/plinth/modules/roundcube/privileged.py old mode 100755 new mode 100644 similarity index 56% rename from actions/roundcube rename to plinth/modules/roundcube/privileged.py index 8b22bffcb..bfcc99fe5 --- a/actions/roundcube +++ b/plinth/modules/roundcube/privileged.py @@ -1,37 +1,17 @@ -#!/usr/bin/python3 # SPDX-License-Identifier: AGPL-3.0-or-later -""" -Configuration helper for Roundcube server. -""" +"""Configure roundcube.""" -import argparse -import json import pathlib import re from plinth import action_utils +from plinth.actions import privileged _config_file = pathlib.Path('/etc/roundcube/freedombox-config.php') -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('pre-install', - help='Perform Roundcube pre-install configuration') - subparsers.add_parser('setup', help='Setup basic configuration') - subparsers.add_parser('get-config', help='Print current configuration') - subparser = subparsers.add_parser('set-config', help='Set configuration') - subparser.add_argument('--local-only', choices=['True', 'False'], - help='Set current configuration') - - subparsers.required = True - return parser.parse_args() - - -def subcommand_pre_install(_): +@privileged +def pre_install(): """Preseed debconf values before packages are installed.""" action_utils.debconf_set_selections([ 'roundcube-core roundcube/dbconfig-install boolean true', @@ -39,7 +19,8 @@ def subcommand_pre_install(_): ]) -def subcommand_setup(_): +@privileged +def setup(): """Add FreedomBox configuration and include from main configuration.""" if not _config_file.exists(): _config_file.write_text(' dict[str, bool]: """Print the current configuration as JSON.""" pattern = r'\s*\$config\[\s*\'([^\']*)\'\s*\]\s*=\s*\'([^\']*)\'\s*;' _config = {} @@ -65,16 +47,17 @@ def subcommand_get_config(_): pass local_only = _config.get('default_host') == 'localhost' - print(json.dumps({'local_only': local_only})) + return {'local_only': local_only} -def subcommand_set_config(arguments): +@privileged +def set_config(local_only: bool): """Set the configuration.""" config = '''