container

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 16 Imported by: 0

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

View Source
const (
	DefaultNetworkName = "vega-network"
	LabelProject       = "vega.project"
	LabelManagedBy     = "vega.managed-by"
	DefaultImage       = "node:20-slim"
)
View Source
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

type ExecResult struct {
	ExitCode int
	Stdout   string
	Stderr   string
}

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) Close

func (m *Manager) Close() error

Close closes the Docker client.

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) GetLogs

func (m *Manager) GetLogs(ctx context.Context, projectName string, tail int) (string, error)

GetLogs returns logs from a project's container.

func (*Manager) GetProjectStatus

func (m *Manager) GetProjectStatus(ctx context.Context, projectName string) (*ProjectStatus, error)

GetProjectStatus returns the status of a project's container.

func (*Manager) IsAvailable

func (m *Manager) IsAvailable() bool

IsAvailable returns whether Docker is available.

func (*Manager) ListProjectContainers

func (m *Manager) ListProjectContainers(ctx context.Context) ([]string, error)

ListProjectContainers returns all vega-managed containers.

func (*Manager) RemoveProject

func (m *Manager) RemoveProject(ctx context.Context, projectName string) error

RemoveProject stops and removes a project's container.

func (*Manager) StartProject

func (m *Manager) StartProject(ctx context.Context, cfg ContainerConfig) (string, error)

StartProject starts a container for a project.

func (*Manager) StopProject

func (m *Manager) StopProject(ctx context.Context, projectName string) error

StopProject stops a project's container.

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.

type ProjectStatus

type ProjectStatus struct {
	ContainerID string
	Running     bool
	Image       string
	Created     time.Time
}

ProjectStatus holds the status of a project container.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL