2018-01-24 06:04:39 -05:00
|
|
|
package clone
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-24 06:04:39 -05:00
|
|
|
packerCommon "github.com/hashicorp/packer/common"
|
2017-05-09 10:23:57 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2017-08-25 01:08:08 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-01-24 06:04:39 -05:00
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/common"
|
2017-08-23 20:06:50 -04:00
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
2018-04-25 07:22:38 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-05-09 10:23:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
|
|
|
state := new(multistep.BasicStateBag)
|
2017-08-25 01:08:08 -04:00
|
|
|
state.Put("config", b.config)
|
2018-01-24 06:04:39 -05:00
|
|
|
state.Put("comm", &b.config.Comm)
|
2017-05-09 10:23:57 -04:00
|
|
|
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{
|
2017-07-01 20:52:10 -04:00
|
|
|
config: &b.config.CloneConfig,
|
2017-05-09 10:23:57 -04:00
|
|
|
},
|
2017-07-01 18:34:50 -04:00
|
|
|
&StepConfigureHardware{
|
|
|
|
config: &b.config.HardwareConfig,
|
2017-05-09 10:23:57 -04:00
|
|
|
},
|
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{
|
|
|
|
Config: &b.config.RunConfig,
|
|
|
|
},
|
2018-02-12 07:56:29 -05:00
|
|
|
&common.StepWaitForIp{},
|
2018-01-16 08:16:08 -05:00
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Comm,
|
2018-01-24 06:04:39 -05:00
|
|
|
Host: common.CommHost,
|
|
|
|
SSHConfig: common.SshConfig,
|
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
|
|
|
|
|
|
|
// Run!
|
2018-01-24 06:04:39 -05:00
|
|
|
b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui)
|
2017-05-09 10:23:57 -04:00
|
|
|
b.runner.Run(state)
|
|
|
|
|
2018-01-24 09:56:14 -05:00
|
|
|
if err := common.CheckRunStatus(state); err != nil {
|
|
|
|
return nil, err
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|