diff --git a/actions/minetest b/actions/minetest new file mode 100755 index 000000000..67c8f2106 --- /dev/null +++ b/actions/minetest @@ -0,0 +1,59 @@ +#!/usr/bin/python3 +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +""" +Configuration helper for Minetest server. +""" + +import argparse + +from plinth import action_utils + + +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('enable', help='Enable Minetest server') + subparsers.add_parser('disable', help='Disable Minetest server') + + return parser.parse_args() + + +def subcommand_enable(_): + """Enable and start server.""" + action_utils.service_enable('minetest-server') + + +def subcommand_disable(_): + """Disable and stop server.""" + action_utils.service_disable('minetest-server') + + +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/data/etc/plinth/modules-enabled/minetest b/data/etc/plinth/modules-enabled/minetest new file mode 100644 index 000000000..3f170f34f --- /dev/null +++ b/data/etc/plinth/modules-enabled/minetest @@ -0,0 +1 @@ +plinth.modules.minetest diff --git a/data/usr/lib/firewalld/services/minetest-plinth.xml b/data/usr/lib/firewalld/services/minetest-plinth.xml new file mode 100644 index 000000000..89cbfaae5 --- /dev/null +++ b/data/usr/lib/firewalld/services/minetest-plinth.xml @@ -0,0 +1,6 @@ + + + Minetest Server + Minetest is a multiplayer infinite-world block sandbox. + + diff --git a/plinth/modules/minetest/__init__.py b/plinth/modules/minetest/__init__.py new file mode 100644 index 000000000..202581b53 --- /dev/null +++ b/plinth/modules/minetest/__init__.py @@ -0,0 +1,61 @@ +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +""" +Plinth module for minetest. +""" + +from django.utils.translation import ugettext_lazy as _ + +from plinth import action_utils +from plinth import cfg +from plinth import service as service_module + +depends = ['plinth.modules.apps'] + +service = None + + +def init(): + """Initialize the minetest module.""" + menu = cfg.main_menu.get('apps:index') + menu.add_urlname(_('Block Sandbox (Minetest)'), 'glyphicon-th-large', + 'minetest:index', 325) + + global service + service = service_module.Service( + 'minetest-plinth', _('Minetest Server'), + is_external=True, enabled=is_enabled()) + + +def is_enabled(): + """Return whether the service is enabled.""" + return action_utils.service_is_enabled('minetest-server') + + +def is_running(): + """Return whether the service is running.""" + return action_utils.service_is_running('minetest-server') + + +def diagnose(): + """Run diagnostics and return the results.""" + results = [] + + results.append(action_utils.diagnose_port_listening(30000, 'udp4')) + + return results diff --git a/plinth/modules/minetest/forms.py b/plinth/modules/minetest/forms.py new file mode 100644 index 000000000..87e1fbaa8 --- /dev/null +++ b/plinth/modules/minetest/forms.py @@ -0,0 +1,30 @@ +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +""" +Forms for the minetest module. +""" + +from django import forms +from django.utils.translation import ugettext_lazy as _ + + +class MinetestForm(forms.Form): + """Minetest configuration form.""" + enabled = forms.BooleanField( + label=_('Enable Minetest server'), + required=False) diff --git a/plinth/modules/minetest/templates/minetest.html b/plinth/modules/minetest/templates/minetest.html new file mode 100644 index 000000000..d4669149c --- /dev/null +++ b/plinth/modules/minetest/templates/minetest.html @@ -0,0 +1,64 @@ +{% extends "base.html" %} +{% comment %} +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +{% endcomment %} + +{% load bootstrap %} +{% load i18n %} + +{% block content %} + +

{% trans "Block Sandbox (Minetest)" %}

+ +

+ {% blocktrans trimmed %} + Minetest is a multiplayer infinite-world block sandbox. This module + enables the Minetest server to be run on this {{ box_name }}, on the + default port (30000). To connect to the server, a + Minetest client + is needed. + {% endblocktrans %} +

+ +

{% trans "Status" %}

+ +

+ {% if status.is_running %} + + {% trans "Minetest server is running" %} + {% else %} + + {% trans "Minetest server is not running" %} + {% endif %} +

+ + {% include "diagnostics_button.html" with module="minetest" %} + + +

{% trans "Configuration" %}

+ +
+ {% csrf_token %} + + {{ form|bootstrap }} + + +
+ +{% endblock %} diff --git a/plinth/modules/minetest/tests/__init__.py b/plinth/modules/minetest/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plinth/modules/minetest/urls.py b/plinth/modules/minetest/urls.py new file mode 100644 index 000000000..a297c4c6b --- /dev/null +++ b/plinth/modules/minetest/urls.py @@ -0,0 +1,29 @@ +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +""" +URLs for the minetest module. +""" + +from django.conf.urls import url + +from . import views + + +urlpatterns = [ + url(r'^apps/minetest/$', views.index, name='index'), +] diff --git a/plinth/modules/minetest/views.py b/plinth/modules/minetest/views.py new file mode 100644 index 000000000..27ae00da7 --- /dev/null +++ b/plinth/modules/minetest/views.py @@ -0,0 +1,78 @@ +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +""" +Views for the minetest module. +""" + +from django.contrib import messages +from django.template.response import TemplateResponse +from django.utils.translation import ugettext as _ + +from .forms import MinetestForm +from plinth import actions +from plinth import package +from plinth.modules import minetest + + +def on_install(): + """Notify that the service is now enabled.""" + minetest.service.notify_enabled(None, True) + + +@package.required(['minetest-server'], on_install=on_install) +def index(request): + """Serve configuration page.""" + status = get_status() + + form = None + + if request.method == 'POST': + form = MinetestForm(request.POST, prefix='minetest') + if form.is_valid(): + _apply_changes(request, status, form.cleaned_data) + status = get_status() + form = MinetestForm(initial=status, prefix='minetest') + else: + form = MinetestForm(initial=status, prefix='minetest') + + return TemplateResponse(request, 'minetest.html', + {'title': _('Block Sandbox (Minetest)'), + 'status': status, + 'form': form}) + + +def get_status(): + """Get the current service status.""" + return {'enabled': minetest.is_enabled(), + 'is_running': minetest.is_running()} + + +def _apply_changes(request, old_status, new_status): + """Apply the changes.""" + modified = False + + if old_status['enabled'] != new_status['enabled']: + sub_command = 'enable' if new_status['enabled'] else 'disable' + actions.superuser_run('minetest', [sub_command]) + minetest.service.notify_enabled(None, new_status['enabled']) + modified = True + + if modified: + messages.success(request, _('Configuration updated')) + else: + messages.info(request, _('Setting unchanged'))