From 28f84ad0b17cd5b6ab1c6a3392bd5cbadd86a2b4 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Fri, 21 Aug 2015 20:14:01 +0530 Subject: [PATCH] datetime: New module for date & time operations - Enable/disable ntpd from this module. Since ntpd has implications on anonymity. - Implement NTP diagonstics from freedombox-setup. - Eventually provide ability to set date & time of the system. - Eventually move timezone configuration to this module. --- actions/datetime | 60 ++++++++++++++ data/etc/plinth/modules-enabled/datetime | 1 + plinth/modules/datetime/__init__.py | 76 +++++++++++++++++ plinth/modules/datetime/forms.py | 30 +++++++ .../modules/datetime/templates/datetime.html | 50 +++++++++++ plinth/modules/datetime/tests/__init__.py | 0 plinth/modules/datetime/urls.py | 28 +++++++ plinth/modules/datetime/views.py | 82 +++++++++++++++++++ 8 files changed, 327 insertions(+) create mode 100755 actions/datetime create mode 100644 data/etc/plinth/modules-enabled/datetime create mode 100644 plinth/modules/datetime/__init__.py create mode 100644 plinth/modules/datetime/forms.py create mode 100644 plinth/modules/datetime/templates/datetime.html create mode 100644 plinth/modules/datetime/tests/__init__.py create mode 100644 plinth/modules/datetime/urls.py create mode 100644 plinth/modules/datetime/views.py diff --git a/actions/datetime b/actions/datetime new file mode 100755 index 000000000..fce52ef62 --- /dev/null +++ b/actions/datetime @@ -0,0 +1,60 @@ +#!/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 . +# + +""" +Configuration helper for date and time +""" + +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 NTP service') + subparsers.add_parser('disable', help='Disable NTP service') + + return parser.parse_args() + + +def subcommand_enable(_): + """Start service.""" + action_utils.service_enable('ntp') + + +def subcommand_disable(_): + """Stop service.""" + action_utils.service_disable('ntp') + + +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/datetime b/data/etc/plinth/modules-enabled/datetime new file mode 100644 index 000000000..eca0b8b49 --- /dev/null +++ b/data/etc/plinth/modules-enabled/datetime @@ -0,0 +1 @@ +plinth.modules.datetime diff --git a/plinth/modules/datetime/__init__.py b/plinth/modules/datetime/__init__.py new file mode 100644 index 000000000..534d44318 --- /dev/null +++ b/plinth/modules/datetime/__init__.py @@ -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 . +# + +""" +Plinth module to configure system date and time +""" + +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 + + +depends = ['plinth.modules.system'] + +service = None + + +def init(): + """Intialize the date/time module.""" + menu = cfg.main_menu.get('system:index') + menu.add_urlname(_('Date & Time'), 'glyphicon-time', + 'datetime:index', 900) + + global service + service = service_module.Service( + 'ntp', _('Network Time Server'), + is_external=False, enabled=is_enabled()) + + +def is_enabled(): + """Return whether the module is enabled.""" + return action_utils.service_is_enabled('ntp') + + +def is_running(): + """Return whether the service is running.""" + return action_utils.service_is_running('ntp') + + +def diagnose(): + """Run diagnostics and return the results.""" + results = [] + results.append(_diagnose_ntp_server_count()) + + return results + + +def _diagnose_ntp_server_count(): + """Diagnose minimum NTP server count.""" + result = 'failed' + try: + output = subprocess.check_output(['ntpq', '-n', '-c', 'lpeers']) + if len(output.decode().splitlines()[2:]): + result = 'passed' + except subprocess.CalledProcessError: + pass + + return [_('NTP client in contact with servers'), result] diff --git a/plinth/modules/datetime/forms.py b/plinth/modules/datetime/forms.py new file mode 100644 index 000000000..6fbbe93d7 --- /dev/null +++ b/plinth/modules/datetime/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 configuring date and time +""" + +from django import forms +from gettext import gettext as _ + + +class DateTimeForm(forms.Form): + """Date/time configuration form.""" + enabled = forms.BooleanField( + label=_('Enable network time'), + required=False) diff --git a/plinth/modules/datetime/templates/datetime.html b/plinth/modules/datetime/templates/datetime.html new file mode 100644 index 000000000..e14d8cf16 --- /dev/null +++ b/plinth/modules/datetime/templates/datetime.html @@ -0,0 +1,50 @@ +{% 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 %} + +{% block content %} + +

Date & Time

+ +

Network time server is a program that maintians the system time in synchronization with servers on the Internet.

+ +

Status

+ +

+ {% if status.is_running %} + Network time server is running + {% else %} + Network time server is not running + {% endif %} +

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

Configuration

+ +
+ {% csrf_token %} + + {{ form|bootstrap }} + + +
+ +{% endblock %} diff --git a/plinth/modules/datetime/tests/__init__.py b/plinth/modules/datetime/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plinth/modules/datetime/urls.py b/plinth/modules/datetime/urls.py new file mode 100644 index 000000000..467522211 --- /dev/null +++ b/plinth/modules/datetime/urls.py @@ -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 . +# + +""" +URLs for the date and time module +""" + +from django.conf.urls import patterns, url + + +urlpatterns = patterns( + 'plinth.modules.datetime.views', + url(r'^sys/datetime/$', 'index', name='index'), + ) diff --git a/plinth/modules/datetime/views.py b/plinth/modules/datetime/views.py new file mode 100644 index 000000000..190ccb62f --- /dev/null +++ b/plinth/modules/datetime/views.py @@ -0,0 +1,82 @@ +# +# 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 configuring date and time +""" + +from django.contrib import messages +from django.template.response import TemplateResponse +from gettext import gettext as _ +import logging + +from .forms import DateTimeForm +from plinth import actions +from plinth import package +from plinth.modules import datetime + +logger = logging.getLogger(__name__) + + +def on_install(): + """Notify that the service is now enabled.""" + datetime.service.notify_enabled(None, True) + + +@package.required(['ntp']) +def index(request): + """Serve configuration page.""" + status = get_status() + + form = None + + if request.method == 'POST': + form = DateTimeForm(request.POST, prefix='datetime') + # pylint: disable=E1101 + if form.is_valid(): + _apply_changes(request, status, form.cleaned_data) + status = get_status() + form = DateTimeForm(initial=status, prefix='datetime') + else: + form = DateTimeForm(initial=status, prefix='datetime') + + return TemplateResponse(request, 'datetime.html', + {'title': _('Voice Chat (Datetime)'), + 'status': status, + 'form': form}) + + +def get_status(): + """Get the current settings from server.""" + return {'enabled': datetime.is_enabled(), + 'is_running': datetime.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('datetime', [sub_command]) + datetime.service.notify_enabled(None, new_status['enabled']) + modified = True + + if modified: + messages.success(request, _('Configuration updated')) + else: + messages.info(request, _('Setting unchanged'))