2013-12-25 17:52:40 -05:00
|
|
|
package vmx
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2013-12-25 17:52:40 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-12-26 17:26:09 -05:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
|
2020-11-11 18:04:28 -05:00
|
|
|
"github.com/hashicorp/packer/common/commonsteps"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-25 17:52:40 -05:00
|
|
|
)
|
|
|
|
|
2015-01-13 16:43:24 -05:00
|
|
|
// Builder implements packer.Builder and builds the actual VMware
|
2013-12-25 17:52:40 -05:00
|
|
|
// images.
|
|
|
|
type Builder struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
config Config
|
2013-12-25 17:52:40 -05:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
2013-12-25 17:52:40 -05:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2013-12-25 17:52:40 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, nil
|
2013-12-25 17:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes a Packer build and returns a packer.Artifact representing
|
2018-03-28 20:19:15 -04:00
|
|
|
// a VMware image.
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2018-10-29 14:24:26 -04:00
|
|
|
driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig, b.config.VMName)
|
2013-12-25 17:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating VMware driver: %s", err)
|
|
|
|
}
|
2020-08-26 18:45:59 -04:00
|
|
|
// Before we get deep into the build, make sure ovftool is present and
|
|
|
|
// credentials are valid, if we're going to use ovftool.
|
|
|
|
if err := driver.VerifyOvfTool(b.config.SkipExport, b.config.SkipValidateCredentials); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-25 17:52:40 -05:00
|
|
|
|
|
|
|
// Set up the state.
|
|
|
|
state := new(multistep.BasicStateBag)
|
2016-05-17 03:50:00 -04:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2013-12-25 17:52:40 -05:00
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2017-02-06 02:13:37 -05:00
|
|
|
state.Put("sshConfig", &b.config.SSHConfig)
|
2017-03-07 02:03:04 -05:00
|
|
|
state.Put("driverConfig", &b.config.DriverConfig)
|
2018-12-03 18:43:02 -05:00
|
|
|
state.Put("temporaryDevices", []string{}) // Devices (in .vmx) created by packer during building
|
2013-12-25 17:52:40 -05:00
|
|
|
|
|
|
|
// Build the steps.
|
2013-12-25 18:01:57 -05:00
|
|
|
steps := []multistep.Step{
|
2014-05-09 20:24:19 -04:00
|
|
|
&vmwcommon.StepPrepareTools{
|
2014-05-10 00:12:14 -04:00
|
|
|
RemoteType: b.config.RemoteType,
|
2014-05-09 20:24:19 -04:00
|
|
|
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
|
|
|
|
},
|
2013-12-25 18:01:57 -05:00
|
|
|
&vmwcommon.StepOutputDir{
|
2020-08-18 14:27:12 -04:00
|
|
|
Force: b.config.PackerForce,
|
|
|
|
OutputConfig: &b.config.OutputConfig,
|
|
|
|
RemoteType: b.config.RemoteType,
|
|
|
|
VMName: b.config.VMName,
|
2013-12-25 18:01:57 -05:00
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepCreateFloppy{
|
2016-10-11 17:43:50 -04:00
|
|
|
Files: b.config.FloppyConfig.FloppyFiles,
|
2016-09-28 00:31:42 -04:00
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2019-09-12 08:25:22 -04:00
|
|
|
Label: b.config.FloppyConfig.FloppyLabel,
|
2014-05-06 20:20:26 -04:00
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepCreateCD{
|
2020-09-10 18:38:28 -04:00
|
|
|
Files: b.config.CDConfig.CDFiles,
|
|
|
|
Label: b.config.CDConfig.CDLabel,
|
|
|
|
},
|
2019-09-19 14:53:28 -04:00
|
|
|
&vmwcommon.StepRemoteUpload{
|
2020-05-28 05:02:09 -04:00
|
|
|
Key: "floppy_path",
|
|
|
|
Message: "Uploading Floppy to remote machine...",
|
|
|
|
DoCleanup: true,
|
|
|
|
Checksum: "none",
|
2019-09-19 14:53:28 -04:00
|
|
|
},
|
2020-08-26 04:13:11 -04:00
|
|
|
&vmwcommon.StepCreateDisks{
|
|
|
|
OutputDir: &b.config.OutputDir,
|
|
|
|
CreateMainDisk: false,
|
|
|
|
DiskName: b.config.DiskName,
|
|
|
|
MainDiskSize: 0,
|
|
|
|
AdditionalDiskSize: b.config.AdditionalDiskSize,
|
|
|
|
DiskAdapterType: b.config.DiskAdapterType,
|
|
|
|
DiskTypeId: b.config.DiskTypeId,
|
|
|
|
},
|
2013-12-26 10:34:27 -05:00
|
|
|
&StepCloneVMX{
|
2013-12-26 10:36:00 -05:00
|
|
|
Path: b.config.SourcePath,
|
2020-08-18 14:27:12 -04:00
|
|
|
OutputDir: &b.config.OutputDir,
|
2013-12-26 10:36:00 -05:00
|
|
|
VMName: b.config.VMName,
|
2018-06-16 20:38:42 -04:00
|
|
|
Linked: b.config.Linked,
|
2013-12-26 10:34:27 -05:00
|
|
|
},
|
2013-12-26 17:14:19 -05:00
|
|
|
&vmwcommon.StepConfigureVMX{
|
2020-09-10 18:38:28 -04:00
|
|
|
CustomData: b.config.VMXData,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
DisplayName: b.config.VMXDisplayName,
|
|
|
|
DiskAdapterType: b.config.DiskAdapterType,
|
|
|
|
CDROMAdapterType: "",
|
2013-12-26 17:14:19 -05:00
|
|
|
},
|
|
|
|
&vmwcommon.StepSuppressMessages{},
|
2020-01-27 10:49:31 -05:00
|
|
|
&vmwcommon.StepHTTPIPDiscover{},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepHTTPServer{
|
2014-09-14 07:28:55 -04:00
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
2020-05-27 16:34:24 -04:00
|
|
|
HTTPAddress: b.config.HTTPAddress,
|
2014-09-14 07:28:55 -04:00
|
|
|
},
|
2017-01-31 05:05:49 -05:00
|
|
|
&vmwcommon.StepUploadVMX{
|
|
|
|
RemoteType: b.config.RemoteType,
|
|
|
|
},
|
2014-09-14 07:28:55 -04:00
|
|
|
&vmwcommon.StepConfigureVNC{
|
2020-09-16 05:02:46 -04:00
|
|
|
Enabled: !b.config.DisableVNC && !b.config.VNCOverWebsocket,
|
2016-08-19 06:49:23 -04:00
|
|
|
VNCBindAddress: b.config.VNCBindAddress,
|
|
|
|
VNCPortMin: b.config.VNCPortMin,
|
|
|
|
VNCPortMax: b.config.VNCPortMax,
|
|
|
|
VNCDisablePassword: b.config.VNCDisablePassword,
|
2014-09-14 07:28:55 -04:00
|
|
|
},
|
2017-02-01 06:32:02 -05:00
|
|
|
&vmwcommon.StepRegister{
|
2017-03-07 02:03:04 -05:00
|
|
|
Format: b.config.Format,
|
|
|
|
KeepRegistered: b.config.KeepRegistered,
|
2018-10-25 17:17:35 -04:00
|
|
|
SkipExport: b.config.SkipExport,
|
2017-02-01 06:32:02 -05:00
|
|
|
},
|
2013-12-26 17:26:09 -05:00
|
|
|
&vmwcommon.StepRun{
|
|
|
|
DurationBeforeStop: 5 * time.Second,
|
|
|
|
Headless: b.config.Headless,
|
|
|
|
},
|
2020-09-16 05:02:46 -04:00
|
|
|
&vmwcommon.StepVNCConnect{
|
|
|
|
VNCEnabled: !b.config.DisableVNC,
|
|
|
|
VNCOverWebsocket: b.config.VNCOverWebsocket,
|
|
|
|
InsecureConnection: b.config.InsecureConnection,
|
|
|
|
DriverConfig: &b.config.DriverConfig,
|
|
|
|
},
|
2020-09-17 04:38:33 -04:00
|
|
|
&vmwcommon.StepVNCBootCommand{
|
|
|
|
Config: b.config.VNCConfig,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
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,
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
|
2013-12-26 17:28:15 -05:00
|
|
|
},
|
2014-05-09 20:24:19 -04:00
|
|
|
&vmwcommon.StepUploadTools{
|
2014-05-10 00:12:14 -04:00
|
|
|
RemoteType: b.config.RemoteType,
|
2014-05-09 20:24:19 -04:00
|
|
|
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
|
2014-05-10 00:12:14 -04:00
|
|
|
ToolsUploadPath: b.config.ToolsUploadPath,
|
2015-05-27 17:21:15 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-05-09 20:24:19 -04:00
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepProvision{},
|
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2018-09-14 14:03:23 -04:00
|
|
|
Comm: &b.config.SSHConfig.Comm,
|
|
|
|
},
|
2013-12-26 17:31:23 -05:00
|
|
|
&vmwcommon.StepShutdown{
|
|
|
|
Command: b.config.ShutdownCommand,
|
|
|
|
Timeout: b.config.ShutdownTimeout,
|
|
|
|
},
|
2013-12-26 17:32:38 -05:00
|
|
|
&vmwcommon.StepCleanFiles{},
|
2015-09-04 12:37:48 -04:00
|
|
|
&vmwcommon.StepCompactDisk{
|
|
|
|
Skip: b.config.SkipCompaction,
|
|
|
|
},
|
2014-05-09 12:25:15 -04:00
|
|
|
&vmwcommon.StepConfigureVMX{
|
2018-11-09 13:54:31 -05:00
|
|
|
CustomData: b.config.VMXDataPost,
|
|
|
|
SkipFloppy: true,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
DisplayName: b.config.VMXDisplayName,
|
2014-05-09 12:25:15 -04:00
|
|
|
},
|
2017-05-24 19:44:35 -04:00
|
|
|
&vmwcommon.StepCleanVMX{
|
|
|
|
RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet,
|
2017-10-12 19:38:18 -04:00
|
|
|
VNCEnabled: !b.config.DisableVNC,
|
2017-05-24 19:44:35 -04:00
|
|
|
},
|
2017-02-03 08:56:13 -05:00
|
|
|
&vmwcommon.StepUploadVMX{
|
|
|
|
RemoteType: b.config.RemoteType,
|
|
|
|
},
|
2017-03-07 02:03:04 -05:00
|
|
|
&vmwcommon.StepExport{
|
|
|
|
Format: b.config.Format,
|
|
|
|
SkipExport: b.config.SkipExport,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
OVFToolOptions: b.config.OVFToolOptions,
|
2020-08-25 15:18:38 -04:00
|
|
|
OutputDir: &b.config.OutputConfig.OutputDir,
|
2017-03-07 02:03:04 -05:00
|
|
|
},
|
2013-12-25 18:01:57 -05:00
|
|
|
}
|
2013-12-25 17:52:40 -05:00
|
|
|
|
|
|
|
// Run the steps.
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2013-12-25 17:52:40 -05:00
|
|
|
|
|
|
|
// Report any errors.
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were interrupted or cancelled, then just exit.
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
2018-10-25 17:17:35 -04:00
|
|
|
|
2018-10-25 18:07:07 -04:00
|
|
|
// Artifact
|
2018-11-02 14:10:51 -04:00
|
|
|
log.Printf("Generating artifact...")
|
2020-08-18 14:27:12 -04:00
|
|
|
exportOutputPath := state.Get("export_output_path").(string) // set in StepOutputDir
|
2018-11-02 14:10:51 -04:00
|
|
|
return vmwcommon.NewArtifact(b.config.RemoteType, b.config.Format, exportOutputPath,
|
2018-10-25 18:07:07 -04:00
|
|
|
b.config.VMName, b.config.SkipExport, b.config.KeepRegistered, state)
|
2013-12-25 17:52:40 -05:00
|
|
|
}
|