Files
Packer-Cn/builder/vsphere/clone/builder.go
T

117 lines
2.8 KiB
Go
Raw Normal View History

2018-01-24 14:04:39 +03:00
package clone
2017-05-09 17:23:57 +03:00
import (
2019-05-25 00:45:53 -05:00
"context"
2020-02-14 11:42:29 -05:00
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/builder/vsphere/common"
"github.com/hashicorp/packer/builder/vsphere/driver"
2018-01-24 14:04:39 +03:00
packerCommon "github.com/hashicorp/packer/common"
2018-11-01 00:42:24 +03:00
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
2017-08-25 02:08:08 -03:00
"github.com/hashicorp/packer/packer"
2017-05-09 17:23:57 +03:00
)
type Builder struct {
config Config
2017-05-09 17:23:57 +03:00
runner multistep.Runner
}
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
2020-01-13 15:52:05 -08:00
warnings, errs := b.config.Prepare(raws...)
2017-05-09 17:23:57 +03:00
if errs != nil {
2020-01-13 15:52:05 -08:00
return nil, warnings, errs
2017-05-09 17:23:57 +03:00
}
2020-01-13 15:52:05 -08:00
return nil, warnings, nil
2017-05-09 17:23:57 +03:00
}
2019-05-25 00:45:53 -05:00
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
2017-05-09 17:23:57 +03:00
state := new(multistep.BasicStateBag)
state.Put("hook", hook)
state.Put("ui", ui)
var steps []multistep.Step
steps = append(steps,
2018-01-24 14:04:39 +03:00
&common.StepConnect{
Config: &b.config.ConnectConfig,
2017-07-02 03:52:10 +03:00
},
2017-05-19 07:44:27 +03:00
&StepCloneVM{
2018-05-07 00:26:04 +03:00
Config: &b.config.CloneConfig,
Location: &b.config.LocationConfig,
Force: b.config.PackerConfig.PackerForce,
2017-05-09 17:23:57 +03:00
},
2018-05-06 00:41:14 +03:00
&common.StepConfigureHardware{
Config: &b.config.HardwareConfig,
2017-05-09 17:23:57 +03:00
},
2018-05-05 01:48:16 +03:00
&common.StepConfigParams{
Config: &b.config.ConfigParamsConfig,
},
)
if b.config.Comm.Type != "none" {
steps = append(steps,
2018-01-30 20:25:05 +03:00
&common.StepRun{
2018-11-01 00:42:24 +03:00
Config: &b.config.RunConfig,
SetOrder: false,
2018-01-30 20:25:05 +03:00
},
2018-05-30 14:52:27 +03:00
&common.StepWaitForIp{
2019-07-08 17:48:37 +03:00
Config: &b.config.WaitIpConfig,
2018-05-30 14:52:27 +03:00
},
&communicator.StepConnect{
Config: &b.config.Comm,
Host: common.CommHost(b.config.Comm.Host()),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
2018-01-24 14:04:39 +03:00
&packerCommon.StepProvision{},
&common.StepShutdown{
Config: &b.config.ShutdownConfig,
},
)
}
steps = append(steps,
2018-01-24 14:04:39 +03:00
&common.StepCreateSnapshot{
CreateSnapshot: b.config.CreateSnapshot,
2017-06-27 10:32:59 +03:00
},
2018-01-24 14:04:39 +03:00
&common.StepConvertToTemplate{
2017-06-27 10:32:59 +03:00
ConvertToTemplate: b.config.ConvertToTemplate,
2017-05-09 17:23:57 +03:00
},
)
2017-05-09 17:23:57 +03:00
if b.config.Export != nil {
steps = append(steps, &common.StepExport{
Name: b.config.Export.Name,
Force: b.config.Export.Force,
Images: b.config.Export.Images,
Manifest: b.config.Export.Manifest,
OutputDir: b.config.Export.OutputDir.OutputDir,
Options: b.config.Export.Options,
})
}
2018-01-24 14:04:39 +03:00
b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui)
2019-05-25 00:45:53 -05:00
b.runner.Run(ctx, state)
2017-05-09 17:23:57 +03:00
2018-05-07 00:26:04 +03:00
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
2017-05-09 17:23:57 +03:00
}
2018-05-07 00:26:04 +03:00
if _, ok := state.GetOk("vm"); !ok {
return nil, nil
}
2018-01-24 14:04:39 +03:00
artifact := &common.Artifact{
Name: b.config.VMName,
VM: state.Get("vm").(*driver.VirtualMachine),
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
2017-05-09 17:23:57 +03:00
}
if b.config.Export != nil {
artifact.Outconfig = &b.config.Export.OutputDir
}
2017-05-09 17:23:57 +03:00
return artifact, nil
}