Files
llmgw/internal/cli/tui_home.go
T
Ilker ManapandClaude Opus 5.5 38f2671ed9 Open setup screens or a console when llmgw runs bare in a terminal
With nothing installed, sudo llmgw asks for one machine or three, takes how
to reach the other two (ssh agent, key or password, and a sudo password used
only for the install), checks every machine first, shows the plan and its
command line, and installs. On a control plane it opens a console with the
cluster's state and the next steps. Machines are reached with Go's ssh so a
password works; the store's etcd and access rules use this machine's routed
address rather than a client-facing name.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-03 21:33:59 +02:00

260 lines
7.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cli
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"runtime"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
)
// A bare `llmgw` in a terminal: the setup screens on a machine with nothing
// installed, the console on a control plane. Anywhere else — a pipe, a
// script, another operating system — it prints its help, as it always did.
func interactive(cmd *cobra.Command) bool {
return isTerminal(cmd.OutOrStdout()) && isatty.IsTerminal(os.Stdin.Fd())
}
func runHome(cmd *cobra.Command) error {
out := cmd.OutOrStdout()
if runtime.GOOS != "linux" {
fmt.Fprintln(out, "The setup and console screens run on the Linux machine they manage.")
fmt.Fprintln(out)
return cmd.Help()
}
if os.Geteuid() != 0 {
return errors.New("run it as root — sudo llmgw — since it installs software and holds the certificate authority")
}
switch {
// Installed is the service in place, not only its configuration: a setup
// that stopped halfway has written server.yaml and nothing that runs, and
// the setup screens are what finish it.
case exists("/etc/llmgw/server.yaml") && exists("/etc/systemd/system/llmgw-server.service"):
return runConsole(cmd.Context(), out)
case exists("/etc/llmgw/agent.yaml"):
fmt.Fprintln(out, "This machine is a node. Its cluster is managed from the control plane:")
fmt.Fprintln(out, "run llmgw there. Here, `llmgw doctor` checks the machine.")
return nil
}
return runSetupTUI(cmd.Context(), out)
}
func exists(p string) bool {
_, err := os.Stat(p)
return err == nil
}
// What the console asks to be done once its screen closes: each runs its own
// screen, and the console comes back after.
type consoleAction struct {
kind string // "", "node", "patroni"
targets []string
}
type summaryMsg struct {
text string
store bool
err error
}
type consoleItem struct {
label, command, ask string
kind string
n int // how many targets to ask for
}
type consoleModel struct {
client *ctlClient
summary string
store bool
err error
focus int
asking bool
asked []string
prompt textField
action consoleAction
width int
spin spinnerModel
loaded bool
}
func (m consoleModel) items() []consoleItem {
its := []consoleItem{{label: "Add a node", kind: "node", n: 1,
ask: "user@address of the machine", command: "sudo llmgw node add <user>@<address>"}}
if !m.store {
its = append(its, consoleItem{label: "Replicate the store across two machines (Patroni)", kind: "patroni", n: 2,
ask: "user@address of database machine",
command: "sudo --preserve-env=SSH_AUTH_SOCK llmgw server init --patroni <user>@<a> --patroni <user>@<b> --no-mirror --no-node --no-model"})
}
return append(its, consoleItem{label: "Leave", kind: "quit"})
}
func (m consoleModel) fetch() tea.Msg {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var s adminapi.Summary
if err := m.client.get(ctx, "/api/v1/summary", &s); err != nil {
return summaryMsg{err: err}
}
var b bytes.Buffer
c := &cobra.Command{}
c.SetOut(&b)
printSummary(c, s)
return summaryMsg{text: b.String(), store: len(s.Store) > 0}
}
func (m consoleModel) Init() tea.Cmd { return tea.Batch(m.fetch, m.spin.Tick) }
func (m consoleModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
case spinTickMsg:
var cmd tea.Cmd
m.spin, cmd = m.spin.Update(msg)
return m, cmd
case summaryMsg:
m.loaded = true
m.err = msg.err
if msg.err == nil {
m.summary, m.store = msg.text, msg.store
}
return m, tea.Tick(5*time.Second, func(time.Time) tea.Msg { return m.fetch() })
case tea.KeyMsg:
if msg.Type == tea.KeyCtrlC {
return m, tea.Quit
}
its := m.items()
if m.asking {
switch msg.Type {
case tea.KeyEsc:
m.asking, m.asked = false, nil
case tea.KeyEnter:
v := strings.TrimSpace(m.prompt.Value())
if v == "" {
return m, nil
}
m.asked = append(m.asked, v)
m.prompt.SetValue("")
if it := its[m.focus]; len(m.asked) == it.n {
m.action = consoleAction{kind: it.kind, targets: m.asked}
return m, tea.Quit
}
default:
var cmd tea.Cmd
m.prompt, cmd = m.prompt.Update(msg)
return m, cmd
}
return m, nil
}
switch msg.String() {
case "up", "k", "shift+tab":
m.focus = (m.focus - 1 + len(its)) % len(its)
case "down", "j", "tab":
m.focus = (m.focus + 1) % len(its)
case "q":
return m, tea.Quit
case "enter":
it := its[m.focus]
if it.kind == "quit" {
return m, tea.Quit
}
m.asking, m.asked = true, nil
m.prompt = newTextField()
m.prompt.Placeholder, m.prompt.Width = "ubuntu@10.0.0.20", 40
m.prompt.Focus()
}
}
return m, nil
}
func (m consoleModel) View() string {
var b strings.Builder
host, _ := os.Hostname()
b.WriteString(sTitle.Render("llmgw · console") + " " + sDim.Render(host) + "\n\n")
switch {
case !m.loaded:
b.WriteString(" " + m.spin.View() + " asking the control plane…\n")
case m.err != nil:
b.WriteString(sBad.Render("The control plane does not answer: ") + m.err.Error() + "\n" +
sDim.Render("systemctl status llmgw-server says why.") + "\n")
default:
b.WriteString(m.summary)
}
b.WriteString("\n" + lipgloss.NewStyle().Bold(true).Render("Do") + "\n")
for i, it := range m.items() {
line := " " + it.label
if i == m.focus {
line = sTitle.Render("› " + it.label)
}
b.WriteString(line + "\n")
if i == m.focus && it.command != "" {
b.WriteString(" " + sDim.Render(it.command) + "\n")
}
}
if m.asking {
it := m.items()[m.focus]
ask := it.ask
if it.n > 1 {
ask = fmt.Sprintf("%s %d of %d", ask, len(m.asked)+1, it.n)
}
b.WriteString("\n" + ask + "\n " + m.prompt.View() + "\n" + sDim.Render("enter · esc to cancel") + "\n")
} else {
b.WriteString("\n" + sDim.Render("↑↓ to choose · enter · q to leave · refreshed every five seconds") + "\n")
}
return b.String()
}
// runConsole shows the console, runs what is chosen on it, and comes back.
func runConsole(ctx context.Context, out io.Writer) error {
addr := "127.0.0.1:9090"
if cfg, err := config.Load("/etc/llmgw/server.yaml"); err == nil && cfg.AdminListen != "" {
addr = cfg.AdminListen
if strings.HasPrefix(addr, ":") {
addr = "127.0.0.1" + addr
}
}
for {
m := consoleModel{client: newCtlClient(addr), spin: newSpinner(), width: 100}
final, err := tea.NewProgram(m, tea.WithOutput(out)).Run()
if err != nil {
return err
}
act := final.(consoleModel).action
switch act.kind {
case "":
return nil
case "node":
c := newNodeAddCmd()
c.SetArgs(act.targets)
c.SetOut(out)
err = c.ExecuteContext(ctx)
case "patroni":
in := newDefaultServerInit()
in.patroni = act.targets
// Already set up: only the store changes.
in.noMirror, in.noNode, in.noModel = true, true, true
err = in.runInitTUI(ctx, out)
}
if err != nil {
fmt.Fprintln(out, sBad.Render("✗ ")+err.Error())
}
fmt.Fprint(out, "\n"+sDim.Render("enter to go back to the console")+" ")
_, _ = bufio.NewReader(os.Stdin).ReadString('\n')
}
}