Clarify the whole error / success handling

This commit is contained in:
Alexandre Aubin 2019-04-22 22:15:16 +02:00
parent 9e3d302832
commit 779a16dab1

View file

@ -602,8 +602,6 @@ def tools_upgrade(operation_logger, auth, apps=None, system=False):
# TODO : i18n # TODO : i18n
raise YunohostError("Please specify --apps OR --system") raise YunohostError("Please specify --apps OR --system")
failure = False
if system is True: if system is True:
# Check that there's indeed some packages to upgrade # Check that there's indeed some packages to upgrade
@ -633,7 +631,7 @@ def tools_upgrade(operation_logger, auth, apps=None, system=False):
# #
# "Regular" packages upgrade # "Regular" packages upgrade
# #
if not failure and noncritical_packages_upgradable: if noncritical_packages_upgradable:
# TODO : i18n # TODO : i18n
logger.info("Upgrading 'regular' (non-yunohost-related) packages ...") logger.info("Upgrading 'regular' (non-yunohost-related) packages ...")
@ -645,31 +643,26 @@ def tools_upgrade(operation_logger, auth, apps=None, system=False):
# Doublecheck with apt-mark showhold that packages are indeed held ... # Doublecheck with apt-mark showhold that packages are indeed held ...
held_packages = check_output("apt-mark showhold").split("\n") held_packages = check_output("apt-mark showhold").split("\n")
if any(p not in held_packages for p in critical_packages): if any(p not in held_packages for p in critical_packages):
failure = True
logger.warning('Unable to hold critical packages ...') logger.warning('Unable to hold critical packages ...')
logger.error(m18n.n('packages_upgrade_failed'))
# FIXME : watdo here, should this be an exception or just an
# error
operation_logger.error(m18n.n('packages_upgrade_failed')) operation_logger.error(m18n.n('packages_upgrade_failed'))
raise YunohostError(m18n.n('packages_upgrade_failed'))
if not failure: logger.debug("Running apt command :\n{}".format(dist_upgrade))
logger.debug("Running apt command :\n{}".format(dist_upgrade))
callbacks = ( callbacks = (
lambda l: logger.info(l.rstrip()), lambda l: logger.info(l.rstrip()),
lambda l: logger.warning(l.rstrip()), lambda l: logger.warning(l.rstrip()),
) )
returncode = call_async_output(dist_upgrade, callbacks, shell=True) returncode = call_async_output(dist_upgrade, callbacks, shell=True)
if returncode != 0: if returncode != 0:
failure = True logger.warning('unable to upgrade packages: %s' % ', '.join(noncritical_packages_upgradable))
logger.warning('unable to upgrade packages: %s' % ', '.join(noncritical_packages_upgradable)) operation_logger.error(m18n.n('packages_upgrade_failed'))
logger.error(m18n.n('packages_upgrade_failed')) raise YunohostError(m18n.n('packages_upgrade_failed'))
operation_logger.error(m18n.n('packages_upgrade_failed'))
# #
# Critical packages upgrade # Critical packages upgrade
# #
if not failure and critical_packages_upgradable: if critical_packages_upgradable:
# TODO : i18n # TODO : i18n
logger.info("Upgrading 'special' (yunohost-related) packages ...") logger.info("Upgrading 'special' (yunohost-related) packages ...")
@ -681,12 +674,9 @@ def tools_upgrade(operation_logger, auth, apps=None, system=False):
# Doublecheck with apt-mark showhold that packages are indeed unheld ... # Doublecheck with apt-mark showhold that packages are indeed unheld ...
unheld_packages = check_output("apt-mark showhold").split("\n") unheld_packages = check_output("apt-mark showhold").split("\n")
if any(p in unheld_packages for p in critical_packages): if any(p in unheld_packages for p in critical_packages):
failure = True
logger.warning('Unable to unhold critical packages ...') logger.warning('Unable to unhold critical packages ...')
logger.error(m18n.n('packages_upgrade_failed'))
# FIXME : watdo here, should this be an exception or just an
# error
operation_logger.error(m18n.n('packages_upgrade_failed')) operation_logger.error(m18n.n('packages_upgrade_failed'))
raise YunohostError(m18n.n('packages_upgrade_failed'))
# #
# Here we use a dirty hack to run a command after the current # Here we use a dirty hack to run a command after the current
@ -715,17 +705,14 @@ def tools_upgrade(operation_logger, auth, apps=None, system=False):
logger.debug("Running command :\n{}".format(command)) logger.debug("Running command :\n{}".format(command))
os.system(command) os.system(command)
return
# TODO / FIXME : return from this function immediately,
# otherwise the apps upgrade might happen and it's gonna be a mess
# FIXME / open question : what about "permanently" mark yunohost # FIXME / open question : what about "permanently" mark yunohost
# as "hold" to avoid accidental deletion of it... # as "hold" to avoid accidental deletion of it...
# (so, only unhold it during the upgrade) # (so, only unhold it during the upgrade)
elif not failure: else:
logger.success(m18n.n('system_upgraded'))
logger.info(m18n.n('done'))
operation_logger.success() operation_logger.success()
@ -733,13 +720,9 @@ def tools_upgrade(operation_logger, auth, apps=None, system=False):
try: try:
app_upgrade(auth, app=apps) app_upgrade(auth, app=apps)
except Exception as e: except Exception as e:
failure = True
logger.warning('unable to upgrade apps: %s' % str(e)) logger.warning('unable to upgrade apps: %s' % str(e))
logger.error(m18n.n('app_upgrade_some_app_failed')) logger.error(m18n.n('app_upgrade_some_app_failed'))
if not failure:
logger.success(m18n.n('system_upgraded'))
# Return API logs if it is an API call # Return API logs if it is an API call
is_api = True if msettings.get('interface') == 'api' else False is_api = True if msettings.get('interface') == 'api' else False
if is_api: if is_api: