chore: add current provisioning state before migration

This commit is contained in:
Jesús Pérez 2025-09-22 23:11:41 +01:00
parent a9703b4748
commit 50745b0f22
660 changed files with 88126 additions and 0 deletions

View file

@ -0,0 +1,82 @@
use lib_provisioning *
#use ../lib_provisioning/utils/generate.nu *
use utils.nu *
# Provider middleware now available through lib_provisioning
# > Clusters services
export def "main create" [
name?: string # Server hostname in settings
...args # Args for create command
--infra (-i): string # infra directory
--settings (-s): string # Settings path
--outfile (-o): string # Output file
--cluster_pos (-p): int # Server position in settings
--check (-c) # Only check mode no clusters will be created
--wait (-w) # Wait clusters to be created
--select: string # Select with task as option
--debug (-x) # Use Debug mode
--xm # Debug with PROVISIONING_METADATA
--xc # Debuc for task and services locally PROVISIONING_DEBUG_CHECK
--xr # Debug for remote clusters PROVISIONING_DEBUG_REMOTE
--xld # Log level with DEBUG PROVISIONING_LOG_LEVEL=debug
--metadata # Error with metadata (-xm)
--notitles # not tittles
--helpinfo (-h) # For more details use options "help" (no dashes)
--out: string # Print Output format: json, yaml, text (default)
]: nothing -> nothing {
if ($out | is-not-empty) {
$env.PROVISIONING_OUT = $out
$env.PROVISIONING_NO_TERMINAL = true
}
provisioning_init $helpinfo "cluster create" $args
#parse_help_command "cluster create" $name --ismod --end
# print "on cluster main create"
if $debug { $env.PROVISIONING_DEBUG = true }
if $metadata { $env.PROVISIONING_METADATA = true }
if $name != null and $name != "h" and $name != "help" {
let curr_settings = (find_get_settings --infra $infra --settings $settings)
if ($curr_settings.data.clusters | find $name| length) == 0 {
_print $"🛑 invalid name ($name)"
exit 1
}
}
let task = if ($args | length) > 0 {
($args| get 0)
} else {
let str_task = (($env.PROVISIONING_ARGS? | default "") | str replace "create " " " )
let str_task = if $name != null {
($str_task | str replace $name "")
} else {
$str_task
}
($str_task | str trim | split row " " | get -o 0 | default "" |
split row "-" | get -o 0 | default "" | str trim )
}
let other = if ($args | length) > 0 { ($args| skip 1) } else { "" }
let ops = $"($env.PROVISIONING_ARGS? | default "") " | str replace $"($task) " "" | str trim
let run_create = {
let curr_settings = (find_get_settings --infra $infra --settings $settings)
$env.WK_CNPROV = $curr_settings.wk_path
let match_name = if $name == null or $name == "" { "" } else { $name}
on_clusters $curr_settings $check $wait $outfile $match_name $cluster_pos
}
match $task {
"" if $name == "h" => {
^$"($env.PROVISIONING_NAME)" -mod cluster create help --notitles
},
"" if $name == "help" => {
^$"($env.PROVISIONING_NAME)" -mod cluster create --help
print (provisioning_options "create")
},
"" => {
let result = desktop_run_notify $"($env.PROVISIONING_NAME) clusters create" "-> " $run_create --timeout 11sec
#do $run_create
},
_ => {
if $task != "" { print $"🛑 invalid_option ($task)" }
print $"\nUse (_ansi blue_bold)($env.PROVISIONING_NAME) -h(_ansi reset) for help on commands and options"
}
}
# "" | "create"
if not $env.PROVISIONING_DEBUG { end_run "" }
}

View file

@ -0,0 +1,82 @@
use lib_provisioning *
#use ../lib_provisioning/utils/generate.nu *
use utils.nu *
# Provider middleware now available through lib_provisioning
# > Clusters services
export def "main generate" [
name?: string # Server hostname in settings
...args # Args for generate command
--infra (-i): string # Infra directory
--settings (-s): string # Settings path
--outfile (-o): string # Output file
--cluster_pos (-p): int # Server position in settings
--check (-c) # Only check mode no clusters will be generated
--wait (-w) # Wait clusters to be generated
--select: string # Select with task as option
--debug (-x) # Use Debug mode
--xm # Debug with PROVISIONING_METADATA
--xc # Debuc for task and services locally PROVISIONING_DEBUG_CHECK
--xr # Debug for remote clusters PROVISIONING_DEBUG_REMOTE
--xld # Log level with DEBUG PROVISIONING_LOG_LEVEL=debug
--metadata # Error with metadata (-xm)
--notitles # not tittles
--helpinfo (-h) # For more details use options "help" (no dashes)
--out: string # Print Output format: json, yaml, text (default)
]: nothing -> nothing {
if ($out | is-not-empty) {
$env.PROVISIONING_OUT = $out
$env.PROVISIONING_NO_TERMINAL = true
}
provisioning_init $helpinfo "cluster generate" $args
#parse_help_command "cluster generate" $name --ismod --end
# print "on cluster main generate"
if $debug { $env.PROVISIONING_DEBUG = true }
if $metadata { $env.PROVISIONING_METADATA = true }
# if $name != null and $name != "h" and $name != "help" {
# let curr_settings = (find_get_settings --infra $infra --settings $settings)
# if ($curr_settings.data.clusters | find $name| length) == 0 {
# _print $"🛑 invalid name ($name)"
# exit 1
# }
# }
let task = if ($args | length) > 0 {
($args| get 0)
} else {
let str_task = (($env.PROVISIONING_ARGS? | default "") | str replace "generate " " " )
let str_task = if $name != null {
($str_task | str replace $name "")
} else {
$str_task
}
($str_task | str trim | split row " " | get -o 0 | default "" |
split row "-" | get -o 0 | default "" | str trim )
}
let other = if ($args | length) > 0 { ($args| skip 1) } else { "" }
let ops = $"($env.PROVISIONING_ARGS? | default "") " | str replace $"($task) " "" | str trim
let run_generate = {
let curr_settings = (find_get_settings --infra $infra --settings $settings)
$env.WK_CNPROV = $curr_settings.wk_path
let match_name = if $name == null or $name == "" { "" } else { $name}
# on_clusters $curr_settings $check $wait $outfile $match_name $cluster_pos
}
match $task {
"" if $name == "h" => {
^$"($env.PROVISIONING_NAME)" -mod cluster generate help --notitles
},
"" if $name == "help" => {
^$"($env.PROVISIONING_NAME)" -mod cluster generate --help
print (provisioning_options "generate")
},
"" => {
let result = desktop_run_notify $"($env.PROVISIONING_NAME) clusters generate" "-> " $run_generate --timeout 11sec
#do $run_generate
},
_ => {
if $task != "" { print $"🛑 invalid_option ($task)" }
print $"\nUse (_ansi blue_bold)($env.PROVISIONING_NAME) -h(_ansi reset) for help on commands and options"
}
}
# "" | "generate"
if not $env.PROVISIONING_DEBUG { end_run "" }
}

View file

@ -0,0 +1,121 @@
use utils.nu servers_selector
#use clusters/run.nu run_cluster
def install_from_server [
defs: record
server_cluster_path: string
wk_server: string
]: nothing -> bool {
_print $"($defs.cluster.name) on ($defs.server.hostname) install (_ansi purple_bold)from ($defs.cluster_install_mode)(_ansi reset)"
run_cluster $defs ($env.PROVISIONING_RUN_CLUSTERS_PATH | path join $defs.cluster.name | path join $server_cluster_path)
($wk_server | path join $defs.cluster.name)
}
def install_from_library [
defs: record
server_cluster_path: string
wk_server: string
]: nothing -> bool {
_print $"($defs.cluster.name) on ($defs.server.hostname) installed (_ansi purple_bold)from library(_ansi reset)"
run_cluster $defs ($env.PROVISIONING_CLUSTERS_PATH |path join $defs.cluster.name | path join $defs.cluster_profile)
($wk_server | path join $defs.cluster.name)
}
export def on_clusters [
settings: record
match_cluster: string
match_server: string
iptype: string
check: bool
]: nothing -> bool {
# use ../../../providers/prov_lib/middleware.nu mw_get_ip
_print $"Running (_ansi yellow_bold)clusters(_ansi reset) ..."
if $env.PROVISIONING_SOPS? == null {
# A SOPS load env
$env.CURRENT_INFRA_PATH = $"($settings.infra_path)/($settings.infra)"
use sops_env.nu
}
let ip_type = if $iptype == "" { "public" } else { $iptype }
mut server_pos = -1
mut cluster_pos = -1
mut curr_cluster = 0
let created_clusters_dirpath = ( $settings.data.created_clusters_dirpath | default "/tmp" |
str replace "./" $"($settings.src_path)/" | str replace "~" $env.HOME | str replace "NOW" $env.NOW
)
let root_wk_server = ($created_clusters_dirpath | path join "on-server")
if not ($root_wk_server | path exists ) { ^mkdir "-p" $root_wk_server }
let dflt_clean_created_clusters = ($settings.data.defaults_servers.clean_created_clusters? | default $created_clusters_dirpath |
str replace "./" $"($settings.src_path)/" | str replace "~" $env.HOME
)
let run_ops = if $env.PROVISIONING_DEBUG { "bash -x" } else { "" }
for srvr in $settings.data.servers {
# continue
_print $"on (_ansi green_bold)($srvr.hostname)(_ansi reset) ..."
$server_pos += 1
$cluster_pos = -1
_print $"On server ($srvr.hostname) pos ($server_pos) ..."
if $match_server != "" and $srvr.hostname != $match_server { continue }
let clean_created_clusters = (($settings.data.servers | get -o $server_pos).clean_created_clusters? | default $dflt_clean_created_clusters )
let ip = if $env.PROVISIONING_DEBUG_CHECK {
"127.0.0.1"
} else {
let curr_ip = (mw_get_ip $settings $srvr $ip_type false | default "")
if $curr_ip == "" {
_print $"🛑 No IP ($ip_type) found for (_ansi green_bold)($srvr.hostname)(_ansi reset) ($server_pos) "
continue
}
#use utils.nu wait_for_server
if not (wait_for_server $server_pos $srvr $settings $curr_ip) {
print $"🛑 server ($srvr.hostname) ($curr_ip) (_ansi red_bold)not in running state(_ansi reset)"
continue
}
$curr_ip
}
let server = ($srvr | merge { ip_addresses: { pub: $ip, priv: $srvr.network_private_ip }})
let wk_server = ($root_wk_server | path join $server.hostname)
if ($wk_server | path exists ) { rm -rf $wk_server }
^mkdir "-p" $wk_server
for cluster in $server.clusters {
$cluster_pos += 1
if $cluster_pos > $curr_cluster { break }
$curr_cluster += 1
if $match_cluster != "" and $match_cluster != $cluster.name { continue }
if not ($env.PROVISIONING_CLUSTERS_PATH | path join $cluster.name | path exists) {
print $"cluster path: ($env.PROVISIONING_CLUSTERS_PATH | path join $cluster.name) (_ansi red_bold)not found(_ansi reset)"
continue
}
if not ($wk_server | path join $cluster.name| path exists) { ^mkdir "-p" ($wk_server | path join $cluster.name) }
let $cluster_profile = if $cluster.profile == "" { "default" } else { $cluster.profile }
let $cluster_install_mode = if $cluster.install_mode == "" { "library" } else { $cluster.install_mode }
let server_cluster_path = ($server.hostname | path join $cluster_profile)
let defs = {
settings: $settings, server: $server, cluster: $cluster,
cluster_install_mode: $cluster_install_mode, cluster_profile: $cluster_profile,
pos: { server: $"($server_pos)", cluster: $cluster_pos}, ip: $ip }
match $cluster.install_mode {
"server" | "getfile" => {
(install_from_server $defs $server_cluster_path $wk_server )
},
"library-server" => {
(install_from_library $defs $server_cluster_path $wk_server)
(install_from_server $defs $server_cluster_path $wk_server )
},
"server-library" => {
(install_from_server $defs $server_cluster_path $wk_server )
(install_from_library $defs $server_cluster_path $wk_server)
},
"library" => {
(install_from_library $defs $server_cluster_path $wk_server)
},
}
if $clean_created_clusters == "yes" { rm -rf ($wk_server | pth join $cluster.name) }
}
if $clean_created_clusters == "yes" { rm -rf $wk_server }
print $"Clusters completed on ($server.hostname)"
}
if ("/tmp/k8s_join.sh" | path exists) { cp "/tmp/k8s_join.sh" $root_wk_server ; rm -r /tmp/k8s_join.sh }
if $dflt_clean_created_clusters == "yes" { rm -rf $root_wk_server }
print $"✅ Clusters (_ansi green_bold)completed(_ansi reset) ....."
#use utils.nu servers_selector
servers_selector $settings $ip_type false
true
}

View file

@ -0,0 +1,5 @@
export use utils.nu *
export use handlers.nu *
export use generate.nu *
export use run.nu *
export use ops.nu *

View file

@ -0,0 +1,13 @@
export def provisioning_options [
source: string
]: nothing -> string {
(
$"(_ansi blue_bold)($env.PROVISIONING_NAME) server ($source)(_ansi reset) options:\n" +
$"(_ansi blue)($env.PROVISIONING_NAME)(_ansi reset) sed - to edit content from a SOPS file\n" +
$"(_ansi blue)($env.PROVISIONING_NAME)(_ansi reset) ssh - to config and get SSH settings for servers\n" +
$"(_ansi blue)($env.PROVISIONING_NAME)(_ansi reset) list [items] - to list items: \n" +
$"[ (_ansi green)providers(_ansi reset) p | (_ansi green)tasks(_ansi reset) t | (_ansi green)services(_ansi reset) s ]\n" +
$"(_ansi blue)($env.PROVISIONING_NAME)(_ansi reset) nu - to run a nushell in ($env.PROVISIONING) path\n" +
$"(_ansi blue)($env.PROVISIONING_NAME)(_ansi reset) qr - to get ($env.PROVISIONING_URL) QR code"
)
}

283
core/nulib/clusters/run.nu Normal file
View file

@ -0,0 +1,283 @@
#use utils.nu cluster_get_file
#use utils/templates.nu on_template_path
use std
def make_cmd_env_temp [
defs: record
cluster_env_path: string
wk_vars: string
]: nothing -> string {
let cmd_env_temp = $"($cluster_env_path)/cmd_env_(mktemp --tmpdir-path $cluster_env_path --suffix ".sh" | path basename)"
# export all 'PROVISIONING_' $env vars to SHELL
($"export NU_LOG_LEVEL=($env.NU_LOG_LEVEL)\n" +
($env | items {|key, value| if ($key | str starts-with "PROVISIONING_") {echo $'export ($key)="($value)"\n'} } | compact --empty | to text)
) | save --force $cmd_env_temp
$cmd_env_temp
}
def run_cmd [
cmd_name: string
title: string
where: string
defs: record
cluster_env_path: string
wk_vars: string
]: nothing -> nothing {
_print $"($title) for ($defs.cluster.name) on ($defs.server.hostname) ($defs.pos.server) ..."
if $defs.check { return }
let runner = (grep "^#!" $"($cluster_env_path)/($cmd_name)" | str trim)
let run_ops = if $env.PROVISIONING_DEBUG { if ($runner | str contains "bash" ) { "-x" } else { "" } } else { "" }
let cmd_env_temp = make_cmd_env_temp $defs $cluster_env_path $wk_vars
if ($wk_vars | path exists) {
let run_res = if ($runner | str ends-with "bash" ) {
(^bash -c $"'source ($cmd_env_temp) ; bash ($run_ops) ($cluster_env_path)/($cmd_name) ($wk_vars) ($defs.pos.server) ($defs.pos.cluster) (^pwd)'" | complete)
} else if ($runner | str ends-with "nu" ) {
(^bash -c $"'source ($cmd_env_temp); ($env.NU) ($env.NU_ARGS) ($cluster_env_path)/($cmd_name)'" | complete)
} else {
(^bash -c $"'source ($cmd_env_temp); ($cluster_env_path)/($cmd_name) ($wk_vars)'" | complete)
}
rm -f $cmd_env_temp
if $run_res.exit_code != 0 {
(throw-error $"🛑 Error server ($defs.server.hostname) cluster ($defs.cluster.name)
($cluster_env_path)/($cmd_name) with ($wk_vars) ($defs.pos.server) ($defs.pos.cluster) (^pwd)"
$run_res.stdout
$where --span (metadata $run_res).span)
exit 1
}
if not $env.PROVISIONING_DEBUG { rm -f $"($cluster_env_path)/prepare" }
}
}
export def run_cluster_library [
defs: record
cluster_path: string
cluster_env_path: string
wk_vars: string
]: nothing -> bool {
if not ($cluster_path | path exists) { return false }
let prov_resources_path = ($defs.settings.data.prov_resources_path | default "" | str replace "~" $env.HOME)
let cluster_server_name = $defs.server.hostname
rm -rf ($cluster_env_path | path join "*.k") ($cluster_env_path | path join "kcl")
mkdir ($cluster_env_path | path join "kcl")
let err_out = ($cluster_env_path | path join (mktemp --tmpdir-path $cluster_env_path --suffix ".err") | path basename)
let kcl_temp = ($cluster_env_path | path join "kcl" | path join (mktemp --tmpdir-path $cluster_env_path --suffix ".k" ) | path basename)
let wk_format = if $env.PROVISIONING_WK_FORMAT == "json" { "json" } else { "yaml" }
let wk_data = { defs: $defs.settings.data, pos: $defs.pos, server: $defs.server }
if $wk_format == "json" {
$wk_data | to json | save --force $wk_vars
} else {
$wk_data | to yaml | save --force $wk_vars
}
if $env.PROVISIONING_USE_KCL {
cd ($defs.settings.infra_path | path join $defs.settings.infra)
let kcl_cluster_path = if ($cluster_path | path join "kcl"| path join $"($defs.cluster.name).k" | path exists) {
($cluster_path | path join "kcl"| path join $"($defs.cluster.name).k")
} else if (($cluster_path | path dirname) | path join "kcl"| path join $"($defs.cluster.name).k" | path exists) {
(($cluster_path | path dirname) | path join "kcl"| path join $"($defs.cluster.name).k")
} else { "" }
if ($kcl_temp | path exists) { rm -f $kcl_temp }
let res = (^kcl import -m $wk_format $wk_vars -o $kcl_temp | complete)
if $res.exit_code != 0 {
print $"❗KCL import (_ansi red_bold)($wk_vars)(_ansi reset) Errors found "
print $res.stdout
rm -f $kcl_temp
cd $env.PWD
return false
}
# Very important! Remove external block for import and re-format it
# ^sed -i "s/^{//;s/^}//" $kcl_temp
open $kcl_temp -r | lines | find -v --regex "^{" | find -v --regex "^}" | save -f $kcl_temp
^kcl fmt $kcl_temp
if $kcl_cluster_path != "" and ($kcl_cluster_path | path exists) { cat $kcl_cluster_path | save --append $kcl_temp }
# } else { print $"❗ No cluster kcl ($defs.cluster.k) path found " ; return false }
if $env.PROVISIONING_KEYS_PATH != "" {
#use sops on_sops
let keys_path = ($defs.settings.src_path | path join $env.PROVISIONING_KEYS_PATH)
if not ($keys_path | path exists) {
if $env.PROVISIONING_DEBUG {
print $"❗Error KEYS_PATH (_ansi red_bold)($keys_path)(_ansi reset) found "
} else {
print $"❗Error (_ansi red_bold)KEYS_PATH(_ansi reset) not found "
}
return false
}
(on_sops d $keys_path) | save --append $kcl_temp
if ($defs.settings.src_path | path join "clusters" | path join $defs.server.hostname | path join $"($defs.cluster.name).k" | path exists ) {
cat ($defs.settings.src_path | path join "clusters" | path join $defs.server.hostname| path join $"($defs.cluster.name).k" ) | save --append $kcl_temp
} else if ($defs.settings.src_path | path join "clusters" | path join $defs.pos.server | path join $"($defs.cluster.name).k" | path exists ) {
cat ($defs.settings.src_path | path join "clusters" | path join $defs.pos.server | path join $"($defs.cluster.name).k" ) | save --append $kcl_temp
} else if ($defs.settings.src_path | path join "clusters" | path join $"($defs.cluster.name).k" | path exists ) {
cat ($defs.settings.src_path | path join "clusters" | path join $"($defs.cluster.name).k" ) | save --append $kcl_temp
}
let res = (^kcl $kcl_temp -o $wk_vars | complete)
if $res.exit_code != 0 {
print $"❗KCL errors (_ansi red_bold)($kcl_temp)(_ansi reset) found "
print $res.stdout
rm -f $wk_vars
cd $env.PWD
return false
}
rm -f $kcl_temp $err_out
} else if ($defs.settings.src_path | path join "clusters" | path join $"($defs.cluster.name).yaml" | path exists) {
cat ($defs.settings.src_path | path join "clusters" | path join $"($defs.cluster.name).yaml" ) | tee { save -a $wk_vars } | ignore
}
cd $env.PWD
}
(^sed -i $"s/NOW/($env.NOW)/g" $wk_vars)
if $defs.cluster_install_mode == "library" {
let cluster_data = (open $wk_vars)
let verbose = if $env.PROVISIONING_DEBUG { true } else { false }
if $cluster_data.cluster.copy_paths? != null {
#use utils/files.nu *
for it in $cluster_data.cluster.copy_paths {
let it_list = ($it | split row "|" | default [])
let cp_source = ($it_list | get -o 0 | default "")
let cp_target = ($it_list | get -o 1 | default "")
if ($cp_source | path exists) {
copy_prov_files $cp_source ($defs.settings.infra_path | path join $defs.settings.infra) $"($cluster_env_path)/($cp_target)" false $verbose
} else if ($"($prov_resources_path)/($cp_source)" | path exists) {
copy_prov_files $prov_resources_path $cp_source $"($cluster_env_path)/($cp_target)" false $verbose
} else if ($cp_source | file exists) {
copy_prov_file $cp_source $"($cluster_env_path)/($cp_target)" $verbose
} else if ($"($prov_resources_path)/($cp_source)" | path exists) {
copy_prov_file $"($prov_resources_path)/($cp_source)" $"($cluster_env_path)/($cp_target)" $verbose
}
}
}
}
rm -f ($cluster_env_path | path join "kcl") ($cluster_env_path | path join "*.k")
on_template_path $cluster_env_path $wk_vars true true
if ($cluster_env_path | path join $"env-($defs.cluster.name)" | path exists) {
^sed -i 's,\t,,g;s,^ ,,g;/^$/d' ($cluster_env_path | path join $"env-($defs.cluster.name)")
}
if ($cluster_env_path | path join "prepare" | path exists) {
run_cmd "prepare" "Prepare" "run_cluster_library" $defs $cluster_env_path $wk_vars
if ($cluster_env_path | path join "resources" | path exists) {
on_template_path ($cluster_env_path | path join "resources") $wk_vars false true
}
}
if not $env.PROVISIONING_DEBUG {
rm -f ($cluster_env_path | path join "*.j2") $err_out $kcl_temp
}
true
}
export def run_cluster [
defs: record
cluster_path: string
env_path: string
]: nothing -> bool {
if not ($cluster_path | path exists) { return false }
if $defs.check { return }
let prov_resources_path = ($defs.settings.data.prov_resources_path | default "" | str replace "~" $env.HOME)
let created_clusters_dirpath = ($defs.settings.data.created_clusters_dirpath | default "/tmp" |
str replace "~" $env.HOME | str replace "NOW" $env.NOW | str replace "./" $"($defs.settings.src_path)/")
let cluster_server_name = $defs.server.hostname
let cluster_env_path = if $defs.cluster_install_mode == "server" { $"($env_path)_($defs.cluster_install_mode)" } else { $env_path }
if not ( $cluster_env_path | path exists) { ^mkdir -p $cluster_env_path }
if not ( $created_clusters_dirpath | path exists) { ^mkdir -p $created_clusters_dirpath }
(^cp -pr $"($cluster_path)/*" $cluster_env_path)
rm -rf $"($cluster_env_path)/*.k" $"($cluster_env_path)/kcl"
let wk_vars = $"($created_clusters_dirpath)/($defs.server.hostname).yaml"
# if $defs.cluster.name == "kubernetes" and ("/tmp/k8s_join.sh" | path exists) { cp -pr "/tmp/k8s_join.sh" $cluster_env_path }
let require_j2 = (^ls ($cluster_env_path | path join "*.j2") err> (if $nu.os-info.name == "windows" { "NUL" } else { "/dev/null" }))
let res = if $defs.cluster_install_mode == "library" or $require_j2 != "" {
(run_cluster_library $defs $cluster_path $cluster_env_path $wk_vars)
}
if not $res {
if not $env.PROVISIONING_DEBUG { rm -f $wk_vars }
return $res
}
let err_out = ($env_path | path join (mktemp --tmpdir-path $env_path --suffix ".err") | path basename)
let tar_ops = if $env.PROVISIONING_DEBUG { "v" } else { "" }
let bash_ops = if $env.PROVISIONING_DEBUG { "bash -x" } else { "" }
let res_tar = (^tar -C $cluster_env_path $"-c($tar_ops)zf" $"/tmp/($defs.cluster.name).tar.gz" . | complete)
if $res_tar.exit_code != 0 {
_print (
$"🛑 Error (_ansi red_bold)tar cluster(_ansi reset) server (_ansi green_bold)($defs.server.hostname)(_ansi reset)" +
$" cluster (_ansi yellow_bold)($defs.cluster.name)(_ansi reset) ($cluster_env_path) -> /tmp/($defs.cluster.name).tar.gz"
)
_print $res_tar.stdout
return false
}
if $defs.check {
if not $env.PROVISIONING_DEBUG {
rm -f $wk_vars
rm -f $err_out
rm -rf $"($cluster_env_path)/*.k" $"($cluster_env_path)/kcl"
}
return true
}
let is_local = (^ip addr | grep "inet " | grep "$defs.ip")
if $is_local != "" and not $env.PROVISIONING_DEBUG_CHECK {
if $defs.cluster_install_mode == "getfile" {
if (cluster_get_file $defs.settings $defs.cluster $defs.server $defs.ip true true) { return false }
return true
}
rm -rf $"/tmp/($defs.cluster.name)"
mkdir $"/tmp/($defs.cluster.name)"
cd $"/tmp/($defs.cluster.name)"
tar x($tar_ops)zf $"/tmp/($defs.cluster.name).tar.gz"
let res_run = (^sudo $bash_ops $"./install-($defs.cluster.name).sh" err> $err_out | complete)
if $res_run.exit_code != 0 {
(throw-error $"🛑 Error server ($defs.server.hostname) cluster ($defs.cluster.name)
./install-($defs.cluster.name).sh ($defs.server_pos) ($defs.cluster_pos) (^pwd)"
$"($res_run.stdout)\n(cat $err_out)"
"run_cluster_library" --span (metadata $res_run).span)
exit 1
}
fi
rm -fr $"/tmp/($defs.cluster.name).tar.gz" $"/tmp/($defs.cluster.name)"
} else {
if $defs.cluster_install_mode == "getfile" {
if (cluster_get_file $defs.settings $defs.cluster $defs.server $defs.ip true false) { return false }
return true
}
if not $env.PROVISIONING_DEBUG_CHECK {
#use ssh.nu *
let scp_list: list<string> = ([] | append $"/tmp/($defs.cluster.name).tar.gz")
if not (scp_to $defs.settings $defs.server $scp_list "/tmp" $defs.ip) {
_print (
$"🛑 Error (_ansi red_bold)ssh_cp(_ansi reset) server (_ansi green_bold)($defs.server.hostname)(_ansi reset) [($defs.ip)] " +
$" cluster (_ansi yellow_bold)($defs.cluster.name)(_ansi reset) /tmp/($defs.cluster.name).tar.gz"
)
return false
}
let cmd = (
$"rm -rf /tmp/($defs.cluster.name) ; mkdir /tmp/($defs.cluster.name) ; cd /tmp/($defs.cluster.name) ;" +
$" sudo tar x($tar_ops)zf /tmp/($defs.cluster.name).tar.gz;" +
$" sudo ($bash_ops) ./install-($defs.cluster.name).sh " # ($env.PROVISIONING_MATCH_CMD) "
)
if not (ssh_cmd $defs.settings $defs.server true $cmd $defs.ip) {
_print (
$"🛑 Error (_ansi red_bold)ssh_cmd(_ansi reset) server (_ansi green_bold)($defs.server.hostname)(_ansi reset) [($defs.ip)] " +
$" cluster (_ansi yellow_bold)($defs.cluster.name)(_ansi reset) install_($defs.cluster.name).sh"
)
return false
}
# if $defs.cluster.name == "kubernetes" { let _res_k8s = (scp_from $defs.settings $defs.server "/tmp/k8s_join.sh" "/tmp" $defs.ip) }
if not $env.PROVISIONING_DEBUG {
let rm_cmd = $"sudo rm -f /tmp/($defs.cluster.name).tar.gz; sudo rm -rf /tmp/($defs.cluster.name)"
let _res = (ssh_cmd $defs.settings $defs.server true $rm_cmd $defs.ip)
rm -f $"/tmp/($defs.cluster.name).tar.gz"
}
}
}
if ($"($cluster_path)/postrun" | path exists ) {
cp $"($cluster_path)/postrun" $"($cluster_env_path)/postrun"
run_cmd "postrun" "PostRune" "run_cluster_library" $defs $cluster_env_path $wk_vars
}
if not $env.PROVISIONING_DEBUG {
rm -f $wk_vars
rm -f $err_out
rm -rf $"($cluster_env_path)/*.k" $"($cluster_env_path)/kcl"
}
true
}

View file

@ -0,0 +1,61 @@
#use ssh.nu *
export def cluster_get_file [
settings: record
cluster: record
server: record
live_ip: string
req_sudo: bool
local_mode: bool
]: nothing -> bool {
let target_path = ($cluster.target_path | default "")
if $target_path == "" {
_print $"🛑 No (_ansi red_bold)target_path(_ansi reset) found in ($server.hostname) cluster ($cluster.name)"
return false
}
let source_path = ($cluster.soruce_path | default "")
if $source_path == "" {
_print $"🛑 No (_ansi red_bold)source_path(_ansi reset) found in ($server.hostname) cluster ($cluster.name)"
return false
}
if $local_mode {
let res = (^cp $source_path $target_path | combine)
if $res.exit_code != 0 {
_print $"🛑 Error get_file [ local-mode ] (_ansi red_bold)($source_path) to ($target_path)(_ansi reset) in ($server.hostname) cluster ($cluster.name)"
_print $res.stdout
return false
}
return true
}
let ip = if $live_ip != "" {
$live_ip
} else {
#use ../../../providers/prov_lib/middleware.nu mw_get_ip
(mw_get_ip $settings $server $server.liveness_ip false)
}
let ssh_key_path = ($server.ssh_key_path | default "")
if $ssh_key_path == "" {
_print $"🛑 No (_ansi red_bold)ssh_key_path(_ansi reset) found in ($server.hostname) cluster ($cluster.name)"
return false
}
if not ($ssh_key_path | path exists) {
_print $"🛑 Error (_ansi red_bold)($ssh_key_path)(_ansi reset) not found for ($server.hostname) cluster ($cluster.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 wk_path = $"/home/($env.SSH_USER)/($source_path | path basename)"
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)"
}
return (ssh_cmd $settings $server false $rm_cmd $ip )
}