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