81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
import json
|
|
from pathlib import Path
|
|
from typing import Optional, Dict, Any
|
|
|
|
|
|
class UpdaterConfig:
|
|
def __init__(
|
|
self,
|
|
min_boot_mb: int = 250,
|
|
min_var_mb: int = 1024,
|
|
min_root_mb: int = 1024,
|
|
min_ram_mb: int = 300,
|
|
auto_remediate: bool = True,
|
|
non_interactive: bool = False,
|
|
check_ac_power: bool = True,
|
|
clean_after_update: bool = False,
|
|
log_file: Optional[str] = "/var/log/linux-updater.log",
|
|
verbose: bool = False,
|
|
) -> None:
|
|
self.min_boot_mb = min_boot_mb
|
|
self.min_var_mb = min_var_mb
|
|
self.min_root_mb = min_root_mb
|
|
self.min_ram_mb = min_ram_mb
|
|
self.auto_remediate = auto_remediate
|
|
self.non_interactive = non_interactive
|
|
self.check_ac_power = check_ac_power
|
|
self.clean_after_update = clean_after_update
|
|
self.log_file = log_file
|
|
self.verbose = verbose
|
|
|
|
@classmethod
|
|
def load(cls, config_path: Optional[Path] = None) -> "UpdaterConfig":
|
|
if config_path is None:
|
|
config_path = Path("/etc/linux-updater.json")
|
|
|
|
if not config_path.exists():
|
|
return cls()
|
|
|
|
try:
|
|
with open(str(config_path), "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
except Exception:
|
|
return cls()
|
|
|
|
thresholds = data.get("thresholds", {})
|
|
behavior = data.get("behavior", {})
|
|
logging = data.get("logging", {})
|
|
|
|
return cls(
|
|
min_boot_mb=thresholds.get("min_boot_mb", 250),
|
|
min_var_mb=thresholds.get("min_var_mb", 1024),
|
|
min_root_mb=thresholds.get("min_root_mb", 1024),
|
|
min_ram_mb=thresholds.get("min_ram_mb", 300),
|
|
auto_remediate=behavior.get("auto_remediate", True),
|
|
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"),
|
|
verbose=logging.get("verbose", False),
|
|
)
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return {
|
|
"thresholds": {
|
|
"min_boot_mb": self.min_boot_mb,
|
|
"min_var_mb": self.min_var_mb,
|
|
"min_root_mb": self.min_root_mb,
|
|
"min_ram_mb": self.min_ram_mb,
|
|
},
|
|
"behavior": {
|
|
"auto_remediate": self.auto_remediate,
|
|
"non_interactive": self.non_interactive,
|
|
"check_ac_power": self.check_ac_power,
|
|
"clean_after_update": self.clean_after_update,
|
|
},
|
|
"logging": {
|
|
"log_file": self.log_file,
|
|
"verbose": self.verbose,
|
|
},
|
|
}
|