Files
ssh-netbox-wrapper/cmd/netssh/supervise.go
T
Sebastian UnterschützandClaude Opus 5.5 1660bb3098
Test / test (pull_request) Successful in 4m15s
feat: NetBox insights, host tools and configurable TUI
- info/open/whois: NetBox details (site, nested locations, rack units,
  role, platform, tenant, IPs per interface), web page, reverse IP lookup
- cache status, site, role, platform, tenant, location chain and rack;
  ask before connecting to hosts in ssh.warn_status (--force skips)
- ssh rules and command restrictions match site/role/platform/tenant/status
- tui.columns selects what the TUI shows; optional TCP reachability column;
  detail view (Ctrl+L), NetBox page (Ctrl+G), status/location filters
- resolver.fallback tries other strategies' addresses when unreachable
- copy-id, known-hosts reset and address-change notices
- export ansible (YAML/INI) and export ssh-config
- token: keyring stores NetBox tokens in the system keyring
- fix stale menu number in testenv configure test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 12:33:59 +01:00

60 lines
2.2 KiB
Go

package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/activity"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/runner"
"github.com/charmbracelet/x/term"
)
func commandHost(h resolvedHost) config.SSHHost {
instance, name := netbox.SplitTarget(h.Name)
return config.SSHHost{Instance: instance, Name: name, Kind: h.Kind, Tags: h.Tags, Status: h.Status, Site: h.Site, Role: h.Role, Platform: h.Platform, Tenant: h.Tenant}
}
func confirmAction(message string, yes bool) error {
if yes {
return nil
}
if !term.IsTerminal(os.Stdin.Fd()) {
return fmt.Errorf("%s requires confirmation; use --yes for unattended execution", message)
}
fmt.Fprintf(os.Stderr, "%s? [y/N] ", message)
answer, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return err
}
if strings.EqualFold(strings.TrimSpace(answer), "y") || strings.EqualFold(strings.TrimSpace(answer), "yes") {
return nil
}
return fmt.Errorf("cancelled")
}
func supervise(cfg *config.Config, h resolvedHost, name string, argv []string, post string, timeout time.Duration, status bool, stdin io.Reader, stdout, stderr io.Writer) int {
start := time.Now()
env := append(preConnectEnv(h, name), "NETSSH_COMMAND="+name)
result := runner.Run(context.Background(), argv, runner.Options{Stdin: stdin, Stdout: stdout, Stderr: stderr, Env: env, Timeout: timeout, Status: status, Label: name + " " + h.Name})
code := result.Code
if result.Started && config.HookCommand(post) != "" {
hook := runner.Run(context.Background(), []string{"sh", "-c", post}, runner.Options{Stdin: nil, Stdout: stderr, Stderr: stderr, Env: append(env, "NETSSH_EXIT_CODE="+strconv.Itoa(code)), Timeout: 30 * time.Second})
if hook.Code != 0 {
fmt.Fprintf(stderr, "post hook failed (exit %d)\n", hook.Code)
if code == 0 {
code = hook.Code
}
}
}
if err := activity.Append(cfg.Activity, activity.Record{Time: start, Host: h.Name, Command: name, Code: code, Duration: time.Since(start)}); err != nil {
fmt.Fprintf(stderr, "activity: %v\n", err)
}
return code
}