2026-01-14 02:00:23 +00:00
|
|
|
# Hetzner Cloud utility functions
|
|
|
|
|
use env.nu *
|
2025-10-07 10:32:04 +01:00
|
|
|
|
2026-01-14 02:00:23 +00: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
|
|
|
}
|
2026-01-14 02:00:23 +00: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 {
|
2026-01-14 02:00:23 +00:00
|
|
|
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 "-")
|
chore: complete KCL to Nickel migration cleanup and setup pre-commit
Clean up 404 KCL references (99.75% complete):
- Rename kcl_* variables to schema_*/nickel_* (kcl_path→schema_path, etc.)
- Update functions: parse_kcl_file→parse_nickel_file
- Update env vars: KCL_MOD_PATH→NICKEL_IMPORT_PATH
- Fix cli/providers-install: add has_nickel and nickel_version variables
- Correct import syntax: .nickel.→.ncl.
- Update 57 files across core, CLI, config, and utilities
Configure pre-commit hooks:
- Activate: nushell-check, nickel-typecheck, markdownlint
- Comment out: Rust hooks (fmt, clippy, test), check-yaml
Testing:
- Module discovery: 9 modules (6 providers, 1 taskserv, 2 clusters) ✅
- Syntax validation: 15 core files ✅
- Pre-commit hooks: all passing ✅
2026-01-08 20:08:46 +00:00
|
|
|
}
|
2025-10-07 10:32:04 +01:00
|
|
|
}
|
2026-01-14 02:00:23 +00: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
|
chore: complete KCL to Nickel migration cleanup and setup pre-commit
Clean up 404 KCL references (99.75% complete):
- Rename kcl_* variables to schema_*/nickel_* (kcl_path→schema_path, etc.)
- Update functions: parse_kcl_file→parse_nickel_file
- Update env vars: KCL_MOD_PATH→NICKEL_IMPORT_PATH
- Fix cli/providers-install: add has_nickel and nickel_version variables
- Correct import syntax: .nickel.→.ncl.
- Update 57 files across core, CLI, config, and utilities
Configure pre-commit hooks:
- Activate: nushell-check, nickel-typecheck, markdownlint
- Comment out: Rust hooks (fmt, clippy, test), check-yaml
Testing:
- Module discovery: 9 modules (6 providers, 1 taskserv, 2 clusters) ✅
- Syntax validation: 15 core files ✅
- Pre-commit hooks: all passing ✅
2026-01-08 20:08:46 +00:00
|
|
|
} else {
|
2026-01-14 02:00:23 +00:00
|
|
|
($response | into string)
|
2025-10-07 10:32:04 +01:00
|
|
|
}
|
2026-01-14 02:00:23 +00: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)}
|
2026-01-14 02:00:23 +00:00
|
|
|
|
|
|
|
|
if not ($missing | is-empty) {
|
|
|
|
|
error make {msg: $"Missing required fields: ($missing | str join ", ")"}
|
2025-10-07 10:32:04 +01:00
|
|
|
}
|
2026-01-14 02:00:23 +00: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")
|
2026-01-14 02:00:23 +00:00
|
|
|
$"($timestamp) (UTC)"
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 10:24:17 +00:00
|
|
|
# Retry function with exponential backoff (no try-catch)
|
2026-01-14 02:00:23 +00:00
|
|
|
export def retry_with_backoff [closure: closure, max_attempts: int = 3, initial_delay: int = 1]: nothing -> any {
|
|
|
|
|
let mut attempts = 0
|
|
|
|
|
let mut delay = $initial_delay
|
|
|
|
|
|
|
|
|
|
loop {
|
2026-01-21 10:24:17 +00:00
|
|
|
let result = (do { $closure | call } | complete)
|
|
|
|
|
if $result.exit_code == 0 {
|
|
|
|
|
return ($result.stdout)
|
2026-01-14 02:00:23 +00:00
|
|
|
}
|
2026-01-21 10:24:17 +00:00
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
}
|