native

package
v0.0.0-...-1b78e83 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: Apache-2.0 Imports: 38 Imported by: 0

Documentation

Overview

Package native implements a native IaC plugin for Docker and process execution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractDockerfileCmdFromContext

func ExtractDockerfileCmdFromContext(contextPath, dockerfilePath string) ([]string, error)

ExtractDockerfileCmdFromContext extracts CMD from a Dockerfile in the build context.

func ParseDockerfileCmd

func ParseDockerfileCmd(dockerfilePath string) ([]string, error)

ParseDockerfileCmd parses a Dockerfile and extracts the CMD instruction.

Types

type BuildOptions

type BuildOptions struct {
	// Context is the build context directory
	Context string

	// Dockerfile is the path to the Dockerfile (relative to context)
	Dockerfile string

	// Tags are the image tags to apply
	Tags []string

	// BuildArgs are build-time variables
	BuildArgs map[string]string

	// Target is the build target stage
	Target string

	// Platform is the target platform (e.g., "linux/amd64")
	Platform string

	// NoCache disables build cache
	NoCache bool

	// Pull always pulls base images
	Pull bool

	// Labels to apply to the image
	Labels map[string]string

	// Output writers for build logs
	Stdout io.Writer
	Stderr io.Writer
}

BuildOptions configures a Docker image build.

type BuildResult

type BuildResult struct {
	// ImageID is the built image ID
	ImageID string

	// Tags are the tags applied to the image
	Tags []string

	// Digest is the image digest (if pushed)
	Digest string

	// Size is the image size in bytes
	Size int64
}

BuildResult contains the result of a Docker build.

type ContainerInfo

type ContainerInfo struct {
	ID    string
	Name  string
	Ports map[string]int
}

ContainerInfo contains container information.

type ContainerOptions

type ContainerOptions struct {
	Image            string
	Name             string
	Command          []string
	Entrypoint       []string
	Environment      map[string]string
	Ports            []PortMapping
	Volumes          []VolumeMount
	Network          string
	Restart          string
	Healthcheck      *Healthcheck
	LogDriver        string            // Docker logging driver (e.g., "fluentd", "json-file")
	LogOptions       map[string]string // Options for the logging driver
	ExtraHosts       []string          // Additional /etc/hosts entries (e.g., "host.docker.internal:host-gateway")
	ResolveLocalhost bool              // Replace "localhost" in env var values with "host.docker.internal"
	Wait             bool              // Wait for container to exit before returning (for one-shot tasks)
	OnProgress       func(string)      // Optional callback for sub-status updates (e.g., "pulling image...", "health check 5/30")
}

ContainerOptions defines options for creating a container.

type DestroyCommand

type DestroyCommand struct {
	Command     []interface{}          `yaml:"command"`               // Command to execute
	Image       string                 `yaml:"image,omitempty"`       // If set, run in a Docker container
	Network     string                 `yaml:"network,omitempty"`     // Docker network (when using image)
	WorkDir     string                 `yaml:"working_dir,omitempty"` // Working directory
	Environment map[string]interface{} `yaml:"environment,omitempty"` // Environment variables
}

DestroyCommand defines a command to run when a resource is destroyed. The command is resolved at apply time (expressions like ${inputs.*} are evaluated) and persisted in state so it's available during teardown.

type DockerClient

type DockerClient struct {
	// contains filtered or unexported fields
}

DockerClient wraps the Docker SDK client.

func NewDockerClient

func NewDockerClient() (*DockerClient, error)

NewDockerClient creates a new Docker client.

func (*DockerClient) BuildImage

func (d *DockerClient) BuildImage(ctx context.Context, opts BuildOptions) (*BuildResult, error)

BuildImage builds a Docker image from a Dockerfile.

func (*DockerClient) ContainerMatchesConfig

func (d *DockerClient) ContainerMatchesConfig(ctx context.Context, containerID string, opts ContainerOptions) bool

ContainerMatchesConfig checks if a running container matches the desired configuration. Returns true if the container can be reused (image matches, etc.).

func (*DockerClient) CreateNetwork

func (d *DockerClient) CreateNetwork(ctx context.Context, name string) (string, error)

CreateNetwork creates a Docker network.

func (*DockerClient) CreateVolume

func (d *DockerClient) CreateVolume(ctx context.Context, name string) (string, error)

CreateVolume creates a Docker volume.

func (*DockerClient) Exec

func (d *DockerClient) Exec(ctx context.Context, command []string, workDir string, env map[string]string) (string, error)

Exec executes a command on the host.

func (*DockerClient) GetContainerByName

func (d *DockerClient) GetContainerByName(ctx context.Context, name string) (string, error)

GetContainerByName finds a container by name and returns its ID. Returns empty string if not found.

func (*DockerClient) InspectContainer

func (d *DockerClient) InspectContainer(ctx context.Context, containerID string) (*ContainerInfo, error)

InspectContainer returns information about a container.

func (*DockerClient) IsContainerRunning

func (d *DockerClient) IsContainerRunning(ctx context.Context, containerID string) (bool, error)

IsContainerRunning checks if a container is running.

func (*DockerClient) NetworkExists

func (d *DockerClient) NetworkExists(ctx context.Context, networkID string) (bool, error)

NetworkExists checks if a network exists.

func (*DockerClient) PushImage

func (d *DockerClient) PushImage(ctx context.Context, imageName string, authConfig registry.AuthConfig) (string, error)

PushImage pushes an image to a registry.

func (*DockerClient) RemoveContainer

func (d *DockerClient) RemoveContainer(ctx context.Context, containerID string) error

RemoveContainer stops and removes a container.

func (*DockerClient) RemoveImage

func (d *DockerClient) RemoveImage(ctx context.Context, imageID string, force bool) error

RemoveImage removes a Docker image.

func (*DockerClient) RemoveNetwork

func (d *DockerClient) RemoveNetwork(ctx context.Context, networkID string) error

RemoveNetwork removes a Docker network.

func (*DockerClient) RemoveVolume

func (d *DockerClient) RemoveVolume(ctx context.Context, volumeName string) error

RemoveVolume removes a Docker volume.

func (*DockerClient) RunContainer

func (d *DockerClient) RunContainer(ctx context.Context, opts ContainerOptions) (string, error)

RunContainer creates and starts a container.

func (*DockerClient) RunOneShot

func (d *DockerClient) RunOneShot(ctx context.Context, opts RunOneShotOptions) (string, error)

RunOneShot runs a command in a temporary Docker container and returns the output.

func (*DockerClient) TagImage

func (d *DockerClient) TagImage(ctx context.Context, sourceImage, targetImage string) error

TagImage tags an image with a new tag.

func (*DockerClient) VolumeExists

func (d *DockerClient) VolumeExists(ctx context.Context, volumeName string) (bool, error)

VolumeExists checks if a volume exists.

type EvalContext

type EvalContext struct {
	Inputs    map[string]interface{}
	Resources map[string]*ResourceState
}

EvalContext provides values for expression evaluation.

type GracefulStop

type GracefulStop struct {
	Signal  string        // Signal name (e.g., "SIGTERM")
	Timeout time.Duration // Time to wait before SIGKILL
}

GracefulStop defines graceful shutdown configuration.

type Healthcheck

type Healthcheck struct {
	Command     []string
	Interval    string
	Timeout     string
	Retries     int
	StartPeriod string
}

Healthcheck defines a health check.

type InputDef

type InputDef struct {
	Type        string      `yaml:"type"`
	Required    bool        `yaml:"required"`
	Default     interface{} `yaml:"default"`
	Description string      `yaml:"description"`
	Sensitive   bool        `yaml:"sensitive"`
}

InputDef defines a module input.

type Module

type Module struct {
	Plugin    string               `yaml:"plugin"` // Must be "native"
	Type      string               `yaml:"type"`   // Primary resource type hint
	Inputs    map[string]InputDef  `yaml:"inputs"`
	Resources map[string]Resource  `yaml:"resources"`
	Outputs   map[string]OutputDef `yaml:"outputs"`

	// ResourceOrder preserves the YAML declaration order of resources.
	// The Apply loop iterates in this order so that sequentially-declared
	// resources execute top-to-bottom (matching author expectations).
	ResourceOrder []string `yaml:"-"`
}

Module represents a native module definition.

func LoadModule

func LoadModule(path string) (*Module, error)

LoadModule loads a native module definition from a path.

type OutputDef

type OutputDef struct {
	Value       string `yaml:"value"` // Expression to evaluate
	Description string `yaml:"description"`
	Sensitive   bool   `yaml:"sensitive"`
}

OutputDef defines a module output.

type Plugin

type Plugin struct {
	// contains filtered or unexported fields
}

Plugin implements the IaC plugin interface for native execution.

func NewPlugin

func NewPlugin() (*Plugin, error)

NewPlugin creates a new native plugin instance.

func (*Plugin) Apply

func (p *Plugin) Apply(ctx context.Context, opts iac.RunOptions) (*iac.ApplyResult, error)

func (*Plugin) Destroy

func (p *Plugin) Destroy(ctx context.Context, opts iac.RunOptions) error

func (*Plugin) Import

func (p *Plugin) Import(ctx context.Context, opts iac.ImportOptions) (*iac.ImportResult, error)

func (*Plugin) Name

func (p *Plugin) Name() string

func (*Plugin) Preview

func (p *Plugin) Preview(ctx context.Context, opts iac.RunOptions) (*iac.PreviewResult, error)

func (*Plugin) Refresh

func (p *Plugin) Refresh(ctx context.Context, opts iac.RunOptions) (*iac.RefreshResult, error)

type PortMapping

type PortMapping struct {
	ContainerPort int
	HostPort      int
	Protocol      string
}

PortMapping defines a port mapping.

type ProcessInfo

type ProcessInfo struct {
	PID         int
	Name        string
	Command     []string
	Environment map[string]string
	WorkingDir  string
}

ProcessInfo contains information about a running process.

type ProcessManager

type ProcessManager struct {
	// contains filtered or unexported fields
}

ProcessManager manages local processes.

func NewProcessManager

func NewProcessManager() *ProcessManager

NewProcessManager creates a new process manager.

func (*ProcessManager) GetProcessInfo

func (pm *ProcessManager) GetProcessInfo(name string) (*ProcessInfo, error)

GetProcessInfo returns information about a running process.

func (*ProcessManager) IsProcessRunning

func (pm *ProcessManager) IsProcessRunning(name string) bool

IsProcessRunning checks if a process is running.

func (*ProcessManager) StartProcess

func (pm *ProcessManager) StartProcess(ctx context.Context, opts ProcessOptions) (*ProcessInfo, error)

StartProcess starts a new process.

func (*ProcessManager) StopAll

func (pm *ProcessManager) StopAll(timeout time.Duration)

StopAll stops all managed processes.

func (*ProcessManager) StopAllWithPrefix

func (pm *ProcessManager) StopAllWithPrefix(prefix string, timeout time.Duration)

StopAllWithPrefix stops all processes whose names start with the given prefix. This is used for environment cleanup to stop all processes for an environment.

func (*ProcessManager) StopProcess

func (pm *ProcessManager) StopProcess(name string, timeout time.Duration) error

StopProcess stops a running process.

type ProcessOptions

type ProcessOptions struct {
	Name        string
	WorkingDir  string
	Command     []string
	Environment map[string]string
	// Readiness check configuration
	Readiness *ReadinessCheck
	// Graceful stop configuration
	GracefulStop *GracefulStop
	// Stdout receives process stdout. If nil, output is discarded.
	Stdout io.Writer
	// Stderr receives process stderr. If nil, output is discarded.
	Stderr io.Writer
}

ProcessOptions defines options for running a process.

type ReadinessCheck

type ReadinessCheck struct {
	Type     string        // "http" or "tcp"
	Endpoint string        // For HTTP: full URL, for TCP: host:port
	Interval time.Duration // How often to check
	Timeout  time.Duration // Total time to wait for ready
}

ReadinessCheck defines a process readiness check.

type ResolvedDestroyCommand

type ResolvedDestroyCommand struct {
	Command     []string          `json:"command"`
	Image       string            `json:"image,omitempty"`
	Network     string            `json:"network,omitempty"`
	WorkDir     string            `json:"working_dir,omitempty"`
	Environment map[string]string `json:"environment,omitempty"`
}

ResolvedDestroyCommand is the state-persisted form of a destroy command with all expressions already evaluated.

type Resource

type Resource struct {
	Type       string                 `yaml:"type"`
	When       string                 `yaml:"when,omitempty"`
	Properties map[string]interface{} `yaml:"properties"`
	DependsOn  []string               `yaml:"depends_on"`
	Destroy    *DestroyCommand        `yaml:"destroy,omitempty"`
}

Resource defines a native resource.

type ResourceState

type ResourceState struct {
	Type       string                 `json:"type"`
	ID         interface{}            `json:"id"`
	Properties map[string]interface{} `json:"properties"`
	Outputs    map[string]interface{} `json:"outputs"`
	// DestroyCmd holds a resolved destroy command to execute during teardown.
	// Nil if the resource has no custom destroy behaviour.
	DestroyCmd *ResolvedDestroyCommand `json:"destroy_cmd,omitempty"`
}

ResourceState represents a single resource's state.

type RunOneShotOptions

type RunOneShotOptions struct {
	Image            string
	Command          []string
	Environment      map[string]string
	Network          string
	WorkDir          string
	ResolveLocalhost bool // Replace "localhost" in env var values with "host.docker.internal"
}

RunOneShotOptions defines options for a one-shot container.

type State

type State struct {
	ModulePath string                    `json:"module_path"`
	Inputs     map[string]interface{}    `json:"inputs"`
	Resources  map[string]*ResourceState `json:"resources"`
	Outputs    map[string]interface{}    `json:"outputs"`
}

State represents the persisted state of native resources.

type VolumeMount

type VolumeMount struct {
	Name   string
	Source string
	Path   string
}

VolumeMount defines a volume mount.

Jump to

Keyboard shortcuts

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