115 lines
4.2 KiB
Nu
115 lines
4.2 KiB
Nu
#!/usr/bin/env nu
|
|
#
|
|
# hermes-vm — host-side control of the Hermes Agent VM itself: start/stop,
|
|
# console, config inspection, snapshots and backups. For driving Hermes the
|
|
# agent running inside it, see hermes-ctl instead.
|
|
#
|
|
# Installed by the hermenix NixOS module (modules/host.nix) as part of
|
|
# environment.systemPackages, wrapped with the per-host vsock CID, guest SSH
|
|
# alias and disk layout baked in as env vars. The defaults below match the
|
|
# module's own option defaults, so `nix run .#hermes-vm` also works
|
|
# out of the box against a stock setup.
|
|
#
|
|
# Requires nu/lib/common.nu to be concatenated in front of this file - see
|
|
# flake.nix and modules/host.nix. Not runnable standalone.
|
|
|
|
def guest-user [] { "hermes" }
|
|
def guest-host [] { $env.HERMES_GUEST_NAME? | default "hermes" }
|
|
def vsock-cid [] { ($env.HERMES_VSOCK_CID? | default "77") | into int }
|
|
def disk-base-dir [] { $env.HERMES_DISK_BASE_DIR? | default "/var/lib/hermes-vm" }
|
|
def snap-dir [] { $"(disk-base-dir)/snapshots" }
|
|
|
|
def ssh-args [] { vsock-ssh-args (vsock-cid) (guest-user) (guest-host) }
|
|
|
|
# Run a single already-fully-formed remote command string, or open an
|
|
# interactive shell if none is given. A single string, not rest args: nu's
|
|
# parser tries to match any bare --flag-shaped token against this command's
|
|
# own flags before it ever reaches a rest parameter, so `hermes-vm ssh sudo
|
|
# foo --bar` would fail to parse. Quote the whole remote command instead, the
|
|
# same way hermenix.nu's own `remote` subcommand has to.
|
|
def gssh [cmd?: string] {
|
|
let argv = if $cmd == null { ssh-args } else { (ssh-args) ++ [$cmd] }
|
|
run-argv $argv
|
|
}
|
|
|
|
def usage [] {
|
|
"usage: hermes-vm {ssh [cmd]|console|config|config-pull [dir]|start|stop|restart|snapshot|snapshots|rollback <snap>|backup|backups}"
|
|
}
|
|
|
|
def main [] {
|
|
print (usage)
|
|
}
|
|
|
|
def "main ssh" [cmd?: string] { gssh $cmd }
|
|
|
|
def "main console" [] { ^journalctl -u hermes-vm -f -o cat }
|
|
|
|
def "main start" [] { ^systemctl start hermes-vm }
|
|
def "main stop" [] { ^systemctl stop hermes-vm }
|
|
def "main restart" [] { ^systemctl restart hermes-vm }
|
|
|
|
# What is the guest actually running right now?
|
|
def "main config" [] {
|
|
gssh 'echo "== current generation =="; \
|
|
readlink -f /run/current-system; \
|
|
echo; echo "== generations =="; \
|
|
sudo nix-env --list-generations -p /nix/var/nix/profiles/system | tail -n 10; \
|
|
echo; echo "== /etc/nixos history =="; \
|
|
git -C /etc/nixos log --oneline -n 15; \
|
|
echo; echo "== uncommitted =="; \
|
|
git -C /etc/nixos status --short'
|
|
}
|
|
|
|
# Copy the guest's live config here for reading or diffing.
|
|
def "main config-pull" [dest: string = "./hermes-etc-nixos"] {
|
|
mkdir $dest
|
|
(gssh "tar -C /etc/nixos -cf - .") | ^tar -C $dest -xf -
|
|
say $"pulled to ($dest)"
|
|
}
|
|
|
|
def "main snapshot" [] { ^systemctl start hermes-vm-snapshot }
|
|
|
|
def "main snapshots" [] {
|
|
let entries = (glob $"(snap-dir)/state-*" | where {|p| ($p | path type) == "dir" })
|
|
if ($entries | is-empty) {
|
|
say "no snapshots"
|
|
return
|
|
}
|
|
$entries
|
|
| each {|p|
|
|
{
|
|
snapshot: ($p | path basename)
|
|
size: (du $p | get physical.0)
|
|
created: (ls -D $p | get modified.0)
|
|
}
|
|
}
|
|
| sort-by created
|
|
| print
|
|
}
|
|
|
|
# Restore the state disk from a snapshot. The system disk is left alone - it
|
|
# holds nothing worth restoring.
|
|
def "main rollback" [snapshot: string] {
|
|
let snap = $"(snap-dir)/($snapshot)"
|
|
if not ($snap | path exists) { die $"no such snapshot: ($snapshot)" }
|
|
warn "Restores Hermes's identity, memories, workspace and NixOS"
|
|
warn $"config to ($snapshot). Everything since is lost."
|
|
if not (confirm-word "rollback" "type 'rollback' to confirm:") { die "aborted" }
|
|
^systemctl stop hermes-vm
|
|
let base = (disk-base-dir)
|
|
let stamp = (date now | format date "%s")
|
|
^mv $"($base)/state" $"($base)/state.old-($stamp)"
|
|
^btrfs subvolume snapshot $snap $"($base)/state"
|
|
^systemctl start hermes-vm
|
|
ok "restored; previous state kept as state.old-*"
|
|
}
|
|
|
|
def "main backup" [] { ^systemctl start hermes-vm-backup }
|
|
|
|
def "main backups" [] {
|
|
let repo = (^systemctl show hermes-vm-backup -p Environment --value
|
|
| str trim | split row " " | where {|e| $e | str starts-with "RESTIC_REPOSITORY=" }
|
|
| get 0? | default "" | str replace "RESTIC_REPOSITORY=" "")
|
|
^restic -r $repo snapshots --tag hermes
|
|
}
|