2014-03-24 06:20:32 -04:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2015-06-13 17:42:43 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2014-03-24 06:20:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const BuilderId = "fnoeding.null"
|
|
|
|
|
|
|
|
type Builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2014-03-24 06:20:32 -04:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
2014-03-24 06:20:32 -04:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2014-03-24 06:20:32 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, nil
|
2014-03-24 06:20:32 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2017-03-05 17:22:33 -05:00
|
|
|
steps := []multistep.Step{}
|
|
|
|
|
2019-09-25 16:38:12 -04:00
|
|
|
steps = append(steps,
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.CommConfig,
|
|
|
|
Host: CommHost(b.config.CommConfig.Host()),
|
|
|
|
SSHConfig: b.config.CommConfig.SSHConfigFunc(),
|
|
|
|
},
|
|
|
|
)
|
2014-03-24 06:20:32 -04:00
|
|
|
|
2017-03-05 17:22:33 -05:00
|
|
|
steps = append(steps,
|
2020-11-11 18:04:28 -05:00
|
|
|
new(commonsteps.StepProvision),
|
2017-03-05 17:22:33 -05:00
|
|
|
)
|
|
|
|
|
2014-03-24 06:20:32 -04:00
|
|
|
// Setup the state bag and initial state for the steps
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2020-02-13 11:35:23 -05:00
|
|
|
state.Put("instance_id", "Null")
|
2014-03-24 06:20:32 -04:00
|
|
|
|
|
|
|
// Run!
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2014-03-24 06:20:32 -04:00
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// No errors, must've worked
|
|
|
|
artifact := &NullArtifact{}
|
|
|
|
return artifact, nil
|
|
|
|
}
|