2018-01-24 09:56:14 -05:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
|
|
|
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"
|
2018-01-24 09:56:14 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/common"
|
|
|
|
"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(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
|
|
|
state := new(multistep.BasicStateBag)
|
2019-01-04 09:07:09 -05:00
|
|
|
state.Put("cache", cache)
|
2018-01-24 09:56:14 -05:00
|
|
|
state.Put("comm", &b.config.Comm)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
2018-02-01 06:47:09 -05:00
|
|
|
var steps []multistep.Step
|
2018-01-24 09:56:14 -05:00
|
|
|
|
|
|
|
steps = append(steps,
|
|
|
|
&common.StepConnect{
|
|
|
|
Config: &b.config.ConnectConfig,
|
|
|
|
},
|
2019-01-04 09:07:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if b.config.ISOUrls != nil {
|
|
|
|
steps = append(steps,
|
|
|
|
&packerCommon.StepDownload{
|
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
ChecksumType: b.config.ISOChecksumType,
|
|
|
|
Description: "ISO",
|
|
|
|
Extension: b.config.TargetExtension,
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
TargetPath: b.config.TargetPath,
|
|
|
|
Url: b.config.ISOUrls,
|
|
|
|
},
|
|
|
|
&StepRemoteUpload{
|
|
|
|
Datastore: b.config.Datastore,
|
|
|
|
Host: b.config.Host,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps,
|
2018-01-24 09:56:14 -05:00
|
|
|
&StepCreateVM{
|
2018-05-06 17:26:04 -04:00
|
|
|
Config: &b.config.CreateConfig,
|
|
|
|
Location: &b.config.LocationConfig,
|
2018-11-16 10:37:54 -05:00
|
|
|
Force: b.config.PackerConfig.PackerForce,
|
2018-01-24 09:56:14 -05:00
|
|
|
},
|
2018-05-05 17:41:14 -04:00
|
|
|
&common.StepConfigureHardware{
|
|
|
|
Config: &b.config.HardwareConfig,
|
|
|
|
},
|
2018-10-29 20:18:57 -04:00
|
|
|
&StepAddCDRom{
|
|
|
|
Config: &b.config.CDRomConfig,
|
|
|
|
},
|
2018-05-04 18:48:16 -04:00
|
|
|
&common.StepConfigParams{
|
2018-04-25 05:23:37 -04:00
|
|
|
Config: &b.config.ConfigParamsConfig,
|
2018-02-26 16:18:06 -05:00
|
|
|
},
|
2018-01-24 09:56:14 -05:00
|
|
|
)
|
|
|
|
|
2018-01-30 11:47:38 -05:00
|
|
|
if b.config.Comm.Type != "none" {
|
|
|
|
steps = append(steps,
|
2018-05-04 17:10:43 -04:00
|
|
|
&packerCommon.StepCreateFloppy{
|
|
|
|
Files: b.config.FloppyFiles,
|
|
|
|
Directories: b.config.FloppyDirectories,
|
|
|
|
},
|
|
|
|
&StepAddFloppy{
|
|
|
|
Config: &b.config.FloppyConfig,
|
|
|
|
Datastore: b.config.Datastore,
|
|
|
|
Host: b.config.Host,
|
|
|
|
},
|
2018-05-30 07:59:44 -04:00
|
|
|
&packerCommon.StepHTTPServer{
|
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
|
|
|
},
|
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: true,
|
2018-01-30 12:25:05 -05:00
|
|
|
},
|
2018-02-17 21:13:56 -05:00
|
|
|
&StepBootCommand{
|
|
|
|
Config: &b.config.BootConfig,
|
2018-12-22 18:13:31 -05:00
|
|
|
Ctx: b.config.ctx,
|
|
|
|
VMName: b.config.VMName,
|
2018-02-17 21:13:56 -05:00
|
|
|
},
|
2018-05-30 07:52:27 -04:00
|
|
|
&common.StepWaitForIp{
|
|
|
|
Config: &b.config.WaitIpConfig,
|
|
|
|
},
|
2018-01-30 11:47:38 -05:00
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Comm,
|
2018-12-23 12:16:49 -05:00
|
|
|
Host: common.CommHost(b.config.Comm.SSHHost),
|
2018-01-30 11:47:38 -05:00
|
|
|
SSHConfig: common.SshConfig,
|
|
|
|
},
|
|
|
|
&packerCommon.StepProvision{},
|
|
|
|
&common.StepShutdown{
|
|
|
|
Config: &b.config.ShutdownConfig,
|
|
|
|
},
|
2018-05-04 17:10:43 -04:00
|
|
|
&StepRemoveFloppy{
|
|
|
|
Datastore: b.config.Datastore,
|
|
|
|
Host: b.config.Host,
|
|
|
|
},
|
2018-01-30 11:47:38 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps,
|
2018-10-29 20:18:57 -04:00
|
|
|
&StepRemoveCDRom{},
|
2018-01-30 11:47:38 -05:00
|
|
|
&common.StepCreateSnapshot{
|
|
|
|
CreateSnapshot: b.config.CreateSnapshot,
|
|
|
|
},
|
|
|
|
&common.StepConvertToTemplate{
|
|
|
|
ConvertToTemplate: b.config.ConvertToTemplate,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-01-24 09:56:14 -05:00
|
|
|
b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui)
|
|
|
|
b.runner.Run(state)
|
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
2018-01-24 09:56:14 -05:00
|
|
|
}
|
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
if _, ok := state.GetOk("vm"); !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-01-24 09:56:14 -05:00
|
|
|
artifact := &common.Artifact{
|
|
|
|
Name: b.config.VMName,
|
|
|
|
VM: state.Get("vm").(*driver.VirtualMachine),
|
|
|
|
}
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|