Files
2026-10-09 14:35:12 -04:00

188 lines
8.0 KiB
Nu

def "main migrate" [
source: string # existing flat HERMES_HOME to copy from (read-only)
--vsock-cid: int = 77 # must match services.hermenix.vsockCid on the host
--guest-user: string = "hermes"
--dest: string = "/var/lib/hermes" # where it lands in the guest; match services.hermenix.hermesHome
--rewrite-from: list<string> = [host.docker.internal fwdesk.lab.internal castle.hub.internal]
--rewrite-to: list<string> = [10.77.0.1 10.77.0.1 10.0.12.1]
--rewrite-files: list<string> = [config.yaml auth.json context_length_cache.yaml .env]
] {
if not ($source | path exists) { die $"($source) is not a directory" }
if ($rewrite_from | length) != ($rewrite_to | length) {
die "--rewrite-from and --rewrite-to must have the same length"
}
let ssh_base = (vsock-ssh-args $vsock_cid $guest_user "hermes")
def gssh [cmd: string] { run-argv ($ssh_base ++ [$cmd]) }
say $"Source \(read-only\): ($source)"
say $"Destination \(guest\): ($guest_user)@hermes:($dest) \(over vsock, cid ($vsock_cid)\)"
print ""
say "This will:"
say $" 1. tar '($source)' \(not modifying it\) and stream it into the guest"
say " 2. rewrite these hostnames in the migrated config, in the guest only:"
for i in 0..(($rewrite_from | length) - 1) {
say $" ($rewrite_from | get $i) -> ($rewrite_to | get $i)"
}
print ""
if not (confirm-word "migrate" "Type 'migrate' to proceed:") { die "aborted" }
step 1 "confirming guest is reachable"
gssh "true"
step 2 $"streaming ($source) -> guest:($dest) \(this can take a while for 8G+\)"
# ignore-failed-read: rootless-docker uid leftovers.
# exclude .local/bin + .hermes-installed: don't shadow guest native install.
^tar --ignore-failed-read --exclude='./.local/bin' --exclude='./.hermes-installed' -C $source -cf - . | run-argv ($ssh_base ++ [$"mkdir -p '($dest)' && tar -C '($dest)' -xf -"])
step 3 "rewriting stale hostnames in the guest"
let last_idx = ($rewrite_from | length) - 1
let sed_expr = (0..$last_idx
| each {|i| $"s#($rewrite_from | get $i)#($rewrite_to | get $i)#g" }
| str join "; ")
for f in $rewrite_files {
let path = $"($dest)/($f)"
gssh $"[ -f '($path)' ] && sed -i '($sed_expr)' '($path)' && echo ' rewrote ($f)' || echo ' ($f) not present, skipping'"
}
print ""
ok $"Done. The original at ($source) is untouched."
note "Tip: use `hermenix migrate connections <source> <dest>` to carry just"
note "the connection config files between two folder deployments."
print ""
say "Next:"
say $" - export HERMES_HOME=($dest) inside the guest (and set services.hermenix.hermesHome)"
say " - finish Hermes configuration with the upstream CLI/docs"
say " - fold secrets into the guest via: hermenix ctl secrets edit"
say " - verify: hermenix ctl status / logs — only then retire the old deployment"
}
def "main migrate connections" [
source: string # existing flat HERMES_HOME to copy from (read-only)
dest: string = "" # destination HERMES_HOME folder; not required when --to-guest is set
--files: list<string> = [config.yaml auth.json .env] # files (relative to HERMES_HOME) to copy
--extra-dirs: list<string> = [connections] # subdirectories to copy when present
--rewrite-from: list<string> = [host.docker.internal fwdesk.lab.internal castle.hub.internal]
--rewrite-to: list<string> = [10.77.0.1 10.77.0.1 10.0.12.1]
--to-guest # stream into the running guest over vsock instead of a local folder
--vsock-cid: int = 77 # must match services.hermenix.vsockCid; only used with --to-guest
--guest-user: string = "hermes"
--guest-dest: string = "/var/lib/hermes" # destination inside the guest; only used with --to-guest
] {
mut to_guest = $to_guest
if $dest == "" {
$to_guest = true
note "no <dest> given — assuming --to-guest (streaming into the running guest)"
}
if not ($source | path exists) { die $"source path does not exist: ($source)" }
if ($rewrite_from | length) != ($rewrite_to | length) {
die "--rewrite-from and --rewrite-to must have the same length"
}
let existing_files = $files | where {|f| ($"($source)/($f)" | path exists) }
let existing_dirs = $extra_dirs | where {|d| ($"($source)/($d)" | path exists) }
if ($existing_files | is-empty) and ($existing_dirs | is-empty) {
die $"none of the requested connection files/dirs were found under ($source)"
}
let has_rewrites = ($rewrite_from | length) > 0
let sed_expr = if $has_rewrites {
let last_idx = ($rewrite_from | length) - 1
(0..$last_idx
| each {|i| $"s#($rewrite_from | get $i)#($rewrite_to | get $i)#g" }
| str join "; ")
} else { "" }
say $"Source \(read-only\): ($source)"
if $to_guest {
say $"Destination \(guest\): ($guest_user)@hermes:($guest_dest) \(over vsock, cid ($vsock_cid)\)"
} else {
say $"Destination \(local\): ($dest)"
}
print ""
say "Files to copy:"
for f in $existing_files { say $" ($f)" }
for d in $existing_dirs { say $" ($d)/ \(directory\)" }
if $has_rewrites {
print ""
say "Hostname rewrites applied in destination only:"
for i in 0..(($rewrite_from | length) - 1) {
say $" ($rewrite_from | get $i) -> ($rewrite_to | get $i)"
}
}
print ""
if not (confirm-word "copy" "Type 'copy' to proceed:") { die "aborted" }
if $to_guest {
let ssh_base = (vsock-ssh-args $vsock_cid $guest_user "hermes")
def gssh [cmd: string] { run-argv ($ssh_base ++ [$cmd]) }
step 1 "confirming guest is reachable"
gssh "true"
step 2 $"streaming connection files ($source) -> guest:($guest_dest)"
let tar_targets = $existing_files | append $existing_dirs
(
^tar --ignore-failed-read -C $source -cf - ...$tar_targets
| run-argv ($ssh_base ++ [$"mkdir -p '($guest_dest)' && tar -C '($guest_dest)' -xf -"])
)
if $has_rewrites {
step 3 "rewriting stale hostnames in the guest"
for f in $existing_files {
let path = $"($guest_dest)/($f)"
gssh $"[ -f '($path)' ] && sed -i '($sed_expr)' '($path)' && echo ' rewrote ($f)' || echo ' ($f) not present, skipping'"
}
for d in $existing_dirs {
gssh $"find '($guest_dest)/($d)' -type f -exec sed -i '($sed_expr)' {{}} + 2>/dev/null && echo ' rewrote ($d)/' || true"
}
}
} else {
step 1 "creating destination directory"
mkdir $dest
step 2 $"copying connection files ($source) -> ($dest)"
for f in $existing_files {
let src_path = $"($source)/($f)"
let dst_path = $"($dest)/($f)"
^cp --preserve=timestamps $src_path $dst_path
ok $f
}
for d in $existing_dirs {
let src_dir = $"($source)/($d)"
let dst_dir = $"($dest)/($d)"
^cp -r --preserve=timestamps $src_dir $dst_dir
ok $"($d)/"
}
if $has_rewrites {
step 3 "rewriting stale hostnames in destination"
for f in $existing_files {
let path = $"($dest)/($f)"
if ($path | path exists) {
^sed -i $sed_expr $path
ok $" rewrote ($f)"
}
}
for d in $existing_dirs {
let dir = $"($dest)/($d)"
if ($dir | path exists) {
^find $dir -type f -exec sed -i $sed_expr "{}" "+"
ok $" rewrote ($d)/"
}
}
}
}
print ""
ok $"Done. ($source) is untouched."
if not $to_guest {
print ""
say "Review the destination before use:"
say $" ($dest)"
say "Then run `hermenix migrate connections ($dest) <next-dest>` or fold it"
say "into the guest with `hermenix migrate connections ($source) --to-guest`."
}
}