195 lines
8.0 KiB
Markdown
195 lines
8.0 KiB
Markdown
# Linux System Updater: Developer Guide
|
|
|
|
This guide provides technical specifications, architectural details, and developer workflows for contributing to `linux-updater`.
|
|
|
|
---
|
|
|
|
## 1. Architectural Overview
|
|
|
|
`linux-updater` follows a clean separation of concerns:
|
|
|
|
```text
|
|
linux_updater/
|
|
├── __init__.py # Package version definition
|
|
├── __main__.py # CLI invocation entrypoint (python3 -m linux_updater)
|
|
├── cli.py # Argument parsing, formatting, execution controller
|
|
├── config.py # JSON configuration loader & default thresholds
|
|
├── models.py # Core data transfer objects (DistroInfo, CheckResult, etc.)
|
|
├── core/
|
|
│ ├── completion.py # Shell autocompletion script generator (Bash & Zsh)
|
|
│ ├── detector.py # Distro detection via /etc/os-release parsing
|
|
│ ├── health.py # Pre-flight health diagnostics & package DB checking
|
|
│ ├── lock.py # Concurrency advisory singleton lock
|
|
│ ├── remediation.py # Automated remediation actions (cache, stale locks, db repair)
|
|
│ └── runner.py # Subprocess runner with timestamped file logging
|
|
└── managers/
|
|
├── __init__.py # get_manager() factory
|
|
├── base.py # PackageManager abstract base class interface
|
|
├── apt.py # Debian/Ubuntu (APT) driver
|
|
├── dnf.py # RHEL/Fedora/CentOS/AlmaLinux/Rocky (DNF) driver
|
|
└── pacman.py # Arch Linux (Pacman) driver
|
|
```
|
|
|
|
---
|
|
|
|
## 2. Hard Constraints & Coding Standards
|
|
|
|
1. **Python >= 3.6 Standard Library Baseline:**
|
|
- **NO** external `pip` dependencies. Zero third-party packages allowed.
|
|
- **NO** `dataclasses` (introduced in Python 3.7). Use standard classes or `NamedTuple`.
|
|
- **NO** `subprocess.run(capture_output=True)` (introduced in Python 3.7). Use `stdout=subprocess.PIPE, stderr=subprocess.PIPE`.
|
|
- **NO** modern union operators like `int | None` (introduced in Python 3.10). Use `Optional[int]`.
|
|
2. **Deterministic Return Codes:**
|
|
- Always map execution outcomes to standard exit codes (`0` clean, `1` error, `2` reboot required, `3` user abort).
|
|
3. **Container-First Verification:**
|
|
- All tests against actual package managers and distributions MUST be run inside clean Podman containers, never on the host system.
|
|
|
|
---
|
|
|
|
## 3. Implementing a New Package Manager Driver
|
|
|
|
To add support for a new package manager (e.g. `zypper` for openSUSE or `apk` for Alpine):
|
|
|
|
1. **Subclass `PackageManager`** in `linux_updater/managers/<name>.py`:
|
|
|
|
```python
|
|
from pathlib import Path
|
|
from typing import List, Optional
|
|
from linux_updater.managers.base import PackageManager
|
|
from linux_updater.models import CommandResult, PackageUpdate, RebootStatus
|
|
|
|
class ZypperManager(PackageManager):
|
|
@property
|
|
def name(self) -> str:
|
|
return "zypper"
|
|
|
|
def refresh_repositories(self) -> CommandResult:
|
|
return self.runner.run(["zypper", "refresh"])
|
|
|
|
def check_updates(self) -> List[PackageUpdate]:
|
|
# Parse 'zypper list-updates'
|
|
...
|
|
|
|
def apply_updates(self, packages=None, dry_run=False, non_interactive=False) -> CommandResult:
|
|
cmd = ["zypper", "update"]
|
|
if dry_run:
|
|
cmd.append("--dry-run")
|
|
if non_interactive:
|
|
cmd.append("-y")
|
|
if packages:
|
|
cmd.extend(packages)
|
|
return self.runner.run(cmd)
|
|
|
|
def release_upgrade(self, target_release=None, dry_run=False, non_interactive=False) -> CommandResult:
|
|
cmd = ["zypper", "dup"]
|
|
if dry_run:
|
|
cmd.append("--dry-run")
|
|
if non_interactive:
|
|
cmd.append("-y")
|
|
return self.runner.run(cmd)
|
|
|
|
def clean_cache(self) -> CommandResult:
|
|
return self.runner.run(["zypper", "clean", "-a"])
|
|
|
|
def autoremove(self) -> CommandResult:
|
|
return self.runner.run(["zypper", "rm", "-u"])
|
|
|
|
def is_reboot_required(self) -> RebootStatus:
|
|
res = self.runner.run(["zypper", "needs-rebooting"])
|
|
return RebootStatus(required=(res.return_code != 0))
|
|
|
|
def get_lock_files(self) -> List[Path]:
|
|
return [Path("/var/run/zypp.pid")]
|
|
```
|
|
|
|
2. **Register in `linux_updater/managers/__init__.py`:**
|
|
```python
|
|
def get_manager(family: str, runner: Optional[CommandRunner] = None) -> PackageManager:
|
|
...
|
|
elif family == "suse":
|
|
return ZypperManager(runner=runner)
|
|
```
|
|
|
|
3. **Add Distro Mapping in `linux_updater/models.py` and `core/detector.py`:**
|
|
Ensure `/etc/os-release` `ID` or `ID_LIKE` values map to the new family.
|
|
|
|
---
|
|
|
|
## 4. Testing & Quality Assurance
|
|
|
|
### Local Unit Tests
|
|
Unit tests use mocks and run locally with Python's built-in `unittest` runner:
|
|
|
|
```bash
|
|
python3 -m unittest discover tests
|
|
```
|
|
|
|
### Container Integration Tests (Podman)
|
|
Automated multi-distro container integration testing is orchestrated by `tests/containers/run_tests.sh`:
|
|
|
|
```bash
|
|
# Test all distributions (Debian 12, Ubuntu 22.04, Fedora 40, AlmaLinux 8):
|
|
./tests/containers/run_tests.sh all
|
|
|
|
# Test a single distribution:
|
|
./tests/containers/run_tests.sh debian
|
|
./tests/containers/run_tests.sh ubuntu
|
|
./tests/containers/run_tests.sh fedora
|
|
./tests/containers/run_tests.sh almalinux8
|
|
```
|
|
|
|
The script automatically mounts `/workspace:Z`, builds `.deb` or `.rpm` packages from source, installs the package inside the container, verifies the binary, runs health checks, tests routine update (dry-run), tests release-upgrade (dry-run), and verifies `/var/log/linux-updater/` logging.
|
|
|
|
---
|
|
|
|
## 5. Building Distribution Packages
|
|
|
|
Run the pure Python builder script:
|
|
|
|
```bash
|
|
python3 packaging/build_packages.py
|
|
```
|
|
|
|
Generated artifacts:
|
|
- `dist/linux-updater-2.0.0.tar.gz`: Standalone distribution archive containing `install.sh`.
|
|
- `dist/linux-updater_2.0.0_all.deb`: Native Debian/Ubuntu binary package.
|
|
- `dist/rpm/RPMS/noarch/linux-updater-2.0.0-1.*.noarch.rpm`: Native RPM package.
|
|
|
|
---
|
|
|
|
## 6. CI/CD Pipeline & Automated Release Workflow (Gitea Actions)
|
|
|
|
`linux-updater` incorporates an automated CI/CD pipeline defined in `.gitea/workflows/release.yml`.
|
|
|
|
### A. Pipeline Architecture & Execution Environment
|
|
- **Runner Host:** VM `giteaworker` on Synology NAS (`192.168.11.2`), running Fedora with `act_runner` in `hostexecutor` mode.
|
|
- **Workflow Tag:** `runs-on: fedora`.
|
|
- **Triggers:**
|
|
- Pushing any semantic version tag matching `v*` (e.g. `v2.0.0`).
|
|
- Manual execution via Gitea Web UI or `tea` CLI (`workflow_dispatch`).
|
|
|
|
### B. Pipeline Stages
|
|
|
|
```mermaid
|
|
flowchart TD
|
|
A["Push Tag v* / Dispatch"] --> B["Checkout & Verify Environment (rpmbuild, python3)"]
|
|
B --> C["Run Unit Test Suite (python3 -m unittest discover tests)"]
|
|
C --> D["Build Distribution Packages (build_packages.py)"]
|
|
D --> E["Upload to Gitea Package Registry (RPM & Debian)"]
|
|
D --> F["Attach Artifacts to Gitea Release (API /releases)"]
|
|
E --> G["Link Packages to Repository (API /packages/.../link)"]
|
|
```
|
|
|
|
1. **Environment Verification:** Verifies Python 3 runtime and ensures `rpmbuild` is installed on the runner.
|
|
2. **Quality Gate:** Runs full unit test suite (`python3 -m unittest discover tests`). Any test regression immediately aborts the pipeline.
|
|
3. **Package Compilation:** Invokes `packaging/build_packages.py` to produce native `.deb`, `.rpm`, and `.tar.gz` packages in `dist/`.
|
|
4. **Publishing to Gitea Package Registry:**
|
|
- RPM: `PUT /api/packages/soda64/rpm/upload`
|
|
- Debian: `PUT /api/packages/soda64/debian/pool/stable/main/upload`
|
|
- Links the uploaded packages to the `linux-updater` repository via `POST /api/v1/packages/soda64/<type>/linux-updater/-/link/linux-updater`.
|
|
- **Secret Requirement:** The upload requires a personal access token with `package:write` scope stored in repository secrets as `PACKAGES_GENERATION`.
|
|
5. **Gitea Release & Asset Attachment:**
|
|
- Queries or creates the Gitea Release corresponding to tag `vX.Y.Z`.
|
|
- Idempotently purges older assets on re-runs to prevent duplicated attachments.
|
|
- Uploads binary artifacts (`.tar.gz`, `.deb`, `.rpm`) as downloadable Release Assets for operators.
|