mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(coderd/templatebuilder): validation extraction, static file bundling, Windows OS (#26633)
Extract validation helpers to a dedicated file, add static file bundling for base templates, and add Windows OS support. **Commit 1: Extract validation to `validate.go`** Move `validateVariableValue`, `validateStringValue`, `validateNumberValue`, `validateBoolValue`, `toHCLLiteral`, `hclQuote`, and `isSimpleJSONValue` from `compose.go` into `validate.go`. Corresponding tests move to `validate_internal_test.go`. This keeps `compose.go` focused on the compose pipeline. **Commit 2: Static file bundling** Add `StaticFiles` field to `ComposeResult` and a `collectStaticFiles` helper that walks the base template FS to collect non-template files (e.g. cloud-init `.tftpl` inputs). `BundleTar` now writes these files into the output archive in sorted order for deterministic output. This fixes `aws-linux`, whose cloud-init files were embedded but never included in the tar. **Commit 3: Windows OS support** Add `BaseOSWindows` constant and register `"windows"` in `validBaseOS` so that base templates with `os="windows"` can be loaded and used for module compatibility filtering. > [!NOTE] > This PR was authored by Coder Agents on behalf of @jeremyruppel.
This commit is contained in:
+118
-47
@@ -17,12 +17,14 @@ import (
|
||||
type BaseOS string
|
||||
|
||||
const (
|
||||
BaseOSLinux BaseOS = "linux"
|
||||
BaseOSLinux BaseOS = "linux"
|
||||
BaseOSWindows BaseOS = "windows"
|
||||
)
|
||||
|
||||
// validBaseOS maps base.json os strings to their typed equivalents.
|
||||
var validBaseOS = map[string]BaseOS{
|
||||
"linux": BaseOSLinux,
|
||||
"linux": BaseOSLinux,
|
||||
"windows": BaseOSWindows,
|
||||
}
|
||||
|
||||
//go:embed bases
|
||||
@@ -55,8 +57,9 @@ type parsedBase struct {
|
||||
Manifest BaseManifest
|
||||
Templates map[string]*template.Template
|
||||
FS fs.FS
|
||||
Readme string // full README.md content (including frontmatter)
|
||||
Prerequisites string // content between prerequisite comment markers
|
||||
Readme string // full README.md content (including frontmatter)
|
||||
Prerequisites string // content between prerequisite comment markers
|
||||
ExtraFiles map[string][]byte // non-template, non-manifest files (e.g. .tftpl)
|
||||
}
|
||||
|
||||
var loadBases = sync.OnceValues(func() (map[string]*parsedBase, error) {
|
||||
@@ -83,57 +86,85 @@ func parseBasesFromFS(fsys fs.FS) (map[string]*parsedBase, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
manifestPath := path.Join(dir.Name(), "base.json")
|
||||
data, err := fs.ReadFile(sub, manifestPath)
|
||||
base, err := parseBaseDir(sub, dir.Name())
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("read %s: %w", manifestPath, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var manifest BaseManifest
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&manifest); err != nil {
|
||||
return nil, xerrors.Errorf("decode %s: %w", manifestPath, err)
|
||||
}
|
||||
|
||||
if manifest.ID == "" {
|
||||
return nil, xerrors.Errorf("base in %s has empty id", dir.Name())
|
||||
}
|
||||
if _, ok := validBaseOS[manifest.OS]; !ok && manifest.OS != "" {
|
||||
return nil, xerrors.Errorf("base %q has unknown os %q", manifest.ID, manifest.OS)
|
||||
}
|
||||
if bases[manifest.ID] != nil {
|
||||
return nil, xerrors.Errorf("duplicate base id %q", manifest.ID)
|
||||
}
|
||||
|
||||
baseFS, err := fs.Sub(sub, dir.Name())
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("sub fs for %s: %w", dir.Name(), err)
|
||||
}
|
||||
|
||||
templates, err := parseTemplatesFromFS(baseFS)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("parse templates for base %q: %w", manifest.ID, err)
|
||||
}
|
||||
|
||||
readmeData, err := fs.ReadFile(baseFS, "README.md")
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("read README.md for base %q: %w", manifest.ID, err)
|
||||
}
|
||||
readme := string(readmeData)
|
||||
|
||||
bases[manifest.ID] = &parsedBase{
|
||||
Manifest: manifest,
|
||||
Templates: templates,
|
||||
FS: baseFS,
|
||||
Readme: readme,
|
||||
Prerequisites: ExtractPrerequisites(readme),
|
||||
if bases[base.Manifest.ID] != nil {
|
||||
return nil, xerrors.Errorf("duplicate base id %q", base.Manifest.ID)
|
||||
}
|
||||
bases[base.Manifest.ID] = base
|
||||
}
|
||||
|
||||
return bases, nil
|
||||
}
|
||||
|
||||
// parseBaseDir loads a single base template directory: reads the
|
||||
// manifest, pre-parses Go templates, reads the README, and collects
|
||||
// extra files.
|
||||
func parseBaseDir(parent fs.FS, dirName string) (*parsedBase, error) {
|
||||
manifest, err := parseManifest(parent, dirName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
baseFS, err := fs.Sub(parent, dirName)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("sub fs for %s: %w", dirName, err)
|
||||
}
|
||||
|
||||
templates, err := parseTemplatesFromFS(baseFS)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("parse templates for base %q: %w", manifest.ID, err)
|
||||
}
|
||||
|
||||
readmeData, err := fs.ReadFile(baseFS, "README.md")
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("read README.md for base %q: %w", manifest.ID, err)
|
||||
}
|
||||
readme := string(readmeData)
|
||||
|
||||
extraFiles, err := collectExtraFilesFromFS(baseFS)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("collect extra files for base %q: %w", manifest.ID, err)
|
||||
}
|
||||
|
||||
return &parsedBase{
|
||||
Manifest: manifest,
|
||||
Templates: templates,
|
||||
FS: baseFS,
|
||||
Readme: readme,
|
||||
Prerequisites: ExtractPrerequisites(readme),
|
||||
ExtraFiles: extraFiles,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseManifest reads and validates a base.json file from the given
|
||||
// directory within parent.
|
||||
func parseManifest(parent fs.FS, dirName string) (BaseManifest, error) {
|
||||
manifestPath := path.Join(dirName, "base.json")
|
||||
data, err := fs.ReadFile(parent, manifestPath)
|
||||
if err != nil {
|
||||
return BaseManifest{}, xerrors.Errorf("read %s: %w", manifestPath, err)
|
||||
}
|
||||
|
||||
var manifest BaseManifest
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&manifest); err != nil {
|
||||
return BaseManifest{}, xerrors.Errorf("decode %s: %w", manifestPath, err)
|
||||
}
|
||||
|
||||
if manifest.ID == "" {
|
||||
return BaseManifest{}, xerrors.Errorf("base in %s has empty id", dirName)
|
||||
}
|
||||
if _, ok := validBaseOS[manifest.OS]; !ok && manifest.OS != "" {
|
||||
return BaseManifest{}, xerrors.Errorf("base %q has unknown os %q", manifest.ID, manifest.OS)
|
||||
}
|
||||
|
||||
return manifest, nil
|
||||
}
|
||||
|
||||
// parseTemplatesFromFS walks the filesystem and pre-parses all .tf.tmpl files
|
||||
// into Go templates. Returned keys are paths relative to the FS root.
|
||||
func parseTemplatesFromFS(fsys fs.FS) (map[string]*template.Template, error) {
|
||||
@@ -264,3 +295,43 @@ func BasePrerequisites(exampleID string) string {
|
||||
}
|
||||
return bases[exampleID].Prerequisites
|
||||
}
|
||||
|
||||
// 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 BaseExtraFiles(exampleID string) map[string][]byte {
|
||||
bases, err := loadBases()
|
||||
if err != nil || bases[exampleID] == nil {
|
||||
return nil
|
||||
}
|
||||
return bases[exampleID].ExtraFiles
|
||||
}
|
||||
|
||||
// collectExtraFilesFromFS walks a base template filesystem and returns
|
||||
// all files that are not Go templates (.tf.tmpl), the manifest
|
||||
// (base.json), or the README. These are raw files that must be included
|
||||
// in the output archive (e.g. Terraform templatefile() inputs).
|
||||
func collectExtraFilesFromFS(baseFS fs.FS) (map[string][]byte, error) {
|
||||
files := make(map[string][]byte)
|
||||
err := fs.WalkDir(baseFS, ".", func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if p == "base.json" || p == "README.md" || strings.HasSuffix(p, ".tf.tmpl") {
|
||||
return nil
|
||||
}
|
||||
data, err := fs.ReadFile(baseFS, p)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("read %s: %w", p, err)
|
||||
}
|
||||
files[p] = data
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user