fix exit codes on upgrade failure

This commit is contained in:
Fredrik Eriksson 2024-07-13 07:40:23 +02:00
parent 0e577bebc5
commit 2a880b5033
Signed by: feffe
GPG Key ID: E6B5580B853D322B
4 changed files with 35 additions and 25 deletions

View File

@ -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
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()
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

View File

@ -1,2 +1 @@
LOGNAME="sau"

View File

@ -5,3 +5,5 @@ class PlatformNotSupported(Exception):
class UnknownServiceError(Exception):
pass
class UpgradeError(Exception):
pass

View File

@ -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