package vmx import ( "errors" "fmt" "log" "time" vmwcommon "github.com/hashicorp/packer/builder/vmware/common" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) // Builder implements packer.Builder and builds the actual VMware // 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 VMware image. func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig, &b.config.CommConfig, b.config.VMName) if err != nil { return nil, fmt.Errorf("Failed creating VMware driver: %s", err) } // Determine the output dir implementation var dir vmwcommon.OutputDir switch d := driver.(type) { case vmwcommon.OutputDir: dir = d default: dir = new(vmwcommon.LocalOutputDir) } if b.config.RemoteType != "" && b.config.Format != "" { b.config.OutputDir = b.config.VMName } dir.SetOutputDir(b.config.OutputDir) // Set up the state. state := new(multistep.BasicStateBag) state.Put("config", b.config) state.Put("debug", b.config.PackerDebug) state.Put("dir", dir) state.Put("driver", driver) state.Put("hook", hook) state.Put("ui", ui) state.Put("sshConfig", &b.config.SSHConfig) state.Put("driverConfig", &b.config.DriverConfig) // Build the steps. steps := []multistep.Step{ &vmwcommon.StepPrepareTools{ RemoteType: b.config.RemoteType, ToolsUploadFlavor: b.config.ToolsUploadFlavor, }, &vmwcommon.StepOutputDir{ Force: b.config.PackerForce, }, &common.StepCreateFloppy{ Files: b.config.FloppyConfig.FloppyFiles, Directories: b.config.FloppyConfig.FloppyDirectories, }, &StepCloneVMX{ OutputDir: b.config.OutputDir, Path: b.config.SourcePath, VMName: b.config.VMName, Linked: b.config.Linked, }, &vmwcommon.StepConfigureVMX{ CustomData: b.config.VMXData, VMName: b.config.VMName, }, &vmwcommon.StepSuppressMessages{}, &common.StepHTTPServer{ HTTPDir: b.config.HTTPDir, HTTPPortMin: b.config.HTTPPortMin, HTTPPortMax: b.config.HTTPPortMax, }, &vmwcommon.StepUploadVMX{ RemoteType: b.config.RemoteType, }, &vmwcommon.StepConfigureVNC{ Enabled: !b.config.DisableVNC, VNCBindAddress: b.config.VNCBindAddress, VNCPortMin: b.config.VNCPortMin, VNCPortMax: b.config.VNCPortMax, VNCDisablePassword: b.config.VNCDisablePassword, }, &vmwcommon.StepRegister{ Format: b.config.Format, KeepRegistered: b.config.KeepRegistered, }, &vmwcommon.StepRun{ DurationBeforeStop: 5 * time.Second, Headless: b.config.Headless, }, &vmwcommon.StepTypeBootCommand{ BootWait: b.config.BootWait, VNCEnabled: !b.config.DisableVNC, BootCommand: b.config.FlatBootCommand(), VMName: b.config.VMName, Ctx: b.config.ctx, KeyInterval: b.config.VNCConfig.BootKeyInterval, }, &communicator.StepConnect{ Config: &b.config.SSHConfig.Comm, Host: driver.CommHost, SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(), }, &vmwcommon.StepUploadTools{ RemoteType: b.config.RemoteType, ToolsUploadFlavor: b.config.ToolsUploadFlavor, ToolsUploadPath: b.config.ToolsUploadPath, Ctx: b.config.ctx, }, &common.StepProvision{}, &common.StepCleanupTempKeys{ Comm: &b.config.SSHConfig.Comm, }, &vmwcommon.StepShutdown{ Command: b.config.ShutdownCommand, Timeout: b.config.ShutdownTimeout, }, &vmwcommon.StepCleanFiles{}, &vmwcommon.StepCompactDisk{ Skip: b.config.SkipCompaction, }, &vmwcommon.StepConfigureVMX{ CustomData: b.config.VMXDataPost, SkipFloppy: true, VMName: b.config.VMName, }, &vmwcommon.StepCleanVMX{ RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet, VNCEnabled: !b.config.DisableVNC, }, &vmwcommon.StepUploadVMX{ RemoteType: b.config.RemoteType, }, &vmwcommon.StepExport{ Format: b.config.Format, SkipExport: b.config.SkipExport, VMName: b.config.VMName, OVFToolOptions: b.config.OVFToolOptions, }, } // Run the steps. b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) 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.") } // Compile the artifact list var files []string if b.config.RemoteType != "" && b.config.Format != "" { dir = new(vmwcommon.LocalOutputDir) dir.SetOutputDir(b.config.OutputDir) files, err = dir.ListFiles() } else { files, err = state.Get("dir").(vmwcommon.OutputDir).ListFiles() } if err != nil { return nil, err } return vmwcommon.NewArtifact(dir, files, b.config.RemoteType != ""), nil } // Cancel. func (b *Builder) Cancel() { if b.runner != nil { log.Println("Cancelling the step runner...") b.runner.Cancel() } }