upgrades: Simplify dist upgrades checks using exceptions

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2025-03-04 20:48:34 -08:00 committed by James Valleroy
parent 9ddfbc4fed
commit 32739bb8b7
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 28 additions and 31 deletions

View File

@ -221,9 +221,23 @@ def try_start_dist_upgrade(test=False):
"""Try to start dist upgrade."""
from plinth.notification import Notification
result = privileged.start_dist_upgrade(test)
dist_upgrade_started = result['dist_upgrade_started']
reason = result['reason']
try:
privileged.start_dist_upgrade(test, _log_error=False)
except RuntimeError as exception:
reason = exception.args[0]
else:
logger.info('Started dist upgrade.')
title = gettext_noop('Distribution update started')
message = gettext_noop(
'Started update to next stable release. This may take a long '
'time to complete.')
Notification.update_or_create(id='upgrades-dist-upgrade-started',
app_id='upgrades', severity='info',
title=title, message=message, actions=[{
'type': 'dismiss'
}], group='admin')
return
if 'found-previous' in reason:
logger.info(
'Found previous dist-upgrade. If it was interrupted, it will '
@ -250,20 +264,8 @@ def try_start_dist_upgrade(test=False):
title=title, message=message, actions=[{
'type': 'dismiss'
}], group='admin')
elif 'started-dist-upgrade' in reason:
logger.info('Started dist upgrade.')
title = gettext_noop('Distribution update started')
message = gettext_noop(
'Started update to next stable release. This may take a long '
'time to complete.')
Notification.update_or_create(id='upgrades-dist-upgrade-started',
app_id='upgrades', severity='info',
title=title, message=message, actions=[{
'type': 'dismiss'
}], group='admin')
else:
logger.warning('Unhandled result of start-dist-upgrade: %s, %s',
dist_upgrade_started, reason)
logger.warning('Unhandled result of start-dist-upgrade: %s', reason)
def is_backports_requested():

View File

@ -75,7 +75,7 @@ def _get_new_codename(test_upgrade: bool) -> str | None:
return None
def check(test_upgrade=False) -> tuple[bool, str]:
def check(test_upgrade=False):
"""Check if a distribution upgrade be performed.
Check for new stable release, if updates are enabled, and if there is
@ -87,30 +87,28 @@ def check(test_upgrade=False) -> tuple[bool, str]:
if not.
"""
if action_utils.service_is_running('freedombox-dist-upgrade'):
return (True, 'found-previous')
raise RuntimeError('found-previous')
from plinth.modules.upgrades import get_current_release
release, dist = get_current_release()
if release in ['unstable', 'testing', 'n/a']:
return (False, f'already-{release}')
raise RuntimeError(f'already-{release}')
codename = _get_new_codename(test_upgrade)
if not codename:
return (False, 'codename-not-found')
raise RuntimeError('codename-not-found')
if codename == dist:
return (False, f'already-{dist}')
raise RuntimeError(f'already-{dist}')
if not utils.check_auto():
return (False, 'upgrades-not-enabled')
raise RuntimeError('upgrades-not-enabled')
if not utils.is_sufficient_free_space():
return (False, 'not-enough-free-space')
raise RuntimeError('not-enough-free-space')
_sources_list_update(dist, codename)
return (True, 'started-dist-upgrade')
@contextlib.contextmanager
def _snapshot_run_and_disable() -> Generator[None, None, None]:

View File

@ -236,7 +236,7 @@ def activate_backports(develop: bool = False):
@privileged
def start_dist_upgrade(test: bool = False) -> dict[str, str | bool]:
def start_dist_upgrade(test: bool = False):
"""Start dist upgrade process.
Check if a new stable release is available, and start dist-upgrade process
@ -244,11 +244,8 @@ def start_dist_upgrade(test: bool = False) -> dict[str, str | bool]:
"""
_release_held_freedombox()
upgrade_ready, reason = distupgrade.check(test)
if upgrade_ready:
distupgrade.start_service()
return {'dist_upgrade_started': upgrade_ready, 'reason': reason}
distupgrade.check(test)
distupgrade.start_service()
@privileged