From 304e2aea627bbcfcb38395626ba09032c846e1e5 Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Tue, 20 Jan 2015 20:44:21 -0500 Subject: [PATCH] Add form for enabling automatic upgrades. --- actions/upgrades | 16 +++- .../templates/upgrades_configure.html | 34 +++++++++ plinth/modules/upgrades/upgrades.py | 74 ++++++++++++++++++- plinth/modules/upgrades/urls.py | 1 + 4 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 plinth/modules/upgrades/templates/upgrades_configure.html diff --git a/actions/upgrades b/actions/upgrades index d72064b4f..cb260e0a2 100755 --- a/actions/upgrades +++ b/actions/upgrades @@ -30,13 +30,17 @@ AUTO_CONF_FILE = '/etc/apt/apt.conf.d/20auto-upgrades' def parse_arguments(): - """REturn parsed command line arguments as dictionary""" + """Return parsed command line arguments as dictionary""" parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') # Run unattended-upgrades subparsers.add_parser('run', help='Upgrade packages on the system') + # Check if automatic upgrades are enabled + subparsers.add_parser('check-auto', + help='Check if automatic upgrades are enabled') + # Enable automatic upgrades subparsers.add_parser('enable-auto', help='Enable automatic upgrades') @@ -64,6 +68,11 @@ def subcommand_run(_): print('%s', output.decode()) +def subcommand_check_auto(_): + """Check if automatic upgrades are enabled""" + print(os.path.isfile(AUTO_CONF_FILE)) + + def subcommand_enable_auto(_): """Enable automatic upgrades""" try: @@ -79,7 +88,10 @@ def subcommand_enable_auto(_): def subcommand_disable_auto(_): """Disable automatic upgrades""" - os.remove(AUTO_CONF_FILE) + try: + os.rename(AUTO_CONF_FILE, AUTO_CONF_FILE + '.disabled') + except FileNotFoundError: + print('Already disabled.') def setup(): diff --git a/plinth/modules/upgrades/templates/upgrades_configure.html b/plinth/modules/upgrades/templates/upgrades_configure.html new file mode 100644 index 000000000..06230657a --- /dev/null +++ b/plinth/modules/upgrades/templates/upgrades_configure.html @@ -0,0 +1,34 @@ +{% 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 %} + +
+ {% csrf_token %} + + {{ form|bootstrap }} + + + +
+ +{% endblock %} diff --git a/plinth/modules/upgrades/upgrades.py b/plinth/modules/upgrades/upgrades.py index d6557af43..50f109059 100644 --- a/plinth/modules/upgrades/upgrades.py +++ b/plinth/modules/upgrades/upgrades.py @@ -19,7 +19,10 @@ Plinth module for upgrades """ +from django import forms +from django.contrib import messages from django.contrib.auth.decorators import login_required +from django.core.urlresolvers import reverse_lazy from django.template.response import TemplateResponse from gettext import gettext as _ @@ -28,6 +31,11 @@ from plinth import cfg from plinth import package from plinth.errors import ActionError +subsubmenu = [{'url': reverse_lazy('upgrades:index'), + 'text': _('Upgrade Packages')}, + {'url': reverse_lazy('upgrades:configure'), + 'text': _('Automatic Upgrades')}] + def init(): """Initialize the module""" @@ -41,7 +49,8 @@ def init(): def index(request): """Serve the index page""" return TemplateResponse(request, 'upgrades.html', - {'title': _('Package Upgrades')}) + {'title': _('Package Upgrades'), + 'subsubmenu': subsubmenu}) @login_required @@ -59,5 +68,68 @@ def run(request): return TemplateResponse(request, 'upgrades_run.html', {'title': _('Package Upgrades'), + 'subsubmenu': subsubmenu, 'upgrades_output': output, 'upgrades__error': error}) + + +class ConfigureForm(forms.Form): + """Configuration form""" + auto_upgrades_enabled = forms.BooleanField( + label=_('Enable automatic upgrades'), required=False, + help_text=_('When enabled, the unattended-upgrades program will be \ +run once per day. It will attempt to perform any package upgrades that are \ +available.')) + + +@login_required +@package.required('unattended-upgrades') +def configure(request): + """Serve the configuration form""" + status = get_status() + + form = None + + if request.method == 'POST': + form = ConfigureForm(request.POST, prefix='upgrades') + if form.is_valid(): + _apply_changes(request, status, form.cleaned_data) + status = get_status() + form = ConfigureForm(initial=status, prefix='upgrades') + else: + form = ConfigureForm(initial=status, prefix='upgrades') + + return TemplateResponse(request, 'upgrades_configure.html', + {'title': _('Automatic Upgrades'), + 'form': form, + 'subsubmenu': subsubmenu}) + + +def get_status(): + """Return the current status""" + output = actions.run('upgrades', ['check-auto']) + return {'auto_upgrades_enabled': 'True' in output.split()} + + +def _apply_changes(request, old_status, new_status): + """Apply the form changes""" + if old_status['auto_upgrades_enabled'] \ + == new_status['auto_upgrades_enabled']: + messages.info(request, _('Setting unchanged')) + return + + if new_status['auto_upgrades_enabled']: + option = 'enable-auto' + else: + option = 'disable-auto' + + output = actions.superuser_run('upgrades', [option]) + + if 'Error' in output: + messages.error(request, + _('Error when configuring unattended-upgrades: %s') % + output) + elif option == 'enable-auto': + messages.success(request, _('Automatic upgrades enabled')) + else: + messages.success(request, _('Automatic upgrades disabled')) diff --git a/plinth/modules/upgrades/urls.py b/plinth/modules/upgrades/urls.py index 25c50fa85..848570f75 100644 --- a/plinth/modules/upgrades/urls.py +++ b/plinth/modules/upgrades/urls.py @@ -26,4 +26,5 @@ urlpatterns = patterns( 'plinth.modules.upgrades.upgrades', url(r'^sys/upgrades/$', 'index', name='index'), url(r'^sys/upgrades/run/$', 'run', name='run'), + url(r'^sys/upgrades/configure/$', 'configure', name='configure'), )