upgrades: Add button to test dist-upgrade in development mode

Tests:

- In non-development mode, button does not appear.

- On testing system, button does not appear.

- On stable system in development mode, the button appears.

- Pressing the button starts a dist-upgrade.

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
[sunil: Join strings that fit in the same line]
[sunil: Fix indentation in template]
[sunil: Change 'dist-upgrade' to 'distribution upgrade' in UI strings]
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
James Valleroy 2022-08-16 19:44:01 -04:00 committed by Sunil Mohan Adapa
parent f2bc91e876
commit 83bfc51931
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
4 changed files with 95 additions and 47 deletions

View File

@ -189,9 +189,18 @@ def setup_repositories(_):
def check_dist_upgrade(_): def check_dist_upgrade(_):
"""Check for upgrade to new stable release.""" """Check for upgrade to new stable release."""
from plinth.notification import Notification
if is_dist_upgrade_enabled(): if is_dist_upgrade_enabled():
output = actions.superuser_run('upgrades', ['start-dist-upgrade']) try_start_dist_upgrade()
def try_start_dist_upgrade(test=False):
"""Try to start dist upgrade."""
from plinth.notification import Notification
command = ['start-dist-upgrade']
if test:
command.append('--test')
output = actions.superuser_run('upgrades', command)
result = json.loads(output) result = json.loads(output)
dist_upgrade_started = result['dist_upgrade_started'] dist_upgrade_started = result['dist_upgrade_started']
reason = result['reason'] reason = result['reason']
@ -205,8 +214,7 @@ def check_dist_upgrade(_):
logger.warning('Skip dist upgrade: Codename not found in release ' logger.warning('Skip dist upgrade: Codename not found in release '
'file.') 'file.')
elif 'upgrades-not-enabled' in reason: elif 'upgrades-not-enabled' in reason:
logger.info('Skip dist upgrade: Automatic updates are not ' logger.info('Skip dist upgrade: Automatic updates are not enabled.')
'enabled.')
elif 'test-not-set' in reason: elif 'test-not-set' in reason:
logger.info('Skip dist upgrade: --test is not set.') logger.info('Skip dist upgrade: --test is not set.')
elif 'not-enough-free-space' in reason: elif 'not-enough-free-space' in reason:
@ -217,9 +225,9 @@ def check_dist_upgrade(_):
'start the distribution update. Please ensure at least 5 GB ' 'start the distribution update. Please ensure at least 5 GB '
'is free. Distribution update will be retried after 24 hours,' 'is free. Distribution update will be retried after 24 hours,'
' if enabled.') ' if enabled.')
Notification.update_or_create( Notification.update_or_create(id='upgrades-dist-upgrade-free-space',
id='upgrades-dist-upgrade-free-space', app_id='upgrades', app_id='upgrades', severity='warning',
severity='warning', title=title, message=message, actions=[{ title=title, message=message, actions=[{
'type': 'dismiss' 'type': 'dismiss'
}], group='admin') }], group='admin')
elif 'started-dist-upgrade' in reason: elif 'started-dist-upgrade' in reason:
@ -230,8 +238,7 @@ def check_dist_upgrade(_):
'time to complete.') 'time to complete.')
Notification.update_or_create(id='upgrades-dist-upgrade-started', Notification.update_or_create(id='upgrades-dist-upgrade-started',
app_id='upgrades', severity='info', app_id='upgrades', severity='info',
title=title, message=message, title=title, message=message, actions=[{
actions=[{
'type': 'dismiss' 'type': 'dismiss'
}], group='admin') }], group='admin')
else: else:
@ -303,3 +310,14 @@ def can_enable_dist_upgrade():
"""Return whether dist upgrade can be enabled.""" """Return whether dist upgrade can be enabled."""
release, _ = get_current_release() release, _ = get_current_release()
return release not in ['unstable', 'testing'] return release not in ['unstable', 'testing']
def can_test_dist_upgrade():
"""Return whether dist upgrade can be tested."""
return can_enable_dist_upgrade() and cfg.develop
def test_dist_upgrade():
"""Test dist-upgrade from stable to testing."""
if can_test_dist_upgrade():
try_start_dist_upgrade(test=True)

View File

@ -132,4 +132,22 @@
</div> </div>
</p> </p>
{% endif %} {% endif %}
{% if can_test_dist_upgrade %}
<h3>{% trans "Test Distribution Upgrade" %}</h3>
<p>
{% blocktrans trimmed %}
This will attempt to upgrade the system from stable to
testing. <strong>It is meant only for development use.</strong>
{% endblocktrans %}
</p>
<div class="btn-toolbar">
<form class="form" method="post"
action="{% url 'upgrades:test-dist-upgrade' %}">
{% csrf_token %}
<input type="submit" class="btn btn-danger"
value="{% trans "Test distribution upgrade now" %}"/>
</form>
</div>
{% endif %}
{% endblock %} {% endblock %}

View File

@ -21,4 +21,6 @@ urlpatterns = [
views.UpdateFirstbootProgressView.as_view(), views.UpdateFirstbootProgressView.as_view(),
name='update-firstboot-progress'), name='update-firstboot-progress'),
re_path(r'^sys/upgrades/upgrade/$', views.upgrade, name='upgrade'), re_path(r'^sys/upgrades/upgrade/$', views.upgrade, name='upgrade'),
re_path(r'^sys/upgrades/test-dist-upgrade/$', views.test_dist_upgrade,
name='test-dist-upgrade'),
] ]

View File

@ -45,6 +45,7 @@ class UpgradesConfigurationView(AppView):
context['version'] = __version__ context['version'] = __version__
context['new_version'] = is_newer_version_available() context['new_version'] = is_newer_version_available()
context['os_release'] = get_os_release() context['os_release'] = get_os_release()
context['can_test_dist_upgrade'] = upgrades.can_test_dist_upgrade()
return context return context
def form_valid(self, form): def form_valid(self, form):
@ -213,3 +214,12 @@ class UpdateFirstbootProgressView(TemplateView):
context['next_step'] = first_boot.next_step() context['next_step'] = first_boot.next_step()
context['refresh_page_sec'] = 3 if context['is_busy'] else None context['refresh_page_sec'] = 3 if context['is_busy'] else None
return context return context
def test_dist_upgrade(request):
"""Test dist-upgrade from stable to testing."""
if request.method == 'POST':
upgrades.test_dist_upgrade()
messages.success(request, _('Starting distribution upgrade test.'))
return redirect(reverse_lazy('upgrades:index'))