Files
2026-11-01 15:10:39 -05:00

115 lines
3.7 KiB
Go

package node
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/coder/websocket"
"slackerbot/internal/model"
)
type Node interface {
Name() string
Info(ctx context.Context) (NodeInfo, error)
Running(ctx context.Context) ([]RunningMachine, error)
IsRunning(machine string) bool
Start(ctx context.Context, spec StartSpec) error
Poweroff(ctx context.Context, machine string) error
SetLimits(ctx context.Context, machine string, size model.Size) error
SetQuota(ctx context.Context, drive, machine string, mb int64) error
DialAgent(ctx context.Context, machine, secret string) (MsgConn, error)
WaitStopped(ctx context.Context, machine string) error
ExportHome(ctx context.Context, drive, machine, parent string) (id string, err error)
ReadExport(ctx context.Context, id string, max int) (data []byte, eof bool, err error)
CloseExport(ctx context.Context, id string) error
ImportHome(ctx context.Context, drive, machine string) (id string, err error)
WriteImport(ctx context.Context, id string, data []byte) error
EndReceive(ctx context.Context, id string) error
FinishImport(ctx context.Context, id string, quotaMB int64) error
AbortImport(ctx context.Context, id string) error
DeleteHome(ctx context.Context, drive, machine string) error
// Paths are relative to the home folder.
ReadFile(ctx context.Context, drive, machine, path string, offset int64, limit int) (data []byte, size int64, err error)
WriteFile(ctx context.Context, drive, machine, path string, offset int64, data []byte) error
}
// Returned when a node runs an older version that doesn't know a call.
var ErrTooOld = errors.New("this node runs an older version")
type NodeInfo struct {
Name string `json:"name"`
Version string `json:"version,omitempty"`
Total model.Size `json:"total"`
Usable model.Size `json:"usable"`
Drives []DriveInfo `json:"drives"`
Running []RunningMachine `json:"running"`
}
type DriveInfo struct {
Path string `json:"path"`
FreeMB int64 `json:"free_mb"`
ReserveMB int64 `json:"reserve_mb"`
HomesMB map[string]int64 `json:"homes_mb"`
}
type RunningMachine struct {
Name string `json:"name"`
Since time.Time `json:"since"`
// Booted an older system than the node has now.
Outdated bool `json:"outdated,omitempty"`
}
type StartSpec struct {
Machine string `json:"machine"`
Description string `json:"description"`
Secret string `json:"secret"`
Size model.Size `json:"size"`
Drive string `json:"drive"`
StorageMaxMB int64 `json:"storage_max_mb"`
Env map[string]string `json:"env"`
}
type NodeOfflineError struct{ Node string }
func (e *NodeOfflineError) Error() string { return fmt.Sprintf("node %s is offline", e.Node) }
type NodeFullError struct{ Node string }
func (e *NodeFullError) Error() string { return fmt.Sprintf("node %s is full", e.Node) }
type MsgConn interface {
Read(ctx context.Context) ([]byte, error)
Write(ctx context.Context, b []byte) error
Close() error
}
type wsConn struct{ c *websocket.Conn }
func (w wsConn) Read(ctx context.Context) ([]byte, error) {
_, b, err := w.c.Read(ctx)
return b, err
}
func (w wsConn) Write(ctx context.Context, b []byte) error {
return w.c.Write(ctx, websocket.MessageText, b)
}
func (w wsConn) Close() error { return w.c.Close(websocket.StatusNormalClosure, "") }
func DialAgentWS(ctx context.Context, addr, secret string) (MsgConn, error) {
conn, _, err := websocket.Dial(ctx, "ws://"+addr+"/acp", &websocket.DialOptions{
HTTPHeader: http.Header{"X-Secret-Key": []string{secret}},
})
if err != nil {
return nil, fmt.Errorf("connecting to goose: %w", err)
}
conn.SetReadLimit(32 << 20)
return wsConn{conn}, nil
}