feat: implement slice 2 pre-flight health checks and auto-remediation engine
This commit is contained in:
1 parent
63c6cc846b
commit
62ce7e63f8
7 files changed
+780
-38
No files matched your search
+96
-37
@@ -1,17 +1,54 @@
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import List, Optional
|
||||
|
||||
from linux_updater import __version__
|
||||
from linux_updater.config import UpdaterConfig
|
||||
from linux_updater.core.detector import DistroDetector
|
||||
from linux_updater.models import DistroFamily
|
||||
|
||||
|
||||
def create_parser() -> argparse.ArgumentParser:
|
||||
common = argparse.ArgumentParser(add_help=False)
|
||||
common.add_argument(
|
||||
"-c", "--config",
|
||||
type=str,
|
||||
default="/etc/linux-updater.json",
|
||||
help="Path to JSON configuration file.",
|
||||
)
|
||||
common.add_argument(
|
||||
"-y", "--yes", "--non-interactive",
|
||||
dest="non_interactive",
|
||||
action="store_true",
|
||||
help="Run without interactive confirmation prompts (ideal for cron and automation).",
|
||||
)
|
||||
common.add_argument(
|
||||
"--dry-run",
|
||||
action="store_true",
|
||||
help="Simulate execution without modifying system state.",
|
||||
)
|
||||
common.add_argument(
|
||||
"--no-remediation",
|
||||
action="store_true",
|
||||
help="Disable automatic remediation of failed pre-flight health checks.",
|
||||
)
|
||||
common.add_argument(
|
||||
"--skip-preflight",
|
||||
action="store_true",
|
||||
help="Skip pre-flight health checks (not recommended).",
|
||||
)
|
||||
common.add_argument(
|
||||
"--json",
|
||||
action="store_true",
|
||||
help="Output structured JSON results.",
|
||||
)
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="linux-updater",
|
||||
description="Unified, safe multi-distribution Linux system updater and health inspector.",
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
parents=[common],
|
||||
)
|
||||
parser.add_argument(
|
||||
"-v", "--version",
|
||||
@@ -19,44 +56,13 @@ def create_parser() -> argparse.ArgumentParser:
|
||||
version=f"%(prog)s {__version__}",
|
||||
help="Show program's version number and exit.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c", "--config",
|
||||
type=str,
|
||||
default="/etc/linux-updater.json",
|
||||
help="Path to JSON configuration file.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-y", "--yes", "--non-interactive",
|
||||
dest="non_interactive",
|
||||
action="store_true",
|
||||
help="Run without interactive confirmation prompts (ideal for cron and automation).",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--dry-run",
|
||||
action="store_true",
|
||||
help="Simulate execution without modifying system state.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-remediation",
|
||||
action="store_true",
|
||||
help="Disable automatic remediation of failed pre-flight health checks.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--skip-preflight",
|
||||
action="store_true",
|
||||
help="Skip pre-flight health checks (not recommended).",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--json",
|
||||
action="store_true",
|
||||
help="Output structured JSON results.",
|
||||
)
|
||||
|
||||
subparsers = parser.add_subparsers(dest="command", help="Available subcommands")
|
||||
|
||||
# Subcommand: update
|
||||
update_parser = subparsers.add_parser(
|
||||
"update",
|
||||
parents=[common],
|
||||
help="Inspect health, auto-remediate safe issues, and apply package updates.",
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
)
|
||||
@@ -70,6 +76,7 @@ def create_parser() -> argparse.ArgumentParser:
|
||||
# Subcommand: check
|
||||
subparsers.add_parser(
|
||||
"check",
|
||||
parents=[common],
|
||||
help="Check system health and list available updates without making modifications.",
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
)
|
||||
@@ -77,6 +84,7 @@ def create_parser() -> argparse.ArgumentParser:
|
||||
# Subcommand: release-upgrade
|
||||
release_parser = subparsers.add_parser(
|
||||
"release-upgrade",
|
||||
parents=[common],
|
||||
help="Perform a major distribution release upgrade with additional safety guardrails.",
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
)
|
||||
@@ -89,6 +97,7 @@ def create_parser() -> argparse.ArgumentParser:
|
||||
# Subcommand: clean
|
||||
clean_parser = subparsers.add_parser(
|
||||
"clean",
|
||||
parents=[common],
|
||||
help="Clean downloaded package cache and remove orphaned packages.",
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
)
|
||||
@@ -101,6 +110,7 @@ def create_parser() -> argparse.ArgumentParser:
|
||||
# Subcommand: health
|
||||
subparsers.add_parser(
|
||||
"health",
|
||||
parents=[common],
|
||||
help="Run only the pre-flight health diagnostic checks.",
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
)
|
||||
@@ -112,7 +122,6 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
parser = create_parser()
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
# If no subcommand provided, default to update or show help if no args given
|
||||
if args.command is None:
|
||||
args.command = "update"
|
||||
|
||||
@@ -123,10 +132,60 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
print(f"Linux System Updater v{__version__}")
|
||||
print(f"Detected System: {distro.pretty_name} (Family: {distro.family.value})")
|
||||
|
||||
if distro.family == DistroFamily.UNKNOWN:
|
||||
if not args.json:
|
||||
print(f"Error: Unsupported or unrecognized Linux distribution: {distro.distro_id}", file=sys.stderr)
|
||||
return 1
|
||||
config = UpdaterConfig.load(Path(args.config) if args.config else None)
|
||||
if args.non_interactive:
|
||||
config.non_interactive = True
|
||||
if args.no_remediation:
|
||||
config.auto_remediate = False
|
||||
|
||||
# Create dummy manager for health checks when specific manager isn't wired yet
|
||||
from linux_updater.managers.base import PackageManager
|
||||
from linux_updater.models import RebootStatus
|
||||
|
||||
class FallbackManager(PackageManager):
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return distro.family.value
|
||||
def refresh_repositories(self): pass
|
||||
def check_updates(self): return []
|
||||
def apply_updates(self, packages=None, dry_run=False, non_interactive=False): pass
|
||||
def release_upgrade(self, target_release=None, non_interactive=False): pass
|
||||
def clean_cache(self): pass
|
||||
def autoremove(self): pass
|
||||
def is_reboot_required(self) -> RebootStatus:
|
||||
reboot_p = Path("/var/run/reboot-required")
|
||||
return RebootStatus(required=reboot_p.exists(), reason="reboot-required flag found" if reboot_p.exists() else "")
|
||||
def get_lock_files(self) -> List[Path]:
|
||||
if distro.family == DistroFamily.DEBIAN:
|
||||
return [Path("/var/lib/dpkg/lock-frontend"), Path("/var/lib/dpkg/lock")]
|
||||
elif distro.family == DistroFamily.RHEL:
|
||||
return [Path("/var/lib/rpm/.rpm.lock")]
|
||||
elif distro.family == DistroFamily.ARCH:
|
||||
return [Path("/var/lib/pacman/db.lck")]
|
||||
return []
|
||||
|
||||
manager = FallbackManager()
|
||||
from linux_updater.core.health import HealthChecker
|
||||
checker = HealthChecker(config=config, manager=manager)
|
||||
|
||||
if args.command == "health":
|
||||
report = checker.run_all_checks()
|
||||
if args.json:
|
||||
import json
|
||||
print(json.dumps(report.to_dict(), indent=2))
|
||||
else:
|
||||
print("\nPre-flight Health Diagnostics:")
|
||||
print("-" * 65)
|
||||
for res in report.results:
|
||||
status_symbol = "[ PASS ]" if res.status.value == "PASS" else (
|
||||
"[ WARN ]" if res.status.value == "WARN" else "[ FAIL ]"
|
||||
)
|
||||
print(f"{status_symbol} {res.name:<22} {res.message}")
|
||||
print("-" * 65)
|
||||
status_text = "PASSED" if report.passed else "FAILED"
|
||||
print(f"Overall Pre-flight Status: {status_text}\n")
|
||||
|
||||
return 0 if report.passed else 1
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
Reference in new issue
Block a user