diff --git a/README.md b/README.md index 944c380..addb801 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,30 @@ the machine. Neither needs anything from you. --- +## Running it on a remote host + +`setup` and `migrate` both act on whatever machine they run on — local +disks, local sudo, vsock into the local guest. There's no way around SSHing +into the target host first. `hermenix remote` does that SSH step for you: + +```bash +hermenix remote fwdesk.lab.internal setup +hermenix remote fwdesk.lab.internal migrate /var/lib/hermes +``` + +It clones this repo to `~/hermenix` on the remote host **only if that +directory doesn't already exist there**, then `cd`s into it and runs +`nix run .#hermenix -- `. An existing checkout is +never touched beyond running the command — it won't overwrite a host's own +`guest/common.nix` (cache key, authorized keys) or anything else it's +accumulated. Override the checkout path or repo URL with `--dir`/`--repo` if +you need to. + +It connects with `ssh -t`, which allocates a real pty — required for the +interactive prompts in `setup`/`migrate` to work at all over the connection. + +--- + ## SSH into the guest ```bash diff --git a/nu/hermenix.nu b/nu/hermenix.nu index 6b4a73e..bbba0db 100644 --- a/nu/hermenix.nu +++ b/nu/hermenix.nu @@ -2,14 +2,17 @@ # # hermenix — host-side tooling for the Hermes Agent VM. # -# Two subcommands: +# Three subcommands: # hermenix setup guided host-side setup: disks, cache signing # key, guest/common.nix, build + write the image # hermenix migrate carry an existing flat HERMES_HOME (e.g. from # a prior Docker deployment) into a running # guest, unchanged +# hermenix remote ... run either of the above ON a remote host over +# SSH, instead of having to SSH in yourself first # -# Neither runs `nixos-rebuild` — that stays yours to run, watching it happen. +# None of these run `nixos-rebuild` — that stays yours to run, watching it +# happen. # ---------------------------------------------------------------- output --- @@ -35,6 +38,11 @@ def confirm-word [word: string, prompt: string] { (input $" ($prompt) ") == $word } +# Single-quote a string for safe embedding in a remote shell command line. +def shquote [s: string] { + "'" + ($s | str replace --all "'" "'\\''") + "'" +} + # Run a command, sudo-prefixed unless already root (set once via # $env.HERMENIX_NEED_SUDO at the top of `main setup`). Fails loudly, like any # other external command, if the command fails. @@ -56,8 +64,9 @@ def main [] { print "" print " hermenix setup guided host-side setup" print " hermenix migrate carry an existing flat HERMES_HOME into the guest" + print " hermenix remote ... run setup/migrate on a remote host over SSH" print "" - print "run `hermenix --help` for details on either." + print "run `hermenix --help` for details on any of these." } # ------------------------------------------------------------------ setup --- @@ -451,3 +460,31 @@ def "main migrate" [ say "deployment retired. Decrypt secrets/hermes-env.age yourself and fold" say $"anything relevant into ($dest)/.env \(hermes-ctl secrets edit\)." } + +# ----------------------------------------------------------------- remote --- + +# Run `hermenix setup` or `hermenix migrate` on a remote host over SSH, +# instead of having to SSH in and run it locally yourself. `setup` and +# `migrate` both act on whatever machine they run on (local disks, local +# sudo, vsock into the local guest) - this just saves the manual SSH step. +# +# Clones `--repo` to `--dir` on the remote ONLY if `--dir` doesn't already +# exist there - an existing checkout is never touched beyond running the +# command, so a host's own guest/common.nix edits (cache key, authorized +# keys) or in-progress disk state are never at risk of being overwritten. +def "main remote" [ + host: string # SSH target, e.g. fwdesk.lab.internal + ...args: string # forwarded to the remote `hermenix` invocation + --dir: string = "hermenix" # checkout path on the remote, relative to $HOME + --repo: string = "ssh://gitea@minipc1.lab.internal:22/ari/hermenix.git" +] { + if ($args | is-empty) { + die "usage: hermenix remote [args...]" + } + + let quoted_args = ($args | each {|a| shquote $a } | str join " ") + let remote_cmd = $"if [ ! -d (shquote $dir) ]; then git clone (shquote $repo) (shquote $dir); fi && cd (shquote $dir) && nix run .#hermenix -- ($quoted_args)" + + say $"Running on ($host): ($args | str join ' ')" + ^ssh -t -o StrictHostKeyChecking=accept-new $host $remote_cmd +}