github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/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 VRDPPortMin: b.config.VRDPPortMin, 82 VRDPPortMax: b.config.VRDPPortMax, 83 }, 84 new(vboxcommon.StepAttachFloppy), 85 &vboxcommon.StepForwardSSH{ 86 CommConfig: &b.config.SSHConfig.Comm, 87 HostPortMin: b.config.SSHHostPortMin, 88 HostPortMax: b.config.SSHHostPortMax, 89 SkipNatMapping: b.config.SSHSkipNatMapping, 90 }, 91 &vboxcommon.StepVBoxManage{ 92 Commands: b.config.VBoxManage, 93 Ctx: b.config.ctx, 94 }, 95 &vboxcommon.StepRun{ 96 BootWait: b.config.BootWait, 97 Headless: b.config.Headless, 98 }, 99 &vboxcommon.StepTypeBootCommand{ 100 BootCommand: b.config.BootCommand, 101 VMName: b.config.VMName, 102 Ctx: b.config.ctx, 103 }, 104 &communicator.StepConnect{ 105 Config: &b.config.SSHConfig.Comm, 106 Host: vboxcommon.CommHost, 107 SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig), 108 SSHPort: vboxcommon.SSHPort, 109 }, 110 &vboxcommon.StepUploadVersion{ 111 Path: b.config.VBoxVersionFile, 112 }, 113 &vboxcommon.StepUploadGuestAdditions{ 114 GuestAdditionsMode: b.config.GuestAdditionsMode, 115 GuestAdditionsPath: b.config.GuestAdditionsPath, 116 Ctx: b.config.ctx, 117 }, 118 new(common.StepProvision), 119 &vboxcommon.StepShutdown{ 120 Command: b.config.ShutdownCommand, 121 Timeout: b.config.ShutdownTimeout, 122 }, 123 new(vboxcommon.StepRemoveDevices), 124 &vboxcommon.StepVBoxManage{ 125 Commands: b.config.VBoxManagePost, 126 Ctx: b.config.ctx, 127 }, 128 &vboxcommon.StepExport{ 129 Format: b.config.Format, 130 OutputDir: b.config.OutputDir, 131 ExportOpts: b.config.ExportOpts.ExportOpts, 132 SkipNatMapping: b.config.SSHSkipNatMapping, 133 }, 134 } 135 136 // Run the steps. 137 if b.config.PackerDebug { 138 pauseFn := common.MultistepDebugFn(ui) 139 state.Put("pauseFn", pauseFn) 140 b.runner = &multistep.DebugRunner{ 141 Steps: steps, 142 PauseFn: pauseFn, 143 } 144 } else { 145 b.runner = &multistep.BasicRunner{Steps: steps} 146 } 147 b.runner.Run(state) 148 149 // Report any errors. 150 if rawErr, ok := state.GetOk("error"); ok { 151 return nil, rawErr.(error) 152 } 153 154 // If we were interrupted or cancelled, then just exit. 155 if _, ok := state.GetOk(multistep.StateCancelled); ok { 156 return nil, errors.New("Build was cancelled.") 157 } 158 159 if _, ok := state.GetOk(multistep.StateHalted); ok { 160 return nil, errors.New("Build was halted.") 161 } 162 163 return vboxcommon.NewArtifact(b.config.OutputDir) 164 } 165 166 // Cancel. 167 func (b *Builder) Cancel() { 168 if b.runner != nil { 169 log.Println("Cancelling the step runner...") 170 b.runner.Cancel() 171 } 172 }