- Introduce new unit test targets for Workspace and Sandbox V2 integration in the Makefile, ensuring comprehensive testing of the new features. - Update CI workflows to support MongoDB service for Sandbox V2 tests and improve Docker image handling. - Implement Workspace as a first-class entity, allowing for persistent storage decoupled from container lifecycles. - Enhance the Box struct to manage workspace IDs and update related methods for improved functionality. - Refactor tests to accommodate new Workspace features, ensuring robust testing of the integration. These changes significantly improve the testing framework and functionality of the Sandbox V2, enhancing the overall architecture and user experience.
178 lines
3.4 KiB
Go
178 lines
3.4 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"io"
|
|
"time"
|
|
|
|
"github.com/yaoapp/yao/tai"
|
|
)
|
|
|
|
type LifecyclePolicy string
|
|
|
|
const (
|
|
OneShot LifecyclePolicy = "oneshot"
|
|
Session LifecyclePolicy = "session"
|
|
LongRunning LifecyclePolicy = "longrunning"
|
|
Persistent LifecyclePolicy = "persistent"
|
|
)
|
|
|
|
const DefaultStopTimeout = 2 * time.Second
|
|
|
|
type Pool struct {
|
|
Name string
|
|
Addr string
|
|
Options []tai.Option
|
|
MaxPerUser int
|
|
MaxTotal int
|
|
IdleTimeout time.Duration
|
|
MaxLifetime time.Duration
|
|
StopTimeout time.Duration // SIGTERM grace period before SIGKILL; 0 = DefaultStopTimeout
|
|
}
|
|
|
|
type PoolInfo struct {
|
|
Name string
|
|
Addr string
|
|
Connected bool
|
|
Boxes int
|
|
MaxPerUser int
|
|
MaxTotal int
|
|
IdleTimeout time.Duration
|
|
MaxLifetime time.Duration
|
|
}
|
|
|
|
type PortMapping struct {
|
|
ContainerPort int
|
|
HostPort int
|
|
HostIP string
|
|
Protocol string
|
|
}
|
|
|
|
type CreateOptions struct {
|
|
ID string
|
|
Owner string
|
|
Labels map[string]string
|
|
Pool string
|
|
Image string
|
|
WorkDir string
|
|
User string
|
|
Env map[string]string
|
|
Memory int64
|
|
CPUs float64
|
|
VNC bool
|
|
Ports []PortMapping
|
|
Policy LifecyclePolicy
|
|
IdleTimeout time.Duration
|
|
|
|
StopTimeout time.Duration // SIGTERM grace period; 0 = pool default or DefaultStopTimeout
|
|
|
|
WorkspaceID string // workspace to mount; empty = no workspace
|
|
MountMode string // "rw" (default) or "ro"
|
|
MountPath string // container path; default "/workspace"
|
|
}
|
|
|
|
type ListOptions struct {
|
|
Owner string
|
|
Pool string
|
|
Labels map[string]string
|
|
}
|
|
|
|
type execConfig struct {
|
|
WorkDir string
|
|
Env map[string]string
|
|
Timeout time.Duration
|
|
}
|
|
|
|
type ExecOption func(*execConfig)
|
|
|
|
func WithWorkDir(dir string) ExecOption {
|
|
return func(c *execConfig) {
|
|
c.WorkDir = dir
|
|
}
|
|
}
|
|
|
|
func WithEnv(env map[string]string) ExecOption {
|
|
return func(c *execConfig) {
|
|
c.Env = env
|
|
}
|
|
}
|
|
|
|
func WithTimeout(timeout time.Duration) ExecOption {
|
|
return func(c *execConfig) {
|
|
c.Timeout = timeout
|
|
}
|
|
}
|
|
|
|
type ExecResult struct {
|
|
ExitCode int
|
|
Stdout string
|
|
Stderr string
|
|
}
|
|
|
|
type ExecStream struct {
|
|
Stdout io.ReadCloser
|
|
Stderr io.ReadCloser
|
|
Stdin io.WriteCloser
|
|
Wait func() (int, error)
|
|
Cancel func()
|
|
}
|
|
|
|
type attachConfig struct {
|
|
Protocol string
|
|
Path string
|
|
Headers map[string]string
|
|
}
|
|
|
|
type AttachOption func(*attachConfig)
|
|
|
|
func WithProtocol(protocol string) AttachOption {
|
|
return func(c *attachConfig) {
|
|
c.Protocol = protocol
|
|
}
|
|
}
|
|
|
|
func WithPath(path string) AttachOption {
|
|
return func(c *attachConfig) {
|
|
c.Path = path
|
|
}
|
|
}
|
|
|
|
func WithHeaders(headers map[string]string) AttachOption {
|
|
return func(c *attachConfig) {
|
|
c.Headers = headers
|
|
}
|
|
}
|
|
|
|
// ImagePullOptions configures an image pull operation.
|
|
type ImagePullOptions struct {
|
|
Auth *RegistryAuth // nil = anonymous / public
|
|
}
|
|
|
|
// RegistryAuth holds credentials for a private container registry.
|
|
type RegistryAuth struct {
|
|
Username string
|
|
Password string
|
|
Server string
|
|
}
|
|
|
|
type ServiceConn struct {
|
|
Read func() ([]byte, error)
|
|
Write func(data []byte) error
|
|
Events <-chan []byte
|
|
URL string
|
|
Close func() error
|
|
}
|
|
|
|
type BoxInfo struct {
|
|
ID string
|
|
ContainerID string
|
|
Pool string
|
|
Owner string
|
|
Status string
|
|
Policy LifecyclePolicy
|
|
Labels map[string]string
|
|
Image string
|
|
CreatedAt time.Time
|
|
LastActive time.Time
|
|
ProcessCount int
|
|
VNC bool
|
|
}
|