Add dynamic apt-cacher-ng configuration system for collaborators
Some checks are pending
Checks / Spelling (push) Waiting to run
Checks / Python Linters (push) Waiting to run
Checks / Shell Linters (push) Waiting to run
Checks / 📦 Packit config lint (push) Waiting to run
Checks / 🔍 Check for valid snapshot urls (push) Waiting to run
Checks / 🔍 Check JSON files for formatting consistency (push) Waiting to run
Generate / Documentation (push) Waiting to run
Generate / Test Data (push) Waiting to run
Tests / Unittest (push) Waiting to run
Tests / Assembler test (legacy) (push) Waiting to run
Tests / Smoke run: unittest as normal user on default runner (push) Waiting to run
Some checks are pending
Checks / Spelling (push) Waiting to run
Checks / Python Linters (push) Waiting to run
Checks / Shell Linters (push) Waiting to run
Checks / 📦 Packit config lint (push) Waiting to run
Checks / 🔍 Check for valid snapshot urls (push) Waiting to run
Checks / 🔍 Check JSON files for formatting consistency (push) Waiting to run
Generate / Documentation (push) Waiting to run
Generate / Test Data (push) Waiting to run
Tests / Unittest (push) Waiting to run
Tests / Assembler test (legacy) (push) Waiting to run
Tests / Smoke run: unittest as normal user on default runner (push) Waiting to run
This commit is contained in:
parent
39abab18f7
commit
eb18f1a514
7 changed files with 570 additions and 25 deletions
140
osbuild/config_manager.py
Normal file
140
osbuild/config_manager.py
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Configuration Manager for Debian Forge
|
||||
Handles reading configuration from multiple sources with priority order
|
||||
"""
|
||||
|
||||
import os
|
||||
import configparser
|
||||
from pathlib import Path
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
|
||||
class DebianForgeConfig:
|
||||
"""Configuration manager for Debian Forge settings"""
|
||||
|
||||
def __init__(self, config_dir: str = "config"):
|
||||
self.config_dir = Path(config_dir)
|
||||
self.config = configparser.ConfigParser()
|
||||
self._load_config()
|
||||
|
||||
def _load_config(self):
|
||||
"""Load configuration from multiple sources with priority order"""
|
||||
# Priority order (highest to lowest):
|
||||
# 1. Environment variables
|
||||
# 2. Local config file (debian-forge.local.conf)
|
||||
# 3. Default config file (debian-forge.conf)
|
||||
|
||||
# Load default config
|
||||
default_config = self.config_dir / "debian-forge.conf"
|
||||
if default_config.exists():
|
||||
self.config.read(default_config)
|
||||
|
||||
# Load local config (overrides defaults)
|
||||
local_config = self.config_dir / "debian-forge.local.conf"
|
||||
if local_config.exists():
|
||||
self.config.read(local_config)
|
||||
|
||||
# Environment variables override everything
|
||||
self._apply_environment_overrides()
|
||||
|
||||
def _apply_environment_overrides(self):
|
||||
"""Apply environment variable overrides"""
|
||||
# apt-cacher-ng proxy
|
||||
env_proxy = os.environ.get("DEBIAN_FORGE_APT_PROXY")
|
||||
if env_proxy is not None:
|
||||
if "apt-cacher-ng" not in self.config:
|
||||
self.config.add_section("apt-cacher-ng")
|
||||
self.config["apt-cacher-ng"]["url"] = env_proxy
|
||||
|
||||
# Build settings
|
||||
env_suite = os.environ.get("DEBIAN_FORGE_DEFAULT_SUITE")
|
||||
if env_suite is not None:
|
||||
if "build" not in self.config:
|
||||
self.config.add_section("build")
|
||||
self.config["build"]["default_suite"] = env_suite
|
||||
|
||||
env_arch = os.environ.get("DEBIAN_FORGE_DEFAULT_ARCH")
|
||||
if env_arch is not None:
|
||||
if "build" not in self.config:
|
||||
self.config.add_section("build")
|
||||
self.config["build"]["default_arch"] = env_arch
|
||||
|
||||
def get_apt_proxy(self) -> Optional[str]:
|
||||
"""Get apt-cacher-ng proxy URL if configured"""
|
||||
try:
|
||||
url = self.config.get("apt-cacher-ng", "url", fallback="")
|
||||
return url.strip() if url.strip() else None
|
||||
except (configparser.NoSectionError, configparser.NoOptionError):
|
||||
return None
|
||||
|
||||
def get_build_setting(self, key: str, default: Any = None) -> Any:
|
||||
"""Get a build setting value"""
|
||||
try:
|
||||
return self.config.get("build", key, fallback=default)
|
||||
except (configparser.NoSectionError, configparser.NoOptionError):
|
||||
return default
|
||||
|
||||
def get_stage_setting(self, key: str, default: Any = None) -> Any:
|
||||
"""Get a stage setting value"""
|
||||
try:
|
||||
return self.config.get("stages", key, fallback=default)
|
||||
except (configparser.NoSectionError, configparser.NoOptionError):
|
||||
return default
|
||||
|
||||
def get_logging_setting(self, key: str, default: Any = None) -> Any:
|
||||
"""Get a logging setting value"""
|
||||
try:
|
||||
return self.config.get("logging", key, fallback=default)
|
||||
except (configparser.NoSectionError, configparser.NoOptionError):
|
||||
return default
|
||||
|
||||
def is_apt_proxy_enabled(self) -> bool:
|
||||
"""Check if apt-cacher-ng proxy is enabled"""
|
||||
return self.get_apt_proxy() is not None
|
||||
|
||||
def get_all_settings(self) -> Dict[str, Dict[str, Any]]:
|
||||
"""Get all configuration settings as a dictionary"""
|
||||
result = {}
|
||||
for section in self.config.sections():
|
||||
result[section] = dict(self.config[section])
|
||||
return result
|
||||
|
||||
def print_config(self):
|
||||
"""Print current configuration for debugging"""
|
||||
print("Debian Forge Configuration:")
|
||||
print("=" * 40)
|
||||
for section, settings in self.get_all_settings().items():
|
||||
print(f"\n[{section}]")
|
||||
for key, value in settings.items():
|
||||
print(f" {key} = {value}")
|
||||
|
||||
# Show environment overrides
|
||||
env_proxy = os.environ.get("DEBIAN_FORGE_APT_PROXY")
|
||||
if env_proxy:
|
||||
print(f"\nEnvironment Override:")
|
||||
print(f" DEBIAN_FORGE_APT_PROXY = {env_proxy}")
|
||||
|
||||
|
||||
# Global configuration instance
|
||||
config = DebianForgeConfig()
|
||||
|
||||
|
||||
def get_apt_proxy() -> Optional[str]:
|
||||
"""Get apt-cacher-ng proxy URL (convenience function)"""
|
||||
return config.get_apt_proxy()
|
||||
|
||||
|
||||
def is_apt_proxy_enabled() -> bool:
|
||||
"""Check if apt-cacher-ng proxy is enabled (convenience function)"""
|
||||
return config.is_apt_proxy_enabled()
|
||||
|
||||
|
||||
def get_build_setting(key: str, default: Any = None) -> Any:
|
||||
"""Get a build setting (convenience function)"""
|
||||
return config.get_build_setting(key, default)
|
||||
|
||||
|
||||
def get_stage_setting(key: str, default: Any = None) -> Any:
|
||||
"""Get a stage setting (convenience function)"""
|
||||
return config.get_stage_setting(key, default)
|
||||
Loading…
Add table
Add a link
Reference in a new issue