github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/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/helper/communicator" 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 // Create the driver that we'll use to communicate with VirtualBox 37 driver, err := vboxcommon.NewDriver() 38 if err != nil { 39 return nil, fmt.Errorf("Failed creating VirtualBox driver: %s", err) 40 } 41 42 // Set up the state. 43 state := new(multistep.BasicStateBag) 44 state.Put("config", b.config) 45 state.Put("debug", b.config.PackerDebug) 46 state.Put("driver", driver) 47 state.Put("cache", cache) 48 state.Put("hook", hook) 49 state.Put("ui", ui) 50 51 // Build the steps. 52 steps := []multistep.Step{ 53 &vboxcommon.StepOutputDir{ 54 Force: b.config.PackerForce, 55 Path: b.config.OutputDir, 56 }, 57 new(vboxcommon.StepSuppressMessages), 58 &common.StepCreateFloppy{ 59 Files: b.config.FloppyFiles, 60 }, 61 &common.StepHTTPServer{ 62 HTTPDir: b.config.HTTPDir, 63 HTTPPortMin: b.config.HTTPPortMin, 64 HTTPPortMax: b.config.HTTPPortMax, 65 }, 66 &vboxcommon.StepDownloadGuestAdditions{ 67 GuestAdditionsMode: b.config.GuestAdditionsMode, 68 GuestAdditionsURL: b.config.GuestAdditionsURL, 69 GuestAdditionsSHA256: b.config.GuestAdditionsSHA256, 70 Ctx: b.config.ctx, 71 }, 72 &StepImport{ 73 Name: b.config.VMName, 74 SourcePath: b.config.SourcePath, 75 ImportFlags: b.config.ImportFlags, 76 }, 77 &vboxcommon.StepAttachGuestAdditions{ 78 GuestAdditionsMode: b.config.GuestAdditionsMode, 79 }, 80 &vboxcommon.StepConfigureVRDP{ 81 VRDPBindAddress: b.config.VRDPBindAddress, 82 VRDPPortMin: b.config.VRDPPortMin, 83 VRDPPortMax: b.config.VRDPPortMax, 84 }, 85 new(vboxcommon.StepAttachFloppy), 86 &vboxcommon.StepForwardSSH{ 87 CommConfig: &b.config.SSHConfig.Comm, 88 HostPortMin: b.config.SSHHostPortMin, 89 HostPortMax: b.config.SSHHostPortMax, 90 SkipNatMapping: b.config.SSHSkipNatMapping, 91 }, 92 &vboxcommon.StepVBoxManage{ 93 Commands: b.config.VBoxManage, 94 Ctx: b.config.ctx, 95 }, 96 &vboxcommon.StepRun{ 97 BootWait: b.config.BootWait, 98 Headless: b.config.Headless, 99 }, 100 &vboxcommon.StepTypeBootCommand{ 101 BootCommand: b.config.BootCommand, 102 VMName: b.config.VMName, 103 Ctx: b.config.ctx, 104 }, 105 &communicator.StepConnect{ 106 Config: &b.config.SSHConfig.Comm, 107 Host: vboxcommon.CommHost, 108 SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig), 109 SSHPort: vboxcommon.SSHPort, 110 }, 111 &vboxcommon.StepUploadVersion{ 112 Path: b.config.VBoxVersionFile, 113 }, 114 &vboxcommon.StepUploadGuestAdditions{ 115 GuestAdditionsMode: b.config.GuestAdditionsMode, 116 GuestAdditionsPath: b.config.GuestAdditionsPath, 117 Ctx: b.config.ctx, 118 }, 119 new(common.StepProvision), 120 &vboxcommon.StepShutdown{ 121 Command: b.config.ShutdownCommand, 122 Timeout: b.config.ShutdownTimeout, 123 }, 124 new(vboxcommon.StepRemoveDevices), 125 &vboxcommon.StepVBoxManage{ 126 Commands: b.config.VBoxManagePost, 127 Ctx: b.config.ctx, 128 }, 129 &vboxcommon.StepExport{ 130 Format: b.config.Format, 131 OutputDir: b.config.OutputDir, 132 ExportOpts: b.config.ExportOpts.ExportOpts, 133 SkipNatMapping: b.config.SSHSkipNatMapping, 134 }, 135 } 136 137 // Run the steps. 138 if b.config.PackerDebug { 139 pauseFn := common.MultistepDebugFn(ui) 140 state.Put("pauseFn", pauseFn) 141 b.runner = &multistep.DebugRunner{ 142 Steps: steps, 143 PauseFn: pauseFn, 144 } 145 } else { 146 b.runner = &multistep.BasicRunner{Steps: steps} 147 } 148 b.runner.Run(state) 149 150 // Report any errors. 151 if rawErr, ok := state.GetOk("error"); ok { 152 return nil, rawErr.(error) 153 } 154 155 // If we were interrupted or cancelled, then just exit. 156 if _, ok := state.GetOk(multistep.StateCancelled); ok { 157 return nil, errors.New("Build was cancelled.") 158 } 159 160 if _, ok := state.GetOk(multistep.StateHalted); ok { 161 return nil, errors.New("Build was halted.") 162 } 163 164 return vboxcommon.NewArtifact(b.config.OutputDir) 165 } 166 167 // Cancel. 168 func (b *Builder) Cancel() { 169 if b.runner != nil { 170 log.Println("Cancelling the step runner...") 171 b.runner.Cancel() 172 } 173 }