mirror of
https://github.com/d9pouces/id_configurator.git
synced 2026-11-09 01:47:21 +01:00
4409c38579
Signed-off-by: d9pouces <github@19pouces.net>
82 lines
2.8 KiB
Python
82 lines
2.8 KiB
Python
"""Detect existing APK (Alpine) repositories and configure new ones.
|
|
|
|
If `<src>/etc/apk` exists, the existing `/etc/apk/repositories` file (if any)
|
|
is copied into `<dst>` with every active line disabled (commented out), and
|
|
the repositories declared in the configuration are appended as new, active
|
|
lines. Public keys provided alongside a repository are written to
|
|
`<dst>/etc/apk/keys/`, named after their content so that repositories
|
|
sharing the same key (a common case for official Alpine mirrors) only
|
|
produce a single key file.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
from id_configurator.context import Context
|
|
from id_configurator.modules.base import Module, register
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_APK_DIR = "etc/apk"
|
|
_REPOSITORIES = "etc/apk/repositories"
|
|
_KEYS_DIR = "etc/apk/keys"
|
|
|
|
|
|
def _disable_repositories_content(content: str) -> str:
|
|
lines = [
|
|
line if not line.strip() or line.strip().startswith("#") else f"# {line}"
|
|
for line in content.splitlines()
|
|
]
|
|
return "\n".join(lines) + "\n"
|
|
|
|
|
|
def _key_filename(content: bytes) -> str:
|
|
return f"{hashlib.sha256(content).hexdigest()[:16]}.rsa.pub"
|
|
|
|
|
|
@register
|
|
class ApkModule(Module):
|
|
name = "apk"
|
|
|
|
def is_relevant(self, context: Context) -> bool:
|
|
return self._src_apk_dir(context) is not None
|
|
|
|
@staticmethod
|
|
def _src_apk_dir(context: Context) -> Path | None:
|
|
path = context.src_path(_APK_DIR)
|
|
if path is not None and path.is_dir():
|
|
return path
|
|
return None
|
|
|
|
def apply(self, context: Context) -> None:
|
|
disabled = self._disabled_existing_repositories(context)
|
|
configured = self._configured_repositories_lines(context)
|
|
content = disabled + ("\n".join(configured) + "\n" if configured else "")
|
|
if content:
|
|
context.write_file(_REPOSITORIES, content.encode("utf-8"))
|
|
|
|
@staticmethod
|
|
def _disabled_existing_repositories(context: Context) -> str:
|
|
existing = context.read_src_file(_REPOSITORIES)
|
|
if existing is None:
|
|
return ""
|
|
logger.info("disabled existing apk repositories %s", _REPOSITORIES)
|
|
return _disable_repositories_content(existing.decode("utf-8"))
|
|
|
|
@staticmethod
|
|
def _configured_repositories_lines(context: Context) -> list[str]:
|
|
lines = []
|
|
for repository in context.config.configurations.apk.repositories:
|
|
lines.append(repository.url)
|
|
logger.info("added apk repository %s", repository.name)
|
|
if repository.key:
|
|
filename = _key_filename(repository.key.encode("utf-8"))
|
|
context.write_file(
|
|
f"{_KEYS_DIR}/{filename}", repository.key.encode("utf-8")
|
|
)
|
|
logger.info("installed apk key %s", filename)
|
|
return lines
|