github.com/sneal/packer@v0.5.2/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.StepOutputDir{ 56 Force: b.config.PackerForce, 57 }, 58 &StepCloneVMX{ 59 OutputDir: b.config.OutputDir, 60 Path: b.config.SourcePath, 61 VMName: b.config.VMName, 62 }, 63 &vmwcommon.StepConfigureVMX{ 64 CustomData: b.config.VMXData, 65 }, 66 &vmwcommon.StepSuppressMessages{}, 67 &vmwcommon.StepRun{ 68 BootWait: b.config.BootWait, 69 DurationBeforeStop: 5 * time.Second, 70 Headless: b.config.Headless, 71 }, 72 &common.StepConnectSSH{ 73 SSHAddress: driver.SSHAddress, 74 SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig), 75 SSHWaitTimeout: b.config.SSHWaitTimeout, 76 NoPty: b.config.SSHSkipRequestPty, 77 }, 78 &common.StepProvision{}, 79 &vmwcommon.StepShutdown{ 80 Command: b.config.ShutdownCommand, 81 Timeout: b.config.ShutdownTimeout, 82 }, 83 &vmwcommon.StepCleanFiles{}, 84 &vmwcommon.StepCleanVMX{}, 85 &vmwcommon.StepCompactDisk{ 86 Skip: b.config.SkipCompaction, 87 }, 88 } 89 90 // Run the steps. 91 if b.config.PackerDebug { 92 b.runner = &multistep.DebugRunner{ 93 Steps: steps, 94 PauseFn: common.MultistepDebugFn(ui), 95 } 96 } else { 97 b.runner = &multistep.BasicRunner{Steps: steps} 98 } 99 b.runner.Run(state) 100 101 // Report any errors. 102 if rawErr, ok := state.GetOk("error"); ok { 103 return nil, rawErr.(error) 104 } 105 106 // If we were interrupted or cancelled, then just exit. 107 if _, ok := state.GetOk(multistep.StateCancelled); ok { 108 return nil, errors.New("Build was cancelled.") 109 } 110 111 if _, ok := state.GetOk(multistep.StateHalted); ok { 112 return nil, errors.New("Build was halted.") 113 } 114 115 return vmwcommon.NewLocalArtifact(b.config.OutputDir) 116 } 117 118 // Cancel. 119 func (b *Builder) Cancel() { 120 if b.runner != nil { 121 log.Println("Cancelling the step runner...") 122 b.runner.Cancel() 123 } 124 }