upgrades: Run as background task (Closes: #285).

This commit is contained in:
James Valleroy 2015-11-20 20:16:24 -05:00 committed by Sunil Mohan Adapa
parent e69189c99c
commit 24cd095c6e
3 changed files with 121 additions and 96 deletions

View File

@ -20,23 +20,87 @@
{% load i18n %}
{% block page_head %}
{% if running %}
<meta http-equiv="refresh" content="3"/>
{% endif %}
{% endblock %}
{% block content %}
<h2>{{ title }}</h2>
<p>
{% blocktrans trimmed %}
This will run unattended-upgrades, which will attempt to upgrade
your system with the latest Debian packages. It may take a few
minutes to complete.
{% endblocktrans %}
</p>
{% if result %}
<form class="form" method="post" action="{% url 'upgrades:run' %}">
{% csrf_token %}
{% if result.error %}
<div class="alert alert-danger" role="alert">
{% trans "There was an error while upgrading." %}
</div>
<input type="submit" class="btn btn-primary"
value="{% trans "Upgrade now &raquo;" %}"/>
</form>
<h5>{% trans "Output from unattended-upgrades:" %}</h5>
<pre>{{ result.error }}</pre>
{% endif %}
{% if result.output %}
<div class="row">
<div class="col-lg-6">
<div class="alert alert-success" role="alert">
{% trans "The operating system is up to date now. &nbsp;" %}
<button type="button" class="btn btn-default show-details"
style='display:none'>
{% trans "Show Details" %}
<div class="caret"></div>
</button>
</div>
</div>
</div>
<div class="details">
<h5>{% trans "Output from unattended-upgrades:" %}</h5>
<pre>{{ result.output }}</pre>
</div>
{% endif %}
{% endif %}
{% if not result and not running %}
<p>
{% blocktrans trimmed %}
This will run unattended-upgrades, which will attempt to upgrade
your system with the latest Debian packages. It may take a few
minutes to complete.
{% endblocktrans %}
</p>
<form class="form" method="post" action="{% url 'upgrades:run' %}">
{% csrf_token %}
<input type="submit" class="btn btn-primary"
value="{% trans "Upgrade now &raquo;" %}"/>
</form>
{% endif %}
{% if running %}
<p class="running-status-parent">
<span class="running-status active"></span>
{% trans "Upgrade is running" %}
</p>
{% endif %}
{% endblock %}
{% block page_js %}
<script>
$('.show-details').show();
$('.details').hide();
$('.show-details').click(function() {
$('.details').toggle("slow");
});
</script>
{% endblock %}

View File

@ -1,68 +0,0 @@
{% 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 i18n %}
{% block content %}
<h2>{{title}}</h2>
{% if upgrades_error %}
<div class="alert alert-danger" role="alert">
{% trans "There was an error while upgrading." %}
</div>
<h5>{% trans "Output from unattended-upgrades:" %}</h5>
<pre>{{ upgrades_error }}</pre>
{% endif %}
{% if upgrades_output %}
<div class="row">
<div class="col-lg-6">
<div class="alert alert-success" role="alert">
{% trans "The operating system is up to date now. &nbsp;" %}
<button type="button" class="btn btn-default show-details"
style='display:none'>
{% trans "Show Details" %}
<div class="caret"></div>
</button>
</div>
</div>
</div>
<div class="details">
<h5>{% trans "Output from unattended-upgrades:" %}</h5>
<pre>{{ upgrades_output }}</pre>
</div>
{% endif %}
{% endblock %}
{% block page_js %}
<script>
$('.show-details').show();
$('.details').hide();
$('.show-details').click(function() {
$('.details').toggle("slow");
});
</script>
{% endblock %}

View File

@ -21,6 +21,7 @@ Plinth module for upgrades
from django.contrib import messages
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.utils.translation import ugettext as _, ugettext_lazy
from django.views.decorators.http import require_POST
@ -35,6 +36,8 @@ subsubmenu = [{'url': reverse_lazy('upgrades:index'),
{'url': reverse_lazy('upgrades:upgrade'),
'text': ugettext_lazy('Upgrade Packages')}]
upgrade_process = None
def on_install():
"""Enable automatic upgrades after install."""
@ -66,29 +69,28 @@ def index(request):
@package.required(['unattended-upgrades'], on_install=on_install)
def upgrade(request):
"""Serve the upgrade page."""
if upgrade_process:
result = _collect_upgrade_result(request)
else:
result = None
return TemplateResponse(request, 'upgrades.html',
{'title': _('Package Upgrades'),
'subsubmenu': subsubmenu})
'subsubmenu': subsubmenu,
'running': bool(upgrade_process),
'result': result})
@require_POST
@package.required(['unattended-upgrades'], on_install=on_install)
def run(request):
"""Run upgrades and show the output page."""
output = ''
error = ''
try:
output = actions.superuser_run('upgrades', ['run'])
except ActionError as exception:
output, error = exception.args[1:]
except Exception as exception:
error = str(exception)
def run(_):
"""Start the upgrade process."""
global upgrade_process
if not upgrade_process:
upgrade_process = actions.superuser_run(
'upgrades', ['run'], async=True)
return TemplateResponse(request, 'upgrades_run.html',
{'title': _('Package Upgrades'),
'subsubmenu': subsubmenu,
'upgrades_output': output,
'upgrades_error': error})
return redirect('upgrades:upgrade')
def get_status():
@ -122,3 +124,30 @@ def _apply_changes(request, old_status, new_status):
messages.success(request, _('Automatic upgrades enabled'))
else:
messages.success(request, _('Automatic upgrades disabled'))
def _collect_upgrade_result(request):
"""Handle upgrade process completion."""
global upgrade_process
if not upgrade_process:
return
return_code = upgrade_process.poll()
# Upgrade process is not complete yet
if return_code == None:
return
output, error = upgrade_process.communicate()
output, error = output.decode(), error.decode()
if not return_code:
messages.success(request, _('Upgrade completed.'))
else:
messages.info(request, _('Upgrade failed.'))
upgrade_process = None
return {'return_code': return_code,
'output': output,
'error': error}