refactor: Separate runtime from build. Enhance configuration and registry management, migrate to registry-based component handling

This commit is contained in:
2026-02-22 23:19:16 -08:00
parent 033a76ccfd
commit d52d8829ba
37 changed files with 1271 additions and 414 deletions

View File

@@ -5,27 +5,28 @@ from __future__ import annotations
import argparse
import subprocess
from castle_cli.config import GENERATED_DIR, CastleConfig, ensure_dirs, load_config
from castle_core.generators.caddyfile import find_app_dist, generate_caddyfile
from castle_core.config import GENERATED_DIR
from castle_core.generators.caddyfile import generate_caddyfile_from_registry
from castle_core.registry import REGISTRY_PATH, load_registry
from castle_cli.config import CastleConfig, ensure_dirs, load_config
GATEWAY_COMPONENT = "castle-gateway"
GATEWAY_UNIT = "castle-castle-gateway.service"
def _write_generated_files(config: CastleConfig) -> None:
"""Write generated Caddyfile."""
def _write_generated_files() -> None:
"""Write generated Caddyfile from registry."""
ensure_dirs()
caddyfile_path = GENERATED_DIR / "Caddyfile"
caddyfile_path.write_text(generate_caddyfile(config))
print(f" Generated {caddyfile_path}")
if not REGISTRY_PATH.exists():
print("Error: no registry found. Run 'castle deploy' first.")
return
app_dist = find_app_dist(config)
if app_dist:
print(f" App: {app_dist}")
else:
print(" App: dist/ not found, using fallback")
registry = load_registry()
caddyfile_path = GENERATED_DIR / "Caddyfile"
caddyfile_path.write_text(generate_caddyfile_from_registry(registry))
print(f" Generated {caddyfile_path}")
def run_gateway(args: argparse.Namespace) -> int:
@@ -38,24 +39,29 @@ def run_gateway(args: argparse.Namespace) -> int:
if args.gateway_command == "start":
if getattr(args, "dry_run", False):
return _gateway_dry_run(config)
return _gateway_dry_run()
return _gateway_start(config)
elif args.gateway_command == "stop":
return _gateway_stop()
elif args.gateway_command == "reload":
if getattr(args, "dry_run", False):
return _gateway_dry_run(config)
return _gateway_reload(config)
return _gateway_dry_run()
return _gateway_reload()
elif args.gateway_command == "status":
return _gateway_status()
return 1
def _gateway_dry_run(config: CastleConfig) -> int:
def _gateway_dry_run() -> int:
"""Print generated Caddyfile without applying."""
if not REGISTRY_PATH.exists():
print("Error: no registry found. Run 'castle deploy' first.")
return 1
registry = load_registry()
print("# Caddyfile")
print(generate_caddyfile(config))
print(generate_caddyfile_from_registry(registry))
return 0
@@ -68,7 +74,7 @@ def _gateway_start(config: CastleConfig) -> int:
return 1
print("Generating gateway configuration...")
_write_generated_files(config)
_write_generated_files()
print(f"\nStarting gateway on port {config.gateway.port}...")
return _service_enable(config, GATEWAY_COMPONENT)
@@ -81,14 +87,15 @@ def _gateway_stop() -> int:
return _service_disable(GATEWAY_COMPONENT)
def _gateway_reload(config: CastleConfig) -> int:
def _gateway_reload() -> int:
"""Regenerate config and reload Caddy."""
print("Regenerating gateway configuration...")
_write_generated_files(config)
_write_generated_files()
result = subprocess.run(
["systemctl", "--user", "reload", GATEWAY_UNIT],
capture_output=True, text=True,
capture_output=True,
text=True,
)
if result.returncode == 0:
@@ -98,7 +105,8 @@ def _gateway_reload(config: CastleConfig) -> int:
print("Reload signal sent. Verifying...")
result = subprocess.run(
["systemctl", "--user", "is-active", GATEWAY_UNIT],
capture_output=True, text=True,
capture_output=True,
text=True,
)
if result.stdout.strip() == "active":
print("Gateway running.")
@@ -112,7 +120,8 @@ def _gateway_status() -> int:
"""Show gateway status via systemd."""
result = subprocess.run(
["systemctl", "--user", "is-active", GATEWAY_UNIT],
capture_output=True, text=True,
capture_output=True,
text=True,
)
status = result.stdout.strip()