mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-06-03 10:50:20 +00:00
power: Use privileged decorator for actions
Tests: - Reboot works, the process works in the background showing apps page - Shutdown works, the process works in the background showing apps page Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
e8ea6fff17
commit
7870d43c33
@ -1,43 +0,0 @@
|
||||
#!/usr/bin/python3
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
Configuration helper for power controls.
|
||||
"""
|
||||
|
||||
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('restart', help='Restart the system')
|
||||
subparsers.add_parser('shutdown', help='Shut down the system')
|
||||
|
||||
subparsers.required = True
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def subcommand_restart(_):
|
||||
"""Restart the system."""
|
||||
subprocess.call('reboot')
|
||||
|
||||
|
||||
def subcommand_shutdown(_):
|
||||
"""Shut down the system."""
|
||||
subprocess.call(['shutdown', 'now'])
|
||||
|
||||
|
||||
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()
|
||||
18
plinth/modules/power/privileged.py
Normal file
18
plinth/modules/power/privileged.py
Normal file
@ -0,0 +1,18 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Shutdown/restart the system."""
|
||||
|
||||
import subprocess
|
||||
|
||||
from plinth.actions import privileged
|
||||
|
||||
|
||||
@privileged
|
||||
def restart():
|
||||
"""Restart the system."""
|
||||
subprocess.call('reboot')
|
||||
|
||||
|
||||
@privileged
|
||||
def shutdown():
|
||||
"""Shut down the system."""
|
||||
subprocess.call(['shutdown', 'now'])
|
||||
@ -1,18 +1,17 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
FreedomBox app for power module.
|
||||
"""
|
||||
"""FreedomBox app for power controls."""
|
||||
|
||||
from django.forms import Form
|
||||
from django.shortcuts import redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.urls import reverse
|
||||
|
||||
from plinth import actions
|
||||
from plinth import app as app_module
|
||||
from plinth import package
|
||||
from plinth.views import AppView
|
||||
|
||||
from . import privileged
|
||||
|
||||
|
||||
class PowerAppView(AppView):
|
||||
"""Show power app main page."""
|
||||
@ -32,7 +31,7 @@ def restart(request):
|
||||
form = None
|
||||
|
||||
if request.method == 'POST':
|
||||
actions.superuser_run('power', ['restart'], run_in_background=True)
|
||||
privileged.restart(_run_in_background=True)
|
||||
return redirect(reverse('apps'))
|
||||
|
||||
app = app_module.App.get('power')
|
||||
@ -51,7 +50,7 @@ def shutdown(request):
|
||||
form = None
|
||||
|
||||
if request.method == 'POST':
|
||||
actions.superuser_run('power', ['shutdown'], run_in_background=True)
|
||||
privileged.shutdown(_run_in_background=True)
|
||||
return redirect(reverse('apps'))
|
||||
|
||||
app = app_module.App.get('power')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user