238 lines
7.0 KiB
Python
238 lines
7.0 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
from acupedia_ops.common.env import load_env_file
|
|
from acupedia_ops.common.paths import repo_root
|
|
from acupedia_ops.common.workspace_paths import (
|
|
resolve_workspace_path,
|
|
workspace_base_path,
|
|
)
|
|
|
|
|
|
PATH_VARIABLES: dict[str, str] = {
|
|
"api": "ACUPEDIA_API_PATH",
|
|
"mobile": "ACUPEDIA_MOBILE_PATH",
|
|
"web": "ACUPEDIA_WEB_PORTAL_PATH",
|
|
"admin": "ACUPEDIA_ADMIN_PORTAL_PATH",
|
|
"content": "ACUPEDIA_CONTENT_PATH",
|
|
"ops": "ACUPEDIA_OPS_PATH",
|
|
}
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ComponentConfig:
|
|
name: str
|
|
path_env: str
|
|
required_markers: tuple[str, ...]
|
|
setup_command: tuple[str, ...]
|
|
run_command: tuple[str, ...]
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LocalInfraConfig:
|
|
compose_file: Path
|
|
project_name: str
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class BootstrapDefaults:
|
|
default_target: str
|
|
content_config: Path
|
|
modules_root: Path
|
|
media_manifest: Path | None
|
|
media_root: Path | None
|
|
commerce_plans: Path | None
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LocalWorkspace:
|
|
base_path: Path
|
|
root: Path
|
|
components: dict[str, ComponentConfig]
|
|
paths: dict[str, Path]
|
|
infra: LocalInfraConfig
|
|
bootstrap: BootstrapDefaults
|
|
database_reset_command: tuple[str, ...]
|
|
|
|
def path(self, component: str) -> Path:
|
|
try:
|
|
return self.paths[component]
|
|
except KeyError as exc:
|
|
raise KeyError(f"Unknown workspace component: {component}") from exc
|
|
|
|
|
|
def _as_command(value: object, *, field: str) -> tuple[str, ...]:
|
|
if value is None:
|
|
return ()
|
|
if not isinstance(value, list) or not all(isinstance(v, str) for v in value):
|
|
raise ValueError(f"{field} must be a YAML list of strings")
|
|
return tuple(value)
|
|
|
|
|
|
def _as_markers(value: object, *, field: str) -> tuple[str, ...]:
|
|
if value is None:
|
|
return ()
|
|
if not isinstance(value, list) or not all(isinstance(v, str) for v in value):
|
|
raise ValueError(f"{field} must be a YAML list of strings")
|
|
return tuple(value)
|
|
|
|
|
|
def _resolve_relative(base: Path, raw: str | None) -> Path | None:
|
|
"""Resolve config-relative content paths against the supplied repo root."""
|
|
if raw is None or not str(raw).strip():
|
|
return None
|
|
|
|
path = Path(str(raw)).expanduser()
|
|
if path.is_absolute():
|
|
return path.resolve()
|
|
|
|
return (base / path).resolve()
|
|
|
|
|
|
def load_local_workspace(
|
|
*,
|
|
env_file: Path | None = None,
|
|
config_file: Path | None = None,
|
|
) -> LocalWorkspace:
|
|
ops_checkout = repo_root()
|
|
load_env_file(env_file or (ops_checkout / ".env"), override=False)
|
|
|
|
base = workspace_base_path(required=True)
|
|
assert base is not None
|
|
|
|
paths: dict[str, Path] = {}
|
|
missing_vars: list[str] = []
|
|
|
|
for component, env_name in PATH_VARIABLES.items():
|
|
value = os.environ.get(env_name, "").strip()
|
|
if not value:
|
|
missing_vars.append(env_name)
|
|
continue
|
|
paths[component] = resolve_workspace_path(value, base_path=base)
|
|
|
|
if missing_vars:
|
|
raise RuntimeError(
|
|
"Missing local workspace path variable(s): " + ", ".join(missing_vars)
|
|
)
|
|
|
|
# config/local.yaml belongs to the ops repository. Prefer the resolved
|
|
# ACUPEDIA_OPS_PATH rather than whichever checkout happened to launch us.
|
|
config_path = config_file or (paths["ops"] / "config" / "local.yaml")
|
|
if not config_path.exists():
|
|
raise FileNotFoundError(f"Local configuration not found: {config_path}")
|
|
|
|
raw = yaml.safe_load(config_path.read_text(encoding="utf-8")) or {}
|
|
if not isinstance(raw, dict):
|
|
raise ValueError("config/local.yaml root must be a mapping")
|
|
|
|
component_raw = raw.get("components", {})
|
|
if not isinstance(component_raw, dict):
|
|
raise ValueError("local.components must be a mapping")
|
|
|
|
components: dict[str, ComponentConfig] = {}
|
|
for name, path_env in PATH_VARIABLES.items():
|
|
if name in {"content", "ops"}:
|
|
continue
|
|
|
|
item = component_raw.get(name, {})
|
|
if not isinstance(item, dict):
|
|
raise ValueError(f"components.{name} must be a mapping")
|
|
|
|
components[name] = ComponentConfig(
|
|
name=name,
|
|
path_env=path_env,
|
|
required_markers=_as_markers(
|
|
item.get("required_markers"),
|
|
field=f"components.{name}.required_markers",
|
|
),
|
|
setup_command=_as_command(
|
|
item.get("setup_command"),
|
|
field=f"components.{name}.setup_command",
|
|
),
|
|
run_command=_as_command(
|
|
item.get("run_command"),
|
|
field=f"components.{name}.run_command",
|
|
),
|
|
)
|
|
|
|
infra_raw = raw.get("infrastructure", {})
|
|
if not isinstance(infra_raw, dict):
|
|
raise ValueError("infrastructure must be a mapping")
|
|
|
|
compose_override = os.environ.get("ACUPEDIA_LOCAL_COMPOSE_FILE", "").strip()
|
|
if compose_override:
|
|
# Environment path follows workspace-base semantics.
|
|
compose_file = resolve_workspace_path(
|
|
compose_override,
|
|
base_path=base,
|
|
)
|
|
else:
|
|
# Config path remains relative to the ops repository.
|
|
compose_file = _resolve_relative(
|
|
paths["ops"],
|
|
str(infra_raw.get("compose_file", "docker-compose.local.yml")),
|
|
)
|
|
|
|
assert compose_file is not None
|
|
|
|
infra = LocalInfraConfig(
|
|
compose_file=compose_file,
|
|
project_name=str(infra_raw.get("project_name", "acupedia-local")),
|
|
)
|
|
|
|
bootstrap_raw = raw.get("bootstrap", {})
|
|
if not isinstance(bootstrap_raw, dict):
|
|
raise ValueError("bootstrap must be a mapping")
|
|
|
|
content_root = paths["content"]
|
|
content_config = _resolve_relative(
|
|
content_root,
|
|
str(bootstrap_raw.get("content_config", "config.json")),
|
|
)
|
|
modules_root = _resolve_relative(
|
|
content_root,
|
|
str(bootstrap_raw.get("modules_root", ".")),
|
|
)
|
|
assert content_config is not None
|
|
assert modules_root is not None
|
|
|
|
bootstrap = BootstrapDefaults(
|
|
default_target=str(bootstrap_raw.get("default_target", "content")),
|
|
content_config=content_config,
|
|
modules_root=modules_root,
|
|
media_manifest=_resolve_relative(
|
|
content_root,
|
|
bootstrap_raw.get("media_manifest"),
|
|
),
|
|
media_root=_resolve_relative(
|
|
content_root,
|
|
bootstrap_raw.get("media_root"),
|
|
),
|
|
commerce_plans=_resolve_relative(
|
|
content_root,
|
|
bootstrap_raw.get("commerce_plans"),
|
|
),
|
|
)
|
|
|
|
database_raw = raw.get("database", {})
|
|
if not isinstance(database_raw, dict):
|
|
raise ValueError("database must be a mapping")
|
|
|
|
return LocalWorkspace(
|
|
base_path=base,
|
|
root=paths["ops"],
|
|
components=components,
|
|
paths=paths,
|
|
infra=infra,
|
|
bootstrap=bootstrap,
|
|
database_reset_command=_as_command(
|
|
database_raw.get("reset_command"),
|
|
field="database.reset_command",
|
|
),
|
|
)
|