From 64ab08d8916cff3435dfc1c653938be0eeb514d8 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Thu, 17 Sep 2026 15:23:16 -0400 Subject: [PATCH] carry connections --- nu/hermenix.nu | 3 +- nu/host/migrate.nu | 150 ++++++++++++++++++++++++++++++++++++- nu/tests/cli_smoke_test.nu | 2 +- 3 files changed, 152 insertions(+), 3 deletions(-) diff --git a/nu/hermenix.nu b/nu/hermenix.nu index 7e7f85d..cb2ca52 100644 --- a/nu/hermenix.nu +++ b/nu/hermenix.nu @@ -29,7 +29,8 @@ def main [] { print "usage: hermenix ..." print "" print " hermenix setup guided host-side setup" - print " hermenix migrate carry an existing flat HERMES_HOME into the guest" + print " hermenix migrate carry an existing flat HERMES_HOME into the guest" + print " hermenix migrate connections copy only connection files between folder deployments" print " hermenix remote \"\" run setup/migrate on a remote host over SSH" print "" print " hermenix vm control the VM (start/stop/console/snapshots/backups)" diff --git a/nu/host/migrate.nu b/nu/host/migrate.nu index 85ab66a..a2a7af3 100644 --- a/nu/host/migrate.nu +++ b/nu/host/migrate.nu @@ -1,4 +1,6 @@ -# hermenix migrate — carry an existing flat HERMES_HOME into the guest. +# hermenix migrate — carry an existing flat HERMES_HOME into the guest, +# or selectively copy just the agent-connection files between two folder +# deployments. # # Only ever reads from the source directory. Nothing there is deleted or # moved — the original stays a fully intact fallback. @@ -67,6 +69,8 @@ def "main migrate" [ print "" ok $"Done. The original at ($source) is untouched." + note "Tip: use `hermenix migrate connections ` to carry just" + note "the connection config files between two folder deployments." print "" say "Next, inside the guest (hermenix vm ssh):" say $" export HERMES_HOME=($dest)" @@ -81,3 +85,147 @@ def "main migrate" [ say "deployment retired. Decrypt secrets/hermes-env.age yourself and fold" say $"anything relevant into ($dest)/.env \(hermenix ctl secrets edit\)." } + +# Copy only the agent-connection files from one flat HERMES_HOME to another. +# +# Use this when you want to carry the connections/integrations config across +# deployments without overwriting identity (SOUL.md, memories, etc.). +# +# By default this is a pure local operation (host-to-host folder copy). +# Pass --to-guest to stream the files into the running guest over vsock +# instead, using the same path as `hermenix migrate`. +# +# SAFETY: only ever reads from `source`; the original is never modified. +def "main migrate connections" [ + source: string # existing flat HERMES_HOME to copy from (read-only) + dest: string # destination HERMES_HOME folder; ignored when --to-guest is set + --files: list = [config.yaml auth.json .env] # files (relative to HERMES_HOME) to copy + --extra-dirs: list = [connections] # subdirectories to copy when present + --rewrite-from: list = [host.docker.internal fwdesk.lab.internal castle.hub.internal] + --rewrite-to: list = [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 +] { + 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" + } + + # ---- resolve what we're actually copying -------------------------------- + # Flat files that exist in source. + let existing_files = $files | where {|f| ($"($source)/($f)" | path exists) } + # Subdirectories that exist in source. + 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)" + } + + # ---- build the sed expression for hostname rewrites -------------------- + 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 { "" } + + # ---- announce ----------------------------------------------------------- + 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 { + # ---- guest mode: stream over vsock ---------------------------------- + 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)" + # Build an include list for tar: individual files + full dir trees. + let file_args = $existing_files | each {|f| ["--include" $f] } | flatten + let dir_args = $existing_dirs | each {|d| ["--include" $"($d)/**"] } | flatten + ( + ^tar --ignore-failed-read -C $source ($file_args | append $dir_args | append ["--no-recursion" "-cf" "-"]) . + | 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 { + # ---- local mode: copy on this host ---------------------------------- + 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) ` or fold it" + say "into the guest with `hermenix migrate connections ($source) --to-guest`." + } +} diff --git a/nu/tests/cli_smoke_test.nu b/nu/tests/cli_smoke_test.nu index 2c06cee..61371c7 100644 --- a/nu/tests/cli_smoke_test.nu +++ b/nu/tests/cli_smoke_test.nu @@ -18,7 +18,7 @@ def command-names [] { def "top-level subcommands are registered" [] { let names = (command-names) for n in [ - "main" "main setup" "main migrate" "main remote" + "main" "main setup" "main migrate" "main migrate connections" "main remote" "main vm" "main ctl" "main version" ] { assert ($n in $names) $"missing top-level command: ($n)"