github.com/hashicorp/packer@v1.14.3/builder/null/builder.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package null 5 6 import ( 7 "context" 8 9 "github.com/hashicorp/hcl/v2/hcldec" 10 "github.com/hashicorp/packer-plugin-sdk/communicator" 11 "github.com/hashicorp/packer-plugin-sdk/multistep" 12 "github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps" 13 packersdk "github.com/hashicorp/packer-plugin-sdk/packer" 14 ) 15 16 const BuilderId = "fnoeding.null" 17 18 type Builder struct { 19 config Config 20 runner multistep.Runner 21 } 22 23 func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() } 24 25 func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { 26 warnings, errs := b.config.Prepare(raws...) 27 if errs != nil { 28 return nil, warnings, errs 29 } 30 31 return nil, warnings, nil 32 } 33 34 func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) { 35 steps := []multistep.Step{} 36 37 steps = append(steps, 38 &communicator.StepConnect{ 39 Config: &b.config.CommConfig, 40 Host: CommHost(b.config.CommConfig.Host()), 41 SSHConfig: b.config.CommConfig.SSHConfigFunc(), 42 }, 43 ) 44 45 steps = append(steps, 46 new(commonsteps.StepProvision), 47 ) 48 49 // Setup the state bag and initial state for the steps 50 state := new(multistep.BasicStateBag) 51 state.Put("hook", hook) 52 state.Put("ui", ui) 53 state.Put("instance_id", "Null") 54 55 // Run! 56 b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui) 57 b.runner.Run(ctx, state) 58 59 // If there was an error, return that 60 if rawErr, ok := state.GetOk("error"); ok { 61 return nil, rawErr.(error) 62 } 63 64 // No errors, must've worked 65 artifact := &NullArtifact{} 66 return artifact, nil 67 }