2013-12-23 22:58:41 -07:00
|
|
|
package iso
|
2013-06-04 15:00:58 -07:00
|
|
|
|
|
|
|
import (
|
2019-03-22 14:53:28 +01:00
|
|
|
"context"
|
2013-06-06 14:46:48 -07:00
|
|
|
"errors"
|
2013-06-06 14:38:14 -07:00
|
|
|
"fmt"
|
2013-06-06 09:10:14 -07:00
|
|
|
"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"
|
2013-06-04 15:00:58 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Builder struct {
|
2015-05-27 14:16:28 -07:00
|
|
|
config Config
|
2013-06-04 15:00:58 -07:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 11:25:56 +01:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-16 21:23:05 -08:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2019-12-17 11:25:56 +01:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
2018-11-07 09:42:03 -08:00
|
|
|
if errs != nil {
|
2019-12-16 21:23:05 -08:00
|
|
|
return nil, warnings, errs
|
2014-09-25 14:20:35 +10:00
|
|
|
}
|
|
|
|
|
2019-12-16 21:23:05 -08:00
|
|
|
return nil, warnings, nil
|
2013-06-04 15:00:58 -07:00
|
|
|
}
|
|
|
|
|
2019-03-22 14:53:28 +01:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2018-10-29 11:24:26 -07:00
|
|
|
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)
|
|
|
|
}
|
2020-08-26 15:45:59 -07: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-08-13 08:54:12 -07:00
|
|
|
|
2013-12-24 11:21:02 -07:00
|
|
|
// Setup the state bag
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", &b.config)
|
2016-05-17 03:50:00 -04:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2013-12-24 11:21:02 -07:00
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2017-02-03 15:56:13 +02:00
|
|
|
state.Put("sshConfig", &b.config.SSHConfig)
|
2017-03-05 22:15:53 +02:00
|
|
|
state.Put("driverConfig", &b.config.DriverConfig)
|
2018-12-03 17:43:02 -06:00
|
|
|
state.Put("temporaryDevices", []string{}) // Devices (in .vmx) created by packer during building
|
2013-12-24 11:21:02 -07:00
|
|
|
|
2013-06-04 15:00:58 -07:00
|
|
|
steps := []multistep.Step{
|
2014-05-09 17:24:19 -07:00
|
|
|
&vmwcommon.StepPrepareTools{
|
2014-05-09 21:12:14 -07:00
|
|
|
RemoteType: b.config.RemoteType,
|
2014-05-09 17:24:19 -07:00
|
|
|
ToolsUploadFlavor: b.config.ToolsUploadFlavor,
|
|
|
|
},
|
2013-08-15 14:17:10 -04:00
|
|
|
&common.StepDownload{
|
2020-05-28 11:02:09 +02:00
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
Description: "ISO",
|
|
|
|
Extension: b.config.TargetExtension,
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
TargetPath: b.config.TargetPath,
|
|
|
|
Url: b.config.ISOUrls,
|
2013-08-15 14:17:10 -04:00
|
|
|
},
|
2013-12-24 11:21:02 -07:00
|
|
|
&vmwcommon.StepOutputDir{
|
2020-08-18 11:27:12 -07:00
|
|
|
Force: b.config.PackerForce,
|
|
|
|
OutputConfig: &b.config.OutputConfig,
|
|
|
|
RemoteType: b.config.RemoteType,
|
|
|
|
VMName: b.config.VMName,
|
2013-12-24 11:21:02 -07:00
|
|
|
},
|
2013-07-08 20:56:23 -07:00
|
|
|
&common.StepCreateFloppy{
|
2016-10-11 23:43:50 +02:00
|
|
|
Files: b.config.FloppyConfig.FloppyFiles,
|
2016-09-27 23:31:42 -05:00
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2019-09-12 12:25:22 +00:00
|
|
|
Label: b.config.FloppyConfig.FloppyLabel,
|
2013-07-08 20:56:23 -07:00
|
|
|
},
|
2019-09-20 00:23:28 +05:30
|
|
|
&vmwcommon.StepRemoteUpload{
|
2020-05-28 11:02:09 +02:00
|
|
|
Key: "floppy_path",
|
|
|
|
Message: "Uploading Floppy to remote machine...",
|
|
|
|
DoCleanup: true,
|
|
|
|
Checksum: "none",
|
2014-05-03 18:23:20 +09:00
|
|
|
},
|
2019-09-20 00:23:28 +05:30
|
|
|
&vmwcommon.StepRemoteUpload{
|
2020-05-28 11:02:09 +02:00
|
|
|
Key: "iso_path",
|
|
|
|
Message: "Uploading ISO to remote machine...",
|
|
|
|
DoCleanup: b.config.DriverConfig.CleanUpRemoteCache,
|
|
|
|
Checksum: b.config.ISOChecksum,
|
2013-11-07 12:28:41 -08:00
|
|
|
},
|
2020-08-26 01:13:11 -07:00
|
|
|
&vmwcommon.StepCreateDisks{
|
|
|
|
OutputDir: &b.config.OutputDir,
|
|
|
|
CreateMainDisk: true,
|
|
|
|
DiskName: b.config.DiskName,
|
|
|
|
MainDiskSize: b.config.DiskSize,
|
|
|
|
AdditionalDiskSize: b.config.AdditionalDiskSize,
|
|
|
|
DiskAdapterType: b.config.DiskAdapterType,
|
|
|
|
DiskTypeId: b.config.DiskTypeId,
|
|
|
|
},
|
2013-06-04 16:52:59 -07:00
|
|
|
&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-23 14:38:54 -08:00
|
|
|
},
|
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,
|
2020-05-27 16:34:24 -04:00
|
|
|
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,
|
2016-08-19 12:49:23 +02:00
|
|
|
VNCBindAddress: b.config.VNCBindAddress,
|
|
|
|
VNCPortMin: b.config.VNCPortMin,
|
|
|
|
VNCPortMax: b.config.VNCPortMax,
|
|
|
|
VNCDisablePassword: b.config.VNCDisablePassword,
|
2014-09-05 12:10:40 -07:00
|
|
|
},
|
2017-01-31 12:05:49 +02:00
|
|
|
&vmwcommon.StepRegister{
|
|
|
|
Format: b.config.Format,
|
|
|
|
KeepRegistered: b.config.KeepRegistered,
|
2018-10-25 14:17:35 -07:00
|
|
|
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,
|
|
|
|
},
|
2014-05-12 19:02:30 -07:00
|
|
|
&vmwcommon.StepTypeBootCommand{
|
2018-04-10 23:05:46 -07:00
|
|
|
BootWait: b.config.BootWait,
|
2017-10-09 17:12:33 -07:00
|
|
|
VNCEnabled: !b.config.DisableVNC,
|
2018-04-18 14:10:28 -07:00
|
|
|
BootCommand: b.config.FlatBootCommand(),
|
2014-05-12 19:02:30 -07:00
|
|
|
VMName: b.config.VMName,
|
2015-05-27 14:16:28 -07:00
|
|
|
Ctx: b.config.ctx,
|
2018-08-22 11:25:43 -07:00
|
|
|
KeyInterval: b.config.VNCConfig.BootKeyInterval,
|
2014-05-12 19:02:30 -07:00
|
|
|
},
|
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 17:02:23 +02:00
|
|
|
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
|
2013-07-15 14:22:13 +09:00
|
|
|
},
|
2014-05-09 17:24:19 -07:00
|
|
|
&vmwcommon.StepUploadTools{
|
2014-05-09 21:12:14 -07:00
|
|
|
RemoteType: b.config.RemoteType,
|
2014-05-09 17:24:19 -07:00
|
|
|
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,
|
2014-05-09 17:24:19 -07:00
|
|
|
},
|
2013-07-16 15:44:41 +09:00
|
|
|
&common.StepProvision{},
|
2018-09-14 11:03:23 -07:00
|
|
|
&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{},
|
2015-09-04 10:37:48 -06:00
|
|
|
&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
|
|
|
},
|
2017-05-24 18:44:35 -05:00
|
|
|
&vmwcommon.StepCleanVMX{
|
|
|
|
RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet,
|
2017-10-12 16:38:18 -07:00
|
|
|
VNCEnabled: !b.config.DisableVNC,
|
2017-05-24 18:44:35 -05:00
|
|
|
},
|
2017-01-31 12:05:49 +02:00
|
|
|
&vmwcommon.StepUploadVMX{
|
2015-05-27 14:16:28 -07:00
|
|
|
RemoteType: b.config.RemoteType,
|
2014-11-16 19:31:08 +01:00
|
|
|
},
|
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,
|
2020-08-25 12:18:38 -07:00
|
|
|
OutputDir: &b.config.OutputConfig.OutputDir,
|
2015-02-13 19:13:58 +09:00
|
|
|
},
|
2013-06-04 15:00:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run!
|
2016-09-14 00:04:18 +00:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 14:53:28 +01:00
|
|
|
b.runner.Run(ctx, state)
|
2013-06-04 15:00:58 -07:00
|
|
|
|
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
|
2020-08-18 11:27:12 -07:00
|
|
|
exportOutputPath := state.Get("export_output_path").(string) // set in StepOutputDir
|
2018-11-02 11:10:51 -07:00
|
|
|
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)
|
2013-06-04 15:00:58 -07:00
|
|
|
}
|