github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/vmware/vmx/builder.go (about) 1 package vmx 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 "time" 8 9 "github.com/mitchellh/multistep" 10 vmwcommon "github.com/mitchellh/packer/builder/vmware/common" 11 "github.com/mitchellh/packer/common" 12 "github.com/mitchellh/packer/helper/communicator" 13 "github.com/mitchellh/packer/packer" 14 ) 15 16 // Builder implements packer.Builder and builds the actual VMware 17 // images. 18 type Builder struct { 19 config *Config 20 runner multistep.Runner 21 } 22 23 // Prepare processes the build configuration parameters. 24 func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { 25 c, warnings, errs := NewConfig(raws...) 26 if errs != nil { 27 return warnings, errs 28 } 29 b.config = c 30 31 return warnings, nil 32 } 33 34 // Run executes a Packer build and returns a packer.Artifact representing 35 // a VirtualBox appliance. 36 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 37 driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig) 38 if err != nil { 39 return nil, fmt.Errorf("Failed creating VMware driver: %s", err) 40 } 41 42 // Setup the directory 43 dir := new(vmwcommon.LocalOutputDir) 44 dir.SetOutputDir(b.config.OutputDir) 45 46 // Set up the state. 47 state := new(multistep.BasicStateBag) 48 state.Put("config", b.config) 49 state.Put("debug", b.config.PackerDebug) 50 state.Put("dir", dir) 51 state.Put("driver", driver) 52 state.Put("hook", hook) 53 state.Put("ui", ui) 54 55 // Build the steps. 56 steps := []multistep.Step{ 57 &vmwcommon.StepPrepareTools{ 58 RemoteType: b.config.RemoteType, 59 ToolsUploadFlavor: b.config.ToolsUploadFlavor, 60 }, 61 &vmwcommon.StepOutputDir{ 62 Force: b.config.PackerForce, 63 }, 64 &common.StepCreateFloppy{ 65 Files: b.config.FloppyConfig.FloppyFiles, 66 Directories: b.config.FloppyConfig.FloppyDirectories, 67 }, 68 &StepCloneVMX{ 69 OutputDir: b.config.OutputDir, 70 Path: b.config.SourcePath, 71 VMName: b.config.VMName, 72 }, 73 &vmwcommon.StepConfigureVMX{ 74 CustomData: b.config.VMXData, 75 }, 76 &vmwcommon.StepSuppressMessages{}, 77 &common.StepHTTPServer{ 78 HTTPDir: b.config.HTTPDir, 79 HTTPPortMin: b.config.HTTPPortMin, 80 HTTPPortMax: b.config.HTTPPortMax, 81 }, 82 &vmwcommon.StepConfigureVNC{ 83 VNCBindAddress: b.config.VNCBindAddress, 84 VNCPortMin: b.config.VNCPortMin, 85 VNCPortMax: b.config.VNCPortMax, 86 VNCDisablePassword: b.config.VNCDisablePassword, 87 }, 88 &vmwcommon.StepRun{ 89 BootWait: b.config.BootWait, 90 DurationBeforeStop: 5 * time.Second, 91 Headless: b.config.Headless, 92 }, 93 &vmwcommon.StepTypeBootCommand{ 94 BootCommand: b.config.BootCommand, 95 VMName: b.config.VMName, 96 Ctx: b.config.ctx, 97 }, 98 &communicator.StepConnect{ 99 Config: &b.config.SSHConfig.Comm, 100 Host: driver.CommHost, 101 SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig), 102 }, 103 &vmwcommon.StepUploadTools{ 104 RemoteType: b.config.RemoteType, 105 ToolsUploadFlavor: b.config.ToolsUploadFlavor, 106 ToolsUploadPath: b.config.ToolsUploadPath, 107 Ctx: b.config.ctx, 108 }, 109 &common.StepProvision{}, 110 &vmwcommon.StepShutdown{ 111 Command: b.config.ShutdownCommand, 112 Timeout: b.config.ShutdownTimeout, 113 }, 114 &vmwcommon.StepCleanFiles{}, 115 &vmwcommon.StepCompactDisk{ 116 Skip: b.config.SkipCompaction, 117 }, 118 &vmwcommon.StepConfigureVMX{ 119 CustomData: b.config.VMXDataPost, 120 SkipFloppy: true, 121 }, 122 &vmwcommon.StepCleanVMX{}, 123 } 124 125 // Run the steps. 126 b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) 127 b.runner.Run(state) 128 129 // Report any errors. 130 if rawErr, ok := state.GetOk("error"); ok { 131 return nil, rawErr.(error) 132 } 133 134 // If we were interrupted or cancelled, then just exit. 135 if _, ok := state.GetOk(multistep.StateCancelled); ok { 136 return nil, errors.New("Build was cancelled.") 137 } 138 139 if _, ok := state.GetOk(multistep.StateHalted); ok { 140 return nil, errors.New("Build was halted.") 141 } 142 143 return vmwcommon.NewLocalArtifact(b.config.OutputDir) 144 } 145 146 // Cancel. 147 func (b *Builder) Cancel() { 148 if b.runner != nil { 149 log.Println("Cancelling the step runner...") 150 b.runner.Cancel() 151 } 152 }