clean up of the codebase
This commit is contained in:
1 parent
7822529a38
commit
0ac61b4ece
52 files changed
+2545
-2274
No files matched your search
@@ -0,0 +1,103 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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
|
||||
}
|
||||
|
||||
type NodeInfo struct {
|
||||
Name string `json:"name"`
|
||||
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"`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user