prvng_core/nulib/clusters/utils.nu

103 lines
2.9 KiB
Text
Raw Normal View History

# Hetzner Cloud utility functions
use env.nu *
2025-10-07 10:32:04 +01:00
# Parse record or string to server name
export def parse_server_identifier [input: any]: nothing -> string {
if ($input | describe) == "string" {
$input
} else if ($input | has hostname) {
$input.hostname
} else if ($input | has name) {
$input.name
} else if ($input | has id) {
($input.id | into string)
} else {
($input | into string)
2025-10-07 10:32:04 +01:00
}
}
# Check if IP is valid IPv4
export def is_valid_ipv4 [ip: string]: nothing -> bool {
$ip =~ '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'
}
# Check if IP is valid IPv6
export def is_valid_ipv6 [ip: string]: nothing -> bool {
$ip =~ ':[a-f0-9]{0,4}:' or $ip =~ '^[a-f0-9]{0,4}:[a-f0-9]{0,4}:'
}
# Format record as table for display
2026-01-17 03:57:20 +00:00
export def format_server_table [servers: list]: nothing -> nothing {
let columns = ["id", "name", "status", "public_net", "server_type"]
let formatted = $servers | map {|s|
{
ID: ($s.id | into string)
Name: $s.name
Status: ($s.status | str capitalize)
IP: ($s.public_net.ipv4.ip | default "-")
Type: ($s.server_type.name | default "-")
Location: ($s.location.name | default "-")
}
2025-10-07 10:32:04 +01:00
}
$formatted | table
null
}
# Get error message from API response
export def extract_api_error [response: any]: nothing -> string {
if ($response | has error) {
if ($response.error | has message) {
$response.error.message
} else {
($response.error | into string)
}
} else if ($response | has message) {
$response.message
} else {
($response | into string)
2025-10-07 10:32:04 +01:00
}
}
# Validate server configuration
export def validate_server_config [server: record]: nothing -> bool {
let required = ["hostname", "server_type", "location"]
2026-01-17 03:57:20 +00:00
let missing = $required | where {|f| not ($server | has $f)}
if not ($missing | is-empty) {
error make {msg: $"Missing required fields: ($missing | str join ", ")"}
2025-10-07 10:32:04 +01:00
}
true
}
# Convert timestamp to human readable format
export def format_timestamp [timestamp: int]: nothing -> string {
2026-01-17 03:57:20 +00:00
let date = (now | format date "%Y-%m-%dT%H:%M:%SZ")
$"($timestamp) (UTC)"
}
# Retry function with exponential backoff (no try-catch)
export def retry_with_backoff [closure: closure, max_attempts: int = 3, initial_delay: int = 1]: nothing -> any {
feat(core): three-layer DAG, unified component arch, commands-registry cache, Nushell 0.112.2 migration - DAG architecture: `dag show/validate/export` (nulib/main_provisioning/dag.nu), config loader (lib_provisioning/config/loader/dag.nu), taskserv dag-executor. Backed by schemas/lib/dag/*.ncl; orchestrator emits NATS events via WorkspaceComposition::into_workflow. See ADR-020, ADR-021. - Unified Component Architecture: components/mod.nu, main_provisioning/ {components,workflow,extensions,ontoref-queries}.nu. Full workflow engine with topological sort and NATS subject emission. Blocks A-H complete (libre-daoshi). - Commands-registry: nulib/commands-registry.ncl (Nickel source, 314 lines) + JSON cache at ~/.cache/provisioning/commands-registry.json rebuilt on source change. cli/provisioning fast-path alias expansion avoids cold Nu startup. ADDING_COMMANDS.md documents new-command workflow. - Platform service manager: service-manager.nu (+573), startup.nu (+611), service-check.nu (+255); autostart/bootstrap/health/target refactored. - Nushell 0.112.2 migration: removed all try/catch and bash redirections; external commands prefixed with ^; type signatures enforced. Driven by scripts/refactor-try-catch{,-simplified}.nu. - TTY stack: removed shlib/*-tty.sh; replaced by cli/tty-dispatch.sh, tty-filter.sh, tty-commands.conf. - New domain modules: images/ (golden image lifecycle), workspace/{state,sync}.nu, main_provisioning/{bootstrap,cluster-deploy,fip,state}.nu, commands/{state, build,integrations/auth,utilities/alias}.nu, platform.nu expanded (+874). - Config loader overhaul: loader/core.nu slimmed (-759), cache/core.nu refactored (-454), removed legacy loaders/file_loader.nu (-330). - Thirteen new provisioning-<domain>.nu top-level modules for bash dispatcher. - Tests: test_workspace_state.nu (+351); updates to test_oci_registry, test_services. - README + CHANGELOG updated.
2026-04-17 04:27:33 +01:00
mut attempts = 0
mut delay = $initial_delay
loop {
let result = (do { $closure | call } | complete)
if $result.exit_code == 0 {
return ($result.stdout)
}
$attempts += 1
if $attempts >= $max_attempts {
error make {msg: $"Operation failed after ($attempts) attempts: ($result.stderr)"}
}
print $"Attempt ($attempts) failed, retrying in ($delay) seconds..."
sleep ($delay | into duration)
$delay = $delay * 2
2025-10-07 10:32:04 +01:00
}
}