Files
hermenix/nu/host/migrate.nu
T
2026-09-17 14:14:14 -04:00

84 lines
3.9 KiB
Nu

# hermenix migrate — carry an existing flat HERMES_HOME into the guest.
#
# 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."
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\)."
}