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 <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-08-26 16:43:04 -07:00 committed by James Valleroy
parent b91f1cf922
commit 1027b624aa
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 21 additions and 45 deletions

View File

@ -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()

View File

@ -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()

View File

@ -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)