Files
hermenix/migrate-docker-hermes.sh
T
ariandClaude Sonnet 5 12227460fd Initial hermex: Hermes Agent in a hardened, self-managing NixOS VM
Two-disk (system/state) btrfs-backed QEMU VM, nftables-isolated by default,
adapted from a design provided as loose files. Adds a hermesHome option to
the host module and migrate-docker-hermes.sh so an existing flat HERMES_HOME
(e.g. from a prior Docker deployment) can be carried over intact.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-15 12:20:45 -04:00

112 lines
4.7 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Carry an existing, already-running Hermes deployment (e.g. the
# nousresearch/hermes-agent Docker image, HERMES_HOME bind-mounted from the
# host) into a hermex guest VM, preserving its exact flat layout.
#
# This is written for the hl-fwdesk migration specifically (see the REWRITE
# list below) but the mechanics generalize: point SOURCE at any existing flat
# HERMES_HOME, adjust the rewrite rules for your own network, and run it.
#
# SAFETY: this script only ever READS from SOURCE. It never deletes or moves
# anything there — the original stays a fully intact fallback. If anything
# goes wrong after this, `sudo nixos-rebuild switch --rollback` on the host
# restores the previous (e.g. Docker) deployment pointed at the same,
# untouched source data.
#
# Run this AFTER:
# - setup.sh has built and written the guest image
# - the host's nixos-rebuild has brought up the hermes-vm and it has booted
# at least once (so the state disk is formatted and mounted)
# - `hermes-vm ssh true` succeeds
#
# Usage:
# ./migrate-docker-hermes.sh /var/lib/hermes
#
# What it does:
# 1. Confirms with you before touching anything.
# 2. Tars SOURCE (read-only) and streams it into the guest over the
# existing vsock SSH channel, landing at DEST_IN_GUEST (default
# /var/lib/hermes, i.e. the root of the guest's state disk — matching
# services.hermesVm.hermesHome = DEST_IN_GUEST on the host).
# 3. Rewrites the known-stale Docker/LAN hostnames in the migrated config
# files to addresses the guest can actually reach.
#
# It does NOT install Hermes, start the gateway, or touch secrets/hermes-env.age
# — those steps are manual, documented in README.md and the plan that
# produced this script.
set -euo pipefail
VSOCK_CID="${VSOCK_CID:-77}"
GUEST_USER="${GUEST_USER:-hermes}"
DEST_IN_GUEST="${DEST_IN_GUEST:-/var/lib/hermes}"
SOURCE="${1:-}"
[ -n "$SOURCE" ] || { echo "usage: $0 <source-hermes-home-dir>" >&2; exit 1; }
[ -d "$SOURCE" ] || { echo "error: $SOURCE is not a directory" >&2; exit 1; }
SSH=(ssh
-o ProxyCommand="socat - VSOCK-CONNECT:${VSOCK_CID}:22"
-o StrictHostKeyChecking=accept-new
"${GUEST_USER}@hermes")
# ---------------------------------------------------------------------------
# Rewrite rules applied to the migrated config after it lands in the guest.
# These are specific to the hl-fwdesk migration — edit for your own setup.
#
# host.docker.internal / fwdesk.lab.internal both resolved to hl-fwdesk
# itself under Docker; the guest reaches the same host at its bridge address.
# castle.hub.internal is a *different* LAN device (Home Assistant) reached
# via lanAccess instead.
# ---------------------------------------------------------------------------
REWRITE_FILES=(config.yaml auth.json context_length_cache.yaml .env)
REWRITE_FROM=(host.docker.internal fwdesk.lab.internal castle.hub.internal)
REWRITE_TO=(10.77.0.1 10.77.0.1 10.0.12.1)
echo "Source (read-only): $SOURCE"
echo "Destination (guest): ${GUEST_USER}@hermes:${DEST_IN_GUEST} (over vsock, cid ${VSOCK_CID})"
echo
echo "This will:"
echo " 1. tar '$SOURCE' (not modifying it) and stream it into the guest"
echo " 2. rewrite these hostnames in the migrated config, in the guest only:"
for i in "${!REWRITE_FROM[@]}"; do
echo " ${REWRITE_FROM[$i]} -> ${REWRITE_TO[$i]}"
done
echo
read -r -p "Type 'migrate' to proceed: " confirm
[ "$confirm" = "migrate" ] || { echo "aborted"; exit 1; }
echo
echo "== confirming guest is reachable =="
"${SSH[@]}" true
echo "== streaming $SOURCE -> guest:${DEST_IN_GUEST} (this can take a while for 8G+) =="
tar -C "$SOURCE" -cf - . | "${SSH[@]}" "mkdir -p '${DEST_IN_GUEST}' && tar -C '${DEST_IN_GUEST}' -xf -"
echo "== rewriting stale hostnames in the guest =="
for f in "${REWRITE_FILES[@]}"; do
path="${DEST_IN_GUEST}/${f}"
sed_expr=""
for i in "${!REWRITE_FROM[@]}"; do
sed_expr+="s#${REWRITE_FROM[$i]}#${REWRITE_TO[$i]}#g; "
done
"${SSH[@]}" "[ -f '$path' ] && sed -i '$sed_expr' '$path' && echo ' rewrote $f' || echo ' $f not present, skipping'"
done
echo
echo "Done. The original at $SOURCE is untouched."
echo
echo "Next, inside the guest (hermes-vm ssh):"
echo " export HERMES_HOME=${DEST_IN_GUEST}"
echo " # then the usual install steps from README.md:"
echo " # determinate-nixd login"
echo " # sudo nix flake lock /etc/nixos"
echo " # curl -fsSL https://raw.githubusercontent.com/NousResearch/hermes-agent/main/scripts/install.sh | bash"
echo " # hermes setup"
echo " # hermes gateway install"
echo
echo "Then verify with hermes-ctl status / logs before considering the old"
echo "deployment retired. Decrypt secrets/hermes-env.age yourself and fold"
echo "anything relevant into ${DEST_IN_GUEST}/.env (hermes-ctl secrets edit)."