Scaffold Type

This document describes the design of the scaffold resource type for rendering template directories into target directories.

Overview

The scaffold resource renders files from a source template directory to a target directory:

  • Rendering: Process templates using Go text/template or Jet engine
  • Synchronization: Detect changed, stable, and purgeable files
  • Cleanup: Optionally remove files in the target not present in the source

Templates have access to facts and data from Hiera, enabling dynamic configuration generation from directory structures.

Provider Interface

Scaffold providers must implement the ScaffoldProvider interface:

type ScaffoldProvider interface {
    model.Provider

    Remove(ctx context.Context, prop *model.ScaffoldResourceProperties, state *model.ScaffoldState) error
    Scaffold(ctx context.Context, env *templates.Env, prop *model.ScaffoldResourceProperties, noop bool) (*model.ScaffoldState, error)
    Status(ctx context.Context, env *templates.Env, prop *model.ScaffoldResourceProperties) (*model.ScaffoldState, error)
}

Method Responsibilities

MethodResponsibility
StatusRender in noop mode to determine current state of managed files
ScaffoldRender templates to target directory (or noop to preview changes)
RemoveDelete managed files (changed and stable) and clean up directories

Status Response

The Status method returns a ScaffoldState containing:

type ScaffoldState struct {
    CommonResourceState
    Metadata *ScaffoldMetadata
}

type ScaffoldMetadata struct {
    Name         string                 // Target directory
    Provider     string                 // Provider name (e.g., "choria")
    TargetExists bool                   // Whether target directory exists
    Changed      []string               // Files created or modified
    Purged       []string               // Files removed (not in source)
    Stable       []string               // Files unchanged
    Engine       ScaffoldResourceEngine // Template engine used
}

The Ensure field in CommonResourceState is set to:

  • present if the target directory exists
  • absent if the target directory does not exist

Available Providers

ProviderEngine SupportDocumentation
choriaGo, JetChoria

Ensure States

ValueDescription
presentTarget directory must exist with rendered template files
absentManaged files must be removed from the target

Template Engines

Two template engines are supported:

EngineLibraryDefault DelimitersDescription
goGo text/template{{ / }}Standard Go templates
jetJet templating[[ / ]]Jet template language

The engine defaults to jet if not specified. Delimiters can be customized via left_delimiter and right_delimiter properties.

Properties

PropertyTypeRequiredDescription
sourcestringYesSource template directory path or URL
enginestringNoTemplate engine: go or jet (default: jet)
skip_emptyboolNoSkip empty files in rendered output
left_delimiterstringNoCustom left template delimiter
right_delimiterstringNoCustom right template delimiter
purgeboolNoRemove files in target not present in source
post[]map[string]stringNoPost-processing: glob pattern to command mapping
# Render configuration templates using Jet engine
- scaffold:
    - /etc/app:
        ensure: present
        source: templates/app
        engine: jet
        purge: true

# Render with Go templates and custom delimiters
- scaffold:
    - /etc/myservice:
        ensure: present
        source: templates/myservice
        engine: go
        left_delimiter: "<<"
        right_delimiter: ">>"

# With post-processing commands
- scaffold:
    - /opt/app:
        ensure: present
        source: templates/app
        post:
          - "*.go": "go fmt {}"

Apply Logic

┌─────────────────────────────────────────┐
│ Get current state via Status()          │
└─────────────────┬───────────────────────┘
                  │
                  ▼
┌─────────────────────────────────────────┐
│ Is current state desired state?         │
└─────────────────┬───────────────────────┘
              Yes │         No
                  ▼         │
          ┌───────────┐     │
          │ No change │     │
          └───────────┘     │
                            ▼
              ┌─────────────────────────┐
              │ What is desired ensure? │
              └─────────────┬───────────┘
                            │
            ┌───────────────┴───────────────┐
            │ absent                        │ present
            ▼                               ▼
      ┌───────────┐                   ┌───────────┐
      │ Noop?     │                   │ Noop?     │
      └─────┬─────┘                   └─────┬─────┘
        Yes │     No                    Yes │     No
            ▼     │                         ▼     │
    ┌────────────┐│                 ┌────────────┐│
    │ Set noop   ││                 │ Set noop   ││
    │ message    ││                 │ message    ││
    └────────────┘│                 └────────────┘│
                  ▼                               ▼
        ┌───────────────┐             ┌─────────────────────┐
        │ Remove all    │             │ Scaffold            │
        │ managed files │             │ (render templates)  │
        │ and empty dirs│             │                     │
        └───────────────┘             └─────────────────────┘

Idempotency

The scaffold resource determines idempotency by rendering templates in noop mode and comparing results against the target directory.

State Checks

  1. Ensure absent: Target must not exist, or no managed files remain on disk (Changed and Stable lists empty). Purged files (files not belonging to the scaffold) do not affect this check.
  2. Ensure present: The Changed list must be empty, and the Purged list must be empty when purge is enabled (all files are stable). When purge is disabled, purged files do not affect stability.

Decision Table

For ensure: absent, purged files never affect stability since they don’t belong to the scaffold. For ensure: present, purged files only affect stability when purge is enabled.

When ensure: absent, the Status method filters Changed and Stable lists to only include files that actually exist on disk, so the state reflects reality after removal rather than what the scaffold would create.

DesiredTarget ExistsChanged FilesPurged FilesPurge EnabledStable?
absentNoN/AN/AN/AYes
absentYesNoneAnyN/AYes (no managed files on disk)
absentYesSomeAnyN/ANo (managed files remain)
presentYesNoneNoneAnyYes
presentYesNoneSomeNoYes (purged files ignored)
presentYesNoneSomeYesNo (purge needed)
presentYesSomeAnyAnyNo (render needed)
presentNoN/AN/AAnyNo (target missing)

Source Resolution

The source property is resolved relative to the manager’s working directory when it is a relative path:

parsed, _ := url.Parse(properties.Source)
if parsed == nil || parsed.Scheme == "" {
    if !filepath.IsAbs(properties.Source) {
        t.prop.Source = filepath.Join(mgr.WorkingDirectory(), properties.Source)
    }
}

This allows manifests bundled with template directories to use relative paths. URL sources (with a scheme) are passed through unchanged.

Path Validation

Target paths (the resource name) must be:

  • Absolute (start with /)
  • Canonical (no . or .. components, filepath.Clean(path) == path)

Post-Processing

The post property defines commands to run on rendered files. Each entry is a map where the key is a glob pattern matched against the file’s basename and the value is a command to execute. Use {} as a placeholder for the file’s full path; if omitted, the path is appended as the last argument.

post:
  - "*.go": "go fmt {}"
  - "*.sh": "chmod +x {}"

Post-processing runs immediately after each file is rendered. Validation ensures neither keys nor values are empty.

Noop Mode

In noop mode, the scaffold type queries the current state via Status() and reports what would change without modifying the filesystem. Neither Scaffold() nor Remove() are called.

For ensure: present, the affected count is the number of changed files plus purged files (when purge is enabled). For ensure: absent, the affected count is the number of changed and stable files plus purged files (when purge is enabled).

DesiredAffected CountMessage
presentChanged + Purged (if purge enabled)Would have changed N scaffold files
absentChanged + Stable + Purged (if purge enabled)Would have removed N scaffold files

Changed is set to true only when the affected count is greater than zero. When the resource is already in the desired state, Changed is false and NoopMessage is empty.

Desired State Validation

After applying changes (in non-noop mode), the type verifies the scaffold reached the desired state by checking the changed and purged file lists. If validation fails, ErrDesiredStateFailed is returned.

Subsections of Scaffold Type

Choria Provider

This document describes the implementation details of the Choria scaffold provider for rendering template directories using the choria-io/scaffold library.

Provider Selection

The Choria provider is the default and only scaffold provider. It is always available and returns priority 1 for all scaffold resources.

Operations

Scaffold (Render Templates)

Process:

  1. Check if target directory exists
  2. Configure scaffold with source, target, engine, delimiters, post-processing, and skip_empty settings
  3. Create scaffold instance using the appropriate engine (scaffold.New() for Go, scaffold.NewJet() for Jet)
  4. Call Render() (real mode) or RenderNoop() (noop mode)
  5. Categorize results into changed, stable, and purged file lists

Scaffold Configuration:

Config FieldSource PropertyDescription
TargetDirectoryNameTarget directory for rendered files
SourceDirectorySourceSource template directory
MergeTargetDirectory(always true)Merge into existing target directory
PostPostPost-processing commands
SkipEmptySkipEmptySkip empty rendered files
CustomLeftDelimiterLeftDelimiterCustom template left delimiter
CustomRightDelimiterRightDelimiterCustom template right delimiter

Engine Selection:

EngineConstructorDefault Delimiters
goscaffold.New(){{ / }}
jetscaffold.NewJet()[[ / ]]

Result Categorization:

Scaffold ActionMetadata ListDescription
FileActionEqualStableFile content unchanged
FileActionAddChangedNew file created
FileActionUpdateChangedExisting file modified
FileActionRemovePurgedFile removed from target

File paths in the metadata lists are absolute paths, constructed by joining the target directory with the relative path from the scaffold result.

Purge Behavior:

When purge is enabled and a file has FileActionRemove, the provider deletes the file from disk during Scaffold(). In noop mode, the removal is logged but not performed. When purge is disabled, purged files are only tracked in metadata and not removed.

Status

Process:

  1. Perform a dry-run render (noop mode) to determine what the scaffold would do
  2. When ensure is absent, filter Changed and Stable lists to only include files that actually exist on disk

The noop render reports what would happen if the scaffold were applied. For ensure: present, this is the desired output — it shows what needs to change. For ensure: absent, the raw render output is misleading after removal (it would show files to be added), so the lists are filtered to reflect what managed files actually remain on disk.

State Detection:

Target DirectoryEnsure ValueMetadata
ExistspresentChanged, stable, and purged file lists from render
ExistsabsentChanged and stable filtered to files on disk, purged from render
Does not existAnyEmpty metadata, TargetExists: false

Remove

Process:

  1. Collect managed files from the state’s Changed and Stable lists (purged files are not removed as they don’t belong to the scaffold)
  2. Remove each file individually
  3. Track parent directories of removed files
  4. Iteratively remove empty directories deepest-first
  5. Stop when no more empty directories can be removed
  6. Best-effort removal of the target directory (only succeeds if empty)

File Removal Order:

Files are collected from two metadata lists:

  1. Changed - Files that were added or modified
  2. Stable - Files that were unchanged

Purged files are not removed because they are unrelated to the scaffold and may belong to other processes.

Directory Cleanup:

For each removed file:
    Track its parent directory

Repeat:
    For each tracked directory:
        Skip if it is the target directory itself
        Skip if not empty
        Remove the directory
        Track its parent directory
Until no more directories removed

Best-effort: remove the target directory (fails silently if not empty)

The target directory is removed if empty after all managed files and subdirectories are cleaned up. If unrelated files remain (purged files), the directory is preserved.

Error Handling:

ConditionBehavior
Non-absolute file pathReturn error immediately
File removal failsLog error, continue with remaining files
Directory removal failsLog error, continue with remaining directories
File does not existSilently skip (os.IsNotExist check)
Target directory removal failsLog at debug level, no error returned

Template Environment

Templates receive the full templates.Env environment, which provides access to:

  • facts - System facts for the managed node
  • data - Hiera-resolved configuration data
  • Template helper functions

This allows templates to generate host-specific configurations based on facts and hierarchical data.

Logging

The provider wraps the CCM logger in a scaffold-compatible interface:

type logger struct {
    log model.Logger
}

func (l *logger) Debugf(format string, v ...any)
func (l *logger) Infof(format string, v ...any)

This adapter translates the scaffold library’s Debugf/Infof calls to CCM’s structured logging.

Platform Support

The Choria provider is platform-independent. It uses the choria-io/scaffold library for template rendering, which operates on standard filesystem operations. No platform-specific system calls are used.