mumble: Add new module for installing, enabling/disabling

This commit is contained in:
Sunil Mohan Adapa 2015-03-17 15:57:46 +05:30 committed by James Valleroy
parent 475ddd0c72
commit 2028a63deb
9 changed files with 381 additions and 0 deletions

125
actions/mumble Executable file
View File

@ -0,0 +1,125 @@
#!/usr/bin/python3
# -*- mode: python -*-
#
# 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 Mumble server
"""
import argparse
import subprocess
SERVICE_CONFIG = '/etc/default/mumble-server'
def parse_arguments():
"""Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
# Get whether service is enabled
subparsers.add_parser('get-enabled',
help='Get whether Mumble service is enabled')
# Enable service
subparsers.add_parser('enable', help='Enable Mumble service')
# Disable service
subparsers.add_parser('disable', help='Disable Mumble service')
# Get whether daemon is running
subparsers.add_parser('is-running',
help='Get whether Mumble daemon is running')
return parser.parse_args()
def subcommand_get_enabled(_):
"""Get whether service is enabled."""
try:
with open(SERVICE_CONFIG, 'r') as file:
for line in file:
if line.startswith('MURMUR_DAEMON_START'):
value = line.split('=')[1].strip()
print('yes' if int(value) else 'no')
return
except FileNotFoundError:
pass
print('no')
def subcommand_enable(_):
"""Start service."""
set_service_enable(enable=True)
subprocess.call(['service', 'mumble-server', 'start'])
def subcommand_disable(_):
"""Stop service."""
subprocess.call(['service', 'mumble-server', 'stop'])
set_service_enable(enable=False)
def set_service_enable(enable):
"""Enable/disable daemon; enable: boolean."""
newline = 'MURMUR_DAEMON_START=1\n' if enable \
else 'MURMUR_DAEMON_START=0\n'
with open(SERVICE_CONFIG, 'r') as file:
lines = file.readlines()
for index, line in enumerate(lines):
if line.startswith('MURMUR_DAEMON_START'):
lines[index] = newline
break
with open(SERVICE_CONFIG, 'w') as file:
file.writelines(lines)
def subcommand_is_running(_):
"""Get whether server is running."""
try:
output = subprocess.check_output(['service', 'mumble-server',
'status'])
except subprocess.CalledProcessError:
# If daemon is not running we get a status code != 0 and a
# CalledProcessError
print('no')
else:
running = False
for line in output.decode().split('\n'):
if 'Active' in line and 'running' in line:
running = True
break
print('yes' if running else 'no')
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.mumble

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>Mumble Voice Chat Server</short>
<description>Mumble is an open source, low-latency, encrypted, high quality voice chat software primarily intended for use while gaming. Mumble uses a client-server architecture which allows users to talk to each other via the same server. Enable this if you are running a Mumble server and if you wish to connect external clients such as Mumble desktop client and Plumble Android app to your Mumble server.</description>
<port protocol="tcp" port="64738"/>
<port protocol="udp" port="64738"/>
</service>

View File

@ -0,0 +1,46 @@
#
# 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 to configure Mumble server
"""
from gettext import gettext as _
from plinth import actions
from plinth import cfg
from plinth import service as service_module
depends = ['plinth.modules.apps']
service = None
def init():
"""Intialize the Mumble module."""
menu = cfg.main_menu.get('apps:index')
menu.add_urlname(_('Voice Chat (Mumble)'), 'glyphicon-headphones',
'mumble:index', 50)
output = actions.run('mumble', ['get-enabled'])
enabled = (output.strip() == 'yes')
global service
service = service_module.Service(
'mumble-plinth', _('Mumble Voice Chat Server'),
is_external=True, enabled=enabled)

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/>.
#
"""
Forms for configuring Mumble
"""
from django import forms
from gettext import gettext as _
class MumbleForm(forms.Form):
"""Mumble configuration form."""
enabled = forms.BooleanField(
label=_('Enable Mumble daemon'),
required=False)

View File

@ -0,0 +1,55 @@
{% 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>Voice Chat (Mumble)</h2>
<p>Mumble is an open source, low-latency, encrypted, high quality voice chat
software.</p>
<p>You can connect to your Mumble server on the regular Mumble port 64738.
<a href="http://mumble.info">Clients</a> to connect to Mumble from your
desktop and Android devices are available.</p>
<h3>Status</h3>
<p>
{% if status.is_running %}
<span class='running-status active'></span> Mumble server is running
{% else %}
<span class='running-status inactive'></span> Mumble 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

@ -0,0 +1,28 @@
#
# 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 Mumble module
"""
from django.conf.urls import patterns, url
urlpatterns = patterns(
'plinth.modules.mumble.views',
url(r'^apps/mumble/$', 'index', name='index'),
)

View File

@ -0,0 +1,87 @@
#
# 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 configuring Mumble Server
"""
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.template.response import TemplateResponse
from gettext import gettext as _
import logging
from .forms import MumbleForm
from plinth import actions
from plinth import package
from plinth.modules import mumble
logger = logging.getLogger(__name__)
@login_required
@package.required('mumble-server')
def index(request):
"""Serve configuration page."""
status = get_status()
form = None
if request.method == 'POST':
form = MumbleForm(request.POST, prefix='mumble')
# pylint: disable=E1101
if form.is_valid():
_apply_changes(request, status, form.cleaned_data)
status = get_status()
form = MumbleForm(initial=status, prefix='mumble')
else:
form = MumbleForm(initial=status, prefix='mumble')
return TemplateResponse(request, 'mumble.html',
{'title': _('Voice Chat (Mumble)'),
'status': status,
'form': form})
def get_status():
"""Get the current settings from server."""
output = actions.run('mumble', ['get-enabled'])
enabled = (output.strip() == 'yes')
output = actions.superuser_run('mumble', ['is-running'])
is_running = (output.strip() == 'yes')
status = {'enabled': enabled,
'is_running': is_running}
return status
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('mumble', [sub_command])
mumble.service.notify_enabled(None, new_status['enabled'])
modified = True
if modified:
messages.success(request, _('Configuration updated'))
else:
messages.info(request, _('Setting unchanged'))

View File

@ -122,6 +122,8 @@ setuptools.setup(
package_data={'plinth': ['templates/*',
'modules/*/templates/*']},
data_files=[('/etc/init.d', ['data/etc/init.d/plinth']),
('/usr/lib/firewalld/services/',
glob.glob('data/usr/lib/firewalld/services/*.xml')),
('/usr/lib/freedombox/setup.d/',
['data/usr/lib/freedombox/setup.d/86_plinth']),
('/usr/lib/freedombox/first-run.d',