prole/knoe/milestone.py
chrisfu a069989315 Rename prole-db to knoe-db, add knoe-auth as cluster-internal KDC
Itemized changes:

1. knoe-auth: New cluster-internal KDC and SSO gateway service
   - Created etc/init_knoe_auth.sh based on init_kdc.sh with knoe-auth naming
   - Namespace defaults to SERVICE_NAMESPACE (knoe-system)
   - ConfigMap: knoe-auth-kdc-config, Secret: knoe-auth-secrets
   - Legacy cleanup removes old auth/dog/authority deployments

2. Orchestration: knoe-auth initializes before CloudNativePG
   - Updated prole.sh to insert init_knoe_auth.sh as step 2 (before CNPG)
   - Renumbered all subsequent initialization steps

3. Kong routing: Updated init_kong.sh to route to knoe-auth in SERVICE_NAMESPACE

4. Comment/reference updates for knoe-auth
   - Updated init_common_services.sh, init_service_layer.sh, init_kerberos.sh

5. prole-db renamed to knoe-db across the entire codebase
   - Renamed prole-db/ directory to knoe-db/
   - Renamed all prole-db Kubernetes manifests (deploy/opentofu, k8s/)
   - Renamed scripts: docker-root-knoe-db.sh, docker-run-knoe-db.sh, test-cnpg-knoe-db.sh
   - Renamed etc/init_prole-db-reset.sh to etc/init_knoe-db-reset.sh
   - Renamed etc/prole-db-passwwd.sh to etc/knoe-db-passwwd.sh
   - Renamed mock_val counterparts accordingly
   - Renamed tests/etc/test_init_prole-db-reset.sh to test_init_knoe-db-reset.sh
   - Renamed docs/prole-db-documentation-mcp-architecture.md to knoe-db variant
   - Renamed modes/k3d/prole-db/ to modes/k3d/knoe-db/
   - Renamed prole-db.iml to knoe-db.iml

6. Configuration updates
   - Updated conf/dev, conf/prod, conf/test, conf/service prole.cfg files
   - Updated conf/port-mapping.cfg
   - Updated etc/prole_cfg.sh and mock_val/prole_cfg.sh
   - Updated service/prole.cfg

7. Kubernetes manifests and deploy configuration
   - Updated deploy/opentofu/k3s ArgoCD application YAMLs
   - Updated kong-configmap.yaml and kustomization.yaml
   - Updated k3s/kong-config.yml and prole-resources.yaml
   - Updated prole-mssql-db deployment YAMLs
   - Updated supabase helm render and deploy scripts

8. Infrastructure and GCP Terraform
   - Updated deploy/gcp/terraform: folders, groups, IAM, service-projects

9. Python/installer code updates
   - Updated knoe/core: actions, build_context, controller, env, milestones
   - Updated knoe/milestone.py
   - Updated knoe/ui/screens: cfg, database, database_options, deploy, docker,
     navigation, security, services, validate
   - Updated knoe.spec, status.py

10. Shell script updates
    - Updated etc/: build_db, init_cloudnative_pg, init_cnpg_backup,
      init_db_manager, init_forgejo, init_gitlab, init_monitoring, init_openbao,
      init_port_forwards, init_postgrest, init_supabase_ports, status
    - Updated mock_val/ counterparts for all above scripts
    - Updated prole-net/init-prole-dns.sh
    - Updated bin/prole-kpf.sh, gitea/deploy.sh, supabase/deploy.sh

11. Test updates
    - Updated tests/etc/: test_init_cloudnative_pg*, test_init_cnpg_backup*,
      test_init_kdc*, test_init_kerberos*, test_init_kong*, test_prole_cfg*
    - Updated tests/installer/: test_actions_helpers, test_cfg_save_kubecontext,
      test_controller, test_core_classes, test_milestones, test_milestones_extended,
      test_namespace_propagation
    - Updated tests/: test_database_options, test_navigation,
      test_render_supabase_hostname, test_docker_build_fix,
      test_all_prole_home_fixes, silent_install_test, final_test

12. Documentation updates
    - Updated docs/: DOCKER-BUILD-FIX, PROLE-CFG-SECRETS, PROLE-HOME-DIRECTORY,
      build-system, patent
    - Updated scan/network_description.txt
    - Updated pom.xml

13. Miscellaneous script updates
    - Updated root-level: _adopt_replica_pvcs, _fix_replica_merlin, _import_pi,
      _patch_cluster, _prebind_pvcs, _rebind_d002, _rebind_d002b, test_resolve
    - Updated scripts/generate_spec.py

Co-authored-by: Junie <junie@jetbrains.com>
2026-03-22 22:16:21 -07:00

254 lines
9.2 KiB
Python

"""Milestone abstraction for the UI-agnostic installer core."""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Callable, Sequence
from . import config as inst_config
from .state import InstallerState
ProgressCallback = Callable[[str, float | None], None]
ValidationResult = str | Sequence[str] | None
class Milestone(ABC):
"""Base class for installer milestones.
Milestones encapsulate business logic, validation, and flow control.
They must not import or depend on UI toolkits.
"""
id: str
title: str
def __init__(self, milestone_id: str, title: str):
self.id = milestone_id
self.title = title
def validate(self, state: InstallerState) -> ValidationResult:
"""Return a validation error or None when valid."""
return None
@abstractmethod
def execute(
self, state: InstallerState, progress: ProgressCallback | None = None
) -> None:
"""Execute milestone logic. Use the progress callback to emit updates."""
raise NotImplementedError
def next(self, state: InstallerState) -> str | None:
"""Return the next milestone id or None to finish."""
return None
def _parse_bool(self, val: any, default: bool | None = False) -> bool | None:
return inst_config._parse_bool(val, default)
def _run_cmd(
self,
cmd: str | list[str],
cwd: str | None = None,
on_stdout: callable | None = None,
) -> int:
if isinstance(cmd, str):
cmd = ["bash", "-c", cmd]
import os
import subprocess
try:
env = inst_config._augment_env_for_brew(os.environ.copy())
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
cwd=cwd,
env=env,
)
for line in iter(proc.stdout.readline, ""):
if on_stdout:
on_stdout(line)
return proc.wait()
except Exception:
return 1
def _get_script_env(self, state: InstallerState) -> dict:
import os
from pathlib import Path
env = os.environ.copy()
project_root = inst_config.PROJECT_ROOT
env["PROLE_HOME"] = str(project_root)
env["PROLE_SERVICE"] = str(project_root)
namespace = (state.inputs.get("init_password.db_namespace", "") or "").strip()
env["NAMESPACE"] = namespace or "default"
# Paths — prefer the env-specific conf subdir (e.g. conf/service/) so that
# init scripts source the correct prole.cfg and don't fall back to the
# conf/prole.cfg symlink which may point to a different cluster env.
# Priority: (1) inherited PROLE_CONF from parent shell when it points to a
# valid env-specific subdir, (2) cluster_env-derived subdir, (3) stored value.
_inherited_conf = os.environ.get("PROLE_CONF", "")
_cluster_env_for_conf = state.inputs.get("init_cluster.cluster_env", "")
_env_conf_dir = (
Path(project_root) / "conf" / _cluster_env_for_conf
if _cluster_env_for_conf
else None
)
if _inherited_conf and (Path(_inherited_conf) / "prole.cfg").exists():
env["PROLE_CONF"] = _inherited_conf
elif _env_conf_dir and _env_conf_dir.is_dir():
env["PROLE_CONF"] = str(_env_conf_dir)
else:
env["PROLE_CONF"] = state.inputs.get(
"env_setup.PROLE_CONF", str(project_root / "conf")
)
env["PROLE_DATA"] = state.inputs.get(
"env_setup.PROLE_DATA", str(project_root / "knoe-db" / "data")
)
env["PROLE_LOGS"] = state.inputs.get(
"env_setup.PROLE_LOGS", str(project_root / "logs")
)
env["PROLE_SERVICE"] = state.inputs.get(
"env_setup.PROLE_SERVICE", str(project_root / "etc")
)
service_ns = (
(state.config_data.get("Global", {}) or {})
.get("SERVICE_NAMESPACE", "")
.strip()
)
if not service_ns:
service_ns = (os.environ.get("SERVICE_NAMESPACE") or "").strip()
if not service_ns:
service_ns = env["NAMESPACE"]
env["SERVICE_NAMESPACE"] = service_ns
from knoe.core.env import _deployment_mode_from_env
cluster_env = state.inputs.get("init_cluster.cluster_env", "dev")
mode = _deployment_mode_from_env(cluster_env) or "k3d"
env["PROLE_MODE"] = mode
# Kubeconfig
if mode == "k3s":
kc = Path(env["PROLE_SERVICE"]) / "secrets" / "k3s.kubeconfig"
if not kc.exists():
kc = project_root / "etc" / "secrets" / "k3s.kubeconfig"
if kc.exists():
env["KUBECONFIG"] = str(kc)
elif mode == "k3d":
# Ensure k3d dev clusters have a resolvable KUBECONFIG.
# The cluster may already be running from a previous session;
# merge the kubeconfig so downstream scripts can reach it.
if not (env.get("KUBECONFIG") or "").strip():
import subprocess as _sp
cluster_name = "knoe-dev-cluster"
try:
_sp.run(
[
"k3d",
"kubeconfig",
"merge",
cluster_name,
"--kubeconfig-switch-context",
],
capture_output=True,
text=True,
timeout=10,
)
except Exception:
pass
default_kube = str(Path.home() / ".kube" / "config")
if Path(default_kube).exists():
env["KUBECONFIG"] = default_kube
db_pw = state.inputs.get("init_password.db_password", "").strip()
if db_pw:
db_pw = inst_config._resolve_secret_value(db_pw)
env["DB_PASSWORD"] = db_pw
env["OPENTOFU_ADMIN_PASSWORD"] = db_pw
grafana_pw = (
(state.config_data.get("Monitoring", {}) or {})
.get("GRAFANA_ADMIN_PASSWORD", "")
.strip()
)
resolved_grafana = (
inst_config._resolve_secret_value(grafana_pw) if grafana_pw else ""
)
if (
resolved_grafana
and not inst_config._is_openbao_ref(resolved_grafana)
and not inst_config._is_prole_secret(resolved_grafana)
):
env["GRAFANA_ADMIN_PASSWORD"] = resolved_grafana
elif db_pw:
env["GRAFANA_ADMIN_PASSWORD"] = db_pw
db_user = (state.inputs.get("init_password.db_username", "") or "").strip()
if db_user:
env["KNOE_DB_USER"] = db_user
# Kerberos
env["KRB5_REALM"] = state.inputs.get("kerberos_config.realm", "")
realm = (state.inputs.get("kerberos_config.realm", "") or "").strip()
env["KRB5_KDC"] = state.inputs.get("kerberos_config.kdc", "")
if realm:
env["REALM"] = realm
env["DOMAIN"] = realm.lower()
env["KRB5_USER"] = state.inputs.get("kerberos_config.user", "")
krb_pw = state.inputs.get("kerberos_config.password", "")
if krb_pw:
env["KRB5_PASSWORD"] = inst_config._resolve_secret_value(krb_pw)
if state.inputs.get("kerberos_config.kdc", ""):
env["KRB5_ADMIN"] = state.inputs.get("kerberos_config.kdc", "")
kerberos_enabled = self._parse_bool(
state.inputs.get("kerberos_config.enabled", "False"), default=False
)
if kerberos_enabled is None:
kerberos_enabled = False
env["KERBEROS_ENABLED"] = "True" if kerberos_enabled else "False"
env["ENABLED"] = env["KERBEROS_ENABLED"]
at_rest = state.inputs.get("init_cluster.at_rest_encryption_enabled", "")
if at_rest != "":
env["AT_REST_ENCRYPTION_ENABLED"] = str(at_rest)
argocd_ns = (
(state.config_data.get("Global", {}) or {})
.get("ARGOCD_NAMESPACE", "")
.strip()
)
if not argocd_ns:
argocd_ns = (os.environ.get("ARGOCD_NAMESPACE") or "").strip() or "argocd"
env["ARGOCD_NAMESPACE"] = argocd_ns
registry_ns = (
(state.config_data.get("Global", {}) or {})
.get("REGISTRY_NAMESPACE", "")
.strip()
)
if not registry_ns:
registry_ns = (
os.environ.get("REGISTRY_NAMESPACE") or ""
).strip() or "default"
env["REGISTRY_NAMESPACE"] = registry_ns
if mode == "k3s":
server = state.inputs.get("init_cluster.k3s_server_url", "").strip()
token = state.inputs.get("init_cluster.k3s_token", "").strip()
if token:
token = inst_config._resolve_secret_value(token)
if server:
if not server.startswith("http"):
server = f"https://{server}"
env["PROLE_K3S_SERVER"] = server
if token:
env["PROLE_K3S_TOKEN"] = token
return env