templatebuilder

package
v2.38.7 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: AGPL-3.0 Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultRegistryBase = "registry.coder.com"

DefaultRegistryBase is the module registry host used in rendered module source paths when a ComposeRequest does not carry the deployment's configured registry (CODER_TEMPLATE_BUILDER_REGISTRY_URL). It mirrors the default of the codersdk template-builder registry option.

Variables

This section is empty.

Functions

func BaseDefaultAgentName

func BaseDefaultAgentName(exampleID string) string

BaseDefaultAgentName returns the default agent's name: the one marked default, else the first declared. Empty if the base declares none.

func BaseExtraFiles

func BaseExtraFiles(exampleID string) map[string][]byte

BaseExtraFiles returns the non-template, non-manifest files embedded in the base template directory (e.g. cloud-init .tftpl files). Returns nil if the base is unknown or has no extra files.

func BaseIncludedModules

func BaseIncludedModules(exampleID string) []string

BaseIncludedModules returns the catalog module IDs the given base declares in its own rendered Terraform. Returns nil if the base is unknown or declares none.

func BasePrerequisites

func BasePrerequisites(exampleID string) string

BasePrerequisites returns the prerequisites section extracted from the base template README. Returns an empty string if the base is unknown or has no prerequisites markers.

func BaseReadme

func BaseReadme(exampleID string) string

BaseReadme returns the full README.md content for a base template. Returns an empty string if the base is unknown or has no README.

func BaseTemplateFS

func BaseTemplateFS(exampleID string) (fs.FS, error)

BaseTemplateFS returns a filesystem rooted at the given base template directory within the embedded bases catalog. Returns an error if exampleID is not a known base template.

func BaseTemplateIDs

func BaseTemplateIDs() []string

BaseTemplateIDs returns the set of known base template example IDs.

func BundleTar

func BundleTar(result *ComposeResult) ([]byte, error)

BundleTar packages the compose result into a tar archive suitable for the Coder file store.

func ClassifyProvisionerError

func ClassifyProvisionerError(jobError string, logs []string) string

ClassifyProvisionerError inspects a provisioner job error and its log lines, returning a user-friendly message for known failure modes. For unrecognized errors, the raw jobError is returned with relevant log context appended so the user can diagnose the failure. The message is derived from ClassifyProvisionerErrorCategory so that what a user reads and what telemetry records cannot disagree.

func ExtractAgentResourceName

func ExtractAgentResourceName(rendered []byte) (string, error)

ExtractAgentResourceName finds the coder_agent resource declaration in rendered HCL and returns the reference form to use in module templates. When the agent uses count or for_each, the returned name includes an index suffix (e.g. "dev[0]") so that module templates can reference it as coder_agent.<name>.id. Returns an error unless exactly one coder_agent resource is found; callers that support multiple agents should use ExtractAgentResourceNames instead. The input is expected to be rendered output from our own curated base templates, not arbitrary user HCL.

func ExtractModuleNames

func ExtractModuleNames(rendered []byte) []string

ExtractModuleNames returns the labels of every module block declared in rendered HCL, in declaration order. Because it walks parsed blocks, commented-out references and module strings inside heredocs are naturally ignored. The input is expected to be rendered output from our own curated base templates, not arbitrary user HCL; unparsable input yields no names.

func ExtractPrerequisites

func ExtractPrerequisites(readme string) string

ExtractPrerequisites returns the content between the prerequisites comment markers in a README body. Returns an empty string when either marker is absent.

func ModuleTemplateFS

func ModuleTemplateFS(moduleID string) (fs.FS, error)

ModuleTemplateFS returns an fs.FS rooted at the embedded directory for the given module ID, providing access to its .tf.tmpl file.

func RenderBaseTemplate

func RenderBaseTemplate(exampleID, templatePath string, renderCtx BaseRenderContext) ([]byte, error)

RenderBaseTemplate executes a pre-parsed .tf.tmpl template for the given base, applying the provided render context. Templates are parsed once at first access via sync.OnceValues, so parse errors surface early instead of at render time.

func RenderModuleTemplate

func RenderModuleTemplate(fsys fs.FS, templatePath string, renderCtx ModuleRenderContext) ([]byte, error)

RenderModuleTemplate parses and executes a module .tf.tmpl file from the given filesystem, applying the provided render context.

func StripPrerequisitesMarkers

func StripPrerequisitesMarkers(readme string) string

Remove the prerequisites comment markers from a README body, preserving the content between the markers.

Types

type BaseAgent

type BaseAgent struct {
	Name        string `json:"name"`
	DisplayName string `json:"display_name,omitempty"`
	// Default is the agent modules attach to when they do not name one.
	Default bool `json:"default,omitempty"`
}

BaseAgent is a coder_agent declared in base.json. Name must match the resource label in the base's main.tf.tmpl.

func BaseAgents

func BaseAgents(exampleID string) []BaseAgent

BaseAgents returns the agents declared in a base's manifest, or nil.

type BaseDefaultContext

type BaseDefaultContext struct {
	ContainerImage string `json:"container_image,omitempty"`
}

BaseDefaultContext holds default render values stored in base.json.

type BaseManifest

type BaseManifest struct {
	ID             string             `json:"id"`
	DisplayName    string             `json:"display_name"`
	OS             string             `json:"os"`
	DefaultContext BaseDefaultContext `json:"default_context"`
	Variables      []ModuleVariable   `json:"variables"`
	Agents         []BaseAgent        `json:"agents,omitempty"`
}

BaseManifest is the on-disk schema for a base.json file.

type BaseOS

type BaseOS string

BaseOS enumerates operating systems for base template filtering.

const (
	BaseOSLinux   BaseOS = "linux"
	BaseOSWindows BaseOS = "windows"
)

func BaseTemplateOS

func BaseTemplateOS(exampleID string) BaseOS

BaseTemplateOS resolves the OS for a given example ID. Returns empty string if the example is not a known base template.

type BaseRenderContext

type BaseRenderContext struct {
	ContainerImage string
	ImageOptions   []ImageOption
	// RegistryBase is the module registry host used in rendered module source
	// paths, mirroring ModuleRenderContext.RegistryBase so a base-embedded
	// module honors CODER_TEMPLATE_BUILDER_REGISTRY_URL like a wizard module.
	RegistryBase string
	Variables    map[string]string
}

BaseRenderContext is the data passed to base template .tf.tmpl files.

func DefaultBaseRenderContext

func DefaultBaseRenderContext(exampleID string) BaseRenderContext

DefaultBaseRenderContext returns the render context that produces the canonical default output for a base template.

type ComposeModule

type ComposeModule struct {
	ID string
	// AgentName is the base coder_agent the module attaches to; empty uses the
	// base default. Must name an agent the base declares.
	AgentName string
	// Variables maps variable names to HCL literal values for
	// non-sensitive, non-computed variables.
	Variables map[string]string
}

ComposeModule identifies a module to include and the variable values to render into its module block.

type ComposeRequest

type ComposeRequest struct {
	BaseTemplateID string
	// BaseVariableValues maps base template variable names to their
	// user-supplied values.
	BaseVariableValues map[string]string
	// RegistryURL is the module registry base URL from the deployment
	// config (CODER_TEMPLATE_BUILDER_REGISTRY_URL).
	RegistryURL string
	Modules     []ComposeModule
}

ComposeRequest describes which base template and modules to render.

type ComposeResult

type ComposeResult struct {
	// MainTF is the rendered base template.
	MainTF []byte
	// ModulesTF is the concatenated rendered module blocks. Empty when
	// no modules are selected.
	ModulesTF []byte
	// Readme is the full README.md content from the base template.
	// Empty when the base has no README.
	Readme []byte
	// ExtraFiles holds non-template files from the base directory
	// (e.g. cloud-init .tftpl files). Keys are paths relative to the
	// base directory.
	ExtraFiles map[string][]byte
}

ComposeResult holds the rendered Terraform files ready for bundling.

func Compose

func Compose(req ComposeRequest) (*ComposeResult, error)

Compose renders a base template and the selected modules into Terraform source files.

type ExtractedAgent

type ExtractedAgent struct {
	// Name is the Terraform resource name (the second block label).
	Name string
	// Reference is the form to use in module templates: Name, suffixed with
	// [0] when the agent uses count or for_each and therefore requires an
	// index (e.g. coder_agent.dev[0].id).
	Reference string
}

ExtractedAgent describes a coder_agent resource found in rendered HCL.

func ExtractAgentResourceNames

func ExtractAgentResourceNames(rendered []byte) ([]ExtractedAgent, error)

ExtractAgentResourceNames returns every coder_agent resource declared in rendered HCL, in declaration order. Unlike the singular helper it does not error on multiple agents: enumerating them all lets a base declare more than one agent. Reading count/for_each from each block body is robust to nested blocks (env {}, metadata {}). The input is expected to be rendered output from our own curated base templates, not arbitrary user HCL.

type ImageOption

type ImageOption struct {
	Name  string
	Value string
}

ImageOption represents a container image choice for base template parameters.

type ModuleManifest

type ModuleManifest struct {
	ID            string           `json:"id"`
	DisplayName   string           `json:"display_name"`
	Description   string           `json:"description"`
	Icon          string           `json:"icon"`
	Category      string           `json:"category"`
	Tags          []string         `json:"tags"`
	CompatibleOS  []string         `json:"compatible_os"`
	ConflictsWith []string         `json:"conflicts_with"`
	Namespace     string           `json:"namespace"`
	PinnedVersion string           `json:"pinned_version"`
	Variables     []ModuleVariable `json:"variables"`
}

ModuleManifest represents a module.json file from the bundled catalog. This is the on-disk schema; codersdk.TemplateBuilderModule is the API type.

func LoadModules

func LoadModules() ([]ModuleManifest, error)

LoadModules returns all module manifests from the embedded catalog. Results are cached after the first call, including errors. Each call returns a fresh slice so callers can filter or sort without corrupting the cache.

func (ModuleManifest) CompatibleWithOS

func (m ModuleManifest) CompatibleWithOS(os string) bool

CompatibleWithOS reports whether the module is compatible with the given OS. Modules with an empty CompatibleOS list are compatible with all platforms.

func (ModuleManifest) ToSDK

ToSDK converts a ModuleManifest to the API response type. PinnedVersion is mapped to Version; tags are not part of the API surface. Computed variables are excluded from the output.

type ModuleRenderContext

type ModuleRenderContext struct {
	// RegistryBase is the module registry URL from the deployment config
	// (CODER_TEMPLATE_BUILDER_REGISTRY_URL).
	RegistryBase string
	// PinnedVersion is the module version from the catalog manifest.
	PinnedVersion string
	// AgentResourceName is the Terraform resource name of the coder_agent
	// declared in the base template (e.g. "main" or "dev").
	AgentResourceName string
	// Variables maps variable names to their HCL expressions.
	Variables map[string]string
}

ModuleRenderContext is the data passed to module .tf.tmpl files.

type ModuleVariable

type ModuleVariable struct {
	Name        string          `json:"name"`
	Type        string          `json:"type"`
	Description string          `json:"description"`
	Default     json.RawMessage `json:"default,omitempty"`
	Required    bool            `json:"required"`
	Sensitive   bool            `json:"sensitive"`
	Computed    bool            `json:"computed"`
}

ModuleVariable represents a variable declaration within a module manifest.

func BaseVariables

func BaseVariables(exampleID string) []ModuleVariable

BaseVariables returns the user-facing variables for a given base template ID. Computed variables are excluded. Returns nil if the base is unknown or has no variables.

type ProvisionerErrorCategory

type ProvisionerErrorCategory string

ProvisionerErrorCategory names the failure mode of a provisioner import job. It carries no error text, so it can be reported as telemetry to explain why a build failed without leaking deployment detail.

const (
	// ProvisionerErrorNetwork means the Terraform registry or a provider
	// endpoint was unreachable from the provisioner.
	ProvisionerErrorNetwork ProvisionerErrorCategory = "network"
	// ProvisionerErrorAuth means the provisioner's cloud credentials were
	// missing, expired, or insufficient.
	ProvisionerErrorAuth ProvisionerErrorCategory = "auth"
	// ProvisionerErrorUnknown means the failure matched no known pattern.
	ProvisionerErrorUnknown ProvisionerErrorCategory = "unknown"
)

func ClassifyProvisionerErrorCategory

func ClassifyProvisionerErrorCategory(jobError string, logs []string) ProvisionerErrorCategory

ClassifyProvisionerErrorCategory inspects a provisioner job error and its log lines and reports which known failure mode they match. Network patterns are checked first: a credential lookup that fails because the metadata endpoint is unreachable is a network problem.

Jump to

Keyboard shortcuts

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