feat(reboot): wire automated and interactive system reboot on update and release-upgrade
This commit is contained in:
1 parent
61875621ed
commit
426e8e5b6e
4 files changed
+320
-26
No files matched your search
+124
-15
@@ -55,6 +55,13 @@ def create_parser() -> argparse.ArgumentParser:
|
||||
default=argparse.SUPPRESS,
|
||||
help="Output structured JSON results.",
|
||||
)
|
||||
common.add_argument(
|
||||
"--reboot",
|
||||
dest="auto_reboot",
|
||||
action="store_true",
|
||||
default=argparse.SUPPRESS,
|
||||
help="Automatically reboot the system after a major release upgrade (or if reboot is required).",
|
||||
)
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="linux-updater",
|
||||
@@ -198,17 +205,49 @@ def format_health_report(report) -> None:
|
||||
print(f"Overall Pre-flight Status: {status_text}\n")
|
||||
|
||||
|
||||
def prompt_confirmation(prompt: str = "Do you want to proceed with updates? [Y/n]: ") -> bool:
|
||||
def prompt_confirmation(
|
||||
prompt: str = "Do you want to proceed with updates? [Y/n]: ",
|
||||
default_yes: bool = True,
|
||||
) -> bool:
|
||||
try:
|
||||
choice = input(prompt).strip().lower()
|
||||
if not choice or choice in ("y", "yes", "tak", "t"):
|
||||
return True
|
||||
return False
|
||||
if not choice:
|
||||
return default_yes
|
||||
return choice in ("y", "yes", "tak", "t")
|
||||
except (KeyboardInterrupt, EOFError):
|
||||
print("\nAborted.")
|
||||
return False
|
||||
|
||||
|
||||
def trigger_system_reboot(
|
||||
distro_id: str,
|
||||
is_release_upgrade: bool = False,
|
||||
runner: Optional[CommandRunner] = None,
|
||||
) -> bool:
|
||||
import shutil
|
||||
if runner is None:
|
||||
runner = CommandRunner()
|
||||
|
||||
cmd_candidates: List[List[str]] = []
|
||||
if is_release_upgrade and distro_id.lower() == "fedora":
|
||||
cmd_candidates.append(["dnf5", "offline", "reboot"])
|
||||
cmd_candidates.append(["dnf", "system-upgrade", "reboot"])
|
||||
|
||||
cmd_candidates.append(["systemctl", "reboot"])
|
||||
cmd_candidates.append(["reboot"])
|
||||
|
||||
for cmd in cmd_candidates:
|
||||
exe = shutil.which(cmd[0]) or cmd[0]
|
||||
try:
|
||||
res = runner.run([exe] + cmd[1:])
|
||||
if res.success:
|
||||
return True
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def check_root_privileges() -> bool:
|
||||
if hasattr(os, "geteuid"):
|
||||
return os.geteuid() == 0
|
||||
@@ -310,6 +349,8 @@ def _run(
|
||||
config.non_interactive = True
|
||||
if no_remediation:
|
||||
config.auto_remediate = False
|
||||
if getattr(args, "auto_reboot", False):
|
||||
config.auto_reboot = True
|
||||
|
||||
# Resolve log path according to standards (/var/log/linux-updater/)
|
||||
resolved_log: Optional[Path] = None
|
||||
@@ -472,16 +513,45 @@ def _run(
|
||||
reboot = manager.is_reboot_required()
|
||||
|
||||
if json_output:
|
||||
triggered = False
|
||||
if config.auto_reboot and not dry_run:
|
||||
triggered = trigger_system_reboot(distro.distro_id, is_release_upgrade=False, runner=runner)
|
||||
print(json.dumps({
|
||||
"success": True,
|
||||
"reboot_required": reboot.required,
|
||||
"reboot_reason": reboot.reason,
|
||||
"reboot_triggered": triggered,
|
||||
}, indent=2))
|
||||
else:
|
||||
print("\nUpdates applied successfully!")
|
||||
if reboot.required:
|
||||
if config.auto_reboot:
|
||||
if dry_run:
|
||||
print("[dry-run] Would execute: sudo systemctl reboot")
|
||||
return 0
|
||||
reason_msg = f" ({reboot.reason})" if reboot.required else ""
|
||||
print(f"\nRebooting system automatically (--reboot enabled){reason_msg}...")
|
||||
ok = trigger_system_reboot(distro.distro_id, is_release_upgrade=False, runner=runner)
|
||||
if not ok:
|
||||
print("Error: Failed to initiate system reboot. Please reboot manually.", file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
elif reboot.required:
|
||||
print(f"\n[!] ATTENTION: A system reboot is required ({reboot.reason}).")
|
||||
return 2
|
||||
if not config.non_interactive and not dry_run:
|
||||
prompt_msg = "A system reboot is required to apply changes. Reboot now? [y/N]: "
|
||||
if prompt_confirmation(prompt=prompt_msg, default_yes=False):
|
||||
print("Rebooting system...")
|
||||
ok = trigger_system_reboot(distro.distro_id, is_release_upgrade=False, runner=runner)
|
||||
if not ok:
|
||||
print("Error: Failed to initiate system reboot. Please reboot manually.", file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
else:
|
||||
print("Reboot cancelled. Please reboot your system manually at your earliest convenience.")
|
||||
return 2
|
||||
else:
|
||||
print("Please reboot the system using: sudo systemctl reboot")
|
||||
return 2
|
||||
|
||||
return 0
|
||||
|
||||
@@ -523,7 +593,7 @@ def _run(
|
||||
|
||||
if not config.non_interactive and not dry_run:
|
||||
prompt_msg = f"Are you absolutely sure you want to proceed with upgrade to {target or 'new release'}? [y/N]: "
|
||||
if not prompt_confirmation(prompt=prompt_msg):
|
||||
if not prompt_confirmation(prompt=prompt_msg, default_yes=False):
|
||||
if not json_output:
|
||||
print("Release upgrade cancelled by user.")
|
||||
return 3
|
||||
@@ -544,22 +614,61 @@ def _run(
|
||||
return 1
|
||||
|
||||
reboot = manager.is_reboot_required()
|
||||
is_fedora = distro.distro_id.lower() == "fedora"
|
||||
reboot_reason = (
|
||||
"execute the offline upgrade transaction"
|
||||
if is_fedora
|
||||
else "boot into the new release"
|
||||
)
|
||||
reboot_cmd = (
|
||||
"sudo dnf system-upgrade reboot"
|
||||
if is_fedora
|
||||
else "sudo systemctl reboot"
|
||||
)
|
||||
|
||||
if json_output:
|
||||
triggered = False
|
||||
if config.auto_reboot and not dry_run:
|
||||
triggered = trigger_system_reboot(distro.distro_id, is_release_upgrade=True, runner=runner)
|
||||
print(json.dumps({
|
||||
"success": True,
|
||||
"release_upgrade": True,
|
||||
"target_release": target,
|
||||
"reboot_required": reboot.required,
|
||||
"reboot_required": True if not dry_run else reboot.required,
|
||||
"reboot_triggered": triggered,
|
||||
}, indent=2))
|
||||
else:
|
||||
print("\nRelease upgrade preparation completed successfully!")
|
||||
if distro.distro_id.lower() == "fedora" and not dry_run:
|
||||
print("\n[!] ATTENTION: Fedora offline upgrade packages have been downloaded.")
|
||||
print(" To execute the upgrade and reboot into the new release, run:")
|
||||
print(" sudo dnf system-upgrade reboot")
|
||||
return 2
|
||||
elif reboot.required:
|
||||
print(f"\n[!] ATTENTION: A system reboot is required to boot into the new release ({reboot.reason}).")
|
||||
if dry_run:
|
||||
if config.auto_reboot:
|
||||
print(f"[dry-run] Would execute: {reboot_cmd}")
|
||||
else:
|
||||
print(f"[dry-run] A system reboot would be required ({reboot_cmd}).")
|
||||
return 0
|
||||
|
||||
if config.auto_reboot:
|
||||
print(f"\nRebooting system automatically to {reboot_reason} (--reboot enabled)...")
|
||||
ok = trigger_system_reboot(distro.distro_id, is_release_upgrade=True, runner=runner)
|
||||
if not ok:
|
||||
print("Error: Failed to initiate system reboot. Please reboot manually.", file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
elif not config.non_interactive:
|
||||
prompt_msg = f"\nA system reboot is required to {reboot_reason}.\nReboot now? [y/N]: "
|
||||
if prompt_confirmation(prompt=prompt_msg, default_yes=False):
|
||||
print("Rebooting system...")
|
||||
ok = trigger_system_reboot(distro.distro_id, is_release_upgrade=True, runner=runner)
|
||||
if not ok:
|
||||
print("Error: Failed to initiate system reboot. Please reboot manually.", file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
else:
|
||||
print(f"\n[!] ATTENTION: A system reboot is required.")
|
||||
print(f" To complete the upgrade, run: {reboot_cmd}")
|
||||
return 2
|
||||
else:
|
||||
print(f"\n[!] ATTENTION: A system reboot is required to {reboot_reason}.")
|
||||
print(f" To execute the upgrade, run: {reboot_cmd}")
|
||||
return 2
|
||||
|
||||
return 0
|
||||
|
||||
Reference in new issue
Block a user