prvng_core/nulib/taskservs/utils.nu

142 lines
5.9 KiB
Text
Raw Normal View History

2025-10-07 10:32:04 +01:00
use ../lib_provisioning/utils/ssh.nu *
use ../lib_provisioning/defs/lists.nu *
use ../lib_provisioning/config/accessor.nu *
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
# REMOVED: use lib_provisioning * - causes circular import
# Resolve taskserv/component library directory.
# Search order:
# 1. Flat: taskservs_path/{name}/ (covers components/ and old flat taskservs/)
# 2. Grouped: taskservs_path/{category}/{name}/ (old grouped taskservs/ structure)
# 3. Components sibling: ../components/{name}/ (when called with taskservs/ that no longer exists)
# Returns the directory containing the taskserv, or "" if not found.
export def find-taskserv-dir [taskservs_path: string, name: string]: nothing -> string {
let direct = ($taskservs_path | path join $name)
if ($direct | path exists) { return $direct }
# Try components/ sibling before scanning category subdirs (handles post-migration case)
let components_sibling = ($taskservs_path | path dirname | path join "components" | path join $name)
if ($components_sibling | path exists) { return $components_sibling }
if not ($taskservs_path | path exists) { return "" }
let candidate = (do -i { ls $taskservs_path }
| where type == "dir"
| each {|cat|
let p = ($cat.name | path join $name)
if ($p | path exists) { $p } else { null }
}
| compact)
if ($candidate | is-empty) { "" } else { $candidate | first }
}
2025-10-07 10:32:04 +01:00
export def taskserv_get_file [
settings: record
taskserv: record
server: record
live_ip: string
2025-10-07 10:32:04 +01:00
req_sudo: bool
local_mode: bool
] {
2025-10-07 10:32:04 +01:00
let target_path = ($taskserv.target_path | default "")
if $target_path == "" {
2025-10-07 10:32:04 +01:00
_print $"🛑 No (_ansi red_bold)target_path(_ansi reset) found in ($server.hostname) taskserv ($taskserv.name)"
return false
}
let source_path = ($taskserv.soruce_path | default "")
if $source_path == "" {
2025-10-07 10:32:04 +01:00
_print $"🛑 No (_ansi red_bold)source_path(_ansi reset) found in ($server.hostname) taskserv ($taskserv.name)"
return false
}
if $local_mode {
let res = (^cp $source_path $target_path | combine)
if $res.exit_code != 0 {
2025-10-07 10:32:04 +01:00
_print $"🛑 Error get_file [ local-mode ] (_ansi red_bold)($source_path) to ($target_path)(_ansi reset) in ($server.hostname) taskserv ($taskserv.name)"
_print $res.stdout
return false
}
2025-10-07 10:32:04 +01:00
return true
}
let ip = if $live_ip != "" {
$live_ip
} else {
2025-10-07 10:32:04 +01:00
#use ../../../providers/prov_lib/middleware.nu mw_get_ip
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
(mw_get_ip $settings $server ($server | get -o liveness_ip | default "public") false)
2025-10-07 10:32:04 +01:00
}
let ssh_key_path = ($server.ssh_key_path | default "")
if $ssh_key_path == "" {
2025-10-07 10:32:04 +01:00
_print $"🛑 No (_ansi red_bold)ssh_key_path(_ansi reset) found in ($server.hostname) taskserv ($taskserv.name)"
return false
}
if not ($ssh_key_path | path exists) {
2025-10-07 10:32:04 +01:00
_print $"🛑 Error (_ansi red_bold)($ssh_key_path)(_ansi reset) not found for ($server.hostname) taskserv ($taskserv.name)"
return false
}
mut cmd = if $req_sudo { "sudo" } else { "" }
let wk_path = $"/home/($env.SSH_USER)/($source_path | path basename)"
$cmd = $"($cmd) cp ($source_path) ($wk_path); sudo chown ($env.SSH_USER) ($wk_path)"
let res = (ssh_cmd $settings $server false $cmd $ip)
if not $res { return false }
if not (scp_from $settings $server $wk_path $target_path $ip ) {
return false
}
let rm_cmd = if $req_sudo {
$"sudo rm -f ($wk_path)"
} else {
$"rm -f ($wk_path)"
2025-10-07 10:32:04 +01:00
}
return ( ssh_cmd $settings $server false $rm_cmd $ip )
}
export def find_taskserv [
settings: record,
server: record,
taskserv_name: string,
out: string
] {
let taskservs_list = ($server | get taskservs? | default [])
let taskserv = ($taskservs_list | where {|t| ($t | get name? | default "") == $taskserv_name})
2025-10-07 10:32:04 +01:00
if ($taskserv | is-empty) {
_print $"🛑 No taskserv found" $"for (_ansi yellow_bold)($taskserv_name)(_ansi reset)"
return ""
}
let src_path = ($settings | get src_path? | default "")
let hostname = ($server | get hostname? | default "")
2025-10-07 10:32:04 +01:00
let run_taskservs_path = (get-run-taskservs-path)
mut taskserv_host_path = ($src_path | path join $run_taskservs_path |
path join $hostname | path join $"($taskserv_name).ncl")
2025-10-07 10:32:04 +01:00
let def_taskserv = if ($taskserv_host_path | path exists) {
(open -r $taskserv_host_path)
} else {
$taskserv_host_path = ($src_path | path join $run_taskservs_path | path join $"($taskserv_name).ncl")
2025-10-07 10:32:04 +01:00
if ($taskserv_host_path | path exists) {
(open -r $taskserv_host_path)
} else {
_print $"🛑 No taskserv path found" $"for (_ansi yellow_bold)($taskserv_name)(_ansi reset) in ($taskserv_host_path)"
$taskserv_host_path = ""
""
}
}
let taskservs_path = (get-taskservs-path)
mut main_taskserv_path = ($taskservs_path | path join $taskserv_name | path join "nickel" | path join $"($taskserv_name).ncl")
2025-10-07 10:32:04 +01:00
if not ($main_taskserv_path | path exists) {
$main_taskserv_path = ($taskservs_path | path join $taskserv_name | path join ($taskserv |
get -o profile | default "") | path join "nickel" | path join $"($taskserv_name).ncl")
2025-10-07 10:32:04 +01:00
}
let def_main = if ($main_taskserv_path | path exists) {
(open -r $main_taskserv_path)
} else {
_print $"🛑 No taskserv main path found" $"for (_ansi yellow_bold)($taskserv_name)(_ansi reset) ($main_taskserv_path)"
$main_taskserv_path = ""
""
}
{ path_def: $taskserv_host_path, def: $def_taskserv, path_main: $main_taskserv_path, main: $def_main }
}
export def list_taskservs [
settings: record
] {
2025-10-07 10:32:04 +01:00
let list_taskservs = (taskservs_list)
if ($list_taskservs | length) == 0 {
_print $"🛑 no items found for (_ansi cyan)taskservs list(_ansi reset)"
2025-10-07 10:32:04 +01:00
return
}
2025-10-07 10:32:04 +01:00
$list_taskservs
}