github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/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.FloppyFiles, 66 }, 67 &StepCloneVMX{ 68 OutputDir: b.config.OutputDir, 69 Path: b.config.SourcePath, 70 VMName: b.config.VMName, 71 }, 72 &vmwcommon.StepConfigureVMX{ 73 CustomData: b.config.VMXData, 74 }, 75 &vmwcommon.StepSuppressMessages{}, 76 &common.StepHTTPServer{ 77 HTTPDir: b.config.HTTPDir, 78 HTTPPortMin: b.config.HTTPPortMin, 79 HTTPPortMax: b.config.HTTPPortMax, 80 }, 81 &vmwcommon.StepConfigureVNC{ 82 VNCBindAddress: b.config.VNCBindAddress, 83 VNCPortMin: b.config.VNCPortMin, 84 VNCPortMax: b.config.VNCPortMax, 85 }, 86 &vmwcommon.StepRun{ 87 BootWait: b.config.BootWait, 88 DurationBeforeStop: 5 * time.Second, 89 Headless: b.config.Headless, 90 }, 91 &vmwcommon.StepTypeBootCommand{ 92 BootCommand: b.config.BootCommand, 93 VMName: b.config.VMName, 94 Ctx: b.config.ctx, 95 }, 96 &communicator.StepConnect{ 97 Config: &b.config.SSHConfig.Comm, 98 Host: driver.CommHost, 99 SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig), 100 }, 101 &vmwcommon.StepUploadTools{ 102 RemoteType: b.config.RemoteType, 103 ToolsUploadFlavor: b.config.ToolsUploadFlavor, 104 ToolsUploadPath: b.config.ToolsUploadPath, 105 Ctx: b.config.ctx, 106 }, 107 &common.StepProvision{}, 108 &vmwcommon.StepShutdown{ 109 Command: b.config.ShutdownCommand, 110 Timeout: b.config.ShutdownTimeout, 111 }, 112 &vmwcommon.StepCleanFiles{}, 113 &vmwcommon.StepCompactDisk{ 114 Skip: b.config.SkipCompaction, 115 }, 116 &vmwcommon.StepConfigureVMX{ 117 CustomData: b.config.VMXDataPost, 118 SkipFloppy: true, 119 }, 120 &vmwcommon.StepCleanVMX{}, 121 } 122 123 // Run the steps. 124 if b.config.PackerDebug { 125 pauseFn := common.MultistepDebugFn(ui) 126 state.Put("pauseFn", pauseFn) 127 b.runner = &multistep.DebugRunner{ 128 Steps: steps, 129 PauseFn: pauseFn, 130 } 131 } else { 132 b.runner = &multistep.BasicRunner{Steps: steps} 133 } 134 b.runner.Run(state) 135 136 // Report any errors. 137 if rawErr, ok := state.GetOk("error"); ok { 138 return nil, rawErr.(error) 139 } 140 141 // If we were interrupted or cancelled, then just exit. 142 if _, ok := state.GetOk(multistep.StateCancelled); ok { 143 return nil, errors.New("Build was cancelled.") 144 } 145 146 if _, ok := state.GetOk(multistep.StateHalted); ok { 147 return nil, errors.New("Build was halted.") 148 } 149 150 return vmwcommon.NewLocalArtifact(b.config.OutputDir) 151 } 152 153 // Cancel. 154 func (b *Builder) Cancel() { 155 if b.runner != nil { 156 log.Println("Cancelling the step runner...") 157 b.runner.Cancel() 158 } 159 }