github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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.FloppyConfig.FloppyFiles, 60 Directories: b.config.FloppyConfig.FloppyDirectories, 61 }, 62 &common.StepHTTPServer{ 63 HTTPDir: b.config.HTTPDir, 64 HTTPPortMin: b.config.HTTPPortMin, 65 HTTPPortMax: b.config.HTTPPortMax, 66 }, 67 &vboxcommon.StepDownloadGuestAdditions{ 68 GuestAdditionsMode: b.config.GuestAdditionsMode, 69 GuestAdditionsURL: b.config.GuestAdditionsURL, 70 GuestAdditionsSHA256: b.config.GuestAdditionsSHA256, 71 Ctx: b.config.ctx, 72 }, 73 &common.StepDownload{ 74 Checksum: b.config.Checksum, 75 ChecksumType: b.config.ChecksumType, 76 Description: "OVF/OVA", 77 Extension: "ova", 78 ResultKey: "vm_path", 79 TargetPath: b.config.TargetPath, 80 Url: []string{b.config.SourcePath}, 81 }, 82 &StepImport{ 83 Name: b.config.VMName, 84 ImportFlags: b.config.ImportFlags, 85 }, 86 &vboxcommon.StepAttachGuestAdditions{ 87 GuestAdditionsMode: b.config.GuestAdditionsMode, 88 }, 89 &vboxcommon.StepConfigureVRDP{ 90 VRDPBindAddress: b.config.VRDPBindAddress, 91 VRDPPortMin: b.config.VRDPPortMin, 92 VRDPPortMax: b.config.VRDPPortMax, 93 }, 94 new(vboxcommon.StepAttachFloppy), 95 &vboxcommon.StepForwardSSH{ 96 CommConfig: &b.config.SSHConfig.Comm, 97 HostPortMin: b.config.SSHHostPortMin, 98 HostPortMax: b.config.SSHHostPortMax, 99 SkipNatMapping: b.config.SSHSkipNatMapping, 100 }, 101 &vboxcommon.StepVBoxManage{ 102 Commands: b.config.VBoxManage, 103 Ctx: b.config.ctx, 104 }, 105 &vboxcommon.StepRun{ 106 BootWait: b.config.BootWait, 107 Headless: b.config.Headless, 108 }, 109 &vboxcommon.StepTypeBootCommand{ 110 BootCommand: b.config.BootCommand, 111 VMName: b.config.VMName, 112 Ctx: b.config.ctx, 113 }, 114 &communicator.StepConnect{ 115 Config: &b.config.SSHConfig.Comm, 116 Host: vboxcommon.CommHost(b.config.SSHConfig.Comm.SSHHost), 117 SSHConfig: vboxcommon.SSHConfigFunc(b.config.SSHConfig), 118 SSHPort: vboxcommon.SSHPort, 119 WinRMPort: vboxcommon.SSHPort, 120 }, 121 &vboxcommon.StepUploadVersion{ 122 Path: b.config.VBoxVersionFile, 123 }, 124 &vboxcommon.StepUploadGuestAdditions{ 125 GuestAdditionsMode: b.config.GuestAdditionsMode, 126 GuestAdditionsPath: b.config.GuestAdditionsPath, 127 Ctx: b.config.ctx, 128 }, 129 new(common.StepProvision), 130 &vboxcommon.StepShutdown{ 131 Command: b.config.ShutdownCommand, 132 Timeout: b.config.ShutdownTimeout, 133 Delay: b.config.PostShutdownDelay, 134 }, 135 new(vboxcommon.StepRemoveDevices), 136 &vboxcommon.StepVBoxManage{ 137 Commands: b.config.VBoxManagePost, 138 Ctx: b.config.ctx, 139 }, 140 &vboxcommon.StepExport{ 141 Format: b.config.Format, 142 OutputDir: b.config.OutputDir, 143 ExportOpts: b.config.ExportOpts.ExportOpts, 144 SkipNatMapping: b.config.SSHSkipNatMapping, 145 }, 146 } 147 148 // Run the steps. 149 b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) 150 b.runner.Run(state) 151 152 // Report any errors. 153 if rawErr, ok := state.GetOk("error"); ok { 154 return nil, rawErr.(error) 155 } 156 157 // If we were interrupted or cancelled, then just exit. 158 if _, ok := state.GetOk(multistep.StateCancelled); ok { 159 return nil, errors.New("Build was cancelled.") 160 } 161 162 if _, ok := state.GetOk(multistep.StateHalted); ok { 163 return nil, errors.New("Build was halted.") 164 } 165 166 return vboxcommon.NewArtifact(b.config.OutputDir) 167 } 168 169 // Cancel. 170 func (b *Builder) Cancel() { 171 if b.runner != nil { 172 log.Println("Cancelling the step runner...") 173 b.runner.Cancel() 174 } 175 }