Add form for enabling automatic upgrades.

This commit is contained in:
James Valleroy 2015-01-20 20:44:21 -05:00 committed by fonfon
parent 80eff7fd6e
commit 304e2aea62
4 changed files with 122 additions and 3 deletions

View File

@ -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():

View File

@ -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 <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% block content %}
<form class="form" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn btn-primary btn-md" value="Update setup"/>
</form>
{% endblock %}

View File

@ -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'))

View File

@ -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'),
)