prvng_core/nulib/main_provisioning/contexts.nu
Jesús Pérez 894046ef5a
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

207 lines
6.9 KiB
Text

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 infra? | default null)
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 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 = if ($key in ($context | columns)) { $context | get $key } else { null }
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 (if $reset { "reset" } else { "" }) --context
},
_ => {
invalid_task "context" ($task | default "") --end
},
}
end_run $" create ($task) "
}
# Create workspace context file
export def "create-workspace-context" [
workspace_name: string
workspace_path: string
--set-active = true
] {
let user_config_dir = (setup_config_path)
let context_file = ($user_config_dir | path join $"ws_($workspace_name).yaml")
# Load template and interpolate
let template_path = ([
(get-provisioning-base-path)
"provisioning" "config" "templates" "user-context.yaml.template"
] | path join)
if not ($template_path | path exists) {
error make {
msg: $"Template not found: ($template_path)"
}
}
let template = (open $template_path)
let content = (
$template
| str replace --all "{{workspace.name}}" $workspace_name
| str replace --all "{{workspace.path}}" $workspace_path
| str replace --all "{{now.iso}}" (date now | format date "%Y-%m-%dT%H:%M:%SZ")
)
$content | save $context_file
if $set_active {
set-workspace-active $workspace_name
}
print $"✅ Created workspace context: ($context_file)"
}
# Set workspace as active
export def "set-workspace-active" [
workspace_name: string
] {
let user_config_dir = (setup_config_path)
# Deactivate all workspaces
for file in (ls $"($user_config_dir)/ws_*.yaml" | get name) {
let config = (open $file | from yaml)
let updated = ($config | upsert workspace.active false)
$updated | to yaml | save --force $file
}
# Activate target workspace
let target_file = ($user_config_dir | path join $"ws_($workspace_name).yaml")
if ($target_file | path exists) {
let config = (open $target_file | from yaml)
let updated = ($config | upsert workspace.active true)
$updated | to yaml | save --force $target_file
print $"✅ Set ($workspace_name) as active workspace"
} else {
error make {
msg: $"Workspace context not found: ($target_file)"
}
}
}
# List all workspace contexts
export def "list-workspace-contexts" [] {
let user_config_dir = (setup_config_path)
let ws_files = (do { ls $"($user_config_dir)/ws_*.yaml" } | default [])
$ws_files | each {|file|
let config = (open $file.name | from yaml)
{
name: $config.workspace.name
path: $config.workspace.path
active: ($config.workspace.active | default false)
last_used: ($config.metadata.last_used | default "")
}
}
}
# Get active workspace
export def "get-active-workspace-context" [] {
let contexts = (list-workspace-contexts)
$contexts | where active == true | first
}