tofu

package
v0.46.5 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package tofu provides OpenTofu execution capabilities.

Index

Constants

View Source
const (
	// ProvidersDirEnv overrides the packaged OpenTofu provider mirror.
	ProvidersDirEnv = "STACKKIT_TOFU_PROVIDERS_DIR"
	// ProvidersDirName is the mirror directory shipped beside the packaged
	// tofu binary in every release archive.
	ProvidersDirName = "providers"
	// ProviderRegistryHost is the registry namespace the mirror serves.
	ProviderRegistryHost = "registry.opentofu.org"
	// PinnedLocalProviderVersion is the hashicorp/local release that
	// scripts/release/fetch-opentofu-providers.sh packages. The rendered
	// Stage 1 roots constrain the provider to a range that admits it.
	PinnedLocalProviderVersion = "2.5.3"
)

Variables

View Source
var ErrProviderMirrorMissing = errors.New("packaged OpenTofu provider mirror is missing")

ErrProviderMirrorMissing reports that the packaged offline provider mirror is absent or incomplete for this platform.

View Source
var OfflineInheritedEnv = []string{
	"TF_PLUGIN_CACHE_DIR",
	"TF_PLUGIN_CACHE_MAY_BREAK_DEPENDENCY_LOCK_FILE",
	"TF_CLI_CONFIG_FILE",
	"TF_CLI_ARGS",
	"TF_CLI_ARGS_init",
	"TF_CLI_ARGS_plan",
	"TF_CLI_ARGS_apply",
	"TF_DATA_DIR",
	"TF_WORKSPACE",
	"TOFU_CLI_CONFIG_FILE",
}

OfflineInheritedEnv names host variables that could redirect provider installation, add CLI arguments, or move the working data directory. The runtime executor removes them before every OpenTofu process it starts.

Functions

func BinaryName

func BinaryName() string

BinaryName returns the OpenTofu executable name for the current platform.

func DefaultBinary

func DefaultBinary() string

DefaultBinary resolves the OpenTofu binary used by regular execution. It intentionally avoids PATH fallback so a host-installed OpenTofu cannot make StackKit appear release-ready when the package is missing its own copy.

func EnsureStateDir

func EnsureStateDir(baseDir string) error

EnsureStateDir ensures the state directory exists

func HasTerraformFiles

func HasTerraformFiles(dir string) (bool, error)

HasTerraformFiles checks if directory contains .tf files

func IsTimeoutError

func IsTimeoutError(err error) bool

IsTimeoutError checks if an error is a timeout error

func OfflineCLIConfig added in v0.46.5

func OfflineCLIConfig(providersDir string) []byte

OfflineCLIConfig renders a CLI configuration that installs every registry.opentofu.org provider from the filesystem mirror and forbids direct registry installation, so tofu init never reaches the network.

func PackagedBinaryPath

func PackagedBinaryPath() (string, bool)

PackagedBinaryPath returns the StackKit-packaged OpenTofu binary path. It intentionally does not fall back to PATH: product and release tests must prove that OpenTofu ships with StackKit, not that the host happens to have it.

func PackagedProvidersDir added in v0.46.5

func PackagedProvidersDir() (string, bool)

PackagedProvidersDir returns the StackKit-packaged provider mirror. Like PackagedBinaryPath it never consults PATH or a host plugin cache: the override is STACKKIT_TOFU_PROVIDERS_DIR, otherwise providers/ beside the executable, beside bin/, or in the Debian package's lib directory.

func RequireLocalProviderMirror added in v0.46.5

func RequireLocalProviderMirror(dir string) error

RequireLocalProviderMirror verifies that dir holds the pinned hashicorp/local provider for the running platform in the unpacked filesystem-mirror layout. It fails closed with an actionable error.

func ValidateWorkDir

func ValidateWorkDir(dir string) error

ValidateWorkDir validates the working directory

Types

type Executor

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

Executor handles OpenTofu command execution

func NewExecutor

func NewExecutor(opts ...ExecutorOption) *Executor

NewExecutor creates a new OpenTofu executor

func (*Executor) Apply

func (e *Executor) Apply(ctx context.Context, planFile string) (*Result, error)

Apply runs tofu apply

func (*Executor) Destroy

func (e *Executor) Destroy(ctx context.Context) (*Result, error)

Destroy runs tofu destroy

func (*Executor) Format

func (e *Executor) Format(ctx context.Context) (*Result, error)

Format runs tofu fmt to format configuration files

func (*Executor) GetWorkDir

func (e *Executor) GetWorkDir() string

GetWorkDir returns the working directory

func (*Executor) Graph

func (e *Executor) Graph(ctx context.Context) (*Result, error)

Graph generates a visual graph of resources

func (*Executor) Import

func (e *Executor) Import(ctx context.Context, address, id string) (*Result, error)

Import imports an existing resource into state

func (*Executor) Init

func (e *Executor) Init(ctx context.Context) (*Result, error)

Init runs tofu init

func (*Executor) IsInstalled

func (e *Executor) IsInstalled() bool

IsInstalled checks if tofu is installed

func (*Executor) Output

func (e *Executor) Output(ctx context.Context) (*Result, error)

Output runs tofu output and returns the outputs

func (*Executor) Plan

func (e *Executor) Plan(ctx context.Context, outFile string, destroy bool) (*Result, error)

Plan runs tofu plan

func (*Executor) Providers

func (e *Executor) Providers(ctx context.Context) (*Result, error)

Providers shows required providers

func (*Executor) Refresh

func (e *Executor) Refresh(ctx context.Context) (*Result, error)

Refresh runs tofu refresh to sync state with real infrastructure

func (*Executor) SetAutoApprove

func (e *Executor) SetAutoApprove(autoApprove bool)

SetAutoApprove sets the auto-approve flag dynamically

func (*Executor) SetWorkDir

func (e *Executor) SetWorkDir(dir string)

SetWorkDir sets the working directory

func (*Executor) Show

func (e *Executor) Show(ctx context.Context, planFile string) (*Result, error)

Show runs tofu show on a plan file

func (*Executor) State

func (e *Executor) State(ctx context.Context) (*Result, error)

State returns the current state

func (*Executor) Taint

func (e *Executor) Taint(ctx context.Context, address string) (*Result, error)

Taint marks a resource for recreation

func (*Executor) Untaint

func (e *Executor) Untaint(ctx context.Context, address string) (*Result, error)

Untaint removes the taint from a resource

func (*Executor) Validate

func (e *Executor) Validate(ctx context.Context) (*Result, error)

Validate runs tofu validate

func (*Executor) Version

func (e *Executor) Version(ctx context.Context) (string, error)

Version returns the tofu version

type ExecutorOption

type ExecutorOption func(*Executor)

ExecutorOption configures the Executor

func WithAutoApprove

func WithAutoApprove(autoApprove bool) ExecutorOption

WithAutoApprove enables auto-approve for apply/destroy

func WithBinary

func WithBinary(binary string) ExecutorOption

WithBinary sets the tofu binary path

func WithEnv

func WithEnv(values ...string) ExecutorOption

WithEnv appends environment values for OpenTofu commands.

func WithTimeout

func WithTimeout(timeout time.Duration) ExecutorOption

WithTimeout sets the execution timeout

func WithWorkDir

func WithWorkDir(dir string) ExecutorOption

WithWorkDir sets the working directory

func WithoutInheritedEnv added in v0.46.5

func WithoutInheritedEnv(names ...string) ExecutorOption

WithoutInheritedEnv drops the named variables from the inherited process environment before WithEnv values are appended. It lets a caller guarantee, for example, that no host plugin cache or CLI argument override reaches an offline OpenTofu run.

type PlanChanges

type PlanChanges struct {
	Add     int
	Change  int
	Destroy int
}

PlanChanges represents changes detected by plan

func ParsePlanOutput

func ParsePlanOutput(output string) *PlanChanges

ParsePlanOutput parses plan output to extract changes

type Result

type Result struct {
	Success  bool
	ExitCode int
	Stdout   string
	Stderr   string
	Duration time.Duration
}

Result represents the result of a tofu command

type TimeoutError

type TimeoutError struct {
	Command  string
	Duration time.Duration
}

TimeoutError represents a command timeout

func (*TimeoutError) Error

func (e *TimeoutError) Error() string

Jump to

Keyboard shortcuts

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