feat: add root check, singleton lock, RHEL/Fedora upgrade auto-installer & container tests for AlmaLinux 9 and Arch Linux
This commit is contained in:
1 parent
507a3fd310
commit
44c9423166
10 files changed
+711
-27
No files matched your search
+69
-1
@@ -9,6 +9,7 @@ from linux_updater import __version__
|
||||
from linux_updater.config import UpdaterConfig
|
||||
from linux_updater.core.detector import DistroDetector
|
||||
from linux_updater.core.health import HealthChecker
|
||||
from linux_updater.core.lock import InstanceLock
|
||||
from linux_updater.core.remediation import RemediationEngine
|
||||
from linux_updater.core.runner import CommandRunner
|
||||
from linux_updater.managers import get_manager
|
||||
@@ -153,6 +154,12 @@ def prompt_confirmation(prompt: str = "Do you want to proceed with updates? [Y/n
|
||||
return False
|
||||
|
||||
|
||||
def check_root_privileges() -> bool:
|
||||
if hasattr(os, "geteuid"):
|
||||
return os.geteuid() == 0
|
||||
return True
|
||||
|
||||
|
||||
def main(argv: Optional[List[str]] = None) -> int:
|
||||
parser = create_parser()
|
||||
args = parser.parse_args(argv)
|
||||
@@ -179,6 +186,67 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
print(f"Error: Unsupported or unrecognized Linux distribution: {distro.distro_id}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
# Root privilege check for mutating operations
|
||||
mutating_commands = {"update", "release-upgrade", "clean"}
|
||||
if args.command in mutating_commands and not dry_run:
|
||||
if not check_root_privileges():
|
||||
if json_output:
|
||||
print(json.dumps({
|
||||
"success": False,
|
||||
"error": "permission_denied",
|
||||
"message": f"Root privileges (sudo) are required to execute '{args.command}'."
|
||||
}, indent=2))
|
||||
else:
|
||||
print(
|
||||
f"Error: Root privileges (sudo) are required to execute '{args.command}'.\n"
|
||||
f"Please run with: sudo linux-updater {args.command}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
# Concurrency control: singleton instance lock
|
||||
instance_lock = InstanceLock()
|
||||
if not instance_lock.acquire():
|
||||
holder_pid = instance_lock.get_holder_pid()
|
||||
holder_msg = f" (PID {holder_pid})" if holder_pid else ""
|
||||
if json_output:
|
||||
print(json.dumps({
|
||||
"success": False,
|
||||
"error": "already_running",
|
||||
"message": f"Another instance of linux-updater is already running{holder_msg}."
|
||||
}, indent=2))
|
||||
else:
|
||||
print(
|
||||
f"Error: Another instance of linux-updater is already running{holder_msg}. Aborting.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
try:
|
||||
return _run(
|
||||
args=args,
|
||||
distro=distro,
|
||||
non_interactive=non_interactive,
|
||||
dry_run=dry_run,
|
||||
no_remediation=no_remediation,
|
||||
skip_preflight=skip_preflight,
|
||||
json_output=json_output,
|
||||
config_path=config_path,
|
||||
)
|
||||
finally:
|
||||
instance_lock.release()
|
||||
|
||||
|
||||
def _run(
|
||||
args: argparse.Namespace,
|
||||
distro,
|
||||
non_interactive: bool,
|
||||
dry_run: bool,
|
||||
no_remediation: bool,
|
||||
skip_preflight: bool,
|
||||
json_output: bool,
|
||||
config_path: str,
|
||||
) -> int:
|
||||
config = UpdaterConfig.load(Path(config_path) if config_path else None)
|
||||
if non_interactive:
|
||||
config.non_interactive = True
|
||||
@@ -212,7 +280,7 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
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)
|
||||
manager = get_manager(distro.family, runner=runner, distro_info=distro)
|
||||
checker = HealthChecker(config=config, manager=manager)
|
||||
remediation = RemediationEngine(manager=manager)
|
||||
|
||||
|
||||
Reference in new issue
Block a user