2016-05-26 23:21:55 -04:00
|
|
|
package lxd
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2016-05-26 23:21:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// The unique ID for this builder
|
|
|
|
const BuilderId = "lxd"
|
|
|
|
|
|
|
|
type wrappedCommandTemplate struct {
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2016-05-26 23:21:55 -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
|
|
|
errs := b.config.Prepare(raws...)
|
2016-05-26 23:21:55 -04:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, errs
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, nil
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
|
2016-05-26 23:21:55 -04:00
|
|
|
wrappedCommand := func(command string) (string, error) {
|
|
|
|
b.config.ctx.Data = &wrappedCommandTemplate{Command: command}
|
|
|
|
return interpolate.Render(b.config.CommandWrapper, &b.config.ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
steps := []multistep.Step{
|
2016-05-30 19:57:26 -04:00
|
|
|
&stepLxdLaunch{},
|
|
|
|
&StepProvision{},
|
|
|
|
&stepPublish{},
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the state bag
|
|
|
|
state := new(multistep.BasicStateBag)
|
2019-12-19 11:01:55 -05:00
|
|
|
state.Put("config", &b.config)
|
2016-05-26 23:21:55 -04:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
|
|
|
|
|
|
|
|
// Run
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2016-05-26 23:21:55 -04:00
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
2020-01-30 05:27:58 -05:00
|
|
|
id: state.Get("imageFingerprint").(string),
|
|
|
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
2016-05-26 23:21:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|