provisioning/scripts/generate-constraints.ncl

74 lines
4.3 KiB
Text
Raw Normal View History

# Generate Nickel constraints from TOML master file
# Usage: nickel eval scripts/generate-constraints.ncl > schemas/platform/common/constraints.ncl
let constraints = import "schemas/platform/constraints/constraints.toml" in
# Format constraint function - builds Nickel validator code as string
let format_constraint = fun name desc min_val max_val =>
" # " ++ desc ++ "\n" ++
" " ++ name ++ " = contract.from_validator (fun x =>\n" ++
" if x >= " ++ m%"%{min_val}"% ++ " && x <= " ++ m%"%{max_val}"% ++ " then 'Ok\n" ++
" else 'Error {message = \"" ++ name ++ " must be between " ++ m%"%{min_val}"% ++ " and " ++ m%"%{max_val}"% ++ "\"}\n" ++
" ),"
in
# Header
let header = [
"# Platform Constraints and Validators",
"# AUTOMATICALLY GENERATED from constraints.toml - DO NOT EDIT DIRECTLY",
"# Generated via: nickel eval scripts/generate-constraints.ncl",
"# Source: schemas/platform/constraints/constraints.toml",
"#",
"# Usage: Import in schemas to validate configuration fields",
"# Example: port | constraints.port_standard",
"#",
"# To modify constraints, edit constraints.toml and run:",
"# nickel eval scripts/generate-constraints.ncl > schemas/platform/common/constraints.ncl",
"",
"let contract = std.contract in",
"",
"{",
] in
# Constraints definitions
let constraints_defs = [
format_constraint "port_standard" constraints.common.server.port.description constraints.common.server.port.min constraints.common.server.port.max,
format_constraint "port_high" constraints.common.server.port_high.description constraints.common.server.port_high.min constraints.common.server.port_high.max,
format_constraint "vault_port" constraints.vault_service.port.description constraints.vault_service.port.min constraints.vault_service.port.max,
format_constraint "registry_port" constraints.registry.server_port.description constraints.registry.server_port.min constraints.registry.server_port.max,
format_constraint "workers" constraints.orchestrator.workers.description constraints.orchestrator.workers.min constraints.orchestrator.workers.max,
format_constraint "server_workers" constraints.common.server.workers.description constraints.common.server.workers.min constraints.common.server.workers.max,
format_constraint "max_connections" constraints.common.server.max_connections.description constraints.common.server.max_connections.min constraints.common.server.max_connections.max,
format_constraint "retry_attempts" constraints.orchestrator.queue.retry_attempts.description constraints.orchestrator.queue.retry_attempts.min constraints.orchestrator.queue.retry_attempts.max,
format_constraint "metrics_interval" constraints.common.monitoring.metrics_interval.description constraints.common.monitoring.metrics_interval.min constraints.common.monitoring.metrics_interval.max,
format_constraint "health_check_interval" constraints.common.monitoring.health_check_interval.description constraints.common.monitoring.health_check_interval.min constraints.common.monitoring.health_check_interval.max,
format_constraint "task_timeout" constraints.orchestrator.queue.task_timeout.description constraints.orchestrator.queue.task_timeout.min constraints.orchestrator.queue.task_timeout.max,
format_constraint "tool_timeout" constraints.mcp_server.tools.timeout.description constraints.mcp_server.tools.timeout.min constraints.mcp_server.tools.timeout.max,
format_constraint "keep_alive" constraints.common.server.keep_alive.description constraints.common.server.keep_alive.min constraints.common.server.keep_alive.max,
format_constraint "rate_limit_requests" constraints.control_center.rate_limiting.max_requests.description constraints.control_center.rate_limiting.max_requests.min constraints.control_center.rate_limiting.max_requests.max,
] in
let footer = ["}", ""] in
# Helper to recursively join array into string with newlines
let rec join_with_newlines = fun arr index acc =>
if index >= (std.array.length arr) then
acc
else
let line = std.array.at index arr in
let new_acc =
if acc == "" then
line
else
acc ++ "\n" ++ line
in
join_with_newlines arr (index + 1) new_acc
in
# Join all parts
let header_str = join_with_newlines header 0 "" in
let constraints_str = join_with_newlines constraints_defs 0 "" in
let footer_str = join_with_newlines footer 0 "" in
header_str ++ "\n" ++ constraints_str ++ "\n" ++ footer_str