core+cli: castle apply — one converge verb replaces deploy/start/enable/…
Reconcile the running system to declared config in a single operation, replacing the old deploy && start plus the per-kind enable/disable/ install/uninstall verbs. The mechanism varies by manager (systemd unit / PATH install / gateway route); the verb never does. Core (castle_core.deploy.apply): - render (units, Caddyfile, tunnel) then reconcile: activate every enabled deployment that's down, restart any whose unit bytes changed, deactivate the disabled ones. Returns ApplyResult (the enacted diff). - --plan computes the diff and touches nothing. - restart-on-change is exact: _render_unit_files is the single source of truth for unit bytes, used both to write units and to predict restarts, so the plan can't drift from what deploy writes. Desired state: - DeploymentBase.enabled (default True) — the declarative on/off. apply converges to it; a disabled deployment is defined but not running and gets no gateway route (else it would 502). Registry omits enabled when True, keeping existing registries byte-identical. CLI: - add `castle apply [name] [--plan]`; keep `restart` as the one imperative bounce, plus status/doctor/logs/gateway/program. - retire deploy, start, stop (top-level); service/job deploy|enable| disable|start|stop; tool install|uninstall; program install|uninstall. - next-step strings + doctor hints now say `castle apply`. Verified: core 129 + cli 31 green; live `castle apply` on a converged node reports "nothing to do" with no spurious restarts; --plan shows an empty diff; gateway + api stay up.
This commit is contained in:
60
cli/src/castle_cli/commands/apply.py
Normal file
60
cli/src/castle_cli/commands/apply.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""castle apply — converge the running system to match config.
|
||||
|
||||
The one workhorse verb: renders systemd units + the Caddyfile + tunnel config,
|
||||
then reconciles the runtime (activate what's enabled and down, restart what
|
||||
changed, deactivate what's disabled). Replaces the old `deploy && start` plus the
|
||||
per-kind enable/disable/install/uninstall verbs.
|
||||
|
||||
`--plan` computes and prints the diff without writing or touching the runtime.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
_C = {
|
||||
"activate": "\033[32m", # green
|
||||
"restart": "\033[33m", # yellow
|
||||
"deactivate": "\033[31m", # red
|
||||
"reset": "\033[0m",
|
||||
"dim": "\033[90m",
|
||||
}
|
||||
|
||||
|
||||
def _line(verb_color: str, verb: str, names: list[str]) -> None:
|
||||
if not names:
|
||||
return
|
||||
print(f" {verb_color}{verb}{_C['reset']} {', '.join(sorted(names))}")
|
||||
|
||||
|
||||
def run_apply(args: argparse.Namespace) -> int:
|
||||
from castle_core.deploy import apply
|
||||
|
||||
target = getattr(args, "name", None)
|
||||
plan = getattr(args, "plan", False)
|
||||
|
||||
result = apply(target_name=target, plan=plan)
|
||||
|
||||
# Surface any warnings the render produced (acme prerequisites, tunnel notes).
|
||||
for msg in result.messages:
|
||||
if msg.startswith("Warning"):
|
||||
print(f" {_C['dim']}{msg}{_C['reset']}")
|
||||
|
||||
if plan:
|
||||
print("\n\033[1mPlan\033[0m " + _C["dim"] + "(no changes made)" + _C["reset"])
|
||||
if not result.changed:
|
||||
print(f" {_C['dim']}nothing to do — already converged{_C['reset']}")
|
||||
return 0
|
||||
_line(_C["activate"], "would activate ", result.activated)
|
||||
_line(_C["restart"], "would restart ", result.restarted)
|
||||
_line(_C["deactivate"], "would deactivate", result.deactivated)
|
||||
return 0
|
||||
|
||||
print("\n\033[1mApplied\033[0m")
|
||||
if not result.changed:
|
||||
print(f" {_C['dim']}nothing to do — already converged{_C['reset']}")
|
||||
return 0
|
||||
_line(_C["activate"], "activated ", result.activated)
|
||||
_line(_C["restart"], "restarted ", result.restarted)
|
||||
_line(_C["deactivate"], "deactivated", result.deactivated)
|
||||
return 0
|
||||
@@ -158,12 +158,12 @@ def run_create(args: argparse.Namespace) -> int:
|
||||
if stack == "supabase":
|
||||
print(" # edit migrations/, functions/, public/ — targets the shared substrate")
|
||||
print(f" castle program build {name} # apply migrations to the substrate")
|
||||
print(f" castle deploy && castle gateway reload # serve at /{name}/")
|
||||
print(f" castle apply # serve at {name}.<gateway.domain>")
|
||||
elif stack:
|
||||
print(" uv sync")
|
||||
if kind == "service":
|
||||
print(f" uv run {name} # starts on port {port}")
|
||||
print(f" castle deploy {name}")
|
||||
print(f" castle apply {name}")
|
||||
print(f" castle test {name}")
|
||||
else:
|
||||
print(" # add code, then declare commands: in castle.yaml")
|
||||
|
||||
@@ -116,9 +116,9 @@ def run_delete(args: argparse.Namespace) -> int:
|
||||
|
||||
try:
|
||||
deploy()
|
||||
print("Reconciled runtime (castle deploy).")
|
||||
print("Reconciled runtime (castle apply).")
|
||||
except Exception as e:
|
||||
print(f"warning: reconcile (castle deploy) failed — run 'castle deploy': {e}")
|
||||
print(f"warning: reconcile failed — run 'castle apply': {e}")
|
||||
|
||||
# Optional: delete the source directory.
|
||||
if args.source and source_dir:
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
"""castle deploy — thin CLI wrapper around castle_core.deploy."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
from castle_core.deploy import deploy
|
||||
|
||||
|
||||
def run_deploy(args: argparse.Namespace) -> int:
|
||||
"""Deploy from castle.yaml to ~/.castle/."""
|
||||
target_name = getattr(args, "name", None)
|
||||
result = deploy(target_name=target_name)
|
||||
|
||||
for msg in result.messages:
|
||||
print(f" {msg}")
|
||||
|
||||
print(f"\nDeployed {result.deployed_count} item(s).")
|
||||
if result.deployed_count > 0:
|
||||
print("Run 'castle start' to start all services.")
|
||||
return 0
|
||||
@@ -88,7 +88,7 @@ def run_service_create(args: argparse.Namespace) -> int:
|
||||
print(f" port: {args.port}")
|
||||
if proxy:
|
||||
print(f" subdomain: {name}.<gateway.domain>")
|
||||
print(f"\nNext: castle service deploy {name} && castle service start {name}")
|
||||
print(f"\nNext: castle apply {name}")
|
||||
return 0
|
||||
|
||||
|
||||
@@ -118,5 +118,5 @@ def run_job_create(args: argparse.Namespace) -> int:
|
||||
print(f"Created job '{name}'.")
|
||||
print(f" runs: {args.launcher} ({args.run or args.program or name})")
|
||||
print(f" schedule: {args.schedule}")
|
||||
print(f"\nNext: castle job deploy {name} && castle job enable {name}")
|
||||
print(f"\nNext: castle apply {name}")
|
||||
return 0
|
||||
|
||||
@@ -6,7 +6,7 @@ of checks grouped into Environment, Configuration, Runtime, and TLS & exposure;
|
||||
each check reports ok / warn / fail with a one-line hint when action is needed.
|
||||
|
||||
Exit code: 0 when nothing FAILed (warnings are allowed), 1 otherwise — so it
|
||||
doubles as a scriptable smoke test after `./install.sh` or `castle deploy`.
|
||||
doubles as a scriptable smoke test after `./install.sh` or `castle apply`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -206,7 +206,7 @@ def _check_runtime(config) -> list[Check]:
|
||||
Check(
|
||||
FAIL,
|
||||
"gateway not running",
|
||||
hint="castle deploy && castle start",
|
||||
hint="castle apply",
|
||||
)
|
||||
)
|
||||
|
||||
@@ -227,7 +227,7 @@ def _check_runtime(config) -> list[Check]:
|
||||
checks.append(Check(OK, "castle-api running", detail=detail))
|
||||
else:
|
||||
checks.append(
|
||||
Check(FAIL, "castle-api not running", hint="castle deploy && castle start")
|
||||
Check(FAIL, "castle-api not running", hint="castle apply")
|
||||
)
|
||||
|
||||
# Generated artifacts.
|
||||
@@ -244,7 +244,7 @@ def _check_runtime(config) -> list[Check]:
|
||||
FAIL,
|
||||
"generated specs missing",
|
||||
detail=", ".join(missing),
|
||||
hint="castle deploy",
|
||||
hint="castle apply",
|
||||
)
|
||||
)
|
||||
|
||||
@@ -347,7 +347,7 @@ def _check_tls_exposure(config) -> list[Check]:
|
||||
WARN,
|
||||
"castle-tunnel not running",
|
||||
detail="public routes are down",
|
||||
hint="castle service start castle-tunnel",
|
||||
hint="enable castle-tunnel in its deployment, then: castle apply",
|
||||
)
|
||||
)
|
||||
checks.append(_check_public_dns(config))
|
||||
@@ -364,7 +364,7 @@ def _check_public_dns(config) -> Check:
|
||||
records. The required permission is a single DNS:Edit (Cloudflare's 'Edit zone
|
||||
DNS' template), which also grants the zone lookup — so a correctly-scoped token
|
||||
passes this probe. Write itself isn't exercised (that would mutate); a
|
||||
DNS:Read-only token would false-pass here but `castle deploy` then surfaces a
|
||||
DNS:Read-only token would false-pass here but `castle apply` then surfaces a
|
||||
403 with the fix. Absent token → WARN (CNAMEs stay manual), not a failure.
|
||||
"""
|
||||
import urllib.error
|
||||
|
||||
@@ -20,7 +20,7 @@ def _write_generated_files() -> None:
|
||||
ensure_dirs()
|
||||
|
||||
if not REGISTRY_PATH.exists():
|
||||
print("Error: no registry found. Run 'castle deploy' first.")
|
||||
print("Error: no registry found. Run 'castle apply' first.")
|
||||
return
|
||||
|
||||
registry = load_registry()
|
||||
@@ -56,7 +56,7 @@ def run_gateway(args: argparse.Namespace) -> 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.")
|
||||
print("Error: no registry found. Run 'castle apply' first.")
|
||||
return 1
|
||||
|
||||
registry = load_registry()
|
||||
@@ -126,7 +126,7 @@ def _gateway_status() -> int:
|
||||
print(f"Gateway: {'running' if status == 'active' else status}")
|
||||
|
||||
if not REGISTRY_PATH.exists():
|
||||
print(" (no registry — run 'castle deploy')")
|
||||
print(" (no registry — run 'castle apply')")
|
||||
return 0
|
||||
|
||||
from castle_core.generators.caddyfile import compute_routes
|
||||
|
||||
@@ -147,7 +147,7 @@ def run_info(args: argparse.Namespace) -> int:
|
||||
if deployed.secret_env_keys:
|
||||
print(f" {BOLD}secrets{RESET}: {', '.join(deployed.secret_env_keys)}")
|
||||
else:
|
||||
print(f"\n {DIM}not deployed (run 'castle deploy'){RESET}")
|
||||
print(f"\n {DIM}not applied (run 'castle apply'){RESET}")
|
||||
|
||||
# Show CLAUDE.md if it exists
|
||||
source_dir = None
|
||||
|
||||
@@ -82,12 +82,12 @@ def run_run(args: argparse.Namespace) -> int:
|
||||
|
||||
# Service: deployed command from the registry.
|
||||
if not REGISTRY_PATH.exists():
|
||||
print("Error: no registry found. Run 'castle deploy' first.")
|
||||
print("Error: no registry found. Run 'castle apply' first.")
|
||||
return 1
|
||||
|
||||
registry = load_registry()
|
||||
if name not in registry.deployed:
|
||||
print(f"Error: '{name}' is not a deployed service. Run 'castle deploy' first.")
|
||||
print(f"Error: '{name}' is not a deployed service. Run 'castle apply' first.")
|
||||
return 1
|
||||
|
||||
deployed = registry.deployed[name]
|
||||
|
||||
@@ -7,13 +7,10 @@ import subprocess
|
||||
|
||||
from castle_core.generators.systemd import (
|
||||
SYSTEMD_USER_DIR,
|
||||
generate_timer,
|
||||
generate_unit_from_deployed,
|
||||
timer_name,
|
||||
unit_name,
|
||||
)
|
||||
from castle_core.manifest import kind_for
|
||||
from castle_core.registry import REGISTRY_PATH, load_registry
|
||||
|
||||
from castle_cli.config import (
|
||||
CastleConfig,
|
||||
@@ -45,44 +42,35 @@ _PAST = {"start": "started", "stop": "stopped", "restart": "restarted"}
|
||||
|
||||
|
||||
def run_service_cmd(args: argparse.Namespace) -> int:
|
||||
"""`castle service <enable|disable|start|stop|restart> <name>`."""
|
||||
sub = args.service_command
|
||||
"""`castle service restart <name>` — the imperative bounce (only verb left).
|
||||
|
||||
Lifecycle (deploy/enable/disable/start/stop) is now convergence: `castle apply`.
|
||||
"""
|
||||
config = load_config()
|
||||
if sub == "enable":
|
||||
if getattr(args, "dry_run", False):
|
||||
return _service_dry_run(config, args.name)
|
||||
return _service_enable(config, args.name)
|
||||
if sub == "disable":
|
||||
return _service_disable(config, args.name)
|
||||
if sub in ("start", "stop", "restart"):
|
||||
return _unit_action(config, args.name, sub, is_job=False)
|
||||
return 1
|
||||
return _unit_action(config, args.name, "restart", is_job=False)
|
||||
|
||||
|
||||
def run_job_cmd(args: argparse.Namespace) -> int:
|
||||
"""`castle job <enable|disable|start|stop|restart> <name>` (acts on the timer)."""
|
||||
sub = args.job_command
|
||||
"""`castle job restart <name>` — bounce the job's timer."""
|
||||
config = load_config()
|
||||
if sub == "enable":
|
||||
return _service_enable(config, args.name) # enable_service handles timers
|
||||
if sub == "disable":
|
||||
return _service_disable(config, args.name)
|
||||
if sub in ("start", "stop", "restart"):
|
||||
return _unit_action(config, args.name, sub, is_job=True)
|
||||
return 1
|
||||
return _unit_action(config, args.name, "restart", is_job=True)
|
||||
|
||||
|
||||
def run_platform(args: argparse.Namespace) -> int:
|
||||
"""Top-level `castle start|stop|restart` — the whole platform."""
|
||||
def run_restart(args: argparse.Namespace) -> int:
|
||||
"""Top-level `castle restart [name]` — bounce one deployment, or all of them.
|
||||
|
||||
An imperative op: it re-actualizes current desired state, it does not change it
|
||||
(that's `castle apply`).
|
||||
"""
|
||||
config = load_config()
|
||||
action = args.command
|
||||
if action == "start":
|
||||
return _services_start(config)
|
||||
if action == "stop":
|
||||
return _services_stop(config)
|
||||
if action == "restart":
|
||||
name = getattr(args, "name", None)
|
||||
if not name:
|
||||
return _services_restart(config)
|
||||
return 1
|
||||
dep = config.deployments.get(name)
|
||||
if dep is None:
|
||||
print(f"Error: no deployment '{name}'.")
|
||||
return 1
|
||||
return _unit_action(config, name, "restart", is_job=(kind_for(dep) == "job"))
|
||||
|
||||
|
||||
_GATEWAY_NAME = "castle-gateway"
|
||||
@@ -256,78 +244,3 @@ def _service_status(config: CastleConfig) -> int:
|
||||
print()
|
||||
return 0
|
||||
|
||||
|
||||
def _service_dry_run(config: CastleConfig, name: str) -> int:
|
||||
"""Print the generated systemd unit(s) without installing."""
|
||||
if REGISTRY_PATH.exists():
|
||||
registry = load_registry()
|
||||
if name in registry.deployed:
|
||||
deployed = registry.deployed[name]
|
||||
systemd_spec = None
|
||||
dep = config.deployments.get(name)
|
||||
manage = getattr(dep, "manage", None)
|
||||
if manage and manage.systemd:
|
||||
systemd_spec = manage.systemd
|
||||
|
||||
svc_unit = unit_name(name)
|
||||
svc_content = generate_unit_from_deployed(name, deployed, systemd_spec)
|
||||
print(f"# {svc_unit}")
|
||||
print(svc_content)
|
||||
|
||||
if deployed.schedule:
|
||||
timer_content = generate_timer(
|
||||
name,
|
||||
schedule=deployed.schedule,
|
||||
description=deployed.description,
|
||||
)
|
||||
print(f"# {timer_name(name)}")
|
||||
print(timer_content)
|
||||
return 0
|
||||
|
||||
print(f"Error: '{name}' not found in registry. Run 'castle deploy' first.")
|
||||
return 1
|
||||
|
||||
|
||||
def _services_start(config: CastleConfig) -> int:
|
||||
"""Start all managed services and gateway."""
|
||||
if not REGISTRY_PATH.exists():
|
||||
print("Error: no registry found. Run 'castle deploy' first.")
|
||||
return 1
|
||||
|
||||
ensure_dirs()
|
||||
|
||||
from castle_core.config import SPECS_DIR
|
||||
from castle_core.generators.caddyfile import generate_caddyfile_from_registry
|
||||
|
||||
registry = load_registry()
|
||||
caddyfile_path = SPECS_DIR / "Caddyfile"
|
||||
caddyfile_path.write_text(generate_caddyfile_from_registry(registry))
|
||||
print(f"Generated {caddyfile_path}")
|
||||
|
||||
# Activate every deployment in its mode: systemd unit / timer, gateway route
|
||||
# (static), or PATH install (tool). activate() dispatches by manager.
|
||||
for name in config.deployments:
|
||||
if name not in registry.deployed:
|
||||
print(f" {name}: skipped (not in registry, run 'castle deploy')")
|
||||
continue
|
||||
_service_enable(config, name)
|
||||
|
||||
print(f"\nDashboard: http://localhost:{config.gateway.port}")
|
||||
return 0
|
||||
|
||||
|
||||
def _services_stop(config: CastleConfig) -> int:
|
||||
"""Stop all managed services and jobs."""
|
||||
for name in config.jobs:
|
||||
tmr_unit = timer_name(name)
|
||||
subprocess.run(["systemctl", "--user", "stop", tmr_unit], check=False)
|
||||
svc_unit = unit_name(name)
|
||||
subprocess.run(["systemctl", "--user", "stop", svc_unit], check=False)
|
||||
print(f" {name}: stopped")
|
||||
|
||||
for name in config.services:
|
||||
svc_unit = unit_name(name)
|
||||
subprocess.run(["systemctl", "--user", "stop", svc_unit], check=False)
|
||||
print(f" {name}: stopped")
|
||||
|
||||
return 0
|
||||
|
||||
@@ -137,7 +137,7 @@ def run_tool_info(args: argparse.Namespace) -> int:
|
||||
if comp.system_dependencies:
|
||||
print(f" {BOLD}requires{RESET}: {', '.join(comp.system_dependencies)}")
|
||||
if not installed:
|
||||
print(f"\n {DIM}install with: castle tool install {name}{RESET}")
|
||||
print(f"\n {DIM}enable it in its deployment, then: castle apply{RESET}")
|
||||
else:
|
||||
print(f"\n {DIM}run `{exes[0]} --help` for arguments{RESET}")
|
||||
print()
|
||||
|
||||
Reference in New Issue
Block a user