From a8fa1f83af7b47e06d3267f8c69ee18971eee303 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Fri, 16 Oct 2026 12:23:27 -0400 Subject: [PATCH] net check part 2 --- bin/ABOUTME.md | 55 +++++++++++++++---------------- bin/lib/net.nu | 87 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 101 insertions(+), 41 deletions(-) diff --git a/bin/ABOUTME.md b/bin/ABOUTME.md index 76f043c..f799f66 100644 --- a/bin/ABOUTME.md +++ b/bin/ABOUTME.md @@ -21,7 +21,7 @@ bin/ ├── build.nu # `skyg build image/openwrt` ├── render.nu # `skyg render html-tpl` ├── hm.nu # `skyg hm ` - ├── net.nu # `skyg net check` (per-device NIC info + shared-server speed test) + ├── net.nu # `skyg net check` (per-device NIC info + shared-server speed test), `skyg net last` ├── net-nic.sh # Bash probe piped to each device by `skyg net check` (NIC model, driver, link/supported speeds) ├── helpers.nu # Shared helpers: REPO_ROOT const + internal functions ├── profile.nu # Profile helpers and tab-completion data @@ -31,32 +31,33 @@ bin/ ## Key Commands -| Command | Description | -| ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `skyg os switch` | Rebuild and switch local system | -| `skyg os rollback` | Roll back to previous generation | -| `skyg remote switch ` | Deploy to a remote host | -| `skyg remote install ` | Fresh-install a remote host via nixos-anywhere, using `hosts/.disko.nix`. Always wipes the disks in that file. Aborts if the file is missing, if the host already boots NixOS from disk (unless `--force-wipe`), or if you do not type the short hostname. | -| `skyg remote status ` | Check status of a single remote host | -| `skyg remote status --all` | Check status of all remote hosts | -| `skyg remote boot-all` | Boot all remote hosts | -| `skyg remote reboot-pending` | Reboot every remote host with a pending reboot (`booted-system` != `current-system`). Lists them, asks for confirmation, then reboots one at a time, waiting for each to come back online and sending a desktop notification when it does. | -| `skyg hm switch` | Apply Home Manager config | -| `skyg flake update [input]` | Update flake inputs | -| `skyg flake check` | Run `nix flake check --no-build` | -| `skyg flake check-versions` | Verify NixOS release version consistency in flake.nix | -| `skyg build image iso\|pi1\|pi2` | Build bootable image — ISO (`iso`) or Pi SD card image (`pi1`, `pi2`) | -| `skyg build openwrt [router]` | Deploy OpenWrt config | -| `skyg profiles` | List valid local/remote profiles | -| `skyg secrets list` | List known secret names | -| `skyg secrets show ` | Decrypt a secret to `.tmp/unencrypted-` | -| `skyg secrets edit [--yaml]` | Encrypt a working copy back into the secret | -| `skyg secrets compare ` | Diff the working copy against the stored secret | -| `skyg ca init` | Generate the lab root CA: key → `secrets/lab-ca-key.age`, cert → `configs/pki/lab-ca.crt` (see `configs/pki/ABOUTME.md`) | -| `skyg ca issue --for ` | Mint a TLS leaf cert signed by the lab CA: cert → `configs/pki/.crt`, key → `secrets/-tls-key.age`. Auto-registers missing `secrets.nix` entries. | -| `skyg pkgs update [--all\|]` | Update one or all custom packages | -| `skyg net check [device]` | For every managed device (or just `[device]`): report the NIC on the default route (model, driver, current link speed, supported speeds), then run `speedtest-go` via `nix-shell` against one shared server. Logs progress per device and returns a table. `--server ` picks the server; `--no-speedtest` skips the test. Offline devices are listed, not tested. | -| `skyg render html-tpl ` | Render a Gitea `.tmpl` and serve it on `127.0.0.1` for preview. Foreground — Ctrl+C to stop. `` tab-completes from `**/*.tmpl`. | +| Command | Description | +| ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `skyg os switch` | Rebuild and switch local system | +| `skyg os rollback` | Roll back to previous generation | +| `skyg remote switch ` | Deploy to a remote host | +| `skyg remote install ` | Fresh-install a remote host via nixos-anywhere, using `hosts/.disko.nix`. Always wipes the disks in that file. Aborts if the file is missing, if the host already boots NixOS from disk (unless `--force-wipe`), or if you do not type the short hostname. | +| `skyg remote status ` | Check status of a single remote host | +| `skyg remote status --all` | Check status of all remote hosts | +| `skyg remote boot-all` | Boot all remote hosts | +| `skyg remote reboot-pending` | Reboot every remote host with a pending reboot (`booted-system` != `current-system`). Lists them, asks for confirmation, then reboots one at a time, waiting for each to come back online and sending a desktop notification when it does. | +| `skyg hm switch` | Apply Home Manager config | +| `skyg flake update [input]` | Update flake inputs | +| `skyg flake check` | Run `nix flake check --no-build` | +| `skyg flake check-versions` | Verify NixOS release version consistency in flake.nix | +| `skyg build image iso\|pi1\|pi2` | Build bootable image — ISO (`iso`) or Pi SD card image (`pi1`, `pi2`) | +| `skyg build openwrt [router]` | Deploy OpenWrt config | +| `skyg profiles` | List valid local/remote profiles | +| `skyg secrets list` | List known secret names | +| `skyg secrets show ` | Decrypt a secret to `.tmp/unencrypted-` | +| `skyg secrets edit [--yaml]` | Encrypt a working copy back into the secret | +| `skyg secrets compare ` | Diff the working copy against the stored secret | +| `skyg ca init` | Generate the lab root CA: key → `secrets/lab-ca-key.age`, cert → `configs/pki/lab-ca.crt` (see `configs/pki/ABOUTME.md`) | +| `skyg ca issue --for ` | Mint a TLS leaf cert signed by the lab CA: cert → `configs/pki/.crt`, key → `secrets/-tls-key.age`. Auto-registers missing `secrets.nix` entries. | +| `skyg pkgs update [--all\|]` | Update one or all custom packages | +| `skyg net check [device]` | For every managed device (or just `[device]`): report the NIC on the default route (model, driver, current link speed, supported speeds), then run `speedtest-go` via `nix-shell` against one shared server. Logs progress per device and returns a table. `--server ` picks the server; `--no-speedtest` skips the test. Offline devices are listed, not tested. Every run saves the table to `.tmp/net-checks/.nuon`; `--save ` also writes it to `` (format from extension). The server is saved in `.tmp/net-checks/server.nuon` on first run; later runs ask whether to change it (fuzzy pick list from `speedtest-go --list`). `--server` skips the question and leaves the saved server alone. | +| `skyg net last` | Show the table from the most recent `skyg net check` run (newest file in `.tmp/net-checks/`). | +| `skyg render html-tpl ` | Render a Gitea `.tmpl` and serve it on `127.0.0.1` for preview. Foreground — Ctrl+C to stop. `` tab-completes from `**/*.tmpl`. | ## Agent Note diff --git a/bin/lib/net.nu b/bin/lib/net.nu index a438df5..9f048f1 100644 --- a/bin/lib/net.nu +++ b/bin/lib/net.nu @@ -1,7 +1,10 @@ use profile.nu * +use helpers.nu [REPO_ROOT] const NIC_SCRIPT = path self | path dirname | path join "net-nic.sh" const SSH_OPTS = ["-o" "BatchMode=yes" "-o" "ConnectTimeout=5" "-o" "StrictHostKeyChecking=accept-new"] +const CHECKS_DIR = $REPO_ROOT | path join ".tmp" "net-checks" +const SERVER_CACHE = $CHECKS_DIR | path join "server.nuon" def local-hostname [] { ^cat /proc/sys/kernel/hostname | str trim @@ -63,13 +66,55 @@ def probe-nic [device: string] { | reduce --fold {} {|it, acc| $acc | upsert $it.key ($it.value | str trim) } } -# Closest speedtest.net server to this machine, as {id, rest}, or null. -def closest-server [] { +# Nearby speedtest.net servers as [{id, rest}], closest first. +def list-servers [] { let r = (^bash -c "nix-shell -p speedtest-go --run 'speedtest-go --list'" | complete) $r.stdout | lines | parse --regex '^\[(?P\d+)\]\s+(?P.*)$' - | get 0? +} + +def save-server [s: record] { + mkdir $CHECKS_DIR + $s | save --force $SERVER_CACHE +} + +# Server id to use for all devices: the saved one (asks if you want to change it), +# or the closest one on first run. Returns "" if none could be found. +def pick-server [] { + # ^cat, not open: open may be aliased (e.g. to xdg-open) in the user's config. + let saved = if ($SERVER_CACHE | path exists) { try { ^cat $SERVER_CACHE | from nuon } catch { null } } else { null } + + if $saved == null { + print "Finding the closest speedtest server (shared by all devices)..." + let s = (list-servers | get 0?) + if $s == null { + print $"(ansi yellow)Could not list servers; each device will pick its own.(ansi reset)" + return "" + } + save-server $s + print $"Using server ($s.id): ($s.rest) \(saved to ($SERVER_CACHE)\)" + return $s.id + } + + print $"Saved server ($saved.id): ($saved.rest)" + let ans = (input "Change server? [y/N] " | str trim | str downcase) + if $ans not-in ["y" "yes"] { return $saved.id } + + print "Listing speedtest servers..." + let servers = (list-servers) + if ($servers | is-empty) { + print $"(ansi yellow)Could not list servers; keeping saved server.(ansi reset)" + return $saved.id + } + let s = ($servers | input list --fuzzy --display {|x| $"[($x.id)] ($x.rest)" } "Pick a server (closest first)") + if $s == null { + print "No server picked; keeping saved server." + return $saved.id + } + save-server $s + print $"Using server ($s.id): ($s.rest)" + $s.id } # Run speedtest-go on a device. Assumes dl_speed/ul_speed are bytes/s and latency is ns. @@ -106,6 +151,7 @@ export def "skyg net check" [ device?: string@net-targets, # Only check this device (default: all devices) --server: string = "", # speedtest.net server id (default: closest to this machine) --no-speedtest, # Only report network card details + --save: path, # Also save the table here (format from extension: .csv, .json, .nuon, .md, ...) ] { let all = (net-targets) let devices = if $device == null { $all } else { @@ -115,17 +161,7 @@ export def "skyg net check" [ [$device] } - mut server_id = $server - if (not $no_speedtest) and $server_id == "" { - print "Finding the closest speedtest server (shared by all devices)..." - let s = (closest-server) - if $s == null { - print $"(ansi yellow)Could not list servers; each device will pick its own.(ansi reset)" - } else { - $server_id = $s.id - print $"Using server ($s.id): ($s.rest)" - } - } + let server_id = if (not $no_speedtest) and $server == "" { pick-server } else { $server } let total = ($devices | length) mut rows = [] @@ -173,5 +209,28 @@ export def "skyg net check" [ } print $"Done: checked ($total) device\(s\)." + + mkdir $CHECKS_DIR + let auto_file = ($CHECKS_DIR | path join $"(date now | format date '%Y.%m.%d_%H_%M_%S').nuon") + $rows | save --force $auto_file + print $"Saved to ($auto_file)" + if $save != null { + $rows | save --force $save + print $"Saved to ($save | path expand)" + } + print $"Show the last table again with: (ansi green_bold)skyg net last(ansi reset)" + $rows } + +# Show the table from the most recent `skyg net check` run. +export def "skyg net last" [] { + # glob and ^cat, not ls/open: they may be aliased (e.g. to eza/xdg-open) in the user's config. + let files = (glob ($CHECKS_DIR | path join "*.nuon") | where {|f| $f != $SERVER_CACHE }) + if ($files | is-empty) { + error make {msg: $"No saved net checks in ($CHECKS_DIR). Run `skyg net check` first."} + } + let latest = ($files | sort | last) + print $"From ($latest)" + ^cat $latest | from nuon +}