2013-12-25 17:52:40 -05:00
|
|
|
package vmx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-12-26 17:26:09 -05:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2013-12-25 17:52:40 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
|
|
|
|
"github.com/mitchellh/packer/common"
|
2015-06-13 18:52:44 -04:00
|
|
|
"github.com/mitchellh/packer/helper/communicator"
|
2013-12-25 17:52:40 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
// a VirtualBox appliance.
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
2013-12-27 10:37:39 -05:00
|
|
|
driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig)
|
2013-12-25 17:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating VMware driver: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-12-25 18:01:57 -05:00
|
|
|
// Setup the directory
|
|
|
|
dir := new(vmwcommon.LocalOutputDir)
|
|
|
|
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)
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
Files: b.config.FloppyFiles,
|
|
|
|
},
|
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,
|
2013-12-26 10:34:27 -05:00
|
|
|
},
|
2013-12-26 17:14:19 -05:00
|
|
|
&vmwcommon.StepConfigureVMX{
|
|
|
|
CustomData: b.config.VMXData,
|
|
|
|
},
|
|
|
|
&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,
|
|
|
|
},
|
|
|
|
&vmwcommon.StepConfigureVNC{
|
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
|
|
|
},
|
2013-12-26 17:26:09 -05:00
|
|
|
&vmwcommon.StepRun{
|
|
|
|
BootWait: b.config.BootWait,
|
|
|
|
DurationBeforeStop: 5 * time.Second,
|
|
|
|
Headless: b.config.Headless,
|
|
|
|
},
|
2014-05-12 22:02:30 -04:00
|
|
|
&vmwcommon.StepTypeBootCommand{
|
|
|
|
BootCommand: b.config.BootCommand,
|
|
|
|
VMName: b.config.VMName,
|
2015-05-27 17:21:15 -04:00
|
|
|
Ctx: b.config.ctx,
|
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,
|
|
|
|
SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig),
|
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{},
|
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{
|
|
|
|
CustomData: b.config.VMXDataPost,
|
2014-09-04 00:27:54 -04:00
|
|
|
SkipFloppy: true,
|
2014-05-09 12:25:15 -04:00
|
|
|
},
|
2014-07-21 20:06:43 -04:00
|
|
|
&vmwcommon.StepCleanVMX{},
|
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.")
|
|
|
|
}
|
|
|
|
|
2013-12-26 17:32:38 -05:00
|
|
|
return vmwcommon.NewLocalArtifact(b.config.OutputDir)
|
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()
|
|
|
|
}
|
|
|
|
}
|