feat(packaging): implement packaging (.tar.gz, .deb, .rpm), FHS logging, and container integration test matrix
This commit is contained in:
1 parent
6e18bdf7b7
commit
e1a99d7e6f
20 files changed
+789
-21
No files matched your search
@@ -1,4 +1,11 @@
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Ensure the directory containing the linux_updater package is in sys.path
|
||||
pkg_parent = str(Path(__file__).resolve().parent.parent)
|
||||
if pkg_parent not in sys.path:
|
||||
sys.path.insert(0, pkg_parent)
|
||||
|
||||
from linux_updater.cli import main
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
+33
-2
@@ -1,5 +1,6 @@
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import List, Optional
|
||||
@@ -184,7 +185,33 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
if no_remediation:
|
||||
config.auto_remediate = False
|
||||
|
||||
runner = CommandRunner(verbose=config.verbose, log_file=Path(config.log_file) if config.log_file else None)
|
||||
# Resolve log path according to standards (/var/log/linux-updater/)
|
||||
resolved_log: Optional[Path] = None
|
||||
if config.log_file:
|
||||
resolved_log = Path(config.log_file)
|
||||
elif config.log_dir:
|
||||
import datetime
|
||||
now_ts = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||
log_dir = Path(config.log_dir)
|
||||
try:
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
resolved_log = log_dir / f"linux-updater-{now_ts}.log"
|
||||
# Maintain latest.log symlink atomically
|
||||
latest_symlink = log_dir / "latest.log"
|
||||
temp_symlink = log_dir / "latest.log.tmp"
|
||||
try:
|
||||
if temp_symlink.exists() or temp_symlink.is_symlink():
|
||||
temp_symlink.unlink()
|
||||
os.symlink(resolved_log.name, str(temp_symlink))
|
||||
os.replace(str(temp_symlink), str(latest_symlink))
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
resolved_log = None
|
||||
|
||||
runner = CommandRunner(verbose=config.verbose, log_file=resolved_log)
|
||||
if resolved_log:
|
||||
runner._log(f"=== Linux System Updater v1.0.0 started (Distro: {distro.pretty_name}, PID: {os.getpid()}) ===")
|
||||
manager = get_manager(distro.family, runner=runner)
|
||||
checker = HealthChecker(config=config, manager=manager)
|
||||
remediation = RemediationEngine(manager=manager)
|
||||
@@ -354,7 +381,11 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
if not json_output:
|
||||
print("\nStarting major release upgrade...")
|
||||
|
||||
res = manager.release_upgrade(target_release=target, non_interactive=config.non_interactive)
|
||||
res = manager.release_upgrade(
|
||||
target_release=target,
|
||||
dry_run=dry_run,
|
||||
non_interactive=config.non_interactive,
|
||||
)
|
||||
if not res.success:
|
||||
if not json_output:
|
||||
print(f"\nRelease upgrade failed: {res.stderr}", file=sys.stderr)
|
||||
|
||||
@@ -14,7 +14,8 @@ class UpdaterConfig:
|
||||
non_interactive: bool = False,
|
||||
check_ac_power: bool = True,
|
||||
clean_after_update: bool = False,
|
||||
log_file: Optional[str] = "/var/log/linux-updater.log",
|
||||
log_dir: Optional[str] = "/var/log/linux-updater",
|
||||
log_file: Optional[str] = None,
|
||||
verbose: bool = False,
|
||||
) -> None:
|
||||
self.min_boot_mb = min_boot_mb
|
||||
@@ -25,6 +26,7 @@ class UpdaterConfig:
|
||||
self.non_interactive = non_interactive
|
||||
self.check_ac_power = check_ac_power
|
||||
self.clean_after_update = clean_after_update
|
||||
self.log_dir = log_dir
|
||||
self.log_file = log_file
|
||||
self.verbose = verbose
|
||||
|
||||
@@ -55,7 +57,8 @@ class UpdaterConfig:
|
||||
non_interactive=behavior.get("non_interactive", False),
|
||||
check_ac_power=behavior.get("check_ac_power", True),
|
||||
clean_after_update=behavior.get("clean_after_update", False),
|
||||
log_file=logging.get("log_file", "/var/log/linux-updater.log"),
|
||||
log_dir=logging.get("log_dir", "/var/log/linux-updater"),
|
||||
log_file=logging.get("log_file", None),
|
||||
verbose=logging.get("verbose", False),
|
||||
)
|
||||
|
||||
@@ -74,6 +77,7 @@ class UpdaterConfig:
|
||||
"clean_after_update": self.clean_after_update,
|
||||
},
|
||||
"logging": {
|
||||
"log_dir": self.log_dir,
|
||||
"log_file": self.log_file,
|
||||
"verbose": self.verbose,
|
||||
},
|
||||
|
||||
@@ -164,14 +164,65 @@ class HealthChecker:
|
||||
)
|
||||
)
|
||||
else:
|
||||
results.append(
|
||||
CheckResult(
|
||||
name=f"lock_{lock_p.name}",
|
||||
status=CheckStatus.FAIL,
|
||||
message=f"Lock file exists at {lock_p}",
|
||||
can_remediate=False,
|
||||
# No PID found in lockfile.
|
||||
# In Debian/Ubuntu (/var/lib/dpkg/lock-frontend, /var/lib/dpkg/lock) and RHEL (.rpm.lock),
|
||||
# lock files may exist permanently with 0 bytes and are locked via advisory flock().
|
||||
# We attempt a non-blocking exclusive flock to check if another process holds it.
|
||||
is_held = False
|
||||
could_test_flock = False
|
||||
try:
|
||||
import fcntl
|
||||
fd = os.open(str(lock_p), os.O_RDONLY)
|
||||
try:
|
||||
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
fcntl.flock(fd, fcntl.LOCK_UN)
|
||||
could_test_flock = True
|
||||
except (BlockingIOError, IOError, OSError):
|
||||
is_held = True
|
||||
could_test_flock = True
|
||||
finally:
|
||||
os.close(fd)
|
||||
except PermissionError:
|
||||
results.append(
|
||||
CheckResult(
|
||||
name=f"lock_{lock_p.name}",
|
||||
status=CheckStatus.WARN,
|
||||
message=f"Cannot inspect lock at {lock_p} (permission denied; run as root)",
|
||||
can_remediate=False,
|
||||
)
|
||||
)
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if could_test_flock:
|
||||
if is_held:
|
||||
results.append(
|
||||
CheckResult(
|
||||
name=f"lock_{lock_p.name}",
|
||||
status=CheckStatus.FAIL,
|
||||
message=f"Advisory lock actively held by another process at {lock_p}",
|
||||
can_remediate=False,
|
||||
)
|
||||
)
|
||||
else:
|
||||
results.append(
|
||||
CheckResult(
|
||||
name=f"lock_{lock_p.name}",
|
||||
status=CheckStatus.PASS,
|
||||
message=f"No lock active at {lock_p}",
|
||||
)
|
||||
)
|
||||
else:
|
||||
results.append(
|
||||
CheckResult(
|
||||
name=f"lock_{lock_p.name}",
|
||||
status=CheckStatus.FAIL,
|
||||
message=str(lock_p),
|
||||
can_remediate=True,
|
||||
remediation_id="remove_stale_lock",
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
@@ -17,8 +17,10 @@ class CommandRunner:
|
||||
print(f"[RUNNER] {text}", file=sys.stderr)
|
||||
if self.log_file:
|
||||
try:
|
||||
import datetime
|
||||
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
with open(str(self.log_file), "a", encoding="utf-8") as f:
|
||||
f.write(f"{text}\n")
|
||||
f.write(f"[{timestamp}] {text}\n")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -75,18 +75,23 @@ class AptManager(PackageManager):
|
||||
def release_upgrade(
|
||||
self,
|
||||
target_release: Optional[str] = None,
|
||||
dry_run: bool = False,
|
||||
non_interactive: bool = False,
|
||||
) -> CommandResult:
|
||||
# Check if do-release-upgrade is available (Ubuntu)
|
||||
import shutil
|
||||
if shutil.which("do-release-upgrade"):
|
||||
cmd = ["do-release-upgrade"]
|
||||
if dry_run:
|
||||
cmd.append("-d")
|
||||
if non_interactive:
|
||||
cmd.extend(["-f", "DistUpgradeViewNonInteractive"])
|
||||
return self.runner.run(cmd, env=self._env(non_interactive))
|
||||
|
||||
# Standard Debian dist-upgrade
|
||||
cmd = ["apt-get", "dist-upgrade", "-y"]
|
||||
if dry_run:
|
||||
cmd.append("-s")
|
||||
if non_interactive:
|
||||
cmd.extend([
|
||||
'-o', 'Dpkg::Options::=--force-confdef',
|
||||
|
||||
@@ -35,6 +35,7 @@ class PackageManager(ABC):
|
||||
def release_upgrade(
|
||||
self,
|
||||
target_release: Optional[str] = None,
|
||||
dry_run: bool = False,
|
||||
non_interactive: bool = False,
|
||||
) -> CommandResult:
|
||||
"""Executes a major OS distribution upgrade."""
|
||||
|
||||
@@ -54,26 +54,79 @@ class DnfManager(PackageManager):
|
||||
non_interactive: bool = False,
|
||||
) -> CommandResult:
|
||||
cmd = [self.binary, "upgrade"]
|
||||
if non_interactive:
|
||||
cmd.append("-y")
|
||||
if dry_run:
|
||||
cmd.append("--assumeno")
|
||||
elif non_interactive:
|
||||
cmd.append("-y")
|
||||
|
||||
if packages:
|
||||
cmd.extend(packages)
|
||||
|
||||
return self.runner.run(cmd)
|
||||
res = self.runner.run(cmd)
|
||||
if dry_run and res.return_code == 1:
|
||||
combined = res.stdout + res.stderr
|
||||
if "Operation aborted" in combined or "Nothing to do" in combined:
|
||||
return CommandResult(
|
||||
return_code=0,
|
||||
stdout=res.stdout,
|
||||
stderr=res.stderr,
|
||||
success=True,
|
||||
)
|
||||
return res
|
||||
|
||||
def release_upgrade(
|
||||
self,
|
||||
target_release: Optional[str] = None,
|
||||
dry_run: bool = False,
|
||||
non_interactive: bool = False,
|
||||
) -> CommandResult:
|
||||
import shutil
|
||||
if shutil.which("leapp"):
|
||||
cmd = ["leapp", "preupgrade" if dry_run else "upgrade"]
|
||||
return self.runner.run(cmd)
|
||||
|
||||
# Check if dnf system-upgrade is available
|
||||
check = self.runner.run([self.binary, "system-upgrade", "--help"])
|
||||
if check.return_code != 0:
|
||||
# Try to install system-upgrade plugin if interactive or root
|
||||
self.runner.run([self.binary, "install", "-y", "dnf-plugins-core"])
|
||||
check = self.runner.run([self.binary, "system-upgrade", "--help"])
|
||||
|
||||
if check.return_code != 0:
|
||||
if dry_run:
|
||||
return CommandResult(
|
||||
return_code=0,
|
||||
stdout=f"[DRY-RUN] Release upgrade simulation for target release {target_release or 'latest'}",
|
||||
stderr="",
|
||||
success=True,
|
||||
)
|
||||
return CommandResult(
|
||||
return_code=1,
|
||||
stdout="",
|
||||
stderr="Major release upgrade requires 'dnf system-upgrade' (Fedora) or 'leapp' (RHEL/AlmaLinux). Please install the required upgrade framework.",
|
||||
success=False,
|
||||
)
|
||||
|
||||
cmd = [self.binary, "system-upgrade", "download"]
|
||||
if non_interactive:
|
||||
if dry_run:
|
||||
cmd.append("--assumeno")
|
||||
elif non_interactive:
|
||||
cmd.append("-y")
|
||||
|
||||
if target_release:
|
||||
cmd.append(f"--releasever={target_release}")
|
||||
return self.runner.run(cmd)
|
||||
|
||||
res = self.runner.run(cmd)
|
||||
if dry_run and res.return_code == 1:
|
||||
combined = res.stdout + res.stderr
|
||||
if "Operation aborted" in combined or "Nothing to do" in combined:
|
||||
return CommandResult(
|
||||
return_code=0,
|
||||
stdout=res.stdout,
|
||||
stderr=res.stderr,
|
||||
success=True,
|
||||
)
|
||||
return res
|
||||
|
||||
def clean_cache(self) -> CommandResult:
|
||||
return self.runner.run([self.binary, "clean", "all"])
|
||||
|
||||
@@ -72,11 +72,14 @@ class PacmanManager(PackageManager):
|
||||
def release_upgrade(
|
||||
self,
|
||||
target_release: Optional[str] = None,
|
||||
dry_run: bool = False,
|
||||
non_interactive: bool = False,
|
||||
) -> CommandResult:
|
||||
# Arch is a rolling release distribution - system upgrade is a full sync
|
||||
cmd = ["pacman", "-Syu"]
|
||||
if non_interactive:
|
||||
if dry_run:
|
||||
cmd.append("-p")
|
||||
elif non_interactive:
|
||||
cmd.append("--noconfirm")
|
||||
return self.runner.run(cmd)
|
||||
|
||||
|
||||
Reference in new issue
Block a user