mirror of
https://github.com/Such-Software/adaptive-multi-protocol-gateway.git
synced 2026-12-02 12:53:56 +01:00
Add health deploy apply stage
This commit is contained in:
+135
@@ -14,6 +14,7 @@ from .addresses import (
|
||||
write_captured_address_results,
|
||||
)
|
||||
from .config import GatewayConfig
|
||||
from .health import HealthCheck, health_plan
|
||||
from .install_plan import (
|
||||
InstallStateCopy,
|
||||
InstallSupervisorAction,
|
||||
@@ -87,6 +88,21 @@ class AddressApplyResult:
|
||||
message: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HealthApplyResult:
|
||||
site_id: str
|
||||
protocol: str
|
||||
route: str
|
||||
transport: str
|
||||
profile: str
|
||||
url: str
|
||||
status: str
|
||||
mode: str
|
||||
command: tuple[str, ...]
|
||||
return_code: int | None
|
||||
message: str
|
||||
|
||||
|
||||
CommandRunner = Callable[[tuple[str, ...]], CommandRunResult]
|
||||
|
||||
|
||||
@@ -281,6 +297,59 @@ def apply_addresses(
|
||||
return [_address_result(result, dry_run=dry_run) for result in captures]
|
||||
|
||||
|
||||
def apply_health(
|
||||
config: GatewayConfig,
|
||||
*,
|
||||
dry_run: bool,
|
||||
command_runner: CommandRunner | None = None,
|
||||
) -> list[HealthApplyResult]:
|
||||
checks = health_plan(config)
|
||||
runner = command_runner or _run_command
|
||||
results: list[HealthApplyResult] = []
|
||||
for check in checks:
|
||||
command = _health_command(check)
|
||||
validation = _validate_health_check(check, command=command, dry_run=dry_run)
|
||||
if validation.status == "blocked":
|
||||
results.append(validation)
|
||||
continue
|
||||
if dry_run:
|
||||
results.append(
|
||||
_health_result(
|
||||
check,
|
||||
command=command,
|
||||
status="planned",
|
||||
mode="dry-run",
|
||||
return_code=None,
|
||||
message="would run published health check",
|
||||
)
|
||||
)
|
||||
continue
|
||||
run = runner(command)
|
||||
if run.return_code != 0:
|
||||
results.append(
|
||||
_health_result(
|
||||
check,
|
||||
command=command,
|
||||
status="blocked",
|
||||
mode="live",
|
||||
return_code=run.return_code,
|
||||
message=_command_failure_message(run),
|
||||
)
|
||||
)
|
||||
continue
|
||||
results.append(
|
||||
_health_result(
|
||||
check,
|
||||
command=command,
|
||||
status="passed",
|
||||
mode="live",
|
||||
return_code=run.return_code,
|
||||
message="published health check passed",
|
||||
)
|
||||
)
|
||||
return results
|
||||
|
||||
|
||||
def _validate_state_copy(
|
||||
config: GatewayConfig,
|
||||
copy: InstallStateCopy,
|
||||
@@ -480,6 +549,41 @@ def _validate_start_action(
|
||||
)
|
||||
|
||||
|
||||
def _validate_health_check(
|
||||
check: HealthCheck,
|
||||
*,
|
||||
command: tuple[str, ...],
|
||||
dry_run: bool,
|
||||
) -> HealthApplyResult:
|
||||
mode = "dry-run" if dry_run else "live"
|
||||
if check.status != "planned":
|
||||
return _health_result(
|
||||
check,
|
||||
command=command,
|
||||
status="blocked",
|
||||
mode=mode,
|
||||
return_code=None,
|
||||
message=check.message,
|
||||
)
|
||||
if not command:
|
||||
return _health_result(
|
||||
check,
|
||||
command=command,
|
||||
status="blocked",
|
||||
mode=mode,
|
||||
return_code=None,
|
||||
message="health check command is unavailable",
|
||||
)
|
||||
return _health_result(
|
||||
check,
|
||||
command=command,
|
||||
status="ready",
|
||||
mode=mode,
|
||||
return_code=None,
|
||||
message="health check passed safety checks",
|
||||
)
|
||||
|
||||
|
||||
def _address_result(
|
||||
result: AddressCaptureResult,
|
||||
*,
|
||||
@@ -510,6 +614,30 @@ def _address_result(
|
||||
)
|
||||
|
||||
|
||||
def _health_result(
|
||||
check: HealthCheck,
|
||||
*,
|
||||
command: tuple[str, ...],
|
||||
status: str,
|
||||
mode: str,
|
||||
return_code: int | None,
|
||||
message: str,
|
||||
) -> HealthApplyResult:
|
||||
return HealthApplyResult(
|
||||
site_id=check.site_id,
|
||||
protocol=check.protocol,
|
||||
route=check.route,
|
||||
transport=check.transport,
|
||||
profile=check.profile,
|
||||
url=check.url,
|
||||
status=status,
|
||||
mode=mode,
|
||||
command=command,
|
||||
return_code=return_code,
|
||||
message=message,
|
||||
)
|
||||
|
||||
|
||||
def _result(
|
||||
copy: InstallStateCopy,
|
||||
*,
|
||||
@@ -603,6 +731,13 @@ def start_command(
|
||||
return ()
|
||||
|
||||
|
||||
def _health_command(check: HealthCheck) -> tuple[str, ...]:
|
||||
try:
|
||||
return tuple(shlex.split(check.command))
|
||||
except ValueError:
|
||||
return ()
|
||||
|
||||
|
||||
def _termux_prefix(state_root: Path) -> Path:
|
||||
if len(state_root.parts) >= 3 and state_root.parts[-3:] == ("var", "lib", "ampg"):
|
||||
return state_root.parents[2]
|
||||
|
||||
+38
-1
@@ -23,10 +23,12 @@ from .addresses import (
|
||||
)
|
||||
from .apply import (
|
||||
AddressApplyResult,
|
||||
HealthApplyResult,
|
||||
StateApplyResult,
|
||||
StartApplyResult,
|
||||
SupervisorApplyResult,
|
||||
apply_addresses,
|
||||
apply_health,
|
||||
apply_state,
|
||||
apply_start,
|
||||
apply_supervisor,
|
||||
@@ -162,7 +164,7 @@ def main(argv: list[str] | None = None) -> int:
|
||||
_add_target_selection(deploy_apply_parser)
|
||||
deploy_apply_parser.add_argument(
|
||||
"--stage",
|
||||
choices=("state", "supervisor", "start", "addresses"),
|
||||
choices=("state", "supervisor", "start", "addresses", "health"),
|
||||
required=True,
|
||||
help="Deployment stage to apply.",
|
||||
)
|
||||
@@ -664,6 +666,23 @@ def _cmd_deploy(config, args) -> int:
|
||||
f"registry=\"{_quote(str(address_registry_path(config)))}\""
|
||||
)
|
||||
return 1 if blocked else 0
|
||||
if args.stage == "health":
|
||||
results = apply_health(config, dry_run=args.dry_run)
|
||||
for result in results:
|
||||
_print_health_apply_result(result)
|
||||
blocked = [result for result in results if result.status == "blocked"]
|
||||
passed = [result for result in results if result.status == "passed"]
|
||||
planned = [result for result in results if result.status == "planned"]
|
||||
print(
|
||||
"AMPG_DEPLOY_APPLY_SUMMARY "
|
||||
f"stage=health "
|
||||
f"mode={'dry-run' if args.dry_run else 'live'} "
|
||||
f"results={len(results)} "
|
||||
f"planned={len(planned)} "
|
||||
f"passed={len(passed)} "
|
||||
f"blocked={len(blocked)}"
|
||||
)
|
||||
return 1 if blocked else 0
|
||||
return 1
|
||||
|
||||
|
||||
@@ -1202,6 +1221,24 @@ def _print_address_apply_result(result: AddressApplyResult) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _print_health_apply_result(result: HealthApplyResult) -> None:
|
||||
return_code = "-" if result.return_code is None else str(result.return_code)
|
||||
print(
|
||||
"AMPG_DEPLOY_HEALTH "
|
||||
f"site={result.site_id} "
|
||||
f"protocol={result.protocol} "
|
||||
f"route=\"{_quote(result.route)}\" "
|
||||
f"transport={result.transport} "
|
||||
f"profile={result.profile} "
|
||||
f"mode={result.mode} "
|
||||
f"url=\"{_quote(result.url)}\" "
|
||||
f"status={result.status} "
|
||||
f"return_code={return_code} "
|
||||
f"command=\"{_quote(format_command(result.command))}\" "
|
||||
f"message=\"{_quote(result.message)}\""
|
||||
)
|
||||
|
||||
|
||||
def _print_dns_record(record: DNSRecordPlan) -> None:
|
||||
print(
|
||||
"AMPG_DNS_RECORD "
|
||||
|
||||
@@ -16,6 +16,7 @@ from .approvals import (
|
||||
)
|
||||
from .config import GatewayConfig
|
||||
from .dns import dns_plan
|
||||
from .health import health_plan
|
||||
from .install_plan import (
|
||||
install_artifacts,
|
||||
install_plan,
|
||||
@@ -95,6 +96,7 @@ def deploy_plan(
|
||||
daemon_probe=daemon_probe,
|
||||
),
|
||||
_address_step(config, command_context),
|
||||
_health_step(config, command_context),
|
||||
)
|
||||
status = _overall_status(steps)
|
||||
return DeployPlan(
|
||||
@@ -279,6 +281,38 @@ def _address_step(config: GatewayConfig, commands: _CommandContext) -> DeploySte
|
||||
return DeployStep("addresses", "ready", commands.command("addresses list"), "addresses are known")
|
||||
|
||||
|
||||
def _health_step(config: GatewayConfig, commands: _CommandContext) -> DeployStep:
|
||||
checks = health_plan(config)
|
||||
if not checks:
|
||||
return DeployStep("health", "ready", "-", "no published health checks are needed")
|
||||
blocked = [check for check in checks if check.status == "blocked"]
|
||||
review = [check for check in checks if check.status == "review"]
|
||||
planned = [check for check in checks if check.status == "planned"]
|
||||
if blocked:
|
||||
status = "todo" if _missing_outputs(config) else "blocked"
|
||||
return DeployStep(
|
||||
"health",
|
||||
status,
|
||||
commands.command("health-plan"),
|
||||
f"fix {len(blocked)} health check blocker(s)",
|
||||
)
|
||||
if review:
|
||||
return DeployStep(
|
||||
"health",
|
||||
"review",
|
||||
commands.command("deploy apply --stage addresses --dry-run"),
|
||||
f"resolve {len(review)} health check address review item(s)",
|
||||
)
|
||||
if planned:
|
||||
return DeployStep(
|
||||
"health",
|
||||
"todo",
|
||||
commands.command("deploy apply --stage health --dry-run"),
|
||||
f"run {len(planned)} published health check(s)",
|
||||
)
|
||||
return DeployStep("health", "ready", commands.command("health-plan"), "health checks are ready")
|
||||
|
||||
|
||||
def _apply_preflight_step(
|
||||
config: GatewayConfig,
|
||||
commands: _CommandContext,
|
||||
|
||||
Reference in New Issue
Block a user