provisioning/core/nulib/main_provisioning/create_enhanced.nu
Jesús Pérez 6c538b62c8
feat: Complete config-driven architecture migration v2.0.0
Transform provisioning system from ENV-based to hierarchical config-driven architecture.
This represents a complete system redesign with breaking changes requiring migration.

## Migration Summary
- 65+ files migrated across entire codebase
- 200+ ENV variables replaced with 476 config accessors
- 29 syntax errors fixed across 17 files
- 92% token efficiency maintained during migration

## Core Features Added

### Hierarchical Configuration System
- 6-layer precedence: defaults → user → project → infra → env → runtime
- Deep merge strategy with intelligent precedence rules
- Multi-environment support (dev/test/prod) with auto-detection
- Configuration templates for all environments

### Enhanced Interpolation Engine
- Dynamic variables: {{paths.base}}, {{env.HOME}}, {{now.date}}
- Git context: {{git.branch}}, {{git.commit}}, {{git.remote}}
- SOPS integration: {{sops.decrypt()}} for secrets management
- Path operations: {{path.join()}} for dynamic construction
- Security: circular dependency detection, injection prevention

### Comprehensive Validation
- Structure, path, type, semantic, and security validation
- Code injection and path traversal detection
- Detailed error reporting with actionable messages
- Configuration health checks and warnings

## Architecture Changes

### Configuration Management (core/nulib/lib_provisioning/config/)
- loader.nu: 1600+ line hierarchical config loader with validation
- accessor.nu: 476 config accessor functions replacing ENV vars

### Provider System (providers/)
- AWS, UpCloud, Local providers fully config-driven
- Unified middleware system with standardized interfaces

### Task Services (core/nulib/taskservs/)
- Kubernetes, storage, networking, registry services migrated
- Template-driven configuration generation

### Cluster Management (core/nulib/clusters/)
- Complete lifecycle management through configuration
- Environment-specific cluster templates

## New Configuration Files
- config.defaults.toml: System defaults (84 lines)
- config.*.toml.example: Environment templates (400+ lines each)
- Enhanced CLI: validate, env, multi-environment support

## Security Enhancements
- Type-safe configuration access through validated functions
- SOPS integration for encrypted secrets management
- Input validation preventing injection attacks
- Environment isolation and access controls

## Breaking Changes
⚠️  ENV variables no longer supported as primary configuration
⚠️  Function signatures require --config parameter
⚠️  CLI arguments and return types modified
⚠️  Provider authentication now config-driven

## Migration Path
1. Backup current environment variables
2. Copy config.user.toml.example → config.user.toml
3. Migrate ENV vars to TOML format
4. Validate: ./core/nulib/provisioning validate config
5. Test functionality with new configuration

## Validation Results
 Structure valid
 Paths valid
 Types valid
 Semantic rules valid
 File references valid

System ready for production use with config-driven architecture.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-23 03:36:50 +01:00

153 lines
5.3 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Enhanced create command with better validation and logging
use ../lib_provisioning/config/accessor.nu *
export def "main create enhanced" [
target?: string # server (s) | taskserv (t) | cluster (c)
name?: string # Target name in settings
...args # Args for create command
--serverpos (-p): int # Server position in settings
--check (-c) # Only check mode no servers will be created
--wait (-w) # Wait servers to be created
--infra (-i): string # Infra path
--settings (-s): string # Settings path
--outfile (-o): string # Output file
--debug (-x) # Use Debug mode
--dry-run # Show what would be done without executing
--verbose (-v) # Verbose output
]: nothing -> nothing {
# Set debug mode
if $debug {
$env.PROVISIONING_DEBUG = true
print $"🐛 Debug mode enabled"
}
# Set output format
if ($outfile | is-not-empty) {
$env.PROVISIONING_OUT = $outfile
$env.PROVISIONING_NO_TERMINAL = true
}
# Validate target parameter
if ($target | is-empty) {
print $"🛑 Target parameter is required"
print "💡 Valid targets: server(s), taskserv(t), cluster(cl)"
print "💡 Example: provisioning create enhanced server my-server"
exit 1
}
# Validate target value
let valid_targets = ["server", "servers", "s", "taskserv", "taskservs", "task", "tasks", "t", "clusters", "cl"]
let is_valid_target = ($valid_targets | where {|t| $t == $target} | length) > 0
if not $is_valid_target {
print $"🛑 Invalid target: ($target)"
print $"💡 Valid targets: ($valid_targets | str join ', ')"
exit 1
}
# Log operation start
print $""
print $"📋 Creating ($target)"
print $"─────────────────────────────────────────────────────────────"
print $" Target: ($target)"
print $" Name: ($name | default 'default')"
if $dry_run {
print $"⚠️ DRY RUN MODE - No actual changes will be made"
}
# Validate settings path if provided
if ($settings | is-not-empty) {
if not ($settings | path exists) {
print $"🛑 Settings file not found: ($settings)"
exit 1
}
print $" Using settings: ($settings)"
}
# Validate infra path if provided
if ($infra | is-not-empty) {
if not ($infra | path exists) {
print $"🛑 Infra path not found: ($infra)"
exit 1
}
print $" Using infra: ($infra)"
}
# Execute the appropriate creation command
let use_debug = if $debug { "-x" } else { "" }
try {
match $target {
"server"| "servers" | "s" => {
print $" 📌 Creating server"
if $dry_run {
print $" Would execute: server creation command"
} else {
^$"((get-provisioning-name))" $use_debug -mod "server" ($env.PROVISIONING_ARGS | str replace $target '') --notitles
}
},
"taskserv" | "taskservs" | "task" | "tasks" | "t" => {
print $" 📌 Creating taskserv"
let ops = ($env.PROVISIONING_ARGS | split row " ")
let task = ($ops | get -o 0 | default "")
if $dry_run {
print $" Would execute: taskserv creation for task ($task)"
} else {
^$"((get-provisioning-name))" $use_debug -mod "taskserv" $task ($env.PROVISIONING_ARGS | str replace $"($task) ($target)" '') --notitles
}
},
"clusters"| "clusters" | "cl" => {
print $" 📌 Creating cluster"
if $dry_run {
print $" Would execute: cluster creation command"
} else {
^$"((get-provisioning-name))" $use_debug -mod "cluster" ($env.PROVISIONING_ARGS | str replace $target '') --notitles
}
}
}
if not $dry_run {
print $"✅ Successfully created ($target)"
} else {
print $"✅ Dry run completed successfully"
}
} catch {|err|
print $"🛑 Failed to create ($target)"
print $" Details: ($err.msg)"
exit 1
}
}
# Helper function to validate server configuration
export def validate-server-config [
server_config: record
]: bool {
let required_fields = ["hostname", "ip", "provider"]
let missing_fields = ($required_fields | where {|field|
($server_config | get -o $field | is-empty)
})
if ($missing_fields | length) > 0 {
print $"🛑 Missing required server configuration fields"
$missing_fields | each {|field|
print $" - ($field)"
}
return false
}
print $"✅ Server configuration is valid"
true
}
# Helper function to show creation progress
export def show-creation-progress [
current: int
total: int
operation: string
] {
let percent = (($current * 100) / $total)
print $"📊 ($operation) ($percent)%"
}