2018-01-24 06:04:39 -05:00
|
|
|
package clone
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
import (
|
2019-05-25 01:45:53 -04:00
|
|
|
"context"
|
2020-01-08 13:22:55 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-01-07 19:59:31 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/common"
|
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2018-01-24 06:04:39 -05:00
|
|
|
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"
|
2017-05-09 10:23:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Builder struct {
|
2020-01-13 15:33:56 -05:00
|
|
|
config Config
|
2017-05-09 10:23:57 -04:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:22:55 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2020-01-13 18:52:05 -05:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
2017-05-09 10:23:57 -04:00
|
|
|
if errs != nil {
|
2020-01-13 18:52:05 -05:00
|
|
|
return nil, warnings, errs
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
2020-01-13 18:52:05 -05:00
|
|
|
return nil, warnings, nil
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
2019-05-25 01:45:53 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2017-05-09 10:23:57 -04:00
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
2018-01-16 08:16:08 -05:00
|
|
|
var steps []multistep.Step
|
|
|
|
|
|
|
|
steps = append(steps,
|
2018-01-24 06:04:39 -05:00
|
|
|
&common.StepConnect{
|
|
|
|
Config: &b.config.ConnectConfig,
|
2017-07-01 20:52:10 -04:00
|
|
|
},
|
2017-05-19 00:44:27 -04:00
|
|
|
&StepCloneVM{
|
2018-05-06 17:26:04 -04:00
|
|
|
Config: &b.config.CloneConfig,
|
|
|
|
Location: &b.config.LocationConfig,
|
2018-11-16 10:37:54 -05:00
|
|
|
Force: b.config.PackerConfig.PackerForce,
|
2017-05-09 10:23:57 -04:00
|
|
|
},
|
2018-05-05 17:41:14 -04:00
|
|
|
&common.StepConfigureHardware{
|
|
|
|
Config: &b.config.HardwareConfig,
|
2017-05-09 10:23:57 -04:00
|
|
|
},
|
2018-05-04 18:48:16 -04:00
|
|
|
&common.StepConfigParams{
|
|
|
|
Config: &b.config.ConfigParamsConfig,
|
|
|
|
},
|
2018-01-16 08:16:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2018-10-20 20:57:28 -04:00
|
|
|
SetOrder: false,
|
2018-01-30 12:25:05 -05:00
|
|
|
},
|
2018-05-30 07:52:27 -04:00
|
|
|
&common.StepWaitForIp{
|
2019-07-08 10:48:37 -04:00
|
|
|
Config: &b.config.WaitIpConfig,
|
2018-05-30 07:52:27 -04:00
|
|
|
},
|
2018-01-16 08:16:08 -05:00
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Comm,
|
2018-12-23 12:16:49 -05:00
|
|
|
Host: common.CommHost(b.config.Comm.SSHHost),
|
2019-07-08 11:40:01 -04:00
|
|
|
SSHConfig: b.config.Comm.SSHConfigFunc(),
|
2018-01-16 08:16:08 -05:00
|
|
|
},
|
2018-01-24 06:04:39 -05:00
|
|
|
&packerCommon.StepProvision{},
|
|
|
|
&common.StepShutdown{
|
|
|
|
Config: &b.config.ShutdownConfig,
|
2018-01-16 08:16:08 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps,
|
2018-01-24 06:04:39 -05:00
|
|
|
&common.StepCreateSnapshot{
|
|
|
|
CreateSnapshot: b.config.CreateSnapshot,
|
2017-06-27 03:32:59 -04:00
|
|
|
},
|
2018-01-24 06:04:39 -05:00
|
|
|
&common.StepConvertToTemplate{
|
2017-06-27 03:32:59 -04:00
|
|
|
ConvertToTemplate: b.config.ConvertToTemplate,
|
2017-05-09 10:23:57 -04:00
|
|
|
},
|
2018-01-16 08:16:08 -05:00
|
|
|
)
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2018-01-24 06:04:39 -05:00
|
|
|
b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-05-25 01:45:53 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
if _, ok := state.GetOk("vm"); !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-01-24 06:04:39 -05:00
|
|
|
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),
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
return artifact, nil
|
|
|
|
}
|