238 lines
11 KiB
Nu
238 lines
11 KiB
Nu
# 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.
|
|
#
|
|
# Run this AFTER setup + the host's nixos-rebuild have brought the guest up
|
|
# at least once, and `hermenix vm ssh true` succeeds.
|
|
#
|
|
# Depends on: lib/common.nu, lib/guest.nu.
|
|
|
|
# Carry an existing flat HERMES_HOME (e.g. the nousresearch/hermes-agent
|
|
# Docker image, HERMES_HOME bind-mounted from the host) into a hermenix
|
|
# guest, preserving its exact layout.
|
|
#
|
|
# SAFETY: only ever reads from `source`. Nothing there is deleted or moved —
|
|
# the original stays a fully intact fallback. If anything goes wrong after
|
|
# this, `sudo nixos-rebuild switch --rollback` on the host restores the
|
|
# previous deployment pointed at the same, untouched source data.
|
|
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: a source entry can be unreadable (e.g. uid-mapped
|
|
# leftovers from a prior rootless-container deployment) without that
|
|
# aborting an otherwise-successful migration - tar still warns about it,
|
|
# it just doesn't fail the whole pipeline over it.
|
|
^tar --ignore-failed-read -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, inside the guest (hermenix vm ssh):"
|
|
say $" export HERMES_HOME=($dest)"
|
|
say " # hermes installs itself automatically on first boot - if that's"
|
|
say " # already done (check: hermenix ctl units), just:"
|
|
say " # determinate-nixd login"
|
|
say " # sudo nix flake lock /etc/nixos"
|
|
say " # hermes setup"
|
|
say " # hermes gateway install"
|
|
print ""
|
|
say "Then verify with hermenix ctl status / logs before considering the old"
|
|
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; 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"
|
|
}
|
|
|
|
# ---- 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
|
|
let tar_args = $file_args | append $dir_args | append ["--no-recursion" "-cf" "-" "."]
|
|
(
|
|
^tar --ignore-failed-read -C $source ...$tar_args
|
|
| 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) <next-dest>` or fold it"
|
|
say "into the guest with `hermenix migrate connections ($source) --to-guest`."
|
|
}
|
|
}
|