packer-cn/builder/vmware/iso/builder.go

210 lines
5.7 KiB
Go
Raw Normal View History

2013-12-24 00:58:41 -05:00
package iso
import (
"errors"
"fmt"
2013-06-05 20:52:37 -04:00
"log"
"time"
2015-05-27 17:16:28 -04:00
2017-04-04 16:39:01 -04:00
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
)
type Builder struct {
2015-05-27 17:16:28 -04:00
config Config
runner multistep.Runner
}
// Prepare processes the build configuration parameters.
2013-11-03 00:03:59 -04:00
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) {
driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig, b.config.VMName)
if err != nil {
return nil, fmt.Errorf("Failed creating VMware driver: %s", err)
}
// Determine the output dir implementation
var dir vmwcommon.OutputDir
switch d := driver.(type) {
case vmwcommon.OutputDir:
dir = d
default:
dir = new(vmwcommon.LocalOutputDir)
}
// The OutputDir will track remote esxi output; exportOutputPath preserves
// the path to the output on the machine running Packer.
exportOutputPath := b.config.OutputDir
if b.config.RemoteType != "" {
b.config.OutputDir = b.config.VMName
}
dir.SetOutputDir(b.config.OutputDir)
// Setup the state bag
state := new(multistep.BasicStateBag)
state.Put("cache", cache)
state.Put("config", &b.config)
state.Put("debug", b.config.PackerDebug)
state.Put("dir", dir)
state.Put("driver", driver)
state.Put("hook", hook)
state.Put("ui", ui)
state.Put("sshConfig", &b.config.SSHConfig)
2017-03-05 15:15:53 -05:00
state.Put("driverConfig", &b.config.DriverConfig)
steps := []multistep.Step{
&vmwcommon.StepPrepareTools{
2014-05-10 00:12:14 -04:00
RemoteType: b.config.RemoteType,
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
},
&common.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,
},
&vmwcommon.StepOutputDir{
Force: b.config.PackerForce,
},
&common.StepCreateFloppy{
2016-10-11 17:43:50 -04:00
Files: b.config.FloppyConfig.FloppyFiles,
Directories: b.config.FloppyConfig.FloppyDirectories,
},
&stepRemoteUpload{
Key: "floppy_path",
Message: "Uploading Floppy to remote machine...",
DoCleanup: true,
},
&stepRemoteUpload{
Key: "iso_path",
Message: "Uploading ISO to remote machine...",
},
&stepCreateDisk{},
&stepCreateVMX{},
&vmwcommon.StepConfigureVMX{
2018-11-09 13:54:31 -05:00
CustomData: b.config.VMXData,
VMName: b.config.VMName,
DisplayName: b.config.VMXDisplayName,
},
&vmwcommon.StepSuppressMessages{},
&common.StepHTTPServer{
2014-09-05 14:59:46 -04:00
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax,
},
2014-09-05 15:10:40 -04:00
&vmwcommon.StepConfigureVNC{
2017-10-09 20:12:33 -04:00
Enabled: !b.config.DisableVNC,
VNCBindAddress: b.config.VNCBindAddress,
VNCPortMin: b.config.VNCPortMin,
VNCPortMax: b.config.VNCPortMax,
VNCDisablePassword: b.config.VNCDisablePassword,
2014-09-05 15:10:40 -04:00
},
&vmwcommon.StepRegister{
Format: b.config.Format,
KeepRegistered: b.config.KeepRegistered,
SkipExport: b.config.SkipExport,
},
2013-12-24 20:12:43 -05:00
&vmwcommon.StepRun{
DurationBeforeStop: 5 * time.Second,
Headless: b.config.Headless,
},
&vmwcommon.StepTypeBootCommand{
BootWait: b.config.BootWait,
2017-10-09 20:12:33 -04:00
VNCEnabled: !b.config.DisableVNC,
BootCommand: b.config.FlatBootCommand(),
VMName: b.config.VMName,
2015-05-27 17:16:28 -04:00
Ctx: b.config.ctx,
KeyInterval: b.config.VNCConfig.BootKeyInterval,
},
2015-06-13 18:52:44 -04:00
&communicator.StepConnect{
Config: &b.config.SSHConfig.Comm,
Host: driver.CommHost,
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
},
&vmwcommon.StepUploadTools{
2014-05-10 00:12:14 -04:00
RemoteType: b.config.RemoteType,
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
2014-05-10 00:12:14 -04:00
ToolsUploadPath: b.config.ToolsUploadPath,
2015-05-27 17:16:28 -04:00
Ctx: b.config.ctx,
},
&common.StepProvision{},
&common.StepCleanupTempKeys{
Comm: &b.config.SSHConfig.Comm,
},
2013-12-25 01:33:49 -05:00
&vmwcommon.StepShutdown{
Command: b.config.ShutdownCommand,
2013-12-26 17:31:23 -05:00
Timeout: b.config.ShutdownTimeout,
2013-12-25 01:33:49 -05:00
},
2013-12-24 20:17:58 -05:00
&vmwcommon.StepCleanFiles{},
&vmwcommon.StepCompactDisk{
Skip: b.config.SkipCompaction,
},
&vmwcommon.StepConfigureVMX{
2018-11-09 13:54:31 -05:00
CustomData: b.config.VMXDataPost,
SkipFloppy: true,
VMName: b.config.VMName,
DisplayName: b.config.VMXDisplayName,
},
&vmwcommon.StepCleanVMX{
RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet,
2017-10-12 19:38:18 -04:00
VNCEnabled: !b.config.DisableVNC,
},
&vmwcommon.StepUploadVMX{
2015-05-27 17:16:28 -04:00
RemoteType: b.config.RemoteType,
},
2017-03-05 15:15:53 -05:00
&vmwcommon.StepExport{
Format: b.config.Format,
SkipExport: b.config.SkipExport,
VMName: b.config.VMName,
OVFToolOptions: b.config.OVFToolOptions,
OutputDir: exportOutputPath,
},
}
// Run!
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(state)
2013-06-20 00:20:48 -04:00
// If there was an error, return that
2013-08-31 15:50:25 -04:00
if rawErr, ok := state.GetOk("error"); ok {
2013-06-20 00:20:48 -04:00
return nil, rawErr.(error)
}
2013-06-06 18:12:54 -04:00
// If we were interrupted or cancelled, then just exit.
2013-08-31 15:50:25 -04:00
if _, ok := state.GetOk(multistep.StateCancelled); ok {
2013-06-20 00:20:48 -04:00
return nil, errors.New("Build was cancelled.")
2013-06-06 18:12:54 -04:00
}
2013-08-31 15:50:25 -04:00
if _, ok := state.GetOk(multistep.StateHalted); ok {
2013-06-20 00:20:48 -04:00
return nil, errors.New("Build was halted.")
2013-06-06 18:12:54 -04:00
}
// Compile the artifact list
return vmwcommon.NewArtifact(b.config.RemoteType, b.config.Format, exportOutputPath,
b.config.VMName, b.config.SkipExport, b.config.KeepRegistered, state)
}
func (b *Builder) Cancel() {
2013-06-05 20:52:37 -04:00
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}