2013-12-25 17:52:40 -05:00
|
|
|
package vmx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-12-26 17:26:09 -05:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
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-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 {
|
|
|
|
config *Config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare processes the build configuration parameters.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes a Packer build and returns a packer.Artifact representing
|
2018-03-28 20:19:15 -04:00
|
|
|
// a VMware image.
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
func (b *Builder) Run(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)
|
|
|
|
}
|
|
|
|
|
2017-02-03 08:56:13 -05:00
|
|
|
// Determine the output dir implementation
|
|
|
|
var dir vmwcommon.OutputDir
|
|
|
|
switch d := driver.(type) {
|
|
|
|
case vmwcommon.OutputDir:
|
|
|
|
dir = d
|
|
|
|
default:
|
|
|
|
dir = new(vmwcommon.LocalOutputDir)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
if b.config.RemoteType != "" {
|
2017-03-07 02:03:04 -05:00
|
|
|
b.config.OutputDir = b.config.VMName
|
|
|
|
}
|
2013-12-25 18:01:57 -05:00
|
|
|
dir.SetOutputDir(b.config.OutputDir)
|
|
|
|
|
2013-12-25 17:52:40 -05:00
|
|
|
// Set up the state.
|
|
|
|
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-25 18:01:57 -05:00
|
|
|
state.Put("dir", dir)
|
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{
|
|
|
|
Force: b.config.PackerForce,
|
|
|
|
},
|
2014-05-06 20:20:26 -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,
|
2014-05-06 20:20:26 -04:00
|
|
|
},
|
2013-12-26 10:34:27 -05:00
|
|
|
&StepCloneVMX{
|
2013-12-26 10:36:00 -05:00
|
|
|
OutputDir: b.config.OutputDir,
|
|
|
|
Path: b.config.SourcePath,
|
|
|
|
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{
|
2018-11-09 13:54:31 -05:00
|
|
|
CustomData: b.config.VMXData,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
DisplayName: b.config.VMXDisplayName,
|
2013-12-26 17:14:19 -05:00
|
|
|
},
|
|
|
|
&vmwcommon.StepSuppressMessages{},
|
2015-11-01 17:29:24 -05:00
|
|
|
&common.StepHTTPServer{
|
2014-09-14 07:28:55 -04:00
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
|
|
|
},
|
2017-01-31 05:05:49 -05:00
|
|
|
&vmwcommon.StepUploadVMX{
|
|
|
|
RemoteType: b.config.RemoteType,
|
|
|
|
},
|
2014-09-14 07:28:55 -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-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,
|
|
|
|
},
|
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:21:15 -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-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
|
|
|
},
|
2013-12-26 17:28:15 -05:00
|
|
|
&common.StepProvision{},
|
2018-09-14 14:03:23 -04:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
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,
|
2018-11-02 14:10:51 -04:00
|
|
|
OutputDir: exportOutputPath,
|
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.
|
2016-09-13 20:04:18 -04:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2013-12-25 17:52:40 -05:00
|
|
|
b.runner.Run(state)
|
|
|
|
|
|
|
|
// 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...")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel.
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|