2013-12-24 00:58:41 -05:00
|
|
|
package iso
|
2013-06-04 18:00:58 -04:00
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2013-06-06 17:46:48 -04:00
|
|
|
"errors"
|
2013-06-06 17:38:14 -04:00
|
|
|
"fmt"
|
2013-06-06 12:10:14 -04:00
|
|
|
"time"
|
2015-05-27 17:16:28 -04:00
|
|
|
|
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"
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"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-06-04 18:00:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Builder struct {
|
2015-05-27 17:16:28 -04:00
|
|
|
config Config
|
2013-06-04 18:00:58 -04: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...)
|
2018-11-07 12:42:03 -05:00
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2014-09-25 00:20:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, nil
|
2013-06-04 18:00:58 -04:00
|
|
|
}
|
|
|
|
|
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-08-13 11:54:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating VMware driver: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-12-24 13:21:02 -05:00
|
|
|
// Determine the output dir implementation
|
2017-02-03 08:56:13 -05:00
|
|
|
var dir vmwcommon.OutputDir
|
2013-12-24 13:21:02 -05:00
|
|
|
switch d := driver.(type) {
|
2017-02-03 08:56:13 -05:00
|
|
|
case vmwcommon.OutputDir:
|
2013-12-24 13:21:02 -05:00
|
|
|
dir = d
|
|
|
|
default:
|
|
|
|
dir = new(vmwcommon.LocalOutputDir)
|
|
|
|
}
|
2017-02-24 07:46:00 -05:00
|
|
|
|
2018-11-02 14:10:51 -04:00
|
|
|
// The OutputDir will track remote esxi output; exportOutputPath preserves
|
|
|
|
// the path to the output on the machine running Packer.
|
|
|
|
exportOutputPath := b.config.OutputDir
|
|
|
|
|
2017-02-10 03:49:47 -05:00
|
|
|
if b.config.RemoteType != "" {
|
2015-02-13 05:13:58 -05:00
|
|
|
b.config.OutputDir = b.config.VMName
|
|
|
|
}
|
2013-12-24 13:21:02 -05:00
|
|
|
dir.SetOutputDir(b.config.OutputDir)
|
|
|
|
|
|
|
|
// 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 13:21:02 -05:00
|
|
|
state.Put("dir", dir)
|
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2017-02-03 08:56:13 -05:00
|
|
|
state.Put("sshConfig", &b.config.SSHConfig)
|
2017-03-05 15:15:53 -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-24 13:21:02 -05:00
|
|
|
|
2013-06-04 18:00:58 -04: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-08-15 14:17:10 -04:00
|
|
|
&common.StepDownload{
|
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
ChecksumType: b.config.ISOChecksumType,
|
|
|
|
Description: "ISO",
|
2016-12-17 05:49:54 -05:00
|
|
|
Extension: b.config.TargetExtension,
|
2015-10-20 19:27:47 -04:00
|
|
|
ResultKey: "iso_path",
|
2015-07-28 07:45:02 -04:00
|
|
|
TargetPath: b.config.TargetPath,
|
2015-10-20 19:27:47 -04:00
|
|
|
Url: b.config.ISOUrls,
|
2013-08-15 14:17:10 -04:00
|
|
|
},
|
2013-12-24 13:21:02 -05:00
|
|
|
&vmwcommon.StepOutputDir{
|
|
|
|
Force: b.config.PackerForce,
|
|
|
|
},
|
2013-07-08 23:56:23 -04:00
|
|
|
&common.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,
|
2013-07-08 23:56:23 -04:00
|
|
|
},
|
2019-09-19 14:53:28 -04:00
|
|
|
&vmwcommon.StepRemoteUpload{
|
|
|
|
Key: "floppy_path",
|
|
|
|
Message: "Uploading Floppy to remote machine...",
|
|
|
|
DoCleanup: true,
|
|
|
|
Checksum: "",
|
|
|
|
ChecksumType: "none",
|
2014-05-03 05:23:20 -04:00
|
|
|
},
|
2019-09-19 14:53:28 -04:00
|
|
|
&vmwcommon.StepRemoteUpload{
|
|
|
|
Key: "iso_path",
|
|
|
|
Message: "Uploading ISO to remote machine...",
|
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
ChecksumType: b.config.ISOChecksumType,
|
2013-11-07 15:28:41 -05:00
|
|
|
},
|
2013-06-04 18:00:58 -04:00
|
|
|
&stepCreateDisk{},
|
2013-06-04 19:52:59 -04:00
|
|
|
&stepCreateVMX{},
|
2013-12-24 01:07:43 -05:00
|
|
|
&vmwcommon.StepConfigureVMX{
|
2018-11-09 13:54:31 -05:00
|
|
|
CustomData: b.config.VMXData,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
DisplayName: b.config.VMXDisplayName,
|
2013-12-23 17:38:54 -05:00
|
|
|
},
|
2013-12-24 16:26:44 -05:00
|
|
|
&vmwcommon.StepSuppressMessages{},
|
2020-01-27 10:49:31 -05:00
|
|
|
&vmwcommon.StepHTTPIPDiscover{},
|
2015-11-01 17:29:24 -05:00
|
|
|
&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,
|
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-05 15:10:40 -04:00
|
|
|
},
|
2017-01-31 05:05:49 -05:00
|
|
|
&vmwcommon.StepRegister{
|
|
|
|
Format: b.config.Format,
|
|
|
|
KeepRegistered: b.config.KeepRegistered,
|
2018-10-25 17:17:35 -04:00
|
|
|
SkipExport: b.config.SkipExport,
|
2015-02-13 05:13:58 -05:00
|
|
|
},
|
2013-12-24 20:12:43 -05:00
|
|
|
&vmwcommon.StepRun{
|
|
|
|
DurationBeforeStop: 5 * time.Second,
|
|
|
|
Headless: b.config.Headless,
|
|
|
|
},
|
2014-05-12 22:02:30 -04:00
|
|
|
&vmwcommon.StepTypeBootCommand{
|
2018-04-11 02:05:46 -04:00
|
|
|
BootWait: b.config.BootWait,
|
2017-10-09 20:12:33 -04:00
|
|
|
VNCEnabled: !b.config.DisableVNC,
|
2018-04-18 17:10:28 -04:00
|
|
|
BootCommand: b.config.FlatBootCommand(),
|
2014-05-12 22:02:30 -04:00
|
|
|
VMName: b.config.VMName,
|
2015-05-27 17:16:28 -04:00
|
|
|
Ctx: b.config.ctx,
|
2018-08-22 14:25:43 -04:00
|
|
|
KeyInterval: b.config.VNCConfig.BootKeyInterval,
|
2014-05-12 22:02:30 -04: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 11:02:23 -04:00
|
|
|
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
|
2013-07-15 01:22:13 -04: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:16:28 -04:00
|
|
|
Ctx: b.config.ctx,
|
2014-05-09 20:24:19 -04:00
|
|
|
},
|
2013-07-16 02:44:41 -04:00
|
|
|
&common.StepProvision{},
|
2018-09-14 14:03:23 -04:00
|
|
|
&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{},
|
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-01-31 05:05:49 -05:00
|
|
|
&vmwcommon.StepUploadVMX{
|
2015-05-27 17:16:28 -04:00
|
|
|
RemoteType: b.config.RemoteType,
|
2014-11-16 13:31:08 -05:00
|
|
|
},
|
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,
|
2018-11-02 14:10:51 -04:00
|
|
|
OutputDir: exportOutputPath,
|
2015-02-13 05:13:58 -05:00
|
|
|
},
|
2013-06-04 18:00:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run!
|
2016-09-13 20:04:18 -04:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2013-06-04 18:00:58 -04:00
|
|
|
|
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
|
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-06-04 18:00:58 -04:00
|
|
|
}
|