Documentation
¶
Overview ¶
Package container provides Docker container management for project isolation.
The container package enables agents to execute tools in isolated Docker containers, providing security and reproducibility for agent operations.
Overview ¶
The package provides two main components:
- Manager: Low-level Docker container operations (start, stop, exec)
- ProjectRegistry: High-level project management with persistence
Graceful Degradation ¶
The container system is designed as an optional feature. When Docker is unavailable, Manager.IsAvailable() returns false and operations gracefully degrade to local execution.
Usage with Tools ¶
The container package integrates with govega's Tools system through the WithContainer and WithContainerRouting options, allowing specific tools to be routed to container execution while others run locally.
Example ¶
// Create container manager
cm, err := container.NewManager(baseDir,
container.WithNetworkName("my-network"),
container.WithDefaultImage("node:20-slim"),
)
if err != nil {
log.Fatal(err)
}
defer cm.Close()
// Check availability
if cm.IsAvailable() {
// Start a project container
id, err := cm.StartProject(ctx, container.ContainerConfig{
ProjectName: "my-project",
Image: "python:3.11-slim",
})
if err != nil {
log.Fatal(err)
}
// Execute a command
result, err := cm.Exec(ctx, "my-project", []string{"python", "--version"}, "")
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Stdout)
}
Index ¶
- Constants
- type ContainerConfig
- type ExecResult
- type Manager
- func (m *Manager) Close() error
- func (m *Manager) Exec(ctx context.Context, projectName string, command []string, workDir string) (*ExecResult, error)
- func (m *Manager) GetLogs(ctx context.Context, projectName string, tail int) (string, error)
- func (m *Manager) GetProjectStatus(ctx context.Context, projectName string) (*ProjectStatus, error)
- func (m *Manager) IsAvailable() bool
- func (m *Manager) ListProjectContainers(ctx context.Context) ([]string, error)
- func (m *Manager) RemoveProject(ctx context.Context, projectName string) error
- func (m *Manager) StartProject(ctx context.Context, cfg ContainerConfig) (string, error)
- func (m *Manager) StopProject(ctx context.Context, projectName string) error
- type ManagerOption
- type Project
- type ProjectRegistry
- func (r *ProjectRegistry) ArchiveStaleProjects(ctx context.Context) ([]string, error)
- func (r *ProjectRegistry) CreateProject(ctx context.Context, name, description, image string) (*Project, error)
- func (r *ProjectRegistry) DeleteProject(ctx context.Context, name string) error
- func (r *ProjectRegistry) Exec(ctx context.Context, projectName string, command []string) (*ExecResult, error)
- func (r *ProjectRegistry) GetOrCreateProject(ctx context.Context, name, description, image string) (*Project, error)
- func (r *ProjectRegistry) GetProject(name string) (*Project, error)
- func (r *ProjectRegistry) GetProjectPath(name string) string
- func (r *ProjectRegistry) ListProjects() []*Project
- func (r *ProjectRegistry) Reconcile(ctx context.Context) error
- func (r *ProjectRegistry) StartProject(ctx context.Context, name string) error
- func (r *ProjectRegistry) StopProject(ctx context.Context, name string) error
- type ProjectStatus
Constants ¶
const ( DefaultNetworkName = "vega-network" LabelProject = "vega.project" LabelManagedBy = "vega.managed-by" DefaultImage = "node:20-slim" )
const ( // StaleProjectAge is how long before a project is considered stale. StaleProjectAge = 30 * 24 * time.Hour // 30 days )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContainerConfig ¶
type ContainerConfig struct {
ProjectName string
Image string
WorkDir string
Env []string
Ports map[string]string // container port -> host port
}
ContainerConfig holds configuration for a project container.
type ExecResult ¶
ExecResult holds the result of a command execution.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager handles Docker container operations for projects.
func NewManager ¶
func NewManager(baseDir string, opts ...ManagerOption) (*Manager, error)
NewManager creates a new container manager. If Docker is unavailable, it returns a Manager with available=false.
func (*Manager) Exec ¶
func (m *Manager) Exec(ctx context.Context, projectName string, command []string, workDir string) (*ExecResult, error)
Exec runs a command in a project's container. If the container doesn't exist, it will be auto-created with the default image.
func (*Manager) GetProjectStatus ¶
GetProjectStatus returns the status of a project's container.
func (*Manager) IsAvailable ¶
IsAvailable returns whether Docker is available.
func (*Manager) ListProjectContainers ¶
ListProjectContainers returns all vega-managed containers.
func (*Manager) RemoveProject ¶
RemoveProject stops and removes a project's container.
func (*Manager) StartProject ¶
StartProject starts a container for a project.
type ManagerOption ¶
type ManagerOption func(*Manager)
ManagerOption configures a Manager.
func WithDefaultImage ¶
func WithDefaultImage(img string) ManagerOption
WithDefaultImage sets the default container image.
func WithNetworkName ¶
func WithNetworkName(name string) ManagerOption
WithNetworkName sets a custom Docker network name.
type Project ¶
type Project struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Image string `json:"image"`
Created time.Time `json:"created"`
Status string `json:"status"` // created, running, stopped
Env map[string]string `json:"env,omitempty"`
}
Project represents a managed project.
type ProjectRegistry ¶
type ProjectRegistry struct {
// contains filtered or unexported fields
}
ProjectRegistry manages the project registry.
func NewProjectRegistry ¶
func NewProjectRegistry(baseDir string, manager *Manager) (*ProjectRegistry, error)
NewProjectRegistry creates a new project registry.
func (*ProjectRegistry) ArchiveStaleProjects ¶
func (r *ProjectRegistry) ArchiveStaleProjects(ctx context.Context) ([]string, error)
ArchiveStaleProjects moves projects older than StaleProjectAge to an archive folder and removes them from the active registry.
func (*ProjectRegistry) CreateProject ¶
func (r *ProjectRegistry) CreateProject(ctx context.Context, name, description, image string) (*Project, error)
CreateProject creates a new project.
func (*ProjectRegistry) DeleteProject ¶
func (r *ProjectRegistry) DeleteProject(ctx context.Context, name string) error
DeleteProject removes a project.
func (*ProjectRegistry) Exec ¶
func (r *ProjectRegistry) Exec(ctx context.Context, projectName string, command []string) (*ExecResult, error)
Exec runs a command in a project's container.
func (*ProjectRegistry) GetOrCreateProject ¶
func (r *ProjectRegistry) GetOrCreateProject(ctx context.Context, name, description, image string) (*Project, error)
GetOrCreateProject gets an existing project or creates a new one.
func (*ProjectRegistry) GetProject ¶
func (r *ProjectRegistry) GetProject(name string) (*Project, error)
GetProject returns a project by name.
func (*ProjectRegistry) GetProjectPath ¶
func (r *ProjectRegistry) GetProjectPath(name string) string
GetProjectPath returns the host path to a project's directory.
func (*ProjectRegistry) ListProjects ¶
func (r *ProjectRegistry) ListProjects() []*Project
ListProjects returns all projects.
func (*ProjectRegistry) Reconcile ¶
func (r *ProjectRegistry) Reconcile(ctx context.Context) error
Reconcile syncs the registry with actual container state.
func (*ProjectRegistry) StartProject ¶
func (r *ProjectRegistry) StartProject(ctx context.Context, name string) error
StartProject starts a project's container.
func (*ProjectRegistry) StopProject ¶
func (r *ProjectRegistry) StopProject(ctx context.Context, name string) error
StopProject stops a project's container.