From 1027b624aa951997c38e1a742ee07d1797b5190f Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Fri, 26 Aug 2022 16:43:04 -0700 Subject: [PATCH] help: Use privileged decorator for actions Tests: - Functional tests work - Accessing help/status-log/ works and last 100 logs lines are shown. - When there are no logs, '--no entries--' message is shown. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- actions/help | 38 ------------------------------- plinth/modules/help/privileged.py | 14 ++++++++++++ plinth/modules/help/views.py | 14 ++++++------ 3 files changed, 21 insertions(+), 45 deletions(-) delete mode 100755 actions/help create mode 100644 plinth/modules/help/privileged.py diff --git a/actions/help b/actions/help deleted file mode 100755 index 1c3a58841..000000000 --- a/actions/help +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/python3 -# SPDX-License-Identifier: AGPL-3.0-or-later -""" -Actions for help module. -""" - -import argparse -import subprocess - - -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-logs', help='Get latest FreedomBox logs') - - subparsers.required = True - return parser.parse_args() - - -def subcommand_get_logs(_): - """Get latest FreedomBox logs.""" - command = ['journalctl', '--no-pager', '--lines=100', '--unit=plinth'] - subprocess.run(command, check=True) - - -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() diff --git a/plinth/modules/help/privileged.py b/plinth/modules/help/privileged.py new file mode 100644 index 000000000..a1e6447e8 --- /dev/null +++ b/plinth/modules/help/privileged.py @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +"""Actions for help module.""" + +import subprocess + +from plinth.actions import privileged + + +@privileged +def get_logs() -> str: + """Get latest FreedomBox logs.""" + command = ['journalctl', '--no-pager', '--lines=100', '--unit=plinth'] + process = subprocess.run(command, check=True, stdout=subprocess.PIPE) + return process.stdout.decode() diff --git a/plinth/modules/help/views.py b/plinth/modules/help/views.py index f18d0dd1b..a020252b7 100644 --- a/plinth/modules/help/views.py +++ b/plinth/modules/help/views.py @@ -1,7 +1,5 @@ # SPDX-License-Identifier: AGPL-3.0-or-later -""" -Help app for FreedomBox. -""" +"""Help app for FreedomBox.""" import gzip import json @@ -18,9 +16,11 @@ from django.urls import reverse from django.utils.translation import get_language_from_request from django.utils.translation import gettext as _ -from plinth import __version__, actions, cfg +from plinth import __version__, cfg from plinth.modules.upgrades import views as upgrades_views +from . import privileged + def index(request): """Serve the index page""" @@ -162,7 +162,7 @@ def download_manual(request): def status_log(request): - """Serve the last 100 lines of plinth's status log""" - output = actions.superuser_run('help', ['get-logs']) - context = {'num_lines': 100, 'data': output} + """Serve the last 100 lines of plinth's status log.""" + logs = privileged.get_logs() + context = {'num_lines': 100, 'data': logs} return TemplateResponse(request, 'statuslog.html', context)