Add persistent machine terminals with tmux-backed reattach
This commit is contained in:
@@ -0,0 +1,314 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// terminalStore persists the machine's terminal list; *pipeline.Store
|
||||
// implements it.
|
||||
type terminalStore interface {
|
||||
SaveTerminal(ctx context.Context, terminal protocol.TerminalRecord, keep int) error
|
||||
DeleteTerminal(ctx context.Context, id string) error
|
||||
Terminals(ctx context.Context) ([]protocol.TerminalRecord, error)
|
||||
}
|
||||
|
||||
const (
|
||||
terminalNoTmuxReason = "The Pipelines server restarted and this shell ended with it (tmux is not installed on this machine, so shells do not outlive the server)."
|
||||
terminalGoneReason = "Its tmux session ended while the Pipelines server was stopped."
|
||||
terminalStopReason = "The Pipelines server stopped and this shell ended with it (tmux is not installed on this machine)."
|
||||
// terminalReattachLimit bounds how often a tmux client that exits while
|
||||
// its session lives is replaced before the terminal is given up.
|
||||
terminalReattachLimit = 5
|
||||
)
|
||||
|
||||
// terminalManager owns the machine's terminals (terminal.go in protocol).
|
||||
type terminalManager struct {
|
||||
base context.Context
|
||||
store terminalStore
|
||||
clients *terminalClients
|
||||
tmux *terminalTmux
|
||||
shell func() string
|
||||
home func() (string, error)
|
||||
now func() time.Time
|
||||
flush time.Duration
|
||||
|
||||
mu sync.Mutex
|
||||
terms map[string]*terminalProcess
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func newTerminalManager(base context.Context, store terminalStore, tmux *terminalTmux) *terminalManager {
|
||||
return &terminalManager{
|
||||
base: base, store: store, clients: &terminalClients{}, tmux: tmux,
|
||||
shell: chatShellProgram, home: OperatorHomeDirectory, now: time.Now, flush: 50 * time.Millisecond,
|
||||
terms: map[string]*terminalProcess{},
|
||||
}
|
||||
}
|
||||
|
||||
func newTerminalID() (string, error) {
|
||||
var b [8]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "term_" + hex.EncodeToString(b[:]), nil
|
||||
}
|
||||
|
||||
// Open starts a login shell in cwd at cols x rows and returns its record.
|
||||
// clientID (when set) is subscribed to its output before the shell writes.
|
||||
func (m *terminalManager) Open(ctx context.Context, clientID, cwd string, cols, rows int) (protocol.TerminalRecord, error) {
|
||||
if m.store == nil {
|
||||
return protocol.TerminalRecord{}, errors.New("terminals need the server's store, which is not configured")
|
||||
}
|
||||
if !chatShellTerminals {
|
||||
return protocol.TerminalRecord{}, errors.New(protocol.ChatShellTerminalUnsupported)
|
||||
}
|
||||
home, err := m.home()
|
||||
if err != nil {
|
||||
return protocol.TerminalRecord{}, err
|
||||
}
|
||||
dir, err := resolveTerminalCwd(home, cwd)
|
||||
if err != nil {
|
||||
return protocol.TerminalRecord{}, err
|
||||
}
|
||||
id, err := newTerminalID()
|
||||
if err != nil {
|
||||
return protocol.TerminalRecord{}, err
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.base.Err() != nil {
|
||||
return protocol.TerminalRecord{}, errors.New("the server is stopping")
|
||||
}
|
||||
if running := m.runningLocked(); running >= protocol.TerminalOpenLimit {
|
||||
return protocol.TerminalRecord{}, fmt.Errorf("this machine already has %d open terminals; close one first", running)
|
||||
}
|
||||
cols, rows = chatShellTerminalSize(cols, rows)
|
||||
shell := m.shell()
|
||||
record := protocol.TerminalRecord{ID: id, Cwd: dir, Shell: shell, CreatedAt: m.now().UnixMilli(), Status: protocol.TerminalRunning, Cols: cols, Rows: rows}
|
||||
var cmd *exec.Cmd
|
||||
if m.tmux != nil {
|
||||
record.Tmux = terminalTmuxName(id)
|
||||
if err := m.tmux.create(record.Tmux, dir, shell, cols, rows); err != nil {
|
||||
return protocol.TerminalRecord{}, fmt.Errorf("could not start the terminal: %v", err)
|
||||
}
|
||||
cmd = m.tmux.attach(record.Tmux)
|
||||
} else {
|
||||
cmd = exec.Command(shell, "-l")
|
||||
cmd.Env = terminalEnv(os.Environ(), dir)
|
||||
}
|
||||
cmd.Dir = dir
|
||||
p := newTerminalProcess(record)
|
||||
terminal, err := p.startAttachment(cmd)
|
||||
if err == nil {
|
||||
err = m.store.SaveTerminal(ctx, record, protocol.TerminalEndedKept)
|
||||
if err != nil {
|
||||
p.signal(syscall.SIGKILL)
|
||||
terminal.Close()
|
||||
_ = cmd.Wait()
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if record.Tmux != "" {
|
||||
_ = m.tmux.kill(record.Tmux)
|
||||
}
|
||||
return protocol.TerminalRecord{}, fmt.Errorf("could not start the terminal: %v", err)
|
||||
}
|
||||
m.terms[id] = p
|
||||
if clientID != "" {
|
||||
m.clients.subscribe(clientID, id)
|
||||
}
|
||||
m.wg.Add(1)
|
||||
go m.supervise(p, cmd, terminal)
|
||||
go p.pumpInput()
|
||||
m.publishRecord(record)
|
||||
return record, nil
|
||||
}
|
||||
|
||||
func (m *terminalManager) runningLocked() int {
|
||||
running := 0
|
||||
for _, p := range m.terms {
|
||||
if p.running() {
|
||||
running++
|
||||
}
|
||||
}
|
||||
return running
|
||||
}
|
||||
|
||||
// List is every recorded terminal, oldest first, with the live state of the
|
||||
// ones this process holds.
|
||||
func (m *terminalManager) List(ctx context.Context) ([]protocol.TerminalRecord, error) {
|
||||
if m.store == nil {
|
||||
return nil, errors.New("terminals need the server's store, which is not configured")
|
||||
}
|
||||
records, err := m.store.Terminals(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i, record := range records {
|
||||
if p := m.process(record.ID); p != nil {
|
||||
records[i] = p.snapshot(false, 0)
|
||||
}
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func (m *terminalManager) process(id string) *terminalProcess {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return m.terms[id]
|
||||
}
|
||||
|
||||
func (m *terminalManager) live(id string) (*terminalProcess, error) {
|
||||
p := m.process(id)
|
||||
if p == nil || !p.running() {
|
||||
return nil, errors.New("this terminal is not running")
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Input queues data for a running terminal, in arrival order. It is kept
|
||||
// nowhere else: what the shell echoes is output.
|
||||
func (m *terminalManager) Input(id, data string) error {
|
||||
switch {
|
||||
case data == "":
|
||||
return errors.New("input is required")
|
||||
case len(data) > protocol.ChatShellInputLimit:
|
||||
return errors.New("the input exceeds 16 KiB; send it in parts")
|
||||
case !utf8.ValidString(data):
|
||||
return errors.New("input must be UTF-8 text")
|
||||
}
|
||||
p, err := m.live(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-p.done:
|
||||
return errors.New("this terminal is not running")
|
||||
case p.input <- []byte(data):
|
||||
return nil
|
||||
default:
|
||||
return errors.New("the terminal is not reading its input; wait for it or press Ctrl-C")
|
||||
}
|
||||
}
|
||||
|
||||
// Resize sets a running terminal's size and returns its record.
|
||||
func (m *terminalManager) Resize(id string, cols, rows int) (protocol.TerminalRecord, error) {
|
||||
if cols <= 0 || rows <= 0 {
|
||||
return protocol.TerminalRecord{}, errors.New("positive cols and rows are required")
|
||||
}
|
||||
p, err := m.live(id)
|
||||
if err != nil {
|
||||
return protocol.TerminalRecord{}, err
|
||||
}
|
||||
cols, rows = chatShellTerminalSize(cols, rows)
|
||||
p.mu.Lock()
|
||||
p.record.Cols, p.record.Rows = cols, rows
|
||||
terminal := p.pty
|
||||
p.mu.Unlock()
|
||||
if terminal != nil {
|
||||
if err := resizeChatShellTerminal(terminal, cols, rows); err != nil {
|
||||
return protocol.TerminalRecord{}, err
|
||||
}
|
||||
}
|
||||
return p.snapshot(false, 0), nil
|
||||
}
|
||||
|
||||
// Output is one terminal with its retained output from byte after on. A
|
||||
// terminal ended before this process started has no output.
|
||||
func (m *terminalManager) Output(ctx context.Context, id string, after int64) (protocol.TerminalRecord, error) {
|
||||
if p := m.process(id); p != nil {
|
||||
return p.snapshot(true, after), nil
|
||||
}
|
||||
records, err := m.List(ctx)
|
||||
if err != nil {
|
||||
return protocol.TerminalRecord{}, err
|
||||
}
|
||||
for _, record := range records {
|
||||
if record.ID == id {
|
||||
return record, nil
|
||||
}
|
||||
}
|
||||
return protocol.TerminalRecord{}, errors.New("this machine has no such terminal")
|
||||
}
|
||||
|
||||
// Close ends a terminal (killing its shell and tmux session) and forgets
|
||||
// it. It waits briefly for the shell to go and returns the closed record.
|
||||
func (m *terminalManager) Close(ctx context.Context, id string) (protocol.TerminalRecord, error) {
|
||||
if m.store == nil {
|
||||
return protocol.TerminalRecord{}, errors.New("terminals need the server's store, which is not configured")
|
||||
}
|
||||
p := m.process(id)
|
||||
if p != nil && p.running() {
|
||||
p.mu.Lock()
|
||||
p.closing = true
|
||||
name := p.record.Tmux
|
||||
p.mu.Unlock()
|
||||
if name != "" && m.tmux != nil {
|
||||
if err := m.tmux.kill(name); err != nil {
|
||||
log.Printf("terminal close %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
p.signal(syscall.SIGHUP)
|
||||
select {
|
||||
case <-p.done:
|
||||
case <-time.After(3 * time.Second):
|
||||
p.signal(syscall.SIGKILL)
|
||||
<-p.done
|
||||
}
|
||||
return p.snapshot(false, 0), nil
|
||||
}
|
||||
record, err := m.Output(ctx, id, 0)
|
||||
if err != nil {
|
||||
return protocol.TerminalRecord{}, err
|
||||
}
|
||||
if record.Tmux != "" && m.tmux != nil && m.tmux.alive(record.Tmux) {
|
||||
_ = m.tmux.kill(record.Tmux)
|
||||
}
|
||||
m.forget(id)
|
||||
record.Output, record.OutputStart = "", 0
|
||||
record.Status = protocol.TerminalClosed
|
||||
m.publishRecord(record)
|
||||
return record, nil
|
||||
}
|
||||
|
||||
// forget drops a terminal's record and retained output.
|
||||
func (m *terminalManager) forget(id string) {
|
||||
m.mu.Lock()
|
||||
delete(m.terms, id)
|
||||
m.mu.Unlock()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := m.store.DeleteTerminal(ctx, id); err != nil {
|
||||
log.Printf("terminal delete %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *terminalManager) save(record protocol.TerminalRecord) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := m.store.SaveTerminal(ctx, record, protocol.TerminalEndedKept); err != nil {
|
||||
log.Printf("terminal save %s: %v", record.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *terminalManager) publishRecord(record protocol.TerminalRecord) {
|
||||
record.Output, record.OutputStart = "", 0
|
||||
m.clients.publish(protocol.TerminalEventFrame{TerminalID: record.ID, Terminal: &record})
|
||||
}
|
||||
|
||||
// Wait blocks until every supervised terminal has been released (tests,
|
||||
// shutdown).
|
||||
func (m *terminalManager) Wait() { m.wg.Wait() }
|
||||
Reference in New Issue
Block a user