automatic reset-failed and implement status exit code

This commit is contained in:
JerryXiao 2021-01-18 14:50:38 +08:00
parent 0cad72d401
commit b43589a01e
Signed by: Jerry
GPG key ID: 9D9CE43650FF2BAA
2 changed files with 34 additions and 15 deletions

View file

@ -35,6 +35,15 @@ class checkReport:
self._crit = crit or list()
self._changes = changes or list()
self._date = date
@property
def failed(self, extra_safe: bool = False) -> bool:
if extra_safe:
if self._info or self._warn or self._crit:
return True
else:
if self._warn or self._crit:
return True
return False
def to_dict(self) -> dict:
return {'info': self._info, 'warn': self._warn, 'crit': self._crit, 'changes': self._changes, 'date': self._date}
def summary(self, verbose=True, show_package=False, indent=2) -> str:

View file

@ -213,12 +213,12 @@ def main() -> None:
if getuid() != 0:
logger.error('you need to be root')
exit(1)
if prev_err := has_previous_error():
logger.error(f'Cannot continue, a previous error {prev_err} is still present. Please resolve this issue and run fail-reset.')
if SYSTEMD:
if _s := is_system_failed():
logger.error(f'systemd is in {_s} state, refused')
exit(11)
if prev_err := has_previous_error():
logger.error(f'Cannot continue, a previous error {prev_err} is still present. Please resolve this issue and run fail-reset.')
else:
try:
report = do_system_upgrade(args.debug)
@ -228,13 +228,7 @@ def main() -> None:
write_db(None, e)
raise
else:
exc = None
if EXTRA_SAFE:
if report._info or report._warn or report._crit:
exc = CheckFailed('manual inspection required')
else:
if report._warn or report._crit:
exc = CheckFailed('manual inspection required')
exc = CheckFailed('manual inspection required') if report.failed else None
write_db(report, exc)
if exc:
exit(2)
@ -250,30 +244,46 @@ def main() -> None:
)
except subprocess.CalledProcessError as e:
logger.error(f'needrestart failed with {e.returncode=} {e.output=}')
exc = NeedrestartFailed(f'{e.returncode=}')
write_db(None, exc)
write_db(None, NeedrestartFailed(f'{e.returncode=}'))
exit(2)
else:
logger.debug(f'needrestart {p.stdout=}')
elif args.action == 'status':
count = 0
failed = False
for entry in read_db():
if report_dict := entry.get('report'):
count += 1
report = checkReport(**report_dict)
if count == 1:
failed = report.failed
print(report.summary(verbose=args.verbose, show_package=True))
if count >= args.max:
if count >= args.max and args.max > 0:
break
print()
if failed:
exit(2)
elif args.action == 'fail-reset':
if getuid() != 0:
logger.error('you need to be root')
exit(1)
try:
subprocess.run(["systemctl", "is-failed", "pacroller"],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
encoding='utf-8',
timeout=20
)
except subprocess.CalledProcessError:
pass
else:
subprocess.run(["systemctl", "reset-failed", "pacroller"], timeout=20)
if SYSTEMD:
if _s := is_system_failed():
logger.error(f'systemd is in {_s} state, refused')
exit(11)
if getuid() != 0:
logger.error('you need to be root')
exit(1)
if prev_err := has_previous_error():
write_db(None)
logger.info(f'reset previous error {prev_err}')