github.com/sneal/packer@v0.5.2/builder/virtualbox/ovf/builder.go (about) 1 package ovf 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 8 "github.com/mitchellh/multistep" 9 vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" 10 "github.com/mitchellh/packer/common" 11 "github.com/mitchellh/packer/packer" 12 ) 13 14 // Builder implements packer.Builder and builds the actual VirtualBox 15 // images. 16 type Builder struct { 17 config *Config 18 runner multistep.Runner 19 } 20 21 // Prepare processes the build configuration parameters. 22 func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { 23 c, warnings, errs := NewConfig(raws...) 24 if errs != nil { 25 return warnings, errs 26 } 27 b.config = c 28 29 return warnings, nil 30 } 31 32 // Run executes a Packer build and returns a packer.Artifact representing 33 // a VirtualBox appliance. 34 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 35 // Create the driver that we'll use to communicate with VirtualBox 36 driver, err := vboxcommon.NewDriver() 37 if err != nil { 38 return nil, fmt.Errorf("Failed creating VirtualBox driver: %s", err) 39 } 40 41 // Set up the state. 42 state := new(multistep.BasicStateBag) 43 state.Put("config", b.config) 44 state.Put("driver", driver) 45 state.Put("hook", hook) 46 state.Put("ui", ui) 47 48 // Build the steps. 49 steps := []multistep.Step{ 50 &vboxcommon.StepOutputDir{ 51 Force: b.config.PackerForce, 52 Path: b.config.OutputDir, 53 }, 54 new(vboxcommon.StepSuppressMessages), 55 &common.StepCreateFloppy{ 56 Files: b.config.FloppyFiles, 57 }, 58 &StepImport{ 59 Name: b.config.VMName, 60 SourcePath: b.config.SourcePath, 61 ImportOpts: b.config.ImportOpts, 62 }, 63 /* 64 new(stepAttachGuestAdditions), 65 */ 66 new(vboxcommon.StepAttachFloppy), 67 &vboxcommon.StepForwardSSH{ 68 GuestPort: b.config.SSHPort, 69 HostPortMin: b.config.SSHHostPortMin, 70 HostPortMax: b.config.SSHHostPortMax, 71 }, 72 &vboxcommon.StepVBoxManage{ 73 Commands: b.config.VBoxManage, 74 Tpl: b.config.tpl, 75 }, 76 &vboxcommon.StepRun{ 77 BootWait: b.config.BootWait, 78 Headless: b.config.Headless, 79 }, 80 &common.StepConnectSSH{ 81 SSHAddress: vboxcommon.SSHAddress, 82 SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig), 83 SSHWaitTimeout: b.config.SSHWaitTimeout, 84 }, 85 &vboxcommon.StepUploadVersion{ 86 Path: b.config.VBoxVersionFile, 87 }, 88 /* 89 new(stepUploadGuestAdditions), 90 */ 91 new(common.StepProvision), 92 &vboxcommon.StepShutdown{ 93 Command: b.config.ShutdownCommand, 94 Timeout: b.config.ShutdownTimeout, 95 }, 96 new(vboxcommon.StepRemoveDevices), 97 &vboxcommon.StepExport{ 98 Format: b.config.Format, 99 OutputDir: b.config.OutputDir, 100 }, 101 } 102 103 // Run the steps. 104 if b.config.PackerDebug { 105 b.runner = &multistep.DebugRunner{ 106 Steps: steps, 107 PauseFn: common.MultistepDebugFn(ui), 108 } 109 } else { 110 b.runner = &multistep.BasicRunner{Steps: steps} 111 } 112 b.runner.Run(state) 113 114 // Report any errors. 115 if rawErr, ok := state.GetOk("error"); ok { 116 return nil, rawErr.(error) 117 } 118 119 // If we were interrupted or cancelled, then just exit. 120 if _, ok := state.GetOk(multistep.StateCancelled); ok { 121 return nil, errors.New("Build was cancelled.") 122 } 123 124 if _, ok := state.GetOk(multistep.StateHalted); ok { 125 return nil, errors.New("Build was halted.") 126 } 127 128 return vboxcommon.NewArtifact(b.config.OutputDir) 129 } 130 131 // Cancel. 132 func (b *Builder) Cancel() { 133 if b.runner != nil { 134 log.Println("Cancelling the step runner...") 135 b.runner.Cancel() 136 } 137 }