Adds Service Discovery module

Resolves issue https://github.com/freedombox/Plinth/issues/231
"Option to disable avahi-daemon"
This commit is contained in:
Sean Alexandre 2015-10-03 19:24:12 -04:00 committed by Sunil Mohan Adapa
parent dda0f311b2
commit e6f7e6020f
8 changed files with 305 additions and 0 deletions

61
actions/avahi Executable file
View File

@ -0,0 +1,61 @@
#!/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 <http://www.gnu.org/licenses/>.
#
"""
Configuration helper for service discovery
"""
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 avahi-daemon service')
subparsers.add_parser('disable', help='Disable avahi-daemon service')
return parser.parse_args()
def subcommand_enable(_):
"""Start service."""
action_utils.service_enable('avahi-daemon.socket')
action_utils.service_enable('avahi-daemon.service')
def subcommand_disable(_):
"""Stop service."""
action_utils.service_disable('avahi-daemon.service')
action_utils.service_disable('avahi-daemon.socket')
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 @@
plinth.modules.avahi

View File

@ -0,0 +1,56 @@
#
# 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 <http://www.gnu.org/licenses/>.
#
"""
Plinth module for service discovery
"""
from gettext import gettext as _
import subprocess
from plinth import actions
from plinth import action_utils
from plinth import cfg
from plinth import service as service_module
# pylint: disable=C0103
depends = ['plinth.modules.system']
service = None
def init():
"""Intialize the service discovery module."""
menu = cfg.main_menu.get('system:index')
menu.add_urlname(_('Service Discovery'), 'glyphicon-lamp',
'avahi:index', 950)
global service # pylint: disable=W0603
service = service_module.Service(
'avahi', _('Service Discovery'), ['mdns'],
is_external=False, enabled=is_enabled())
def is_enabled():
"""Return whether the module is enabled."""
return action_utils.service_is_enabled('avahi-daemon')
def is_running():
"""Return whether the service is running."""
return action_utils.service_is_running('avahi-daemon')

View File

@ -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 <http://www.gnu.org/licenses/>.
#
"""
Plinth module for service discovery forms
"""
from django import forms
from gettext import gettext as _
class ServiceDiscoveryForm(forms.Form):
"""Service discovery form."""
enabled = forms.BooleanField(
label=_('Enable service discovery'),
required=False)

View File

@ -0,0 +1,54 @@
{% 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 <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% block content %}
<h2>Service Discovery</h2>
<p>Service discovery is a program that can discover other machines and services
running on your local network. It can also helps those other machines find your
FreedomBox, and the services running on it. Service discovery is not required,
though. Some users decide to disable it to minimize the
<a href="https://en.wikipedia.org/wiki/Attack_surface">attack surface</a>
of their FreedomBox.
<h3>Status</h3>
<p class="running-status-parent">
{% if status.is_running %}
<span class="running-status active"></span> Service discovery server is running
{% else %}
<span class="running-status inactive"></span> Service discovery server is not running
{% endif %}
</p>
<h3>Configuration</h3>
<form class="form" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn btn-primary" value="Update setup"/>
</form>
{% endblock %}

View File

View File

@ -0,0 +1,27 @@
#
# 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 <http://www.gnu.org/licenses/>.
#
"""
URLs for the service discovery module
"""
from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable=C0103
'plinth.modules.avahi.views',
url(r'^sys/avahi/$', 'index', name='index'),
)

View File

@ -0,0 +1,76 @@
#
# 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 <http://www.gnu.org/licenses/>.
#
"""
Plinth module for service discovery views
"""
from django.contrib import messages
from django.template.response import TemplateResponse
from gettext import gettext as _
import logging
from .forms import ServiceDiscoveryForm
from plinth import actions
from plinth import package
from plinth.modules import avahi
logger = logging.getLogger(__name__) # pylint: disable=C0103
@package.required(['avahi-daemon'])
def index(request):
"""Serve configuration page."""
status = get_status()
form = None
if request.method == 'POST':
form = ServiceDiscoveryForm(request.POST)
if form.is_valid():
_apply_changes(request, status, form.cleaned_data)
status = get_status()
form = ServiceDiscoveryForm(initial=status)
else:
form = ServiceDiscoveryForm(initial=status)
return TemplateResponse(request, 'avahi.html',
{'title': _('Service Discovery'),
'status': status,
'form': form})
def get_status():
"""Get the current settings from server."""
return {'enabled': avahi.is_enabled(),
'is_running': avahi.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'
modified = True
actions.superuser_run('avahi', [sub_command])
avahi.service.notify_enabled(None, new_status['enabled'])
messages.success(request, _('Configuration updated'))
if not modified:
messages.info(request, _('Setting unchanged'))