feat: netssh multi — several hosts side by side in tmux or zellij
netssh multi opens one pane per host in a tmux window or zellij tab. Hosts come from names, tag:TAG (NetBox and static hosts), cluster:NAME (NetBox virtualization clusters; devices by default, --kind vm|all), and groups from the config (@name, nested, with cycle detection). Every pane runs netssh HOST, so lookup, rules, and jump hosts apply. - tmux: a new window inside tmux, else a new session that is attached; tiled layout, pane borders with the host and a [SYNC] marker. - zellij: a generated grid layout; a new tab inside zellij, else a new session. - --sync broadcasts typing to all panes; netssh sync toggles it in the current tmux window or zellij tab. - TUI: Space marks hosts, Ctrl+A marks all, Enter opens them together. - Configurator: multiplexer, panes per window, groups. NetBox SearchAll gains a cluster filter (name to cluster IDs per instance), and cache entries keep the host's cluster for completion and an offline fallback. Panes skip the 24-hour background refresh (checked once by multi) and get HOME/XDG paths but no NETSSH_* values, which would expose tokens in the process list. testenv/tests/15-multi.sh checks real tmux and zellij sessions, including a broadcast command arriving on all three cluster nodes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dbb99bdded
commit
a31aab4110
24 files changed
+1754
-40
No files matched your search
+43
-12
@@ -105,6 +105,22 @@ func (c *Client) SearchAll(ctx context.Context, query string, opts SearchOptions
|
||||
if opts.Kind != "" && opts.Kind != "device" && opts.Kind != "vm" {
|
||||
return nil, fmt.Errorf("invalid host kind %q", opts.Kind)
|
||||
}
|
||||
filters := ""
|
||||
if opts.Tag != "" {
|
||||
filters += "&tag=" + url.QueryEscape(opts.Tag)
|
||||
}
|
||||
if opts.Cluster != "" {
|
||||
ids, err := c.clusterIDs(ctx, opts.Cluster)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("clusters: %w", err)
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return nil, nil // no such cluster in this NetBox
|
||||
}
|
||||
for _, id := range ids {
|
||||
filters += fmt.Sprintf("&cluster_id=%d", id)
|
||||
}
|
||||
}
|
||||
var (
|
||||
mu sync.Mutex
|
||||
results []HostEntry
|
||||
@@ -116,7 +132,7 @@ func (c *Client) SearchAll(ctx context.Context, query string, opts SearchOptions
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
devices, err := c.fetchAllDevices(ctx, query, opts.Tag)
|
||||
devices, err := c.fetchAllDevices(ctx, query, filters)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
@@ -131,7 +147,7 @@ func (c *Client) SearchAll(ctx context.Context, query string, opts SearchOptions
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
vms, err := c.fetchAllVMs(ctx, query, opts.Tag)
|
||||
vms, err := c.fetchAllVMs(ctx, query, filters)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
@@ -231,15 +247,27 @@ func (c *Client) searchVMs(ctx context.Context, query, tag string) ([]HostEntry,
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func (c *Client) fetchAllDevices(ctx context.Context, query, tag string) ([]HostEntry, error) {
|
||||
// clusterIDs returns the IDs of the clusters named name (case-insensitive).
|
||||
func (c *Client) clusterIDs(ctx context.Context, name string) ([]int, error) {
|
||||
var resp netboxListResponse[netboxCluster]
|
||||
apiURL := fmt.Sprintf("%s/api/virtualization/clusters/?name__ie=%s&limit=50", c.baseURL, url.QueryEscape(name))
|
||||
if err := c.get(ctx, apiURL, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids := make([]int, 0, len(resp.Results))
|
||||
for _, cluster := range resp.Results {
|
||||
ids = append(ids, cluster.ID)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// fetchAllDevices pages through devices; filters are extra "&key=value" parameters.
|
||||
func (c *Client) fetchAllDevices(ctx context.Context, query, filters string) ([]HostEntry, error) {
|
||||
const pageSize = 50
|
||||
var all []HostEntry
|
||||
for offset := 0; ; offset += pageSize {
|
||||
apiURL := fmt.Sprintf("%s/api/dcim/devices/?name__ic=%s&limit=%d&offset=%d",
|
||||
c.baseURL, url.QueryEscape(query), pageSize, offset)
|
||||
if tag != "" {
|
||||
apiURL += "&tag=" + url.QueryEscape(tag)
|
||||
}
|
||||
c.baseURL, url.QueryEscape(query), pageSize, offset) + filters
|
||||
var resp netboxListResponse[netboxDevice]
|
||||
if err := c.get(ctx, apiURL, &resp); err != nil {
|
||||
return nil, err
|
||||
@@ -254,15 +282,12 @@ func (c *Client) fetchAllDevices(ctx context.Context, query, tag string) ([]Host
|
||||
return all, nil
|
||||
}
|
||||
|
||||
func (c *Client) fetchAllVMs(ctx context.Context, query, tag string) ([]HostEntry, error) {
|
||||
func (c *Client) fetchAllVMs(ctx context.Context, query, filters string) ([]HostEntry, error) {
|
||||
const pageSize = 50
|
||||
var all []HostEntry
|
||||
for offset := 0; ; offset += pageSize {
|
||||
apiURL := fmt.Sprintf("%s/api/virtualization/virtual-machines/?name__ic=%s&limit=%d&offset=%d",
|
||||
c.baseURL, url.QueryEscape(query), pageSize, offset)
|
||||
if tag != "" {
|
||||
apiURL += "&tag=" + url.QueryEscape(tag)
|
||||
}
|
||||
c.baseURL, url.QueryEscape(query), pageSize, offset) + filters
|
||||
var resp netboxListResponse[netboxVM]
|
||||
if err := c.get(ctx, apiURL, &resp); err != nil {
|
||||
return nil, err
|
||||
@@ -343,6 +368,9 @@ func deviceToEntry(d netboxDevice) HostEntry {
|
||||
for _, t := range d.Tags {
|
||||
e.Tags = append(e.Tags, t.Name)
|
||||
}
|
||||
if d.Cluster != nil {
|
||||
e.Cluster = d.Cluster.Name
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -357,6 +385,9 @@ func vmToEntry(v netboxVM) HostEntry {
|
||||
for _, t := range v.Tags {
|
||||
e.Tags = append(e.Tags, t.Name)
|
||||
}
|
||||
if v.Cluster != nil {
|
||||
e.Cluster = v.Cluster.Name
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package netbox
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSearchAllByCluster(t *testing.T) {
|
||||
var deviceQuery, vmQuery string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/api/virtualization/clusters/":
|
||||
if r.URL.Query().Get("name__ie") == "PVE" {
|
||||
fmt.Fprint(w, `{"count":2,"results":[{"id":3,"name":"pve"},{"id":4,"name":"pve"}]}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"count":0,"results":[]}`)
|
||||
case "/api/dcim/devices/":
|
||||
deviceQuery = r.URL.RawQuery
|
||||
fmt.Fprint(w, `{"count":1,"results":[{"id":1,"name":"node1","cluster":{"id":3,"name":"pve"}}]}`)
|
||||
case "/api/virtualization/virtual-machines/":
|
||||
vmQuery = r.URL.RawQuery
|
||||
fmt.Fprint(w, `{"count":1,"results":[{"id":2,"name":"vm1","cluster":{"id":4,"name":"pve"}}]}`)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
c := NewClient(srv.URL, "t", 1)
|
||||
|
||||
hosts, err := c.SearchAll(context.Background(), "", SearchOptions{Cluster: "PVE", Kind: "device"})
|
||||
if err != nil || len(hosts) != 1 || hosts[0].Name != "node1" || hosts[0].Cluster != "pve" {
|
||||
t.Fatalf("got %+v %v", hosts, err)
|
||||
}
|
||||
if want := "name__ic=&limit=50&offset=0&cluster_id=3&cluster_id=4"; deviceQuery != want {
|
||||
t.Errorf("device query %q, want %q", deviceQuery, want)
|
||||
}
|
||||
if vmQuery != "" {
|
||||
t.Error("VMs queried for kind device")
|
||||
}
|
||||
all, err := c.SearchAll(context.Background(), "", SearchOptions{Cluster: "PVE"})
|
||||
if err != nil || len(all) != 2 {
|
||||
t.Fatalf("all kinds: %+v %v", all, err)
|
||||
}
|
||||
none, err := c.SearchAll(context.Background(), "", SearchOptions{Cluster: "missing"})
|
||||
if err != nil || len(none) != 0 {
|
||||
t.Fatalf("unknown cluster: %+v %v", none, err)
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import "strings"
|
||||
type SearchOptions struct {
|
||||
Tag string // filter by tag slug; empty = no filter
|
||||
Kind string // "device" | "vm" | "" (both)
|
||||
// Cluster filters SearchAll by virtualization cluster name (case-insensitive).
|
||||
Cluster string
|
||||
}
|
||||
|
||||
// HostEntry is a unified model for both devices and virtual machines from NetBox.
|
||||
@@ -18,6 +20,13 @@ type HostEntry struct {
|
||||
PrimaryIP4 string // e.g. "10.0.1.5" (prefix length stripped)
|
||||
PrimaryIP6 string
|
||||
Tags []string
|
||||
Cluster string // virtualization cluster name, if assigned
|
||||
}
|
||||
|
||||
// netboxCluster is the nested cluster of a device or virtual machine.
|
||||
type netboxCluster struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// netboxIP represents an IP address as returned by the NetBox API.
|
||||
@@ -32,8 +41,9 @@ type netboxDevice struct {
|
||||
Tags []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"tags"`
|
||||
PrimaryIP4 *netboxIP `json:"primary_ip4"`
|
||||
PrimaryIP6 *netboxIP `json:"primary_ip6"`
|
||||
PrimaryIP4 *netboxIP `json:"primary_ip4"`
|
||||
PrimaryIP6 *netboxIP `json:"primary_ip6"`
|
||||
Cluster *netboxCluster `json:"cluster"`
|
||||
}
|
||||
|
||||
// netboxVM matches the relevant fields of the NetBox /virtualization/virtual-machines/ response.
|
||||
@@ -43,8 +53,9 @@ type netboxVM struct {
|
||||
Tags []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"tags"`
|
||||
PrimaryIP4 *netboxIP `json:"primary_ip4"`
|
||||
PrimaryIP6 *netboxIP `json:"primary_ip6"`
|
||||
PrimaryIP4 *netboxIP `json:"primary_ip4"`
|
||||
PrimaryIP6 *netboxIP `json:"primary_ip6"`
|
||||
Cluster *netboxCluster `json:"cluster"`
|
||||
}
|
||||
|
||||
type netboxListResponse[T any] struct {
|
||||
|
||||
Reference in new issue
Block a user