Files
llmgw/internal/onboard/ssh.go
T
Ilker ManapandClaude Opus 5.5 383c4d5333
CI / test (push) Successful in 11m47s
A node with an NVIDIA card and no driver gets the driver its cards need
On Ubuntu, node add (and server init for its own machine) installs the
server driver ubuntu-drivers recommends for the cards on the PCI bus, with
the kernel modules Canonical builds and signs for the kernel: no DKMS, and
nothing to enrol under Secure Boot. nvidia-smi comes with it, and on an
HGX board the fabric manager, with nvlsm on Blackwell. When the driver
loads only after a restart, the console asks before restarting the
machine and goes on once it is back; --reboot restarts without asking,
--no-driver leaves the driver to the operator. `llmgw driver status` and
`llmgw driver install` do the same by hand.

Tried on node04 with a sysfs naming an RTX PRO 6000 Blackwell: real apt
and ubuntu-drivers installed 595-server-open and the 6.8.0-151 kernel its
signed modules are built for, and said a restart was needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 09:56:56 +01:00

98 lines
3.2 KiB
Go

package onboard
import (
"bytes"
"context"
"os/exec"
"runtime"
"sync"
)
// Remote runs a shell command on the machine being onboarded, with input.
type Remote interface {
Run(ctx context.Context, script string, stdin []byte) ([]byte, error)
}
// SSH is Remote through the system's ssh, so the operator's keys, agent and
// ~/.ssh/config apply exactly as they do when they log in by hand.
//
// Addr is looked up in the ssh config first: a Host block whose HostName is
// Addr is connected through by its name, so its User, IdentityFile and
// ProxyJump apply to a machine given by address, without the address
// having to be added to its Host line.
type SSH struct {
Addr, User string
// FallbackUser logs in when User is empty and the ssh config names no
// user for the machine: under sudo, the person who ran it.
FallbackUser string
once sync.Once
target string
}
// Target is what ssh is given: [user@]host, host being the ssh config's
// name for Addr when it has one.
func (s *SSH) Target(ctx context.Context) string {
s.once.Do(func() {
host := s.Addr
if a := configAlias(ctx, s.Addr); a != "" {
host = a
}
u := s.User
if u == "" && configUser(ctx, host) == "" {
u = s.FallbackUser
}
s.target = host
if u != "" {
s.target = u + "@" + host
}
})
return s.target
}
// sshOptions never prompt: a question nobody is there to answer would hang
// onboarding instead of failing it.
//
// accept-new rather than refusing unknown host keys: a machine provisioned a
// minute ago has a key nobody has seen, and the risk guarded against is a
// substituted machine, whose changed key is still refused.
var sshOptions = []string{
"-o", "BatchMode=yes",
"-o", "ConnectTimeout=10",
"-o", "StrictHostKeyChecking=accept-new",
"-o", "ServerAliveInterval=15",
// "Permanently added … to the list of known hosts" and the like are
// for a person at a prompt; here they would be read as the answer.
"-o", "LogLevel=ERROR",
}
// Run runs script with sh on the machine. Files travel on standard input,
// not scp, which a hardened sshd may not offer.
func (s *SSH) Run(ctx context.Context, script string, stdin []byte) ([]byte, error) {
target := s.Target(ctx)
// "--" before the target: an address beginning with "-" is not read as
// an option.
args := append(append([]string{}, sshOptions...), "--", target, "sh", "-c", shellQuote(script))
cmd := exec.CommandContext(ctx, "ssh", args...)
if stdin != nil {
cmd.Stdin = bytes.NewReader(stdin)
}
// The answer is standard output alone: ssh and the machine's login
// scripts write to standard error, and mixed in they were taken for the
// answer — the first connection's "Warning: Permanently added" read as
// the machine's hostname. Standard error comes back only with a failure,
// where it is usually the reason.
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err := cmd.Run(); err != nil {
return append(stdout.Bytes(), stderr.Bytes()...), err
}
return stdout.Bytes(), nil
}
func hostArch() string { return runtime.GOARCH }
// Reconnects is true: every command is a new connection, so a machine that
// restarted is reached again.
func (s *SSH) Reconnects() bool { return true }