minetest: Use privileged decorator for actions

Tests:

- Functional tests work
- Updating the configuration values works
  - Enable/disable works
  - Editing the max players works
  - Changing all of them together and one at a time

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2022-10-08 18:52:25 -04:00
1 parent f5bfd7a9db
commit 671fb7d424
4 files changed
+56 -123

No files matched your search

+1 -3
View File
@@ -1,7 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
FreedomBox app for Minetest server.
"""
"""FreedomBox app for Minetest server."""
import augeas
from django.urls import reverse_lazy
+45
View File
@@ -0,0 +1,45 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Configure Minetest server."""
from typing import Optional
import augeas
from plinth import action_utils
from plinth.actions import privileged
CONFIG_FILE = '/etc/minetest/minetest.conf'
AUG_PATH = '/files' + CONFIG_FILE + '/.anon'
@privileged
def configure(max_players: Optional[int] = None,
enable_pvp: Optional[bool] = None,
creative_mode: Optional[bool] = None,
enable_damage: Optional[bool] = None):
"""Update configuration file and restart daemon if necessary."""
aug = load_augeas()
if max_players is not None:
aug.set(AUG_PATH + '/max_users', str(max_players))
if enable_pvp is not None:
aug.set(AUG_PATH + '/enable_pvp', str(enable_pvp).lower())
if creative_mode is not None:
aug.set(AUG_PATH + '/creative_mode', str(creative_mode).lower())
if enable_damage is not None:
aug.set(AUG_PATH + '/enable_damage', str(enable_damage).lower())
aug.save()
action_utils.service_try_restart('minetest-server')
def load_augeas():
"""Initialize Augeas."""
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD)
aug.set('/augeas/load/Php/lens', 'Php.lns')
aug.set('/augeas/load/Php/incl[last() + 1]', CONFIG_FILE)
aug.load()
return aug
+10 -24
View File
@@ -1,21 +1,19 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Views for minetest module.
"""
"""Views for minetest module."""
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from plinth import actions
from plinth.modules import names
from plinth.views import AppView
from . import get_configuration
from . import get_configuration, privileged
from .forms import MinetestForm
class MinetestAppView(AppView): # pylint: disable=too-many-ancestors
"""A specialized view for configuring minetest."""
app_id = 'minetest'
template_name = 'minetest.html'
form_class = MinetestForm
@@ -37,35 +35,23 @@ class MinetestAppView(AppView): # pylint: disable=too-many-ancestors
"""Change the configurations of Minetest service."""
data = form.cleaned_data
old_config = get_configuration()
updated = False
changes = {}
if old_config['max_players'] != data['max_players'] \
and data['max_players'] is not None:
actions.superuser_run(
'minetest',
['configure', '--max_players',
str(data['max_players'])])
updated = True
changes['max_players'] = data['max_players']
if old_config['creative_mode'] != data['creative_mode']:
value = 'true' if data['creative_mode'] else 'false'
actions.superuser_run('minetest',
['configure', '--creative_mode', value])
updated = True
changes['creative_mode'] = data['creative_mode']
if old_config['enable_pvp'] != data['enable_pvp']:
value = 'true' if data['enable_pvp'] else 'false'
actions.superuser_run('minetest',
['configure', '--enable_pvp', value])
updated = True
changes['enable_pvp'] = data['enable_pvp']
if old_config['enable_damage'] != data['enable_damage']:
value = 'true' if data['enable_damage'] else 'false'
actions.superuser_run('minetest',
['configure', '--enable_damage', value])
updated = True
changes['enable_damage'] = data['enable_damage']
if updated:
if changes:
privileged.configure(**changes)
messages.success(self.request, _('Configuration updated'))
return super().form_valid(form)