90 lines
2.8 KiB
Go
90 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
)
|
|
|
|
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 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 Size `json:"total"`
|
|
Usable 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 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, "") }
|