xmpp: Update module layout to match other modules.

Split forms and views into separate files.
Add configuration form to enable/disable the service.
This commit is contained in:
James Valleroy 2015-07-20 19:09:37 -04:00 committed by Sunil Mohan Adapa
parent 6bbc622b1a
commit 97220b89f3
7 changed files with 248 additions and 125 deletions

View File

@ -53,6 +53,9 @@ def parse_arguments():
# Setup jwchat apache conf
subparsers.add_parser('setup', help='Setup jwchat apache conf')
subparsers.add_parser('enable', help='Enable XMPP service')
subparsers.add_parser('disable', help='Disable XMPP service')
# Prepare ejabberd for hostname change
pre_hostname_change = subparsers.add_parser(
'pre-change-hostname',
@ -123,6 +126,18 @@ def subcommand_setup(_):
webserver_change.enable('jwchat-plinth')
def subcommand_enable(_):
"""Enable XMPP service"""
action_utils.service_enable('ejabberd')
action_utils.webserver_enable('jwchat-plinth')
def subcommand_disable(_):
"""Disable XMPP service"""
action_utils.webserver_disable('jwchat-plinth')
action_utils.service_disable('ejabberd')
def subcommand_pre_change_hostname(arguments):
"""Prepare ejabberd for hostname change"""
old_hostname = arguments.old_hostname

View File

@ -19,9 +19,85 @@
Plinth module to configure XMPP server
"""
from . import xmpp
from .xmpp import init
from gettext import gettext as _
from plinth import actions
from plinth import action_utils
from plinth import cfg
from plinth import service as service_module
from plinth.signals import pre_hostname_change, post_hostname_change
from plinth.signals import domainname_change
__all__ = ['xmpp', 'init']
depends = ['plinth.modules.apps']
service = None
def init():
"""Initialize the XMPP module"""
menu = cfg.main_menu.get('apps:index')
menu.add_urlname(_('Chat Server (XMPP)'), 'glyphicon-comment',
'xmpp:index', 40)
global service
service = service_module.Service(
'xmpp', _('Chat Server (XMPP)'),
['xmpp-client', 'xmpp-server', 'xmpp-bosh'],
is_external=True, enabled=is_enabled())
pre_hostname_change.connect(on_pre_hostname_change)
post_hostname_change.connect(on_post_hostname_change)
domainname_change.connect(on_domainname_change)
def is_enabled():
"""Return whether the module is enabled."""
return (action_utils.service_is_enabled('ejabberd') and
action_utils.webserver_is_enabled('jwchat-plinth'))
def is_running():
"""Return whether the service is running."""
return action_utils.service_is_running('ejabberd')
def on_pre_hostname_change(sender, old_hostname, new_hostname, **kwargs):
"""
Backup ejabberd database before hostname is changed.
"""
del sender # Unused
del kwargs # Unused
actions.superuser_run('xmpp',
['pre-change-hostname',
'--old-hostname', old_hostname,
'--new-hostname', new_hostname])
def on_post_hostname_change(sender, old_hostname, new_hostname, **kwargs):
"""
Update ejabberd and jwchat config after hostname is changed.
"""
del sender # Unused
del kwargs # Unused
actions.superuser_run('xmpp',
['change-hostname',
'--old-hostname', old_hostname,
'--new-hostname', new_hostname],
async=True)
def on_domainname_change(sender, old_domainname, new_domainname, **kwargs):
"""
Update ejabberd and jwchat config after domain name is changed.
"""
del sender # Unused
del old_domainname # Unused
del kwargs # Unused
actions.superuser_run('xmpp',
['change-domainname',
'--domainname', new_domainname],
async=True)

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 XMPP service.
"""
from django import forms
from gettext import gettext as _
class XmppForm(forms.Form): # pylint: disable=W0232
"""XMPP configuration form."""
enabled = forms.BooleanField(
label=_('Enable XMPP'),
required=False)

View File

@ -22,13 +22,36 @@
{% block content %}
<p>XMPP is an open and standardized communication protocol. Here you
can run and configure your XMPP server, called ejabberd. To actually
communicate, you can use the <a href='/jwchat'>web client</a> or any
other <a href='http://xmpp.org/xmpp-software/clients/'
target='_blank'> XMPP client </a>.</p>
<h2>Chat Server (XMPP)</h2>
<p>XMPP is an open and standardized communication protocol. Here you
can run and configure your XMPP server, called ejabberd.</p>
<p>To actually communicate, you can use the <a href='/jwchat'>web client</a> or
any other <a href='http://xmpp.org/xmpp-software/clients/' target='_blank'>
XMPP client </a>.</p>
<p><a href='/jwchat' target='_blank'class='btn btn-primary'> Launch web
client</a></p>
<h3>Status</h3>
<p>
{% if status.is_running %}
<span class="running-status active"></span> ejabberd is running
{% else %}
<span class="running-status inactive"></span> ejabberd 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

@ -23,6 +23,6 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'plinth.modules.xmpp.xmpp',
'plinth.modules.xmpp.views',
url(r'^apps/xmpp/$', 'index', name='index'),
)

View File

@ -0,0 +1,95 @@
#
# 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 XMPP server
"""
from django.contrib import messages
from django.template.response import TemplateResponse
from gettext import gettext as _
import logging
import socket
from .forms import XmppForm
from plinth import actions
from plinth import package
from plinth.modules import xmpp
logger = logging.getLogger(__name__)
def before_install():
"""Preseed debconf values before the packages are installed."""
fqdn = socket.getfqdn()
domainname = '.'.join(fqdn.split('.')[1:])
logger.info('XMPP service domainname - %s', domainname)
actions.superuser_run('xmpp', ['pre-install', '--domainname', domainname])
def on_install():
"""Setup jwchat apache conf"""
actions.superuser_run('xmpp', ['setup'])
@package.required(['jwchat', 'ejabberd'],
before_install=before_install,
on_install=on_install)
def index(request):
"""Serve configuration page"""
status = get_status()
form = None
if request.method == 'POST':
form = XmppForm(request.POST, prefix='xmpp')
if form.is_valid():
_apply_changes(request, status, form.cleaned_data)
status = get_status()
form = XmppForm(initial=status, prefix='xmpp')
else:
form = XmppForm(initial=status, prefix='xmpp')
return TemplateResponse(request, 'xmpp.html',
{'title': _('Chat Server (XMPP)'),
'status': status,
'form': form})
def get_status():
"""Get the current settings."""
status = {'enabled': xmpp.is_enabled(),
'is_running': xmpp.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('xmpp', [sub_command])
xmpp.service.notify_enabled(None, new_status['enabled'])
modified = True
if modified:
messages.success(request, _('Configuration updated'))
else:
messages.info(request, _('Setting unchanged'))

View File

@ -1,116 +0,0 @@
#
# 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/>.
#
from django import forms
from django.contrib import messages
from django.template.response import TemplateResponse
from gettext import gettext as _
import logging
import socket
from plinth import actions
from plinth import cfg
from plinth import package
from plinth import service
from plinth.signals import pre_hostname_change, post_hostname_change
from plinth.signals import domainname_change
logger = logging.getLogger(__name__)
def init():
"""Initialize the XMPP module"""
menu = cfg.main_menu.get('apps:index')
menu.add_urlname('XMPP', 'glyphicon-comment', 'xmpp:index', 40)
service.Service(
'xmpp-client', _('Chat Server - client connections'),
is_external=True, enabled=True)
service.Service(
'xmpp-server', _('Chat Server - server connections'),
is_external=True, enabled=True)
service.Service(
'xmpp-bosh', _('Chat Server - web interface'), is_external=True,
enabled=True)
pre_hostname_change.connect(on_pre_hostname_change)
post_hostname_change.connect(on_post_hostname_change)
domainname_change.connect(on_domainname_change)
def before_install():
"""Preseed debconf values before the packages are installed."""
fqdn = socket.getfqdn()
domainname = '.'.join(fqdn.split('.')[1:])
logger.info('XMPP service domainname - %s', domainname)
actions.superuser_run('xmpp', ['pre-install', '--domainname', domainname])
def on_install():
"""Setup jwchat apache conf"""
actions.superuser_run('xmpp', ['setup'])
@package.required(['jwchat', 'ejabberd'],
before_install=before_install,
on_install=on_install)
def index(request):
"""Serve XMPP page"""
return TemplateResponse(request, 'xmpp.html',
{'title': _('XMPP Server')})
def on_pre_hostname_change(sender, old_hostname, new_hostname, **kwargs):
"""
Backup ejabberd database before hostname is changed.
"""
del sender # Unused
del kwargs # Unused
actions.superuser_run('xmpp',
['pre-change-hostname',
'--old-hostname', old_hostname,
'--new-hostname', new_hostname])
def on_post_hostname_change(sender, old_hostname, new_hostname, **kwargs):
"""
Update ejabberd and jwchat config after hostname is changed.
"""
del sender # Unused
del kwargs # Unused
actions.superuser_run('xmpp',
['change-hostname',
'--old-hostname', old_hostname,
'--new-hostname', new_hostname],
async=True)
def on_domainname_change(sender, old_domainname, new_domainname, **kwargs):
"""
Update ejabberd and jwchat config after domain name is changed.
"""
del sender # Unused
del old_domainname # Unused
del kwargs # Unused
actions.superuser_run('xmpp',
['change-domainname',
'--domainname', new_domainname],
async=True)