From 2a880b503304eb497077cf39081d87e0b3c38b00 Mon Sep 17 00:00:00 2001 From: Fredrik Eriksson Date: Sat, 13 Jul 2024 07:40:23 +0200 Subject: [PATCH] fix exit codes on upgrade failure --- bin/sau | 14 ++++++++++---- sau/__init__.py | 1 - sau/errors.py | 2 ++ sau/gentoo.py | 43 +++++++++++++++++++++++-------------------- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/bin/sau b/bin/sau index 9fba187..77db1fc 100755 --- a/bin/sau +++ b/bin/sau @@ -8,6 +8,7 @@ import sys import time import sau +import sau.errors import sau.services import sau.platforms @@ -101,11 +102,15 @@ def main(): reboot_required = False reboot_recommended = False - if conf.getboolean('default', 'do_system_upgrade', fallback=True): - reboot_required = platform.system_upgrade() + try: + if conf.getboolean('default', 'do_system_upgrade', fallback=True): + reboot_required = platform.system_upgrade() - if conf.getboolean('default', 'do_package_upgrade', fallback=True): - reboot_required = reboot_required or platform.pkg_upgrade() + if conf.getboolean('default', 'do_package_upgrade', fallback=True): + reboot_required = reboot_required or platform.pkg_upgrade() + except sau.errors.UpgradeError as e: + log.error(f'Upgrade failed: {e}') + return 1 if conf.getboolean('default', 'do_service_restart', fallback=True): reboot_recommended = sau.services.restart_services() @@ -118,6 +123,7 @@ def main(): if reboot_required or reboot_recommended: fork_and_reboot() + def _conf_level_to_logging_level(conf_level): if conf_level.lower() == 'debug': return logging.DEBUG diff --git a/sau/__init__.py b/sau/__init__.py index 1f34017..b768835 100644 --- a/sau/__init__.py +++ b/sau/__init__.py @@ -1,2 +1 @@ - LOGNAME="sau" diff --git a/sau/errors.py b/sau/errors.py index 993af60..665a350 100644 --- a/sau/errors.py +++ b/sau/errors.py @@ -5,3 +5,5 @@ class PlatformNotSupported(Exception): class UnknownServiceError(Exception): pass +class UpgradeError(Exception): + pass diff --git a/sau/gentoo.py b/sau/gentoo.py index e77d52e..86742a6 100644 --- a/sau/gentoo.py +++ b/sau/gentoo.py @@ -92,11 +92,12 @@ def _sync_portage(): ret, out, err = sau.helpers.exec_cmd(cmd, timeout=3600) if ret != 0: - log.warning("Portage sync failed:") + log.error("Portage sync failed:") for line in out.splitlines(): - log.warning("stdout: {}".format(line)) + log.error("stdout: {}".format(line)) for line in err.splitlines(): - log.warning("stderr: {}".format(line)) + log.error("stderr: {}".format(line)) + raise sau.errors.UpgradeError(f'Sync command {cmd} failed') cmd = [ EMAINT_PATH, '-f', 'all' ] ret, out, err = sau.helpers.exec_cmd(cmd, timeout=3600) @@ -134,7 +135,7 @@ def pkg_upgrade(): log.error('stdout: {}'.format(line)) for line in err.splitlines(): log.error('stderr: {}'.format(line)) - return False + raise sau.errors.UpgradeError(f'Failed to calculate upgrade path') do_rebuild = True do_grub = False @@ -164,22 +165,21 @@ def pkg_upgrade(): if do_system_upgrade: do_grub = True else: - log.warning(f"Kernel package {name} has an update, but system upgrade is disabled") - do_rebuild = False + raise sau.errors.UpgradeError(f"Kernel package {name} has an update, but system upgrade is disabled") if not do_rebuild: - log.warning('Some packages require manual attention, did not upgrade') - return False + sau.errors.UpgradeError('Some packages require manual attention, did not upgrade') cmd = [ EMERGE_PATH, '--color', 'n', '-uDNq', '--with-bdeps=y', '@world' ] ret, out, err = sau.helpers.exec_cmd(cmd, timeout=72000) if ret != 0 or err: - log.warning('emerge returned {}'.format(ret)) + log.error('emerge returned {}'.format(ret)) for line in out.splitlines(): - log.warning('stdout: {}'.format(line)) + log.error('stdout: {}'.format(line)) for line in err.splitlines(): - log.warning('stderr: {}'.format(line)) + log.error('stderr: {}'.format(line)) + raise sau.errors.UpgradeError(f'Error during upgrade') else: log.info('upgrade complete') for line in out.splitlines(): @@ -190,11 +190,12 @@ def pkg_upgrade(): ret, out, err = sau.helpers.exec_cmd(cmd, timeout=72000) if ret != 0 or err: - log.warning('preserved-rebuild returned {}'.format(ret)) + log.error('preserved-rebuild returned {}'.format(ret)) for line in out.splitlines(): - log.warning('stdout: {}'.format(line)) + log.error('stdout: {}'.format(line)) for line in err.splitlines(): - log.warning('stderr: {}'.format(line)) + log.error('stderr: {}'.format(line)) + raise sau.errors.UpgradeError(f'Error during preserved-rebuild') else: log.info('preserved-rebuild complete') for line in out.splitlines(): @@ -205,11 +206,12 @@ def pkg_upgrade(): cmd = [ EMERGE_PATH, '--color', 'n', '-q', '--depclean' ] ret, out, err = sau.helpers.exec_cmd(cmd, timeout=3600) if ret != 0 or err: - log.warning('depclean returned {}'.format(ret)) + log.error('depclean returned {}'.format(ret)) for line in out.splitlines(): - log.warning('stdout: {}'.format(line)) + log.error('stdout: {}'.format(line)) for line in err.splitlines(): - log.warning('stderr: {}'.format(line)) + log.error('stderr: {}'.format(line)) + raise sau.errors.UpgradeError(f'Error during depclean') else: log.info('depclean complete') for line in out.splitlines(): @@ -234,11 +236,12 @@ def pkg_upgrade(): cmd = [ GRUB_MKCONFIG, '-o', '/boot/grub/grub.cfg' ] ret, out, err = sau.helpers.exec_cmd(cmd) if ret != 0: - log.warning(f"grub-mkconfig returned {ret}:") + log.error(f"grub-mkconfig returned {ret}:") for line in out.splitlines(): - log.warning('stdout: {}'.format(line)) + log.error('stdout: {}'.format(line)) for line in err.splitlines(): - log.warning('stderr: {}'.format(line)) + log.error('stderr: {}'.format(line)) + raise sau.errors.UpgradeError(f'Failed to reconfiugre grub') else: log.info("grub reconfigured") return True