Files
llmgw/internal/cli/tui_home.go
T
Ilker ManapandClaude Opus 5.5 aa3efcebbe Add a standby control plane with server add, and draw the screens as windows
server add gives another machine this llmgw, a certificate, the
configuration and store address, lets it into the store through Patroni, and
starts it; --vip keeps a floating address on the leader with keepalived,
following a new /leader endpoint. Nodes learn every control plane from the
leader and notice a dead one by HTTP/2 ping instead of after the kernel's
fifteen minutes. The setup, console, node and init screens are drawn full
screen in one frame: title bar, sidebar, bordered panel, key bar, dialogs.

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

269 lines
7.4 KiB
Go

package cli
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"runtime"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"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
height 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, m.height = msg.Width, msg.Height
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 {
host, _ := os.Hostname()
var nav []navItem
for i, it := range m.items() {
nav = append(nav, navItem{Label: it.label, Current: i == m.focus})
}
var body string
switch {
case !m.loaded:
body = "\n " + m.spin.View() + " asking the control plane…"
case m.err != nil:
body = "\n " + sBad.Render("The control plane does not answer:") + "\n " + m.err.Error() +
"\n\n " + sDim.Render("systemctl status llmgw-server says why.")
default:
body = "\n" + indent(m.summary, " ")
}
if it := m.items()[m.focus]; it.command != "" {
body += "\n\n " + sDim.Render("On the command line:") + "\n " + it.command
}
f := frame{
Width: m.width, Height: m.height,
Title: "llmgw console", Where: host,
NavTitle: "Do", Nav: nav,
PanelTitle: "Cluster", Body: body,
Keys: "↑↓ choose · Enter do it · Q leave · refreshed every five seconds",
}
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)
}
f.DialogTitle = it.label
f.Dialog = ask + "\n\n" + m.prompt.View() + "\n\n" + sDim.Render("Enter · Esc cancel")
}
return f.render()
}
func indent(s, pre string) string {
lines := strings.Split(strings.TrimRight(s, "\n"), "\n")
for i := range lines {
lines[i] = pre + lines[i]
}
return strings.Join(lines, "\n")
}
// 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, height: 30}
final, err := tea.NewProgram(m, tea.WithOutput(out), tea.WithAltScreen()).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')
}
}