Files
mirror/app/services/host.py
T
2026-06-03 17:33:19 +03:30

61 lines
1.5 KiB
Python

from __future__ import annotations
import logging
import os
import shutil
import subprocess
from datetime import datetime
from pathlib import Path
from app.config import Config
logger = logging.getLogger(__name__)
def host_path(*parts: str) -> Path:
return Config.host_path(*parts)
def is_container_mode() -> bool:
return bool(Config.HOST_ROOT) and Path(Config.HOST_ROOT).exists()
def run_on_host(command: list[str], timeout: int = 120) -> subprocess.CompletedProcess:
"""Run a command on the host when inside a privileged container."""
if is_container_mode():
full_cmd = ["nsenter", "-t", "1", "-m", "-u", "-i", "-n", "-p", "--"] + command
else:
full_cmd = command
return subprocess.run(
full_cmd,
capture_output=True,
text=True,
timeout=timeout,
check=False,
)
def get_ubuntu_codename() -> str:
result = run_on_host(["lsb_release", "-cs"])
if result.returncode == 0:
return result.stdout.strip()
return "noble"
def get_ubuntu_version() -> str:
result = run_on_host(["lsb_release", "-rs"])
if result.returncode == 0:
return result.stdout.strip()
return "24.04"
SUPPORTED_CODENAMES = {"noble", "jammy", "bionic"}
def validate_codename(codename: str) -> str:
if codename in SUPPORTED_CODENAMES:
return codename
mapping = {"24.04": "noble", "22.04": "jammy", "18.04": "bionic"}
version = get_ubuntu_version()
return mapping.get(version, "noble")