provisioning/core/nulib/main_provisioning/contexts.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

120 lines
4.5 KiB
Plaintext

use ops.nu provisioning_context_options
use ../lib_provisioning/config/accessor.nu *
use ../lib_provisioning/setup *
#> Manage contexts settings
export def "main context" [
task?: string # server (s) | task (t) | service (sv)
name?: string # server (s) | task (t) | service (sv)
--key (-k): string
--value (-v): string
...args # Args for create command
--reset (-r) # Restore defaults
--serverpos (-p): int # Server position in settings
--wait (-w) # Wait servers to be created
--settings (-s): string # Settings path
--outfile (-o): string # Output file
--debug (-x) # Use Debug mode
--xm # Debug with PROVISIONING_METADATA
--xc # Debuc for task and services locally PROVISIONING_DEBUG_CHECK
--xr # Debug for remote servers PROVISIONING_DEBUG_REMOTE
--xld # Log level with DEBUG PROVISIONING_LOG_LEVEL=debug
--metadata # Error with metadata (-xm)
--notitles # not tittles
] {
parse_help_command "context" --task {provisioning_context_options} --end
if $debug { $env.PROVISIONING_DEBUG = true }
let config_path = (setup_config_path)
let default_context_path = ($config_path | path join "default_context.yaml")
let name_context_path = ($config_path | path join $"($name).yaml")
let context_path = ($config_path | path join "context.yaml")
let set_as_default = {
rm -f $context_path
^ln -s $name_context_path $context_path
_print (
$"(_ansi blue_bold)($name)(_ansi reset) set as (_ansi green)default context(_ansi reset)" +
$" in (_ansi default_dimmed)($config_path)(_ansi reset)"
)
}
match $task {
"h" => {
^$"((get-provisioning-name))" context --help
_print (provisioning_context_options)
}
"create" | "c" | "new" => {
if $name == null or $name == "" {
_print $"🛑 No (_ansi red)name(_ansi reset) value "
}
if ($name_context_path |path exists) {
_print $"(_ansi blue_bold)($name)(_ansi reset) already in (_ansi default_dimmed)($config_path)(_ansi reset)"
} else {
^cp $default_context_path $name_context_path
open -r $name_context_path | str replace "infra: " $"infra: ($name)" | save -f $name_context_path
_print $"(_ansi blue_bold)($name)(_ansi reset) created in (_ansi default_dimmed)($config_path)(_ansi reset)"
}
do $set_as_default
},
"default" | "d" => {
if $name == null or $name == "" {
_print $"🛑 No (_ansi red)name(_ansi reset) value "
exit 1
}
if not ($name_context_path | path exists) {
_print $"🛑 No (_ansi red)($name)(_ansi reset) found in (_ansi default_dimmed)($config_path)(_ansi reset) "
exit 1
}
do $set_as_default
},
"remove" | "r" => {
if $name == null {
_print $"🛑 No (_ansi red)name(_ansi reset) value "
exit 1
}
if $name == "" or not ( $name_context_path | path exists) {
_print $"🛑 context path (_ansi blue_bold)($name)(_ansi reset) not found "
exit 1
}
let context = (setup_user_context $name)
let curr_infra = ($context | get -o "infra")
if $curr_infra == $name {
_print (
$"(_ansi blue_bold)($name)(_ansi reset) removed as (_ansi green)default context(_ansi reset) " +
$" in (_ansi default_dimmed)($config_path)(_ansi reset)"
)
}
rm -f $name_context_path $context_path
_print $"(_ansi blue_bold)($name)(_ansi reset) context removed "
},
"edit" | "e" => {
let editor = ($env | get -o EDITOR | default "vi")
let config_path = (setup_user_context_path $name)
^$editor $config_path
},
"view" | "v" => {
_print ((setup_user_context $name) | table -e)
},
"set" | "s" => {
let context = (setup_user_context $name)
let curr_value = ($context | get -o $key)
if $curr_value == null {
_print $"🛑 invalid ($key) in setup "
exit 1
}
if $curr_value == $value {
_print $"🛑 ($key) ($value) already set "
exit 1
}
# if $context != null and ( $context.infra | path exists) { return $context.infra }
let new_context = ($context | update $key $value)
setup_save_context $new_context
},
"i" | "install" => {
install_config $reset --context
},
_ => {
invalid_task "context" ($task | default "") --end
},
}
end_run $" create ($task) "
}