Files
pipelines/internal/pipeline/terminals.go
T
alan 227c036aa5 Start machine terminals at the su login prompt instead of tmux
A new terminal runs su -l for the server's own account in its PTY, so the
operator types the account password before getting a shell. Opening is
refused when su is missing, the server runs as root, or su opens a shell
without asking. The Pipelines-owned tmux backing, its reattach and its
persisted session names are removed: terminals end with the server and
are listed as ended. A starting server ends leftover pipelines-term_*
sessions on tmux -L pipelines only.
2026-11-10 00:37:11 +00:00

90 lines
3.0 KiB
Go

package pipeline
import (
"context"
"database/sql"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// initTerminals creates the additive table of machine terminals. It records
// only what a restarted server needs to list them (id, title, cwd, shell,
// status). Output and typed input are never stored. The tmux column is left
// from servers that kept shells in tmux; it is no longer written.
func (s *Store) initTerminals() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS terminals (
id TEXT PRIMARY KEY,
title TEXT NOT NULL DEFAULT '',
cwd TEXT NOT NULL,
shell TEXT NOT NULL DEFAULT '',
created_at INTEGER NOT NULL,
tmux TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL,
ended_at INTEGER NOT NULL DEFAULT 0,
exit_code INTEGER,
reason TEXT NOT NULL DEFAULT '',
cols INTEGER NOT NULL DEFAULT 0,
rows INTEGER NOT NULL DEFAULT 0);`)
return err
}
// SaveTerminal inserts or replaces one terminal. Saving an ended terminal
// prunes the oldest ended ones beyond keep (keep <= 0 keeps everything).
func (s *Store) SaveTerminal(ctx context.Context, t protocol.TerminalRecord, keep int) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
var exit any
if t.ExitCode != nil {
exit = *t.ExitCode
}
if _, err := tx.ExecContext(ctx, `INSERT INTO terminals
(id, title, cwd, shell, created_at, status, ended_at, exit_code, reason, cols, rows)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET title = excluded.title, status = excluded.status, ended_at = excluded.ended_at,
exit_code = excluded.exit_code, reason = excluded.reason, cols = excluded.cols, rows = excluded.rows`,
t.ID, t.Title, t.Cwd, t.Shell, t.CreatedAt, t.Status, t.EndedAt, exit, t.Reason, t.Cols, t.Rows); err != nil {
return err
}
if keep > 0 && t.Status != protocol.TerminalRunning {
if _, err := tx.ExecContext(ctx, `DELETE FROM terminals WHERE status != ? AND id NOT IN
(SELECT id FROM terminals WHERE status != ? ORDER BY ended_at DESC, id DESC LIMIT ?)`,
protocol.TerminalRunning, protocol.TerminalRunning, keep); err != nil {
return err
}
}
return tx.Commit()
}
// DeleteTerminal removes one terminal's record.
func (s *Store) DeleteTerminal(ctx context.Context, id string) error {
_, err := s.db.ExecContext(ctx, `DELETE FROM terminals WHERE id = ?`, id)
return err
}
// Terminals lists every recorded terminal, oldest first.
func (s *Store) Terminals(ctx context.Context) ([]protocol.TerminalRecord, error) {
rows, err := s.db.QueryContext(ctx, `SELECT id, title, cwd, shell, created_at, status, ended_at, exit_code, reason, cols, rows
FROM terminals ORDER BY created_at, id`)
if err != nil {
return nil, err
}
defer rows.Close()
var out []protocol.TerminalRecord
for rows.Next() {
var t protocol.TerminalRecord
var exit sql.NullInt64
if err := rows.Scan(&t.ID, &t.Title, &t.Cwd, &t.Shell, &t.CreatedAt, &t.Status, &t.EndedAt, &exit, &t.Reason, &t.Cols, &t.Rows); err != nil {
return nil, err
}
if exit.Valid {
code := int(exit.Int64)
t.ExitCode = &code
}
out = append(out, t)
}
return out, rows.Err()
}