generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
80 lines (65 loc) · 2.38 KB
/
builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//go:generate packer-sdc mapstructure-to-hcl2 -type Config
package scaffolding
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 = "scaffolding.builder"
type Config struct {
common.PackerConfig `mapstructure:",squash"`
MockOption string `mapstructure:"mock"`
}
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.scaffolding",
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{
MockConfig: b.config.MockOption,
},
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
}