mirror of
https://github.com/hashicorp/packer.git
synced 2026-10-24 15:12:08 -04:00
backport of commit 1f4f2aebca
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package dynamic
|
||||
|
||||
// packersdk.Artifact implementation
|
||||
type Artifact struct {
|
||||
// StateData should store data such as GeneratedData
|
||||
// to be shared with post-processors
|
||||
StateData map[string]interface{}
|
||||
}
|
||||
|
||||
func (*Artifact) BuilderId() string {
|
||||
return BuilderId
|
||||
}
|
||||
|
||||
func (a *Artifact) Files() []string {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
func (*Artifact) Id() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (a *Artifact) String() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (a *Artifact) State(name string) interface{} {
|
||||
return a.StateData[name]
|
||||
}
|
||||
|
||||
func (a *Artifact) Destroy() error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,NestedFirst,NestedSecond
|
||||
|
||||
package dynamic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/hcl/v2/hcldec"
|
||||
"github.com/hashicorp/packer-plugin-sdk/common"
|
||||
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
||||
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
|
||||
"github.com/hashicorp/packer-plugin-sdk/packer"
|
||||
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
||||
)
|
||||
|
||||
const BuilderId = "dynamic.builder"
|
||||
|
||||
type NestedSecond struct {
|
||||
Name string `mapstructure:"name" required:"true"`
|
||||
}
|
||||
|
||||
type NestedFirst struct {
|
||||
Name string `mapstructure:"name" required:"true"`
|
||||
Nesteds []NestedSecond `mapstructure:"extra" required:"false"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
common.PackerConfig `mapstructure:",squash"`
|
||||
Nesteds []NestedFirst `mapstructure:"extra" required:"false"`
|
||||
}
|
||||
|
||||
type Builder struct {
|
||||
config Config
|
||||
runner multistep.Runner
|
||||
}
|
||||
|
||||
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
||||
|
||||
func (b *Builder) Prepare(raws ...interface{}) (generatedVars []string, warnings []string, err error) {
|
||||
err = config.Decode(&b.config, &config.DecodeOpts{
|
||||
PluginType: "packer.builder.dynamic",
|
||||
Interpolate: true,
|
||||
}, raws...)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
// Return the placeholder for the generated data that will become available to provisioners and post-processors.
|
||||
// If the builder doesn't generate any data, just return an empty slice of string: []string{}
|
||||
buildGeneratedData := []string{"GeneratedMockData"}
|
||||
return buildGeneratedData, nil, nil
|
||||
}
|
||||
|
||||
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
||||
steps := []multistep.Step{}
|
||||
|
||||
steps = append(steps,
|
||||
&StepSayConfig{
|
||||
b.config,
|
||||
},
|
||||
new(commonsteps.StepProvision),
|
||||
)
|
||||
|
||||
// Setup the state bag and initial state for the steps
|
||||
state := new(multistep.BasicStateBag)
|
||||
state.Put("hook", hook)
|
||||
state.Put("ui", ui)
|
||||
|
||||
// Set the value of the generated data that will become available to provisioners.
|
||||
// To share the data with post-processors, use the StateData in the artifact.
|
||||
state.Put("generated_data", map[string]interface{}{
|
||||
"GeneratedMockData": "mock-build-data",
|
||||
})
|
||||
|
||||
// Run!
|
||||
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
|
||||
b.runner.Run(ctx, state)
|
||||
|
||||
// If there was an error, return that
|
||||
if err, ok := state.GetOk("error"); ok {
|
||||
return nil, err.(error)
|
||||
}
|
||||
|
||||
artifact := &Artifact{
|
||||
// Add the builder generated data to the artifact StateData so that post-processors
|
||||
// can access them.
|
||||
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
||||
}
|
||||
return artifact, nil
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// Code generated by "packer-sdc mapstructure-to-hcl2"; DO NOT EDIT.
|
||||
|
||||
package dynamic
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/hcl/v2/hcldec"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
// FlatConfig is an auto-generated flat version of Config.
|
||||
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
|
||||
type FlatConfig struct {
|
||||
PackerBuildName *string `mapstructure:"packer_build_name" cty:"packer_build_name" hcl:"packer_build_name"`
|
||||
PackerBuilderType *string `mapstructure:"packer_builder_type" cty:"packer_builder_type" hcl:"packer_builder_type"`
|
||||
PackerCoreVersion *string `mapstructure:"packer_core_version" cty:"packer_core_version" hcl:"packer_core_version"`
|
||||
PackerDebug *bool `mapstructure:"packer_debug" cty:"packer_debug" hcl:"packer_debug"`
|
||||
PackerForce *bool `mapstructure:"packer_force" cty:"packer_force" hcl:"packer_force"`
|
||||
PackerOnError *string `mapstructure:"packer_on_error" cty:"packer_on_error" hcl:"packer_on_error"`
|
||||
PackerUserVars map[string]string `mapstructure:"packer_user_variables" cty:"packer_user_variables" hcl:"packer_user_variables"`
|
||||
PackerSensitiveVars []string `mapstructure:"packer_sensitive_variables" cty:"packer_sensitive_variables" hcl:"packer_sensitive_variables"`
|
||||
Nesteds []FlatNestedFirst `mapstructure:"extra" required:"false" cty:"extra" hcl:"extra"`
|
||||
}
|
||||
|
||||
// FlatMapstructure returns a new FlatConfig.
|
||||
// FlatConfig is an auto-generated flat version of Config.
|
||||
// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up.
|
||||
func (*Config) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } {
|
||||
return new(FlatConfig)
|
||||
}
|
||||
|
||||
// HCL2Spec returns the hcl spec of a Config.
|
||||
// This spec is used by HCL to read the fields of Config.
|
||||
// The decoded values from this spec will then be applied to a FlatConfig.
|
||||
func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
|
||||
s := map[string]hcldec.Spec{
|
||||
"packer_build_name": &hcldec.AttrSpec{Name: "packer_build_name", Type: cty.String, Required: false},
|
||||
"packer_builder_type": &hcldec.AttrSpec{Name: "packer_builder_type", Type: cty.String, Required: false},
|
||||
"packer_core_version": &hcldec.AttrSpec{Name: "packer_core_version", Type: cty.String, Required: false},
|
||||
"packer_debug": &hcldec.AttrSpec{Name: "packer_debug", Type: cty.Bool, Required: false},
|
||||
"packer_force": &hcldec.AttrSpec{Name: "packer_force", Type: cty.Bool, Required: false},
|
||||
"packer_on_error": &hcldec.AttrSpec{Name: "packer_on_error", Type: cty.String, Required: false},
|
||||
"packer_user_variables": &hcldec.AttrSpec{Name: "packer_user_variables", Type: cty.Map(cty.String), Required: false},
|
||||
"packer_sensitive_variables": &hcldec.AttrSpec{Name: "packer_sensitive_variables", Type: cty.List(cty.String), Required: false},
|
||||
"extra": &hcldec.BlockListSpec{TypeName: "extra", Nested: hcldec.ObjectSpec((*FlatNestedFirst)(nil).HCL2Spec())},
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// FlatNestedFirst is an auto-generated flat version of NestedFirst.
|
||||
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
|
||||
type FlatNestedFirst struct {
|
||||
Name *string `mapstructure:"name" required:"true" cty:"name" hcl:"name"`
|
||||
Nesteds []FlatNestedSecond `mapstructure:"extra" required:"false" cty:"extra" hcl:"extra"`
|
||||
}
|
||||
|
||||
// FlatMapstructure returns a new FlatNestedFirst.
|
||||
// FlatNestedFirst is an auto-generated flat version of NestedFirst.
|
||||
// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up.
|
||||
func (*NestedFirst) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } {
|
||||
return new(FlatNestedFirst)
|
||||
}
|
||||
|
||||
// HCL2Spec returns the hcl spec of a NestedFirst.
|
||||
// This spec is used by HCL to read the fields of NestedFirst.
|
||||
// The decoded values from this spec will then be applied to a FlatNestedFirst.
|
||||
func (*FlatNestedFirst) HCL2Spec() map[string]hcldec.Spec {
|
||||
s := map[string]hcldec.Spec{
|
||||
"name": &hcldec.AttrSpec{Name: "name", Type: cty.String, Required: false},
|
||||
"extra": &hcldec.BlockListSpec{TypeName: "extra", Nested: hcldec.ObjectSpec((*FlatNestedSecond)(nil).HCL2Spec())},
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// FlatNestedSecond is an auto-generated flat version of NestedSecond.
|
||||
// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up.
|
||||
type FlatNestedSecond struct {
|
||||
Name *string `mapstructure:"name" required:"true" cty:"name" hcl:"name"`
|
||||
}
|
||||
|
||||
// FlatMapstructure returns a new FlatNestedSecond.
|
||||
// FlatNestedSecond is an auto-generated flat version of NestedSecond.
|
||||
// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up.
|
||||
func (*NestedSecond) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } {
|
||||
return new(FlatNestedSecond)
|
||||
}
|
||||
|
||||
// HCL2Spec returns the hcl spec of a NestedSecond.
|
||||
// This spec is used by HCL to read the fields of NestedSecond.
|
||||
// The decoded values from this spec will then be applied to a FlatNestedSecond.
|
||||
func (*FlatNestedSecond) HCL2Spec() map[string]hcldec.Spec {
|
||||
s := map[string]hcldec.Spec{
|
||||
"name": &hcldec.AttrSpec{Name: "name", Type: cty.String, Required: false},
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package dynamic
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/packer-plugin-sdk/acctest"
|
||||
)
|
||||
|
||||
//go:embed test-fixtures/template.pkr.hcl
|
||||
var testBuilderHCL2Basic string
|
||||
|
||||
// Run with: PACKER_ACC=1 go test -count 1 -v ./builder/scaffolding/builder_acc_test.go -timeout=120m
|
||||
func TestAccScaffoldingBuilder(t *testing.T) {
|
||||
testCase := &acctest.PluginTestCase{
|
||||
Name: "scaffolding_builder_basic_test",
|
||||
Setup: func() error {
|
||||
return nil
|
||||
},
|
||||
Teardown: func() error {
|
||||
return nil
|
||||
},
|
||||
Template: testBuilderHCL2Basic,
|
||||
Type: "scaffolding-my-builder",
|
||||
Check: func(buildCommand *exec.Cmd, logfile string) error {
|
||||
if buildCommand.ProcessState != nil {
|
||||
if buildCommand.ProcessState.ExitCode() != 0 {
|
||||
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
|
||||
}
|
||||
}
|
||||
|
||||
logs, err := os.Open(logfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable find %s", logfile)
|
||||
}
|
||||
defer logs.Close()
|
||||
|
||||
logsBytes, err := ioutil.ReadAll(logs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to read %s", logfile)
|
||||
}
|
||||
logsString := string(logsBytes)
|
||||
|
||||
buildGeneratedDataLog := "scaffolding-my-builder.basic-example: build generated data: mock-build-data"
|
||||
if matched, _ := regexp.MatchString(buildGeneratedDataLog+".*", logsString); !matched {
|
||||
t.Fatalf("logs doesn't contain expected foo value %q", logsString)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
acctest.TestPlugin(t, testCase)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package dynamic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
||||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
||||
)
|
||||
|
||||
// This is a definition of a builder step and should implement multistep.Step
|
||||
type StepSayConfig struct {
|
||||
cfg Config
|
||||
}
|
||||
|
||||
// Run should execute the purpose of this step
|
||||
func (s *StepSayConfig) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
ui := state.Get("ui").(packersdk.Ui)
|
||||
|
||||
ui.Say("Dynamic builder invoked!")
|
||||
for _, nf := range s.cfg.Nesteds {
|
||||
ui.Say(fmt.Sprintf("Nested first: %s", nf.Name))
|
||||
for _, ns := range nf.Nesteds {
|
||||
ui.Say(fmt.Sprintf("Nested second: %s.%s", nf.Name, ns.Name))
|
||||
}
|
||||
}
|
||||
|
||||
// Determines that should continue to the next step
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
// Cleanup can be used to clean up any artifact created by the step.
|
||||
// A step's clean up always run at the end of a build, regardless of whether provisioning succeeds or fails.
|
||||
func (s *StepSayConfig) Cleanup(_ multistep.StateBag) {
|
||||
// Nothing to clean
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
# Copyright (c) HashiCorp, Inc.
|
||||
# SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
source "scaffolding-my-builder" "basic-example" {
|
||||
mock = "mock-config"
|
||||
}
|
||||
|
||||
build {
|
||||
sources = [
|
||||
"source.scaffolding-my-builder.basic-example"
|
||||
]
|
||||
|
||||
provisioner "shell-local" {
|
||||
inline = [
|
||||
"echo build generated data: ${build.GeneratedMockData}",
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user