packer-cn/clone/builder.go

99 lines
2.2 KiB
Go
Raw Normal View History

package clone
import (
"context"
packerCommon "github.com/hashicorp/packer/common"
2018-10-31 17:42:24 -04:00
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
2017-08-25 01:08:08 -04:00
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/common"
2017-08-23 20:06:50 -04:00
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
)
type Builder struct {
config *Config
runner multistep.Runner
}
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
c, warnings, errs := NewConfig(raws...)
if errs != nil {
return warnings, errs
}
b.config = c
return warnings, nil
}
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
state := new(multistep.BasicStateBag)
state.Put("comm", &b.config.Comm)
state.Put("hook", hook)
state.Put("ui", ui)
var steps []multistep.Step
steps = append(steps,
&common.StepConnect{
Config: &b.config.ConnectConfig,
},
&StepCloneVM{
2018-05-06 17:26:04 -04:00
Config: &b.config.CloneConfig,
Location: &b.config.LocationConfig,
Force: b.config.PackerConfig.PackerForce,
},
2018-05-05 17:41:14 -04:00
&common.StepConfigureHardware{
Config: &b.config.HardwareConfig,
},
&common.StepConfigParams{
Config: &b.config.ConfigParamsConfig,
},
)
if b.config.Comm.Type != "none" {
steps = append(steps,
2018-01-30 12:25:05 -05:00
&common.StepRun{
2018-10-31 17:42:24 -04:00
Config: &b.config.RunConfig,
SetOrder: false,
2018-01-30 12:25:05 -05:00
},
&common.StepWaitForIp{
&b.config.WaitIpConfig,
},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: common.CommHost(b.config.Comm.SSHHost),
SSHConfig: common.SshConfig,
},
&packerCommon.StepProvision{},
&common.StepShutdown{
Config: &b.config.ShutdownConfig,
},
)
}
steps = append(steps,
&common.StepCreateSnapshot{
CreateSnapshot: b.config.CreateSnapshot,
2017-06-27 03:32:59 -04:00
},
&common.StepConvertToTemplate{
2017-06-27 03:32:59 -04:00
ConvertToTemplate: b.config.ConvertToTemplate,
},
)
b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(ctx, state)
2018-05-06 17:26:04 -04:00
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
2018-05-06 17:26:04 -04:00
if _, ok := state.GetOk("vm"); !ok {
return nil, nil
}
artifact := &common.Artifact{
2017-07-02 16:29:50 -04:00
Name: b.config.VMName,
2017-08-23 20:06:50 -04:00
VM: state.Get("vm").(*driver.VirtualMachine),
}
return artifact, nil
}