Files
Packer-Cn/builder/vmware/iso/builder.go
T

208 lines
6.0 KiB
Go
Raw Normal View History

2013-12-23 22:58:41 -07:00
package iso
import (
"context"
"errors"
"fmt"
"time"
2015-05-27 14:16:28 -07:00
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/hcl/v2/hcldec"
2017-04-04 13:39:01 -07:00
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
2018-01-19 16:18:44 -08:00
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 13:39:01 -07:00
"github.com/hashicorp/packer/packer"
)
type Builder struct {
2015-05-27 14:16:28 -07:00
config Config
runner multistep.Runner
}
2019-12-17 11:25:56 +01:00
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
2019-12-17 11:25:56 +01:00
warnings, errs := b.config.Prepare(raws...)
if errs != nil {
return nil, warnings, errs
}
return nil, warnings, nil
}
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig, b.config.VMName)
2013-08-13 08:54:12 -07:00
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)
}
2017-02-24 12:46:00 +00:00
// 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 != "" {
2015-02-13 19:13:58 +09:00
b.config.OutputDir = b.config.VMName
}
dir.SetOutputDir(b.config.OutputDir)
// Setup the state bag
state := new(multistep.BasicStateBag)
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 22:15:53 +02:00
state.Put("driverConfig", &b.config.DriverConfig)
state.Put("temporaryDevices", []string{}) // Devices (in .vmx) created by packer during building
steps := []multistep.Step{
&vmwcommon.StepPrepareTools{
2014-05-09 21:12:14 -07:00
RemoteType: b.config.RemoteType,
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
},
&common.StepDownload{
Checksum: b.config.ISOChecksum,
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 23:43:50 +02:00
Files: b.config.FloppyConfig.FloppyFiles,
Directories: b.config.FloppyConfig.FloppyDirectories,
2019-09-12 12:25:22 +00:00
Label: b.config.FloppyConfig.FloppyLabel,
},
2019-09-20 00:23:28 +05:30
&vmwcommon.StepRemoteUpload{
Key: "floppy_path",
Message: "Uploading Floppy to remote machine...",
DoCleanup: true,
Checksum: "none",
},
2019-09-20 00:23:28 +05:30
&vmwcommon.StepRemoteUpload{
Key: "iso_path",
Message: "Uploading ISO to remote machine...",
DoCleanup: b.config.DriverConfig.CleanUpRemoteCache,
Checksum: b.config.ISOChecksum,
},
&stepCreateDisk{},
&stepCreateVMX{},
2013-12-23 23:07:43 -07:00
&vmwcommon.StepConfigureVMX{
2018-11-09 10:54:31 -08:00
CustomData: b.config.VMXData,
VMName: b.config.VMName,
DisplayName: b.config.VMXDisplayName,
},
2013-12-24 14:26:44 -07:00
&vmwcommon.StepSuppressMessages{},
2020-01-27 16:49:31 +01:00
&vmwcommon.StepHTTPIPDiscover{},
2015-11-01 14:29:24 -08:00
&common.StepHTTPServer{
2014-09-05 11:59:46 -07:00
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax,
HTTPAddress: b.config.HTTPAddress,
2014-09-05 11:59:46 -07:00
},
2014-09-05 12:10:40 -07:00
&vmwcommon.StepConfigureVNC{
2017-10-09 17:12:33 -07:00
Enabled: !b.config.DisableVNC,
VNCBindAddress: b.config.VNCBindAddress,
VNCPortMin: b.config.VNCPortMin,
VNCPortMax: b.config.VNCPortMax,
VNCDisablePassword: b.config.VNCDisablePassword,
2014-09-05 12:10:40 -07:00
},
&vmwcommon.StepRegister{
Format: b.config.Format,
KeepRegistered: b.config.KeepRegistered,
SkipExport: b.config.SkipExport,
2015-02-13 19:13:58 +09:00
},
2013-12-24 18:12:43 -07:00
&vmwcommon.StepRun{
DurationBeforeStop: 5 * time.Second,
Headless: b.config.Headless,
},
&vmwcommon.StepTypeBootCommand{
BootWait: b.config.BootWait,
2017-10-09 17:12:33 -07:00
VNCEnabled: !b.config.DisableVNC,
BootCommand: b.config.FlatBootCommand(),
VMName: b.config.VMName,
2015-05-27 14:16:28 -07:00
Ctx: b.config.ctx,
KeyInterval: b.config.VNCConfig.BootKeyInterval,
},
2015-06-13 18:52:44 -04:00
&communicator.StepConnect{
2015-06-13 19:23:33 -04:00
Config: &b.config.SSHConfig.Comm,
Host: driver.CommHost,
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
2013-07-15 14:22:13 +09:00
},
&vmwcommon.StepUploadTools{
2014-05-09 21:12:14 -07:00
RemoteType: b.config.RemoteType,
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
2014-05-09 21:12:14 -07:00
ToolsUploadPath: b.config.ToolsUploadPath,
2015-05-27 14:16:28 -07:00
Ctx: b.config.ctx,
},
2013-07-16 15:44:41 +09:00
&common.StepProvision{},
&common.StepCleanupTempKeys{
Comm: &b.config.SSHConfig.Comm,
},
2013-12-24 23:33:49 -07:00
&vmwcommon.StepShutdown{
Command: b.config.ShutdownCommand,
2013-12-26 15:31:23 -07:00
Timeout: b.config.ShutdownTimeout,
2013-12-24 23:33:49 -07:00
},
2013-12-24 18:17:58 -07:00
&vmwcommon.StepCleanFiles{},
&vmwcommon.StepCompactDisk{
Skip: b.config.SkipCompaction,
},
2014-05-09 09:25:15 -07:00
&vmwcommon.StepConfigureVMX{
2018-11-09 10:54:31 -08:00
CustomData: b.config.VMXDataPost,
SkipFloppy: true,
VMName: b.config.VMName,
DisplayName: b.config.VMXDisplayName,
2014-05-09 09:25:15 -07:00
},
&vmwcommon.StepCleanVMX{
RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet,
2017-10-12 16:38:18 -07:00
VNCEnabled: !b.config.DisableVNC,
},
&vmwcommon.StepUploadVMX{
2015-05-27 14:16:28 -07:00
RemoteType: b.config.RemoteType,
},
2017-03-05 22:15:53 +02:00
&vmwcommon.StepExport{
Format: b.config.Format,
SkipExport: b.config.SkipExport,
VMName: b.config.VMName,
OVFToolOptions: b.config.OVFToolOptions,
OutputDir: exportOutputPath,
2015-02-13 19:13:58 +09:00
},
}
// Run!
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(ctx, state)
2013-06-19 21:20:48 -07:00
// If there was an error, return that
2013-08-31 12:50:25 -07:00
if rawErr, ok := state.GetOk("error"); ok {
2013-06-19 21:20:48 -07:00
return nil, rawErr.(error)
}
2013-06-06 15:12:54 -07:00
// If we were interrupted or cancelled, then just exit.
2013-08-31 12:50:25 -07:00
if _, ok := state.GetOk(multistep.StateCancelled); ok {
2013-06-19 21:20:48 -07:00
return nil, errors.New("Build was cancelled.")
2013-06-06 15:12:54 -07:00
}
2013-08-31 12:50:25 -07:00
if _, ok := state.GetOk(multistep.StateHalted); ok {
2013-06-19 21:20:48 -07:00
return nil, errors.New("Build was halted.")
2013-06-06 15:12:54 -07:00
}
// Compile the artifact list
return vmwcommon.NewArtifact(b.config.RemoteType, b.config.Format, exportOutputPath,
2018-10-25 15:07:07 -07:00
b.config.VMName, b.config.SkipExport, b.config.KeepRegistered, state)
}