Files
Packer-Cn/builder/lxd/builder.go
T

71 lines
1.7 KiB
Go
Raw Normal View History

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